Archive

Archive for the ‘Expert’ Category

Host projects on Maven Central

Image representing Sonatype as depicted in Cru...

Image via CrunchBase

Recently, when I decided to make my projects available to the Java Community under Open Source license, I did a lot of things like Hosting my code on Google Code, using Maven to build and test the code and also making sure that I have a project site up and running. However, even after all of this, there was one thing that was missing which was the most essential – “Hosting the project files on a repository so that users who may find these utilities useful may use it”.

I found out that Sonatype would allow me to deploy my code to Maven Central Repository. However, after 3 days of trial and error, I have finally been able to release a version of my code to the Central Repo. I am writing this post so that if you ever want to do this, you will not have to go through the same pain as I did.

Read the full article here (http://scratchpad101.com/2011/09/08/project-files-maven-central/).

Unit Testing Framework: EAMSteps

2 September, 2011 1 comment

EAMSteps is a framework that makes Unit Testing easier because of its following principles:
- External data store for test data like excel spreadsheet
- Automated assertions using co-related inputs and outputs
- Minimal lines of code to get started

It all started in 2009, when I decided that in long term I would need to have a framework in place that will help me meet my objectives and I started to look for any existing tools. When I did not find any framework that would help me, I decided that it was time to write one and here after 2 years I have something breathing.

You can read more about this here.

iFramework: Updates

Last week I have made released a framework iFramework with a coupe of features. Here is another release that adds a few more features to the it. You can find the details

Exception Handling

This simple yet powerful implementation provides some of the most desirable features in any Exception Handling framework:

  • Unchecked Exceptions for Business and System errors
  • Message Externalization
  • Message Localization
  • Configurable Message Data Sources

Message Readers

This package provides factory to fetch various types of Message Readers. The current implementation only supports Resource Bundle based message stores, but it can be easily extended to other data stores.

Concurrent Processing

This package provides base classes that allow you to run your functions in a producer and consumer fashion. By using this framework you have to worry about writing your business logic for a Producer and a Consumer and not about multi-threading and how these two entities interact. This framework currently has only one implementation of a Broker i.e AsyncQueueBroker but more will be added in coming months.

Related articles

A new OSS project: iFramework

24 August, 2011 2 comments

If you have been following me for a long time, you would know that I keep saying of releasing some of my work to the community; and today I finally am proud to announce that I am releasing my first projecthttp://scratchpad101.com/2011/08/24/i-framework/. Today this project is just a set of two/thre utility classes, but over next few months I plan to add a lot more to it.

Current Features:

  • Improved Loggers
  • Database Setup Utilities

So, please go visit the project and have a look on what I have. Also, please let me know if you are looking for something special.

Concurrency Patterns: Producer and Consumer

22 August, 2011 1 comment

In enterprise world, where performance holds the key to everything; the Concurrency patterns bring to table a very interesting and effective solution. One specific pattern Producer and Consumer allow us to write programs with high throughput and get the job done much quickly. This pattern provides us a solution for a common problem where we have to migrate data form System 1 to System 2 and in the process we need to do three tasks: Load data from Database based on groups, Process and Update the records back

You can read the complete post here on Scratchpad101.com (http://scratchpad101.com/2011/08/22/concurrency-pattern-producer-consumer/)

TestNG or JUnit

7 August, 2011 7 comments

For many years now, I have always found myself going back to TestNG whenever it comes to doing Unit Testing with Java Code. Everytime, I picked up TestNG, people have asked me why do I go over to TestNG especially with JUnit is provided by the default development environment like Eclipse or Maven. Continuing the same battle, yesterday I started to look into Spring’s testing support. It is also built on top of JUnit. However, in a few minutes of using the same, I was searching for a feature in JUnit that I have always found missing. TestNG provides Parameterized Testing using DataProviders. Given that I was once again asking myself a familiar question – TestNG or JUnit, I decided to document this so that next time I am sure which one and why.

Essentially the same

If you are just going to do some basic Unit Testing, both the frameworks are basically the same. Both the frameworks allow you to test the code in a quick and effective manner. They have had tool support in Eclipse and other IDE. They have also had support in the build frameworks like Ant and Maven. For starters JUnit has always been the choice because it was the first framework for Unit Testing and has always been available. Many people I talk about have not heard about TestNG till we talk about it.

Flexibility

Let us look at a very simple test case for each of the two.

package com.kapil.itrader;
import java.util.Arrays;
import java.util.List;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class FibonacciTest
{
    private Integer input;
    private Integer expected;

    @BeforeClass
    public static void beforeClass()
    {
        // do some initialization
    }

    @Test
    public void FibonacciTest()
    {
        System.out.println("Input: " + input + ". Expected: " + expected);
        Assert.assertEquals(expected, Fibonacci.compute(input));
        assertEquals(expected, Fibonacci.compute(input));
    }
}

Well, this is example showcases I am using a version 4.x+ and am making use of annotations. Priori to release 4.0; JUnit did not support annotations and that was a major advantage that TestNG had over its competitor; but JUnit had quickly adapted. You can notice that JUnit also supports static imports and we can do away with more cumbersome code as in previous versions.

package com.kapil.framework.core;
import junit.framework.Assert;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class BaseTestCase
{
    protected static final ClassPathXmlApplicationContext context;

    static
    {
        context = new ClassPathXmlApplicationContext("rootTestContext.xml");
        context.registerShutdownHook();
    }

    @BeforeSuite
    private void beforeSetup()
    {
       // Do initialization
    }

    @Test
    public void testTrue()
    {
        Assert.assertTrue(false);
    }
}

A first look at the two code, would infer that both are pretty much the same. However, for those who have done enough unit testing, will agree with me that TestNG allows for more flexibility. JUnit requires me to declare my initialization method as static; and consequently anything that I will write in that method has to be static too. JUnit also requires me to have my initialization method as public; but TestNG does not. I can use best practices from OOP in my testing classes as well. TestNG also allows me to declare Test Suite, Groups, Methods and use annotations like @BeforeSuite, @BeforeMethod, @BeforeGroups in addition to @BeforeClass. This is very helpful when it comes to writing any level of integration testing or unit test cases that need to access common data sets.

Test Isolations and Dependency Testing

Junit is very effective when it comes to testing in isolation. It essentially means that there is you can not control the order of execution of tests. And, hence if you have two tests that you want to run in a specific order because of any kind of dependency, you can not do that using JUnit. However, TestNG allows you to do this very effectively. In Junit you can make workaround this problem, but it is not neat and that easy.

Parameter based Testing

A very powerful feature that TestNG offers is “Parameterized Testing”. JUnit has added some support for this in 4.5+ versions, but it is not as effective as TestNG. You may have worked with FIT you would know what I am talking about. However, the support added in JUnit is very basic and not that effective. I have modified my previous test case to include parameterized testing.

package com.kapil.itrader;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FibonacciTest
{
    private Integer input;
    private Integer expected;

    @Parameters
    public static List data()
    {
        return Arrays.asList(new Integer[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
    }

    @BeforeClass
    public static void beforeClass()
    {
        System.out.println("Before");
    }

    public FibonacciTest(Integer input, Integer expected)
    {
        this.input = input;
        this.expected = expected;
    }

    @Test
    public void FibonacciTest()
    {
        System.out.println("Input: " + input + ". Expected: " + expected);
        Assert.assertEquals(expected, Fibonacci.compute(input));
        assertEquals(expected, Fibonacci.compute(input));
    }

}

You will notice that I have used @RunWith annotation to allow my test case to be parameterized. In this case, the inline method – data() which has been annotated with @Parameters will be used to provide data to the class. However, the biggest issue is that the data is passed to class constructor. This allows me to code only logically bound test cases in this class. And, I will end up having multiple test cases for one service because all the various methods in the Service wil require different data sets. The good thing is that there are various open source frameworks which have extended this approach and added their own “RunWith” implementations to allow integration with external entities like CSV, HTML or Excel files.

TestNG provides this support out of the box. Not support for reading from CSV or external files, but from Data Providers.

package com.kapil.itrader.core.managers.admin;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.uhc.simple.common.BaseTestCase;
import com.uhc.simple.core.admin.manager.ILookupManager;
import com.uhc.simple.core.admin.service.ILookupService;
import com.uhc.simple.dataprovider.admin.LookupValueDataProvider;
import com.uhc.simple.dto.admin.LookupValueRequest;
import com.uhc.simple.dto.admin.LookupValueResponse;

/**
 * Test cases to test {@link ILookupService}.
 */
public class LookupServiceTests extends BaseTestCase
{

    @Test(dataProvider = "LookupValueProvider", dataProviderClass = LookupValueDataProvider.class)
    public void testGetAllLookupValues(String row, LookupValueRequest request, LookupValueResponse expectedResponse)
    {
        ILookupManager manager = super.getLookupManager();
        LookupValueResponse actualResponse = manager.getLookupValues(request);
        Assert.assertEquals(actualResponse.getStatus(), expectedResponse.getStatus());
    }
}

The code snippet above showcases that I have used dataProvider as a value to the annotations and then I have provided a class which is responsible for creating the data that is supplied to the method at the time of invocation. Using this mechanism, I can easily write test cases and its data providers in a de-coupled fashion and use it very effectively.

Why I choose TestNG

For me the Parameterized Testing is the biggest reason why I choose TestNG over Junit. However, everything that I have listed above is the reason why I always want to spend a few minutes in setting up TestNG in a new Eclipse setup or maven project. TestNG is very useful when it comes to running big test suites. For a small project or a training exercise JUnit is fine; because anyone can start with it very quickly; but not for projects where we need 1000s of test cases and in most of those test cases you will have various scenarios to cover.

http://kapilvirenahuja.com/tech/2011/08/07/testng-or-junit/

Internet Filter Bubble

Image representing Google as depicted in Crunc...

Image via CrunchBase

I came across this topic on “filter bubble” via a fellow blogger. Although the blogger talks about the filtering in context to Atheism, amongst other things, I find this discussion very relevant to how we are building our web today and also very relevant to my job today.

I have been a part of a project/product where we did have similar discussions and ensuring that people are getting information relevant to them. The filter bubble that Eli Pariser mentions in his talk is exactly what we did try to build in our product and are even trying to do so today. We pick up programs that people have selected and we then pass on content based on the selections.

We do not go to the extent of what has been mentioned in this post where a common search result gives back different results. Eli also accepts the fact that personalization is important and we would definitely want to have some results based on what we like; it is simply a way to provide links that we like. However, it is equally important for me to view what are other areas in this regard and especially the view point of other group; to bring in a little flavor to the topic. however, if Google and Facebook or Yahoo and other giants are not doing that for us, then we are doomed. They need to add capability to ask the user what they want – preferential results or just results. Users need to have options, to decide what they need to view. We do not want things to be edited out for us and us not knowing what has been edited out. That way we just don’t know what we are missing – thats not what internet is supposed to do. It is like going into Nike and not knowing that Reebok also exists. Not Fair.

 

Cairngorm 3

I chanced upon this yesterday (http://sourceforge.net/adobe/cairngorm/home/) and was impressed with what they are trying to do. Building upon Parsley looking to do a lot of injection and also support modules that they have been criticized for in the past.

Looks like Adobe Cairngorm team finally got it right.

Mobile Web – In a different way

This presentation talks about how mobile web should be thought about and does talks about some numbers too. I like three things about the presentation:

 

1. The creativity with whcih it has been built and;

2. Talks about the core of the problem and;

3. Touches upon how we can go about fixing it.

The Architect’s Eye – Communicating Errors

In many of my projects, I have found architects guilty of preparing a design that leaves the error messages out of the question. And now, I come across an article that shows us 35 creative designs of showing a 404 page (http://www.onextrapixel.com/2011/03/09/the-secret-of-a-successful-error-page-with-35-amazing-404-page-designs/). As I was browsing some of these designs, I recall a designer I worked with – Beth. I learnt so much from her about design and especially Information Architecture. I have always found her looking at things differently be it work or a status update she did.

Coming back to error messages, in most applications I have noticed that error messages are cryptic like “An error has occurred, please check again and get back to System Administrator”. The user is needed to log a report with the call center and report what they were doing. Many a times a user would just ignore to do all that because it takes their time to do such stuff and they say we will come back and try another time – of course if it is not urgent. I see two issues here:

1. A user has been asked to do something that an application designer – An Architect could have done by designing the system right

2. The Application team has lost an opportunity of knowing where their application failed because a user chooses not to report it. They lost an opportunity to fix something pro-actively.

 

Categories: Design, Expert, Quality Tags: , ,
Follow

Get every new post delivered to your Inbox.