Overriding JTA into RESOUCE-LOCAL while Unit Testing


references

JTA

Let’s say you have a very simple persistence.xml looks like this.

<persistence-unit name="somePU" transaction-type="JTA">
  <jta-data-source>jdbc/someDS</jta-data-source>
  <exclude-unlisted-classes>false</exclude-unlisted-classes>
  <properties>
    <property name="javax.persistence.validation.mode" value="CALLBACK" />
  </properties>
</persistence-unit>

RESOUCE-LOCAL

You can, maybe you should, override it for unit testing. Following codes uses EclipseLink dependent API.

final Map<String, String> properties = new HashMap<>();
properties.put(PersistenceUnitProperties.TRANSACTION_TYPE,
               PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
properties.put(PersistenceUnitProperties.JDBC_DRIVER,
               "org.apache.derby.jdbc.EmbeddedDriver");
properties.put(PersistenceUnitProperties.JDBC_URL,
               "jdbc:derby:memory:testDB;create=true");
properties.put(PersistenceUnitProperties.DDL_GENERATION,
               PersistenceUnitProperties.CREATE_ONLY);
properties.put(PersistenceUnitProperties.LOGGING_LEVEL,
               Level.FINE.getName());
properties.put(PersistenceUnitProperties.TARGET_SERVER,
               TargetServer.None);
ENTITY_MANAGER_FACTORY
    = createEntityManagerFactory("somePU", properties);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s