Monthly Archive for December, 2007

Spring Tips: Initializing bean using EasyMock for unit test

Unit test is a very good practice for creating robust application. Using Spring, we can avoid using ApplicationContext and instead arrange all dependencies programmatically. However, once in a while, we still need to test using the actual ApplicationContext. This can be problematic if our test using mock from EasyMock.

So here is the example on how to create a mock using EasyMock programmatically:

AccountDAO accountDAOMock = EasyMock.createMock(AccountDAO.class);

Move it to Spring ApplicationContext like this:

<bean id="accountDAOMock" class="org.easymock.EasyMock"
		factory-method="createMock">
      <constructor-arg index="0"
            value="com.argus.camc.datamanagement.interfaces.AccountDAO" />
</bean>

Happy unit testing!

My top Eclipse keyboard shortcut

eclipse logo

If you use Eclipse a lot you must have your favorite keyboard shortcut. Here are mine:

  1. Context Assist (CTRL+SPACE), I believe this is all favorite since they just have so many functionality. You want assistance about class name/method name/variable name. You get all. More than that, it also shows the template available for the context. It even suggests the possible variable name/method name. And it works in all places, even in the dialogs. This is a must know keyboard shortcut for all Eclipse user. Without it, you just will not get anything from Eclipse.
  2. Format (Shift+Ctrl+F), I usually type anything as fast as I can without bothering the format of the code. This shortcut will tidy up all the clutter and make my code readable.
  3. Save (Ctrl+S). OK, I know that this is a must for nearly all applications.
  4. Quick Fix (Ctrl+1). You get error or warning? Many times you will find that this shortcut can solve the problem for you.
  5. Rename (Ctrl+2+R). Since I’m a refactoring guy, I never escape from this task, renaming variables/methods/classes.
  6. Quick Type Hierarchy (Ctrl+T). Nice way to get information about classes tree without opening JavaDoc

So if you are also using Eclipse, what’s your favorite keyboard shortcut?

Java Tips: MessageFormat

Writing a working code is not enough. There is so wide spectrum from where we can distinguish good programmer and a bad one. This is one example of it.

Suppose we need to write a String that concatenate several predefined substrings with several variables. What do we do? Obviously, the most natural thing is to write:

String c = "The book with title " + title + " is sold with price "
              + price + " to " + buyername;

Nothing wrong with the code but obviously if the sentence build many times, you already know that it’s not that efficient. The use of StringBuffer or StringBuilder in Java 5 can make it much better.

String c = new StringBuilder("The book with title ").append(title)
                    .append(" is sold with price ").append(price)
                    .append(" to ").append(buyername);

Great! But not for future development. The code looks cryptic and hard to be changed. The use of MessageFormat is the best solution for such problem (which unfortunately, also the most inefficient).

String result = MessageFormat.format(
     "The book with title {0} is sold with price {1} to {2}",
      new Object[] {title, price, buyername});

This is even better since we can customize the format of the price, for example:

String result = MessageFormat.format(
     "The book with title {0} is sold with price {1,number,currency} to {2}",
      new Object[] {title, price, buyername});

Fixing Samba symbolic links' problem from Leopard

Oh no… all symbolic links are not accessible from Leopard. It shows error message that the target links can’t be found. They are OK with Tiger, so I’m thinking that there is a bug in Leopard. For a while, I can live with copying the file from Linux to Mac and worked with the copy in Mac. But when the first update of Leopard is out, it’s still not fixed so I somehow curious what really happen here.

So today I spent some time to examine the problem and found a suggestion from here to restart the samba server everytime mac connects to it. It’s not that convenient.

Reading more to the links given by it, I found out that simpler solution is available. Just by adding this line to smb.conf (in Ubuntu the location is /etc/samba/smb.conf), the problem can be resolved.

unix extensions = no