I am trying to test my EJBs using the built-in EJB container. In production, I rely on a JTA data source configured on the application server. However, during testing, I want to connect to another database (in Derby's memory).
The problem is that I don’t see how to tell the EJB container to override the JTA data source defined in my persistence.xml file (in src / main / resources / META-INF) with a DB derby connected to my internal memory. The JTA data source is defined in the persistence.xml file as follows:
<jta-data-source>jdbc/myDS</jta-data-source>
Attempt 1: using the persistence.xml test
I tried to create a test persistence.xml file (in src / test / resources / META-INF) that defines:
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" /> <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:myDataBase;create=true;" /> <property name="javax.persistence.jdbc.user" value="sa" /> <property name="javax.persistence.jdbc.password" value="password" />
This is a moot point, though, because when I tell the EJB container to use the module being tested with
Map properties = new HashMap(); properties.put(EJBContainer.MODULES, new File[] { new File("target/classes/") }); ec = EJBContainer.createEJBContainer(properties);
the container just uses the main persistence.xml file instead of my test one.
Attempt 2: Combining core and test classes into one module with diversity
The only way you can make this approach is to use the described approach here - copy the classes for the module under (for example, target / ejb-testing-classes), then copy the testistence.xml file from above, then specify this new location in the EJB container :
Map properties = new HashMap(); properties.put(EJBContainer.MODULES, new File[] { new File("target/ejb-testing-classes/") }); ec = EJBContainer.createEJBContainer(properties);
But that seems overly clumsy. It may also be a problem in the future if I try to deploy pre-packaged modules (i.e. dependencies) in a container, since I will need to explode the cans before merging.
My wishes: data source override properties for an EJB container
I thought there might be additional properties that can be passed to the EJB container, but so far I can only find properties suitable for openEJB or websphere . I use the built-in Glassfish to provide my built-in EJB container as it is the target platform. (I now found the glassfish property - see Update No. 1 below)
Of course, everyone who tried to test EJB with an embedded EJB container and a data source other than the data in the production database encountered this problem. Even this guy just gave up on this point and used the default built-in database, which for me is not an option.
Any help would be greatly appreciated.
Update 1: I found a list of properties that the Glassfish EJB container accepts, and at first it seems that I could use the following property
org.glassfish.ejb.embedded.glassfish.configuration.file
to determine the data source in domain.xml and point the container to it. However, according to the source code , this property is ignored if the install.root property is also not set - and this will mean the need for pre-configuration, the existing installation of glass fish only to run my tests. This would unacceptably reduce the portability of my Maven project. :(
Update 2: I created a JIRA problem for this problem and recommended introducing properties for the EJB container for glass fish, which allows you to configure the JTA data source.