Pradeep's Blog

Google

Monday, April 24, 2006

Advantages of Unit Testing:

My project requires me to perform unit testing. Initially we did not follow test driven development, instead the tests were written only after we had some sort of a stable architecture. Writing these test made me realize the advantages of test driven development & unit testing. Here is what I think are the advantages:
  1. You get a very good understanding of the requirements.
  2. Code quality improves, because of you breaking the functionality into much smaller units for unit testing.
  3. Improves your confidence in the application you are building.
  4. I think it enables you to find the problems early.
  5. You are confident to change existing code.

How to integrate Emma with Ant?

I had to integrate Emma with Ant for my project. I will explain the basic steps involved in doing it. Let’s consider that your directory structure is like this-
Base dir
==Source- Containing the source files.
==Classes – Containing all the class files.
==Test – Containing all the JUNit files.
==Lib – Containing all the jar files needed.

The first task is to set the properties & the class paths. This for our directory structure can be done as:

The second task in your ant code is to compile all the file source and test files.



Now we have the class files in the classes directory.The third and the last task is to perform test coverage using emmajava. But before that, emma task should be defined as shown below:

Now we are all set to use emmajava. It can be done as follows:

You have to substitute the “MyTestSuite”, with a class that starts your testing. Also include & exclude packages according to your needs. The above code generates both html and txt reports (which are placed in the coverage-report folder that was newly created).

The complete documentation of emmajava can be seen at emma’s site .

Monday, April 10, 2006

Do you have a big taxing for loop

Does you program have very big for loops that it has to run through always? Are these loops making your program slower? Here is a method to over come it.

Lets just consider that you program requires to go through a for loop a million times, this means that there are a million conditions checked (for the loop to go on).

for(long i=0;i<=list.size();i++)
{
System.out.println(list.get(i).name);
}
This is very taxing on any application. So here what you can do

try
{
long i=0;
while(true)
{
System.out.println(list.get(i).name);
i++;
}
}
catch(ArrayIndexOutOfBounds e)
{
//don’t handle the exception
}


Isn’t it simple?

Good logging design in java

I had to use java’s logging API in my project. Though lots of the information (API & examples) regarding logging can be found online, most of these examples were very simple. These examples were there to explain only to show how certain things can be done using the logging API. None of these sites really explained how logging could be done for larger projects. I looked into other open source projects like hibernate, to look at how they had done their logging before I could start mine. So with that knowledge of how hibernate has done its logging and from my own experience, here are few tips on logging.

- Its always good to have a separate class that generates the logger object. So that any changes to logging mechanism requires you to make changes in just one place.
- Its also better to have your own Log Formatter class, so that you can create customise logging outputs.
- You may have more than one Logformatter class if you are logging to more than one handler.
- I would also recommend you to have just one handler for one logger (I had more that one handler and it complicates things).

Monday, April 03, 2006

System properties in Java

Want to find out the system's default temp directory, users home directory, user’s name, the path, file or the line separator. These things become very useful if you consider to your application to be platform independent. Finding out these things in java has always been easy.
The System class maintains a set of properties, key/value pairs, which define properties of the current working environment. When the JRE first start these system properties are initialised to contain information about the runtime environment. The complete list of system properties can be found here:

Reading System Properties:

The System class has two methods that you can use to read the system properties: getProperty and getProperties. The example below shows how you can print the user’s home directory.

String home = System.getProperty("java.home");
System.out.println(home);

Writing System Properties:

Though java allows us to overwrite the system properties, it should be done with caution. Any how here’s how it can be done.

java.home =”c:\temp “;

This would make change the user’s home directory to the path specifies in the string.
References:

Logging Packages’s in Java:

Advantages of logging:

Logging can generate detailed information about the operation of an application. Unlike normal debugging statements they can be easily turned off when not needed and are easy to maintain & increase productivity. Unlike debugging statements logging frameworks allows us to generate output to different destinations like console, files, sockets etc. But the best of all these functionality would be the ability to turn them off and on (either completely or partially) according to our needs.

Logging packages for Java:

My project required me to use a logging framework. So I had to decide on the framework that we would be using. Of the various logging packages, log4j and Java’s own logging API are notable. Both of them have very similar functionality. Logj4 has been around for a long time and is used widely, whereas the java’s logging package was added with jre1.4 in 2003.

Java’s logging package has all the functions that log4j has. In fact it is nearly a copy of log4j with a bit more added to it. The difference is in the areas of appender/handler implementations, formatter/layout implementations, configuration flexibility, and in the levels of logging.

My choice:

Even though log4j is thoroughly tested and very popular, my choice was to go with Java’s own API because it’s already bundled with JRE and moreover has all the functionality that I would need.