Wednesday, November 24, 2010

Capturing the parameters passed to expected methodof your mock item

We all use easymock to mock java objects to speed up developing our unit tests and focus on the code fragment.

I would like to point out a great feature of this framework which is not known very much. You need to be familiar with easymock already to get the point.

You can capture the object that is passed to your expectation. Please see the code below with comments.

As you might already noticed above, you can easily get the passed object from capturer object. Capturer object was used with static method "capture" of easymock.

Monday, November 15, 2010

Amazon App Store for Android

Rumors and reports of an Amazon competitor to Google’s own Android App Store were confirmed today with Amazon’s invitation letter to developers, posted here on Ubergizmo. The Amazon App Store, while not particularly shocking news, has several important implications that go way beyond just another source of Apps for Android phone users.

Read full article here.

Friday, October 8, 2010

How to add a certificate?

Do you recieve following error: PKIX path building failed: unable to find valid certification path to requested target

That's perhaps the certifcate you requesting is not valid or self signed. If you really trust to the certificate (perhaps it is your development environment) and want to add it to your server, here is how:

Assume that you are trying to reach

1) Execute this on your console:
openssl s_client -connect

(windows users can use FireFox to see the certificate and export it instead of step1 and step2)

2)Copy the certificate information and save it to a file like abc.pem. The part your are going to copy will look like this:


(You should include BEGIN CERTIFICATE and END CERTIFICATE lines too)

(Windows users, can export the file with firefox; just goto website and click on certificate at bottom right; choose details; and then export)

3) Add it to trusted certs:
sudo /opt/java/bin/keytool -import -alias -keystore $JAVA_HOME/jre/lib/security/cacerts -file abc.pem

(if you do not have an idea what is your keystore password, default is 'changeit')

(windows users should execute just keytool, they should not type (sudo /opt...)

When asked, reply "yes".

Please note that if you are developer; possibly you will want to update cacerts in
JDK_HOME/jre/lib/security/cacerts and JAVA_HOME/lib/security/cacerts

Wednesday, August 18, 2010

XML To Beans with XMLBeans & Maven Integration

If you have the xsd of the xml you are dealing with (if you do not have it, you have to create it; which is an interface between you and the party giving or consuming the xml) you can easily build java beans from it with xml-beans. Or you can use it to parse easily.

In a project I am dealing, I created a separate artifact which has dependencies to xmlbeans artifacts and I have a plug-in which automatically builds the related beans from xds's under a given directory.

Here are the dependencies for xml-beans:

After adding dependencies as described above add xmlbeans plugin so that related xml's will be created automatically when "mvn install" is executed:

Now you have an artifact which you can add as a dependency. If you add/change an xsd under "src/main/resources/xsd" path with each "mvn install" you will have a jar with beans representing your xmls.

With created beans you can create or parse xml documents easily. First here is an xml how to read a file:

And here is an example for creating a document:

opt stands for the options:

Please note that package name is determined from name space. As you might noticed in creation bean from file example package name was com.yamanyar.ish. This means xsd has following declaration at beginning:

And corresponding xml will like:

I hope this helps.

Thursday, August 12, 2010

CanonicalSet: Weak References from Set, Shared Instances

Today I needed to work with an approach that I think it's worth to share.

We were running out of memory issues due to many instances of a class. So I decided to make use of shared instances. Instead of creating the instance every time, it is better to check if it was created before and return it in that case.

In order to achieve that we have to have a collection for the objects we had created to check if it was created already.We need to put them in a set; however putting them in a set is a bit problematic: if we put them in a set there will be a strong reference from the set which prevents it to be garbage collected ever. We need a set which we can add objects with weak references so that if there are no more strong references from outside, it will be garbage collected.

Before starting development I goggled if it was already implemented. And of course it was:

You can download sources from and check it too.

Before this development; I had nearly 300.000 instances and now I have only 1040 instance. Of course you need to be sure that your object is immutable. Also be sure with your hashcode() and equals() methods.

The most notable method will be unique() method on CanonicalSet. Create your regular object and pass to this method. It will return you the one in the set if exists and drop the newly created object.