Monthly Archives: April 2009

DEFINITION OF A BUSINESS OBJECT.

WHAT IS A BUSINESS OBJECT (BO)?

Having both state & behavior is an important criterion for a class to be considered as business object.

It may represent a person, place, thing or concept from business domain.

It should be re-usable.

Generally business objects are classified into:
Entity BO: Nouns from business.
Process BO: Work flow or business process.
Event BO: Causes / caused by action in system. Represents an event.

BOs provide common terminology & ideas that can be shared across organization by technical & non-technical people alike.

They are very intuitive & should make sense to entire organization.

Have ability to evolve with organizational changes through modification / specialization.

BOs should have well defined interfaces so as to hide low-level implementation.

Should have clear & cohesive set of functionality.

References:
Programming Jakarta Struts
By – Chuck Cavaness
O’reilly

Anup Jani
(Bsc, MCP, GNIIT, SCJP, SCWCD, IBM OOAD/UML JCert.)
My profile: www.linkedin.com/in/anupjani
My Blog: www.anupjani.wordpress.com

SIMPLE XML/XSD/XSLT EXAMPLE FOR BEGINNER THAT WORKS!!!

CSS is Style Sheet for HTML
HTML uses predefined tags, and the meaning of each tag is well understood.
The <table> tag in HTML defines a table – and a browser knows how to display it.
Adding styles to HTML elements are simple.
Telling a browser to display an element in a special font or color, is easy with CSS.

XSL is Style Sheet for XML
On the other other hand,
XML does not use predefined tags (we can use any tag-names we like), and therefore the meaning of each tag is not well understood.
A <table> tag could mean an HTML table, a piece of furniture, or something else – and a browser does not know how to display it.

XSL describes how the XML document should be displayed!

You will need:
A good XML editor (even a notepad can do). I used XML Copy Editor 1.2.0.2
A browser. (I used Mozilla Fire Fox 3.0.8).

Create c:\country.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<?xml-stylesheet type=”text/xsl” href=”country.xsl”?>
<country xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=”country.xsd”>

<countryName>Australia</countryName>
<capital>Canberra</capital>
<nationalLanguage>English</nationalLanguage>
<population>21000000</population>
<currency>Australian Dollar</currency>
<nationalIdentities>
<nationalAnthem>Advance Australia Fair</nationalAnthem>
<nationalDay>Australia Day (26 January)</nationalDay>
<nationalColour>Green and Gold</nationalColour>
<nationalGemstone>Opal</nationalGemstone>
<nationalFlower>Wattle (Acacia pycnantha)</nationalFlower>
</nationalIdentities>
<publicHolidays>
<newYearDay>1 January</newYearDay>
<australiaDay>26 January</australiaDay>
<anzacDay>25 April</anzacDay>
<christmasDay>25 December</christmasDay>
<boxingDay>26 December</boxingDay>
<laborDay>Variable Date</laborDay>
<easter>Variable Date</easter>
<queenBirthDay>21 April (Variable Date)</queenBirthDay>
</publicHolidays>
<states>
<stateName>NSW – New South Wales</stateName>
<stateName>VIC – Victoria</stateName>
<stateName>QLD – Queensland</stateName>
<stateName>SA – South Australia</stateName>
<stateName>WA – Western Australia</stateName>
<stateName>TAS – Tasmania</stateName>
</states>
<territories>
<territoryName>ACT – Australian Capital Territory</territoryName>
<territoryName>NT – Northern Territory</territoryName>
</territories>
</country>

Create c:\country.xsd
<?xml version=”1.0″ encoding=”UTF-8″?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”&gt;

<xs:element name=”country”>
<xs:complexType>
<xs:sequence>
<xs:element name=”countryName” type=”xs:string”/>
<xs:element name=”capital” type=”xs:string”/>
<xs:element name=”nationalLanguage” type=”xs:string”/>
<xs:element name=”population” type=”xs:double”/>
<xs:element name=”currency” type=”xs:string”/>
<xs:element name=”nationalIdentities”>
<xs:complexType>
<xs:sequence>
<xs:element name=”nationalAnthem” type=”xs:string”/>
<xs:element name=”nationalDay” type=”xs:string”/>
<xs:element name=”nationalColour” type=”xs:string”/>
<xs:element name=”nationalGemstone” type=”xs:string”/>
<xs:element name=”nationalFlower” type=”xs:string”/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name=”publicHolidays”>
<xs:complexType>
<xs:sequence>
<xs:element name=”newYearDay” maxOccurs=”1″ type=”xs:string”/>
<xs:element name=”australiaDay” maxOccurs=”1″ type=”xs:string”/>
<xs:element name=”anzacDay” maxOccurs=”1″ type=”xs:string”/>
<xs:element name=”christmasDay” maxOccurs=”1″ type=”xs:string”/>
<xs:element name=”boxingDay” maxOccurs=”1″ type=”xs:string”/>
<xs:element name=”laborDay” maxOccurs=”1″ type=”xs:string”/>
<xs:element name=”easter” maxOccurs=”1″ type=”xs:string”/>
<xs:element name=”queenBirthDay” maxOccurs=”1″ type=”xs:string”/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name=”states”>
<xs:complexType>
<xs:sequence>
<xs:element name=”stateName” type=”xs:string” minOccurs=”1″ maxOccurs=”unbounded”/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name=”territories”>
<xs:complexType>
<xs:sequence>
<xs:element name=”territoryName” maxOccurs=”unbounded”/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

Create c:\country.xsl
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221; version=”1.0″>
<xsl:template match=”/”>
<html>
<body>
<xsl:for-each select=”country”>
<xsl:value-of select=”countryName”/><br/>
<xsl:value-of select=”capital”/><br/>
<xsl:value-of select=”nationalLanguage”/><br/>
<xsl:value-of select=”population”/><br/>
<xsl:value-of select=”currency”/><br/>
<xsl:for-each select=”nationalIdentities”>
<xsl:value-of select=”nationalAnthem”/><br/>
<xsl:value-of select=”nationalDay”/><br/>
<xsl:value-of select=”nationalColour”/><br/>
<xsl:value-of select=”nationalGemstone”/><br/>
<xsl:value-of select=”nationalFlower”/><br/>
</xsl:for-each>
<xsl:for-each select=”publicHolidays”>
<xsl:value-of select=”newYearDay”/><br/>
<xsl:value-of select=”australiaDay”/><br/>
<xsl:value-of select=”anzacDay”/><br/>
<xsl:value-of select=”christmasDay”/><br/>
<xsl:value-of select=”boxingDay”/><br/>
<xsl:value-of select=”laborDay”/><br/>
<xsl:value-of select=”easter”/><br/>
<xsl:value-of select=”queenBirthDay”/><br/>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Execute the program
Just open the country.xml in your browser.

Moreover
Above is a very simple output.
You can further beautify your output by modifying your country.xsl
Just create another c:\beautifiedcountry.xsl
And replace the name from “country.xsl” to “beautifiedcountry.xsl” in your country.xml
And open the XML to see the difference.

Copy this c:\beautifiedcountry.xsl
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221; version=”1.0″>
<xsl:template match=”/”>
<html>
<body>
<table border=”2″ bgcolor=”lightgray”>
<tr><u><b>GENERAL INFORMATION</b></u></tr>
<tr>
<xsl:for-each select=”country”>
<tr>
<table border=”1″ bgcolor=”lightblue”>
<tr><td>Country Name</td><td><xsl:value-of select=”countryName”/></td></tr>
<tr><td>Capital</td><td><xsl:value-of select=”capital”/></td></tr>
<tr><td>National Language</td><td><xsl:value-of select=”nationalLanguage”/></td></tr>
<tr><td>Population</td><td><xsl:value-of select=”population”/></td></tr>
<tr><td>Currency</td><td><xsl:value-of select=”currency”/></td></tr>
</table>
</tr>
<tr><br></br></tr>
<tr><u><b>NATIONAL IDENTITIES</b></u></tr>
<tr>
<xsl:for-each select=”nationalIdentities”>
<table border=”1″ bgcolor=”lightyellow”>
<tr>
<td>National Anthem</td><td><xsl:value-of select=”nationalAnthem”/></td>
</tr>
<tr>
<td>National Day</td><td><xsl:value-of select=”nationalDay”/></td>
</tr>
<tr>
<td>National Colour</td><td><xsl:value-of select=”nationalColour”/></td>
</tr>
<tr>
<td>National Gem Stone</td><td><xsl:value-of select=”nationalGemstone”/></td>
</tr>
<tr>
<td>National Flower</td><td><xsl:value-of select=”nationalFlower”/></td>
</tr>
</table>
</xsl:for-each>
</tr>
<tr><br></br></tr>
<tr><u><b>PUBLIC HOLIDAYS</b></u></tr>
<tr>
<xsl:for-each select=”publicHolidays”>
<table border=”1″ bgcolor=”LightGreen”>
<tr>
<td>New Year’s Day</td><td><xsl:value-of select=”newYearDay”/></td>
</tr>
<tr>
<td>Australia Day</td><td><xsl:value-of select=”australiaDay”/></td>
</tr>
<tr>
<td>ANZAC Day</td><td><xsl:value-of select=”anzacDay”/></td>
</tr>
<tr>
<td>Christmas Day</td><td><xsl:value-of select=”christmasDay”/></td>
</tr>
<tr>
<td>Boxing Day</td><td><xsl:value-of select=”boxingDay”/></td>
</tr>
<tr>
<td>Labor Day</td><td><xsl:value-of select=”laborDay”/></td>
</tr>
<tr>
<td>Easter</td><td><xsl:value-of select=”easter”/></td>
</tr>
<tr>
<td>Queen’s Birthday</td><td><xsl:value-of select=”queenBirthDay”/></td>
</tr>
</table>
</xsl:for-each>
</tr>
</xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Recommended Reference:
http://www.w3schools.com/xsl/xsl_languages.asp

Anup Jani
(Bsc, MCP, GNIIT, SCJP, SCWCD, IBM OOAD/UML JCert.)
My profile: http://www.linkedin.com/in/anupjani
My Blog: http://www.anupjani.wordpress.com

SIMPLE JUNIT EXAMPLE FOR BEGINNER THAT WORKS!!!

This Hello World program demonstrates basic features of JUnit.

All you need is:
1. Eclipse
2. JDK1.5
3. junit.jar (Download from http://www.junit.org)

In this program, usage of JUnit is demonstrated in order to test 2 components, namely:
1. Dice.java
2. DiceFactory.java

Test requirement for every new Dice created:
1. Playing a dice every time should get any number between (including) 1 to 6
2. A dice has 6 sides & no two sides should hold the same number.
3. Dice should be unbiased. In an unbiased dice, the sum of 2 opposite side numbers is always 7.
e.g. If back side is 6 then front side has to be 1.

Create Java project in eclipse.
NOTE: Make sure junit.jar is set in your classpath.

Folder structure
C\MyWorkspace\HelloJUnit:
│ .classpath
│ .project

├───bin
│ │ DiceTest.class
│ │
│ └───com
│ ├───mymodel
│ │ Dice.class
│ │
│ └───myservice
│ DiceFactory.class

├───src
│ └───com
│ ├───mymodel
│ │ Dice.java
│ │
│ └───myservice
│ DiceFactory.java

└───test
DiceTest.java

Create Dice.java
package com.mymodel;

import java.util.Random;

/**
* @author Anup
* A dice that has 6 sides, namely:<br>
* top, bottom, left, right, front & back.<br>
*/
public class Dice {

//Dice has six sides, each one carrying a number.
private int topSide = 0;
private int bottomSide = 0;
private int leftSide = 0;
private int rightSide = 0;
private int frontSide = 0;
private int backSide = 0;

/**
* Parameterised constructor.
* @param topSide
* @param bottomSide
* @param leftSide
* @param rightSide
* @param frontSide
* @param backSide
*/
public Dice(int topSide,
int bottomSide,
int leftSide,
int rightSide,
int frontSide,
int backSide) {
this.topSide = topSide;
this.bottomSide = bottomSide;
this.leftSide = leftSide;
this.rightSide = rightSide;
this.frontSide = frontSide;
this.backSide = backSide;
}

/**
* This method plays the dice to get a number.
* @return
*/
public int throwDice() {
return getRandomNumber();
}

public int getTopSide() {
return topSide;
}

public int getBottomSide() {
return bottomSide;
}

public int getLeftSide() {
return leftSide;
}

public int getRightSide() {
return rightSide;
}

public int getFrontSide() {
return frontSide;
}

public int getBackSide() {
return backSide;
}

/**
* Private method to get a random number.
* @return
*/
private int getRandomNumber() {
Random r = new Random();
int n = 0;
while (true) {
n = Math.abs(r.nextInt()) % 7;
if(n <7 && n >0)
return n;
}
}

}
Create DiceFactory.java

package com.myservice;

import com.mymodel.Dice;

/**
* @author Anup
* A factory class that deals with Dice object creation.
*/
public class DiceFactory {

/**
* Creates a dice object.
* @param top
* @param bottom
* @param left
* @param right
* @param front
* @param back
* @return
*/
public Dice getDice(
int top,
int bottom,
int left,
int right,
int front,
int back) {
return new Dice(top, bottom, left, right, front, back);
}

}
Create DiceTest.java
import java.util.HashSet;
import java.util.Set;

import junit.framework.TestCase;

import com.mymodel.Dice;
import com.myservice.DiceFactory;

/**
* @author Anup
* A test case to unit test the Dice Program.
*/
public class DiceTest extends TestCase {

int top = 1;
int bottom = 6;
int left = 4;
int right = 3;
int front = 2;
int back = 5;
DiceFactory diceFactory = null;
Dice dice = null;

//Constructor
public DiceTest(String testMethodName) {
super(testMethodName);
}

//Initialise
public void setUp() {
diceFactory = new DiceFactory();
}

//Dice should not be null.
public void testGetDice() {
dice = diceFactory.getDice(top, bottom, left, right, front, back);
assertNotNull(dice);
System.out.println(“Dice created.”);
this.dice = dice;
}

//When dice thrown, should get any number between (including) 1 to 6
public void testThrowDice() {
dice = diceFactory.getDice(top, bottom, left, right, front, back);
int n = dice.throwDice();
assertTrue(n<7 && n>0);
System.out.println(“Dice number is between (including)1 & 6 = ” + n);
}

//Making sure NO two sides are same.
public void testUniquenessOfSides() {
dice = diceFactory.getDice(top, bottom, left, right, front, back);
Set set = new HashSet();//Set holds unique values.
String args[] = {
dice.getTopSide()+””,
dice.getBottomSide()+””,
dice.getLeftSide()+””,
dice.getRightSide()+””,
dice.getFrontSide()+””,
dice.getBackSide()+””
};
for(String i : args) {
assertTrue(set.add(i));
}
System.out.println(“No duplicates detected!”);
}

//Dice should be unbiased.
//In an unbiased dice, the sum of 2 opposite side numbers is always 7.
//e.g. If back side is 6 then front side has to be 1.
public void testUnbiased() {
dice = diceFactory.getDice(top, bottom, left, right, front, back);
assertEquals(dice.getFrontSide() + dice.getBackSide(), 7);
assertEquals(dice.getLeftSide() + dice.getRightSide(), 7);
assertEquals(dice.getTopSide() + dice.getBottomSide(), 7);
System.out.println(“Dice is unbiased.”);
}

//De-initialise
public void tearDown() {
diceFactory = null;
dice = null;
System.out.println(“Tear down <<<<“);
}

}

Execute the program
Wen you are done building project, right click on project.
Project –> Run As –> JUnitTest
There is no main method here.
That’s the whole idea. We have JUnit test methods instead.

Anup Jani
(Bsc, MCP, GNIIT, SCJP, SCWCD, IBM OOAD/UML JCert.)
My profile: http://www.linkedin.com/in/anupjani
My Blog: http://www.anupjani.wordpress.com