Thursday, November 29, 2007

JUnit Mock Object and JPerf

JUnit is good when writing reusable simple common package. When we try to write JUNIT for business components its always difficult, because of multiple interactions with different servers/components. How to handle this situation? for example, when server X is down system should take alpha path otherwise continue with beta path. Best approach is with Mock object, Mock objects are simple java class which represents external system/components. We can easily simulate this situation by setting status of mock object as "DOWN". So it throws back ServerUnAvailable exception.

Mock object has close relationship with stub, but stub just represents object; does not contain status.

Following are good on this topic,
1. Wiki
2. Article

Why JPERF here, i needed to refresh JPERF for our load testing now so :)

Good thing about JPERF is, its built to run JUNIT test cases. So we don't need to spend time on writing separate code for Load testing. Existing JUNIT code can be easily extended for load testing,

Example code,

int users = 10;int iterations = 20;
Timer timer = new ConstantTimer(1000);
Test testCase = new ExampleTestCase("testOneSecondResponse");
Test repeatedTest = new RepeatedTest(testCase, iterations);
Test loadTest = new LoadTest(repeatedTest, users, timer);

How simple? more

Monday, November 19, 2007

EJB interface Backward compatibility

When working on core piece of enterprise application, we face lot of challenges in terms of versions. We cannot force all the client applications to upgrade to new release at the same time.

1. interface needs to be well defined
2. when planning for next release, need to take care of backward compatibility, this can be easily achieved by serial version id. This can be defined with any random Long number (there is no rule to define at first time).
But what happens, if we missed to specify serial version id at the first release? How to handle this? use "serialver" command with the old class and find out SUID and then define that in the next release.

For example:
serialver test.test.testa.TestA
test.test.testa.TestA: static final long serialVersionUID = -8902737804282067121L;



What are all the changes acceptable as backward compatible?

Compatible Changes:
  • Addition of new fields or classes does not affect serialization, as any new data in the stream is simply ignored by older versions. When the instance of an older version of the class is deserialized, the newly added field will be set to its default value.
  • You can field change access modifiers like private, public, protected or package as they are not reflected to the serial stream.
  • You can change a transient or static field to a non-transient or non-static field, as it is similar to adding a field.
  • You can change the access modifiers for constructors and methods of the class. For instance a previously private method can now be made public, an instance method can be changed to static, etc. The only exception is that you cannot change the default signatures for readObject() and writeObject() if you are implementing custom serialization. The serialization process looks at only instance data, and not the methods of a class.

Incompatible changes:

  • Once a class implements the Serializable interface, you cannot later make it implement the Externalizable interface, since this will result in the creation of an incompatible stream.
  • Deleting fields can cause a problem. Now, when the object is serialized, an earlier version of the class would set the old field to its default value since nothing was available within the stream. Consequently, this default data may lead the newly created object to assume an invalid state.
  • Changing a non-static into static or non-transient into transient is not permitted as it is equivalent to deleting fields.
  • You also cannot change the field types within a class, as this would cause a failure when attempting to read in the original field into the new field.
  • You cannot alter the position of the class in the class hierarchy. Since the fully-qualified class name is written as part of the bytestream, this change will result in the creation of an incompatible stream.
  • You cannot change the name of the class or the package it belongs to, as that information is written to the stream during serialization.

This is just extract from vast subject. To understand more refer below links

Friday, November 16, 2007

XML Graphics

I have been working on Enterprise application for more than 6 years which has got lot of components and business rules. I spent most of my first 2 years to understand business rules and interaction between different components. As a best practise we introduced "Ramp up Session" to understand enterprise applications and different components for new joiners. Always pictorial representation is easy to understand, so we designed this as white board session. But we could not document this (draw picture/slides) because lot of scenarios and keep on changing flow to accommodate business needs. So I was looking for a tool which can be highly configurable. Today while going through Apache open tools found xmlgraphics. This is what I am looking for and it satisfies all my requirements.

  • Easy to configure shapes (2D and 3D) using XML
  • Visual effect can also be defined
  • Animation
  • User interactions
Downloads / Sources:
  • SVG Browser can be downloaded from this link and it has got lot of examples. Try out all samples to understand more.
  • SVG w3c document can be found here
Example SVG file (which i created with my little (30 mins) understanding of SVG):

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="12cm" height="4cm" viewBox="0 0 1200 400"
xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x="100" y="100" width="300" height="200"
fill="yellow" stroke="navy" stroke-width="1" />
<circle cx="700" cy="200" r="100"
fill="red" stroke="blue" stroke-width="1" />
<g stroke="green" >
<line x1="400" y1="200" x2="600" y2="200"
stroke-width="5" />
</g>
<text class="title" x="50%" y="10%" font-size="35" text-anchor="middle" >
Component Connection</text>
<text x="200" y="350" style="font-size:48;
filter:none; fill:black; text- anchor:middle">
Component A</text>
<text x="700" y="350" style="font-size:48; filter:none;
fill:black; text- anchor:middle">
Database</text>
</svg>
Output:

Real cool stuff!!! But I am not sure how much time needed to implement for all scenarios with animation.

Thursday, November 15, 2007

Linux setup

I used to think that UNIX is always difficult to install and understand. Yesterday I got old desktop (2001 DELL) and had to format and install OS. Its not worth getting licensed version of Windows on this system, because its just for my daughter to play :)
So decided to install LINUX, its really simple and what a interface. I looked around places for different LINUX version, but satisfied with fedora 8. I did following steps to install it and could complete within 2 hours

1. Downloaded Fedora from fedora (Live CD direct download), save iso file
2. Burn into CD - Check this link for burn CD using Nero
3. Turn on system with the CD, it picks up LINUX, login as root to get full permission

This can be installed into hard disk if needed. Cool stuff!!!

Instant connection to Internet without much setup, and lots of open tools.

Tuesday, November 13, 2007

Why manual code review is so important?

Not everything can be automated and captured as part of code review. There need to be manual code review to capture some of the issues like,

1. How the configuration parameters are wisely used? or over used? Does it follow standard?
2. We cannot say that if 50% code is java comment then code is well maintainable. Really the comment explains step / business logic?
3. Is it extensible code?
4. Business logic duplication?

I recently reviewed code, which is written by 5 member team for 5 months, oops this really needs rework. We could have avoided this problem, if we implemented agile methodology. This is really internal project and could have implemented easily. Its a learning !!!

Sunday, November 4, 2007

Eclipse - running ant which has got mail task

Have you ever written mail ant task and try to run in Eclipse?


<target name="mail" if="report.dir">
<mail mailhost="${mail.host}" mailport="25"
subject="${mail.subject}" messagefile="${log.file}"
tolist="${to.address}">
<from address="${from.address}"/>
<replyto address="${reply.to}"/>
<attachments>
<fileset dir="${report.dir}/html">
<include name="**/*.html"/>
</fileset>
</attachments>
</mail>
</target>


Failed to initialise MIME mail: javax/mail/MessagingException
This task fails with this error. This is because mail task need mail.jar and activation.jar and this does not come with ant installation. There are two steps need to be added here,

1. Download mail.jar and activation.jar and place under ANT_HOME/lib folder
2. Eclipse cannot automatically detect this, so it needs to be manually configured
a. Select windows > preferences
b. Select Ant > Runtime
c. Select Global entries
d. Click on "Add External jars" and add mail.jar and activation.jar

Now execute ant, it should work.