[jboss-user] [jBPM] - jBPM 5 database testing
Marco Rietveld
do-not-reply at jboss.com
Wed Jul 20 12:01:01 EDT 2011
Marco Rietveld [http://community.jboss.org/people/marco.rietveld] modified the document:
"jBPM 5 database testing"
To view the document, visit: http://community.jboss.org/docs/DOC-17071
--------------------------------------------------------------
*Note: the actual implementation of this has not yet been merged into the trunk of jBPM!*
In order to allow testing to be done on other databases (instead of H2), the following has been done:
- Overview of modifications
- Explanation
- How to test on other databases
h3. Overview of modifications
The jbpm pom.xml (artifact jbpm) now contains:
** Properties to be used to connect to the database of choice
** A database profile that can be used in order to inject a dependency for the jdbc driver jar for the database
** An example profile, which has been commented out, which shows how do test without using a settings file
2. A org.jbpm.persistence.util.PersistenceUtil class has been added to the jbpm-persistence-jpa module.
3. The jbpm-persistence-jpa module now also has a test-jar goal: the test-jar produced contains the PersistenceUtil class mentioned above.
4. In the modules in which persistence is used during testing, the following modifications have been made:
* A datasource.properties file has been added to the src/test/resources directory.
* The pom.xml of the module has been modified so that filtering has been turned on for the src/test/resources directory.
* The persistence.xml file in src/test/resources/META-INF has been modified so that the hibernate.connection.* properties use variables.
How all of these modifications fit together is explained in the following section.
h3. Explanation
We essentially use maven to inject the values we want into the persistence configuration.
Unfortunately, because XA datasource configuration is not uniform across different database vendors, there is also a utility class (org.jbpm.persistence.util.PersistenceUtil) which contains database specific XA datasource configuration and which we use to initiate and configure the XA datasource when testing.
An explanation follows the diagram below:
http://community.jboss.org/servlet/JiveServlet/showImage/102-17071-3-16762/og.jpg http://community.jboss.org/servlet/JiveServlet/downloadImage/102-17071-3-16762/450-303/og.jpg
h5. A. Pom.xml and maven settings:
Properties
The jbpm project root pom (jbpm artifact) contains a number of maven.* properties: a number of these settings are empty, and those that aren't are set to the h2 defaults.
However, we can create a local settings.xml file and run the following:
mvn clean test -ssettings-db.xml -Pdatabase
(The -Pdatabase is explained below)
By filling a local settings.xml file with the jdbc values that we want for, for example, our local PostgreSQL database, we can then use those values later on when running our tests.
Otherwise, we can also directly modify the pom.xml itself and fill the values in there.
Using a profile to inject a dependency
The -Pdatabase option in the mvn command above tells maven to use the profile that has an id of "database".
In the project root pom, we have also defined a "database" profile that uses a dependency. By setting the maven.jdbc.driver.jar property (in either the pom.xml or the settings.xml file that you use) to the path of the appropriate jdbc driver class jar, we can then ensure that the database specific driver classes are available to our tests.
[Depending on demand, we could also add properties to the pom.xml so that the groupId, artifactId, and version of the driver class jar (instead of a system path) are defined and used]
h5. B. Filtering and the datasource.properties and hibernate.xml files:
In the pom.xml of the module in which the testing is done (for example, jbpm-persistence-jpa or jbpm-bam), the following has also been added to the <build> section of the module pom:
<build>
...
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
...
</build>
When maven runs the process-test-resources goal, it +filters+ all files in and under the src/main/resources directory. Any properties that have been defined in the (effective) pom.xml being processed by maven, will be replaced with the values that have been assigned to them.
In this case, the following two files in src/main/resources that are filtered for us:
* datasource.properties
* META-INF/hibernate.xml
In the PersistenceUtil class, we read in the datasource.properties file. The test-compile and process-test-resources goals ensure that the filtered version of this file -- where all maven variables have been replaced with their assigned values -- is placed in the same class path as the rest of the test classes and jars (target/, namely).
hibernate.xml is used by Hibernate when we instantiate an EntityManager.
h5. C. Using the PersistenceUtil in unit tests:
Lastly, when we write any unit tests that use persistence, instead of configuring the datasource and entityManager ourselves, we use the static methods made available to us from the PersistenceUtil class.
In our test class, we can use something like the following code:
import static org.jbpm.persistence.util.PersistenceUtil.*;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
...
import org.junit.After;
import org.junit.Before;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jdbc.PoolingDataSource;
public class MyPersistenceTest {
private PoolingDataSource ds1;
private EntityManagerFactory emf;
@Before
public void setUp() throws Exception {
ds1 = setupPoolingDataSource();
ds1.init();
emf = Persistence.createEntityManagerFactory( PERSISTENCE_UNIT_NAME );
}
@After
public void tearDown() throws Exception {
emf.close();
ds1.close();
}
setupPoolingDataSource() is a static method from the PersistenceUtil Class.
Todo:
- datasource configuration logic in PU class
- what the setupPoolingDataSource method does
[Still under progress.. ]
--------------------------------------------------------------
Comment by going to Community
[http://community.jboss.org/docs/DOC-17071]
Create a new document in jBPM at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2034]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110720/05833448/attachment.html
More information about the jboss-user
mailing list