Abhijat M

Abhijat M

Thursday, 31 January 2013

Junit Tests OpenJPA and Websphere EJB3.0 + DB2 9.0

Junit Tests OpenJPA and Websphere EJB3.0 DB2

For those of us with IBM and their beau software aka tools.

Heres what i had to do to configure the websphere Enterprise container to fire JUnits to persist EJB’s over OpenJPA 1.2.3

You will have to Override persistence.xml file settings to start using JDBC over JTA.

Here’s a snippet from my Abstract Test Case class
Most interesting one is the TransactionMode which is correlated to using

prop.getProperty("jdbc.password"));
  configOverrides.put("openjpa.TransactionMode", "local");

Result:
Allows you to reuse a single persistence unit as Websphere has a bug wherein it would not allow multiple persistence Units to load together. Even worse a separate one for test also fails

Just one more thing
Remember to commit or close connections in the end so:

 @After
 public void tearDown() throws Exception {
  entityTransaction.commit();
  super.tearDown();
 }
Complete Abrstract Template
public abstract class AbstractTestCase extends TestCase {

 private Logger logger = LoggerFactory.getLogger(AbstractTestCase.class);
 
 private EntityManagerFactory emf;

 protected EntityManager em;
  
 protected EntityTransaction entityTransaction = null;
 
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 /**
  * @throws java.lang.Exception
  */
 @AfterClass
 public static void tearDownAfterClass() throws Exception {
 }

 /**
  * @throws java.lang.Exception
  * Finally close connection
  * ensures always
  */
 @Before
 public void setUp() throws Exception{
  PropertyConfigurator.configure(AbstractTestCase.class.getResource("log4j.properties").getFile());
  logger.debug("Initialise Setup");
  Properties prop = new Properties();
   
     try {
               //load a properties file
      prop.load(AbstractTestCase.class.getResourceAsStream("TestSuite.properties"));
 
     } catch (IOException ex) {
      ex.printStackTrace();
        }

  logger.debug("Loading properties file from TestSuite.properties");

  Map configOverrides = new HashMap();
       
  configOverrides.put("openjpa.jdbc.Schema", prop.getProperty("jdbc.schemaname"));    
  configOverrides.put("openjpa.ConnectionURL", prop.getProperty("jdbc.connectionurl"));
  configOverrides.put("openjpa.ConnectionDriverName", prop.getProperty("jdbc.connectionDriverName"));
  configOverrides.put("openjpa.ConnectionUserName", prop.getProperty("jdbc.username"));
  configOverrides.put("openjpa.ConnectionPassword", prop.getProperty("jdbc.password"));
  configOverrides.put("openjpa.TransactionMode", "local");
  logger.debug("Finished load of properties file from TestSuite.properties");

     emf = Persistence.createEntityManagerFactory("Entity", configOverrides);
     
     em = emf.createEntityManager();
     entityTransaction = em.getTransaction();
     entityTransaction.begin();
  super.setUp();
 }
 
 /**
  * @throws java.lang.Exception
  */
 @After
 public void tearDown() throws Exception {
  logger.debug("Initialise tearDown");
  em.close();
  emf.close();
  logger.debug("All connections closed Successfully");
 }