Object content pretty print

January 15, 2013

I had an experience working on a project with a very large team (200+ developers). The team was divided into tiers. There were Portal tier, Enterprise Service tier, and Data Tier. With this kind of team structure, a simple web page loading would invoke software written by all three teams. When things went wrong, it was very difficult to chase down the problem. For that reason, I created a software to print the content of Java objects at run time for logging purpose. I used Java reflection to implement it. Since then, I used it from project to project. It was pretty useful for debugging purpose. Few years after that, I found Apache had created an API for the same functionality.

This blog provides an example of pretty printing of Java object using the Apache common ReflectionToStringBuilder. ReflectionToStringBuilder uses the same method, Java reflection, that I used to print Java object content. The usage is pretty straightforward. All you need to do is to override the toString() method of the classes that you would like to have the pretty print capability with the following,

public String toString() {
           return ReflectionToStringBuilder.toString(this, StandardToStringStyle.MULTI_LINE_STYLE);
}

In order to provide an example, I created three simple classes to show the usage.

prettyprint

Once the toString() methods are overridden, we could use the following code to test it.

package util;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import vo.Person;
import vo.PersonAddress;
import vo.PersonName;

public class Test {

public static void main(String[] args) {

// build test data
Person p = new Person();
PersonAddress pa = new PersonAddress();
pa.setAddressLine1(“5700 Bou Ave “);
pa.setCity(“Rockville”);
pa.setState(“MD”);
pa.setZip(“20852”);

PersonName pn = new PersonName();
pn.setFirstName(“John”);
pn.setLastName(“Doe”);

p.setPersonAddress(pa);
p.setPersonName(pn);

// test the pretty print
System.out.println(ReflectionToStringBuilder.toString(p));

}

}

The expected outcome is here,

vo.Person@5bc85bc8[personName=vo.PersonName@60106010[
FirstName=John
LastName=Doe
MiddleName=<null>
],personAddress=vo.PersonAddress@5e045e04[
addressLine1=5700 Bou Ave
addressLine2=<null>
city=Rockville
state=MD
zip=20852
]]

This is simple and useful for pretty much every projects.

To download the complete example, here are the files.


Unit Testing Effort Estimation (part 1 of 5)

January 13, 2013

As an Architect, I need to provide software project estimates from time to time. There are many project estimation methodologies. The one that I use the most is task breakdown method. I normally start with creating a list of tasks, then I assign man hours to each task to indicate the effort of the task. It is a pretty straightforward method based on expert judgement on each task to come up with an overall effort estimate. The question that I was asked the most about my estimates was the required man hours of unit testing. This blog is an attempt to provide an answer to that question based on my junit test case coding experiences.

This could be valuable for those who needs to plan, budget, or estimate a development effort that involves unit testing. Before I dive into the details,  I need to explain the kind of unit testing and the type of applications that are covered in this discussion. Instead of a long and dry article, I am going to write a series of blogs to provide the answer. In short, this blog is an attempt to provide an answer to the required man hours of unit testing of Java EE applications. Here are the topics that I will cover to get to the final answer.

1. What is unit testing?

  • Unit testing Definition
  • TDD
  • Repeatable unit testing
  • Self-sustainable unit testing
  • Continuous Integration
  • Benefits of unit testing

2. How to unit test Java EE application?

  • Java EE application layers
  • Persistence layer
  • Business layer
  • UI layer
  • Challenges of Java EE unit testing
    • Data Dependency
    • Component Dependency

3. Case Study

  • Example source code
  • Example jUnit test code
  • The Math
  • The effort Estimation

4. Who are doing unit test?


Design a site like this with WordPress.com
Get started