Saturday, June 2, 2012

Setting up FTP in Windows Server 2008

To set up the FTP site, follow these steps:

  1. In User Manager, create a user account for each of your users.

    Note These users need to have the local log on permission. By default, new users belong to the built-in Users group, which has local log on permission.

  2. Using the Windows NT Explorer, on a partition formatted with NTFS, give the FTP site's root folder the following security access types:

    • Administrators: Full Control

    • Everyone: Read

    • System: Read



  3. Create a subfolder for each user. (These subfolders will inherit the root folder's security settings). Make the following security changes:

    • Remove the Everyone group.

    • Change the System account's folder's access to Full Control (instead of Read).

    • Add the user who will use that folder, and give that user Full Control access.



  4. Configure the home page for the user. To do this, follow these steps:




  1. In Control Panel, open Administrative Tools.

  2. Double-click Computer Management.

  3. Expand Local Users and Groups, click Users, right-click the user name, and then click Properties.

  4. Click the Profile tab.

  5. Make sure that Local path is selected under Home Folder, and then type the appropriate path in the Local path box. For example, type the path as d:\Ftp\FolderName.

    Note The user name and the folder name must match. For example, if the user name is JoeUser, the FolderName should also be JoeUser. If the folder name is different, the user will see the same view that is available to Anonymous users. This view is the complete parent folder structure. The user is still denied access to folders to which they are not specifically granted NTFS permissions. However, the parent folder view may be undesirable

  6. Also Check to see FTP service is running.

Tuesday, April 3, 2012

EntityFramework 4 - Code First v Model First (pros and cons)

I have worked on MVC 3 projects using both Code First and Model First. From my personal experience here were the pros and cons I could think of:

Code First Pros | Model First Cons

  • For Model First approach, DB generated names are not friendly especially if the DBA/Organization follows rules such as OAT0012 for table(entity) names and FSN_XXX for column names. Although, you can manually edit this, it will all go away during a refresh/update. But with Code First Approach you can have User(Developer)-friendly names like Person or Account as table(entity) names and AccountID as field names and not worry about that changing.

  • Create relationships as you need them, like the way you want them to behave (bi-directional/uni-directional)

  • Generated code is not always easy to understand, especially when the database is not 3N-normalized. With this, you can add your own relationships to maintain relationships. Now I have also seen Id field mapping to another entity as well as that entity as an object within the same entity. Duh!

  • When working on a team Model First can sometimes be a pain. This is true in initial development when changes are frequent to the database. You could say that this is true for Code First too. To some degree, Yes. But if the DBA's model does not suit your Controller's need and you need to hack into one or two entities, you would have to consider keeping local changes (branching out your repo) and updating the team's changes and applying your changes again.


Code First Cons | Model First Pros

  • You are up and running as soon as the database is ready and you can immediately start working on your service methods.

  • If you have a complex database the relationships that are automatically created for you in a matter of seconds. This is probably why people choose to go with this when they do it.

  • You do not need to read an entire book to get started with EF if you choose this option. Probably another reason to go with this.


Overall, the deciding factors, if I was choosing would be, is there going to be a lot of changes to the database? If so, I would stick to CodeFirst. It also suits Agile way of working. Creating only what you need to get the unit of work done and making changes as you go along. Also, if the database is not normalized, generated code would not be very helpful. But with a complex database and that which is almost ready, I would, for sure, prefer Model First.

Wednesday, January 4, 2012

JUnit 4.10 - Writing your first JUnit 4 test in a TDD Approach

In this article I will show you how to write your first JUnit 4 test.I will use Test Driven Development Approach (TDD from here onwards). This means I will first write an empty method to test. Then I will write the Test Class which will test this method. This of course means that the test will fail. Then once we know what we want from the test, we then refactor our actual method. This is exactly how it works in a TDD approach. We write the test first, then it fails, then we discover what needs to be refactored and we make the change in the class to test.

I will not use any IDE. A simple text editor like Notepad++ will suffice. This is in Windows environment.

  1. Download JUnit 4.10 source from here.

  2. Extract to C:\JUnit4.10.

  3. Add C:\JUnit4.10\junit-4.10.jar to CLASSPATH. Also make sure your JAVA_HOME variable is configured properly. Add your JAVA_HOME folder path to CLASSPATH as well since we will be compiling classes from command prompt.

  4. Now you are ready to write your first class.Since we are going to follow Test Driven Development, we will simply create a stub of the method we want to test.
    So go ahead and create a simple class called Calculator.java

    public class Calculator{

        public int add(int a, int b){
            return null;
        }

    }


    You notice that the method add, which we will test, returns null. This is a very simple test and you can figure out that it should return a + b. But this being TDD approach, we will not write this method. We actually want this method to fail. Now we are ready to write our Test Class.

  5. Go ahead and write the Test Class. We will call it CalculatorTest.java

    import static org.junit.Assert.*;
    import org.junit.Test;

    public class CalculatorTest{

        @Test
        public void testAdd()
        {
            Calculator calc = new Calculator(); //Setup object to Test
            int result = calc.add(2,8); //Call the method to Test
            assertEquals(10, result, 0); //Verify        
        }

    }


    The first thing to note is that I have annotated the method testAdd() with @Test. This is significant. The test suite will specifically look for methods with @Test in order to test it. Also note that test methods are void and the convention is to write testXX() where 'XX' is the name of the method to test.

    In any test method, you will first instantiate (by creating a new object in our case or use DI) the object to test. You would then set all the defaults of the object. In our case, there isn't any. This is the setup portion. You setup the object to test. Next you will call the method to test which may or may not return something. In our case it does. If it does not return, you can simply verify that the method has been called and that there is no exception. Finally, you assert or verify that the outcome is what you expect.

  6. Compile both classes from the command prompt using javac Calculator.java and javac CalculatorTest.java. Both should compile.

  7. Run the test from the command prompt again using java org.junit.runner.JUnitCore CalculatorTest


  8. You will get a Test Failed Error
    .

  9. Now you are ready to refactor!
    You know that your Calculator class needs to return the sum of the two passed-in integers by looking at the test. So you make that change.

    public class Calculator{

        public int add(int a, int b){
            return a+b;
        }

    }



  10. Repeat 6 and 7. You will now get a Test Passed message.


 

This is a very simple test. But think of a scenario when you have to test a very complex method. Even better think of a scenario where your method is tied up with resources(values) from your configuration file that you may not be able to access(acquire) in your local development environment. But you still need to write the method correctly before you can check-in the code. In such a scenario, you will mock out the object in the test class. This will ensure that your method is fully tested and what values of the mocked object you need from the configuration. At the very least, you will know how the method should actually work because tests should be informative.

I will be writing more on testing DAOs and Service layers (mocking) in the future. As usual, I appreciate your comments.

 

Monday, December 19, 2011

Java: Pass by value or Pass by reference

Java: Pass by value or Pass by reference?
Short: Pass by value

This is basically for memory optimization, as I would like to think. Primitive data types have specific length whereas objects do not. In the memory both of these are stored in a continuous sequence of bits. The primitive types are stored from, if you will, low number to high number whereas objects are stored from higher number to lower in order to find the continuous free blocks.

It is generally agreed that primitives are passed by value and objects (memory address) are referenced. However, even the object references are passed by value.

In C# you can actually pass by reference and the original object also gets modified.
E.g
void passMe(ref OrginialObj obj)
{
obj = ....;//original object is modified even after the method returns
}


 

Interface v Abstract Class (in my own words)

Interface based programming works very closely with DI (P2I or program to interface). The container will push the concrete implementation at runtime so the code is loosely coupled. It also promotes Test Driven Development. Why? You can by-pass the configuration files that might be looking to resolve the resources.

As for difference between abstract classes and interface, both are basically contracts that need to be followed by the implementing classes but interface allows the contract (of the methods) to be "upto" the developer. It also means the developer can now implement as many interfaces as necessary. I would like to think of Abstract Classes as "more strict". Programatically, you would be allowed to implement from only one abstract class, neither can be instantiated, and abstract classes can have concrete method definitions.