You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Running and Writing Tests with OpenJPA

OpenJPA's unit tests are written using JUnit. For a template for a simple test case, see the code for TestPersistence.java.

If you want to contribute your own test case then follow the guidelines.

Once you have downloaded and built OpenJPA (see Building and Testing), you can run individual tests using the "test" goal to maven. For example:

mvn test -Dtest=TestPersistence

To get more debugging information (e.g., to see the SQL that is being executed against the database), you can enable trace-level logging from the command line using the "openjpa.loglevel" system property. For example:

$ mvn test -Dtest=TestPersistence -Dopenjpa.loglevel=TRACE

[INFO] Scanning for projects...
[INFO] Reactor build order: 
...
690  test  TRACE  [main] openjpa.jdbc.SQL - <t 4261185, conn 3061987> executing prepstmnt 12659709 
   INSERT INTO AllFieldTypes (id, arrayOfStrings, booleanField, byteField, charField, dateField, 
   doubleField, floatField, intField, longField, shortField, stringField) 
   VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [params=(long) 601, (null) null, (int) 0, (byte) 0, 
   (int) 0, (null) null, (double) 0.0, (float) 0.0, (int) 0, (long) 0, (short) 0, (null) null]
701  test  TRACE  [main] openjpa.jdbc.SQL - <t 4261185, conn 3061987> [11 ms] spent
701  test  TRACE  [main] openjpa.jdbc.JDBC - <t 4261185, conn 3061987> [0 ms] commit
702  test  TRACE  [main] openjpa.jdbc.JDBC - <t 4261185, conn 0> [0 ms] close
...
$

Testing against alternate databases

By default, OpenJPA uses the Derby database for testing. The openjpa-persistence-jdbc/pom.xml POM declares various pre-defined databases against which tests can be executed. For example, to test against the stand-alone HSQLDB database, you can run with the "test-hsqldb" profile:

mvn test -Dtest=TestPersistence -Ptest-hsqldb

For databases that are not in the pre-defined list, you can manually specify connection parameters to use for testing under the "test-custom" profile. You will need to manually provide the driver class and specify all of the connection parameters. For example, to test against Oracle, you might run:

mvn test -Dtest=TestPersistence -Ptest-custom \
  -Dopenjpa.custom.driverjar=$(pwd)/drivers/jdbc-oracle-10_2_0_1_0.jar \
  -Dopenjpa.custom.driverclass=oracle.jdbc.driver.OracleDriver \
  -Dopenjpa.custom.url=jdbc:oracle:thin:@HOST:PORT:DBNAME \
  -Dopenjpa.custom.username=USERNAME \
  -Dopenjpa.custom.password=PASSWORD

If you frequently need to test against another database, you can permanently declare the database connection parameters in the ~/.m2/settings.xml file under a custom profile. For example:

<settings>
    <profiles>
        <profile>
            <id>test-oracle</id>
            <properties>
                <test-custom>true</test-custom>
                <openjpa.custom.driverjar>${user.home}/.m2/privaterepos/jdbc-oracle-10_2_0_1_0.jar</openjpa.custom.driverjar>
                <openjpa.custom.driverclass>oracle.jdbc.driver.OracleDriver</openjpa.custom.driverclass>
                <openjpa.custom.url>jdbc:oracle:thin:@HOST:PORT:DBNAME</openjpa.custom.url>
                <openjpa.custom.username>USERNAME</openjpa.custom.username>
                <openjpa.custom.password>PASSWORD</openjpa.custom.password>
            </properties>
        </profile>
    </profiles>
</settings>

This profile can then be executed by running:

mvn test -Dtest=TestPersistence -Ptest-custom,test-oracle
  • No labels