Using persistence.xml and ejb3unit

January 23, 2008

Lately the “Don’t Repeat Yourself”, or DRY, principle is quite popular. I like it quite a bit myself. However, when using ejb3unit, I found that I had to maintain two lists of persistence classes – one for ejb3unit, and one for the java persistence system. Today I finally decided to address this problem, and I haven’t regretted it!

I wrote a method which finds and parses my META-INF/persistence.xml and returns an array of classes from it. I can pass this to the constructor of the BaseSessionBeanFixture when I create the test case object.

Here it is:

 public static Class[] getDbClasses() {

        URL[] persistenceUnits;

 	try {

 		persistenceUnits ="META-INF/", "persistence.xml");

 	} catch (IOException e) {

 		throw new Error(e);


        Set> classes = new HashSet>();

        for (int i = 0; i < persistenceUnits.length; i++) {

            URL url = persistenceUnits[i];

    		try {

             nu.xom.Builder b = new nu.xom.Builder(false);

             Document d =;

             Nodes unitNodes = d.getRootElement().query("//p:persistence-unit",  			new XPathContext("p", ""));

             for(int j=0; j < unitNodes.size(); j++) {

             	Node unitNode = unitNodes.get(j);

            	Element unitElt = ((Element)unitNode);

 		String unitName = unitElt.getAttributeValue("name");

 	        Nodes classNodes = unitElt.query("//p:class",  			new XPathContext("p", ""));

 	        for(int k=0; k < classNodes.size(); k++) {

            		Node classNode = classNodes.get(k);

             		if(!(classNode instanceof Element))


         			Element classElt = (Element)classNode;



 	            	String className = classNode.getValue();

 	                System.out.println("   class: "+className);

 	            	Class classInstance = Class.forName(className);




    		} catch(Exception x) {


    			throw new Error(x);



        return classes.toArray(new Class[classes.size()]);


This is making use of the “Classpath” utility class from facelets, available here. Also note that you’ll have to replace “my-persistence-context” with your own persistence context name.

This also serves as an example of how to find things in the classpath and configure yourself; I’ve used for my own GWT templating system based on facelets. I used the facelets code to find my own tag libraries, just the way that facelets does. It’s quite a nice model for auto-discovery!

Software testing with

January 11, 2008

I just ran into this interesting tool at  It’s a combination bug reporter and screen capture tool designed to help clearly document and describe test results.  Easily adding some screenshots and a video of what you did seems like a great way to report bugs!  It integrates with trac, bugzilla, and others which lets the tester submit directly to your favorite bug tracker.  It looks very nice!

It’s free during beta and for single-user use, and they’re planning to charge $300/year for it after beta for 1-5 user setups.