Pet catalog for JavaEE 6 Reengineered
Some time ago, I published the famed Pet Catalog application on Github. It doesn’t seem like much, but there are some hours of work (if not days) behind the scenes. I wanted to write down the objectives of this pet project (noticed the joke here?) and the walls I hit trying to achieve them to prevent others to hit them.
Objectives were very simple at first: get the Pet Catalog to work under TomEE, the Web Profile compliant application server based on Tomcat. I also wanted to prove that by running automated integration tests, so Arquillian was my tool to achieve that. JavaEE 6, TomEE, Arquillian: this couldn’t be simpler, could it? Think again…
First of all, it may seem strange but there’s no place on the Internet you can find the Pet Catalog for JavaEE 6, despite it being set as an example during the whole JavaEE evolution; a simple Google search can easily convince you of that. I think there’s a definite lack there but I’m nothing but persistent. In order to get the sources, I had to get the NetBeans IDE: the PetCatalog is available as an example application.
The second obstacle I had to overcome was that whatever I tried, I couldn’t find a way to make Arquillian inject my
EntityManager in my TestNG test class using the
annotation. I don’t know what the problem is, but switching to JUnit
did the trick. This is a great (albeit dirty) lesson to me: when the
worker has tried everything, maybe it’s time to question the tool.
The final blow was an amazingly stupid question but a fundamental one: how do you create a datasource in TomEE during a test? For this one, I googled the entire Internet and it took me some time to finally get it, so here it is in all its glory. Just update your arquillian.xml file to add the following snippet:
<container qualifier="tomee" default="true"> <configuration> <property name="properties"> PetCatalog = new://Resource?type=DataSource PetCatalog.JdbcUrl jdbc:hsqldb:mem:petcat PetCatalog.JdbcDriver org.hsqldb.jdbc.JDBCDriver PetCatalog.UserName sa PetCatalog.Password PetCatalog.JtaManaged true </property> </configuration> </container>
Finally, I got the thing working and created two tests, one to check the number of pets was right, and the other to verify data for a specific item. If (when?) I have time, I will definitely check the Persistence and Graphene extensions of Arquillian. Or you’re welcome to fork the project.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)