Open minds leads to freedom

Communicating my thoughts on software development

When to use TestNG or JUnit

Posted by rnaufal on September 2nd, 2011

There is an interesting discussion between using TestNG or JUnit on Java projects at Javalobby. It’s very worth reading.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , , ,
Posted in Uncategorized | No Comments »

Some interesting Maven tips

Posted by rnaufal on August 24th, 2011

Here are some Maven tips, extracted from Javalobby:

  • Maven rf option: If your project has some modules and the build fails at one of them, it’s possible to run the build only at this module, preventing from running the entire build. You can achieve this behavior by executing this command:
  • mvn -rf my-project clean install

  • Maven pl option: This option allows you to build specific modules instead of building all projects. You can execute the following command to have this behavior:
  • mvn -pl my-project-A,my-project-B clean install

    This command will build only my-project-A and my-project-B.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , , , ,
Posted in Uncategorized | No Comments »

Maven Eclipse plugin using project dependencies

Posted by rnaufal on August 22nd, 2011

Some months ago I was noting the behavior of the Maven Eclipse plugin during the eclipse:eclipse goal.

I realized that its default behavior is to build the dependencies based on the Eclipse projects instead of the installed packages on the repository. During development time, it’s the behavior we need, but if you want to build the Eclipse files using the packages on the repository, you have to use the following command:

mvn eclipse:eclipse -Declipse.useProjectReferences=false

By default, the useProjectReferences flag is set to true, in other words, the plugin will create and reference the projects in Eclipse. Without it you’ll have to use mvn install to make changes available for dependent projects. Very interesting to note.

Update *: After creating the project dependencies, you have to execute mvn install to make it available for mvn eclipse:eclipse on dependent projects for the first time.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , , , ,
Posted in Uncategorized | No Comments »

Configuration classes with Enums

Posted by rnaufal on September 23rd, 2010

As I mentioned on my previous post, an alternative implementation to create Singleton in Java is with Enum types.

Extending the idea, it is interesting to create classes which read configuration values from Properties files with Enum classes. Below is an example:

public enum Configuration {

    HOST("host"),
    PORT("port"),
    MAIL_SERVER("mailServer"),
    INPUT_DIRECTORY("inputDirectory"),
    OUTPUT_DIRECTORY("outputDirectory");

    private static Properties properties;
    static {
        properties = new Properties();
        try {
            properties.load(Configuration.class.getClassLoader().getResourceAsStream(
                "configuration.properties"));
        } catch (Exception e) {
            throw new RuntimeException("Error when loading configuration file", e);
        }
    }

    private String key;

    Configuration(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return properties.getProperty(key);
    }
}

Configuration classes which read values from properties files should be Singletons that are loaded once on the application startup time.

The Configuration values can be used this way:

System.out.println(Configuration.HOST.getValue());

This example shows that the key and value from properties are stored with Enum constants. They are type-safe, they can be easily accessed through code completion in your favourite IDE and they can take advantage of refactoring tools.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , , ,
Posted in Uncategorized | 1 Comment »

Singleton in Java with Enum types

Posted by rnaufal on September 7th, 2010

Java 1.5 introduced the concept of Enum types. They are type-safe constants, which implements equals(), hashCode() and cannot be extended. Each constant can have attributes and override an abstract method created on each Enum class.

Although Singletons are not encouraged, the best way to create it is using Enum types. Here is an example:

public enum Singleton {
    INSTANCE;

    public void sayHello() {
	   System.out.println("Hello World!");
    }
}

And then you call it this way:

Singleton.INSTANCE.sayHello();

Using Enums to create Singletons brings the serialization mechanism already bundled in the Enum type. This technique is described on the Effective Java Second Edition book by Joshua Block.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , , , ,
Posted in Uncategorized | 1 Comment »

Java-Quiz: The Iterator Quiz

Posted by rnaufal on August 3rd, 2010

Danilo sent us an interesting Java-Quiz from the Java Specialists’ Newsletter created by Olivier Croisier. You have to insert your corrective code in place of the //FIXME comment, following these instructions:

  1. Do not modify his existing code, it’s Perfect (of course).
  2. The FIXME tag shows where you’re allowed to insert your corrective code
  3. He must be able to understand your solution when he comes back (so using Reflection is not an option).

Can you find a solution for it?

final List list = new ArrayList() {{ 
      add("Hello"); 
}};
final Iterator iterator = list.iterator();
System.out.println(iterator.next());
list.add("World");
// FIXME : work here while I'm sunbathing
System.out.println(iterator.next());

Later I’ll post my attempt to solve it.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , ,
Posted in Uncategorized | 2 Comments »

Eclipse Log4J template

Posted by rnaufal on May 9th, 2010

My friend Bruno sent me an interesting tip on how to create a Log4J template at Eclipse. Just follow these steps:

  1. Go to Window > Preferences > Java > Editor > Templates
  2. Click New
  3. Write the string logger at the field Name (this name will be used to call the template)
  4. At the field Pattern, write the following:

    private static final Logger LOGGER = Logger.getLogger(${enclosing_type}.class);

  5. Click OK

The variable ${enclosing_type} refers to the enclosing type name. When you are ready, write down logger on the field class declaration to have a logger added to the class.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , , ,
Posted in Uncategorized | No Comments »

Using Hamcrest and JUnit

Posted by rnaufal on March 15th, 2010

Lately I started using the core Hamcrest matchers bundled with the JUnit framework to create more readable unit tests.

Hamcrest matchers were created to improve the readability of unit testing code. It’s a framework which facilitates the creation of matcher objects to match rules specified in unit tests. Some examples will let it to be clearer:

   1 import static org.hamcrest.CoreMatchers.equalTo;
   2 import static org.hamcrest.CoreMatchers.is;
   3 import static org.junit.Assert.assertThat;
   4 
   5 @Test
   6 public void shouldBeTheSamePerson()
   7 {
   8     Person me = new Person( "Rafael" );
   9     Person theOther = new Person( "Rafael" );
  10     assertThat( me, is( theOther ) );
  11 }
  12 
  13 @Test
  14 public void shouldHaveFixedSizeNumbers()
  15 {
  16     List<Integer> numbers = Arrays.asList( 1, 2, 3, 4, 5 );
  17     assertThat( numbers.size(), is( equalTo( 5 ) ) );
  18 }

The first example checks if one Person object is equal to another using the Object equals method, which was overridden in the Person class. The is syntax defines a matcher which is a shorthand to is(equalTo(value)). The second one uses the is(equalTo(value)) matcher to check the size of an integer list of fixed size numbers. The assertThat method is used in conjunction with the is(equalTo(value)) matcher, which makes the test sentence very human readable.

An interesting thing is the possibility to create a custom matcher, like this one which tests if a given list only has even numbers:

   1 public class AreEvenNumbers extends TypeSafeMatcher<Collection<Integer>> {
   2 
   3     @Override
   4     public boolean matchesSafely(Collection<Integer> numbers) {
   5         for (Integer number : numbers) {
   6             if (number % 2 != 0) {
   7                 return false;
   8             }
   9         }
  10         return true;
  11     }
  12 
  13     @Override
  14     public void describeTo(Description description) {
  15         description.appendText("even numbers");
  16     }
  17 
  18     @Factory
  19     public static <T> Matcher<Collection<Integer>> evenNumbers() {
  20         return new AreEvenNumbers();
  21     }
  22 }

And below are two tests which uses the AreEvenNumbers custom matcher:

   1 import static org.hamcrest.CoreMatchers.is;
   2 import static org.junit.Assert.assertThat;
   3 import static br.com.rafael.hamcrest.AreEvenNumbers.evenNumbers;
   4 
   5 @Test
   6 public void shouldHaveOnlyEvenNumbers()
   7 {
   8     List<Integer> numbers = Arrays.asList( 2, 4, 6, 8, 10 );
   9     assertThat( numbers, is( evenNumbers() ) );
  10 }
  11 
  12 @Test
  13 public void shouldNotHaveOddNumbers()
  14 {
  15     List<Integer> numbers = Arrays.asList( 1, 2, 4, 6, 8, 10 );
  16     assertThat( numbers, not( evenNumbers() ) );
  17 }

These two tests use the static factory method evenNumbers to instantiate the matcher on the test code. Not the use of the not matcher on the shouldNotHaveOddNumbers test to assert that no odd numbers are present on the given list. All tests use the static import feature, which turns the test not clean and not cluttered with the class qualification.

I haven’t experienced the other common matchers on unit testing code, like the Beans, Collections and Number ones. I think they turn the tests more readable, clean and easy to change. And you? Have you ever used Hamcrest matcher? If you have other examples of using it, post them here!

Updated: Static imports were added to the testing code.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , , , , ,
Posted in Uncategorized | No Comments »

The power of pair programming

Posted by rnaufal on January 14th, 2010

Some weeks ago I started doing pair programming with some co-workers basically for two things:

  1. code some tasks of an user story
  2. get familiar with a new software code base

I haven’t had an opportunity to put this technique in practice a lot before, but I can say it was extremely important and benefit for the project. Sometimes I was the driver and sometimes I was the observer. The driver is the person who starts coding and the observer is who starts doing the code review. That point is important: code review.

Pair programming encourages the review of the code. Perhaps you won’t have an opportunity to refactor some code as you have when you are pairing with someone. I think code reviews are important because:

  • Reviews increase code quality, because there are 2 people thinking at the same task at the same time.
  • Refactoring areas arise in the design where improvements are needed
  • When you have the strong support of an IDE (as Eclipse), some refactorings (extract method, extract class, introduce parameter) are highly automated
  • Code is more read than written, two people reading the code can understand a lot more about the code base
  • New ideas arise because of different point of views
  • Questions can be solved by the sum of knowledge of the code base

And you? What’s your experience with pair programming?

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , , , , ,
Posted in Uncategorized | 4 Comments »

My paper MIMECORA-DS added as LNCS at SpringerLink

Posted by rnaufal on December 3rd, 2009

I’m proud to announce that my paper A Collaborative Support Approach on UML Sequence Diagrams for Aspect-Oriented Software was added as lecture notes in computer science at SpringerLink.

At this paper it is presented an extension based on the default UML meta-model, named MIMECORA-DS, to show object-object, object-aspect and aspect-aspect interactions applying the UML’s sequence diagram.

You can check it out here.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot

Tags: , , , , , ,
Posted in Uncategorized | 5 Comments »