[jboss-svn-commits] JBL Code SVN: r30090 - in labs/jbosstm/workspace/whitingjr/trunk/performance/src/test/java: org/jboss/jbossts/performance/readonly and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Nov 10 12:22:34 EST 2009
Author: whitingjr
Date: 2009-11-10 12:22:33 -0500 (Tue, 10 Nov 2009)
New Revision: 30090
Modified:
labs/jbosstm/workspace/whitingjr/trunk/performance/src/test/java/auction/test/basic/ProfiledStateTransitions.java
labs/jbosstm/workspace/whitingjr/trunk/performance/src/test/java/org/jboss/jbossts/performance/readonly/JPAExampleTests.java
Log:
Updated to support relaxing database constraints.
Modified: labs/jbosstm/workspace/whitingjr/trunk/performance/src/test/java/auction/test/basic/ProfiledStateTransitions.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/performance/src/test/java/auction/test/basic/ProfiledStateTransitions.java 2009-11-10 17:21:58 UTC (rev 30089)
+++ labs/jbosstm/workspace/whitingjr/trunk/performance/src/test/java/auction/test/basic/ProfiledStateTransitions.java 2009-11-10 17:22:33 UTC (rev 30090)
@@ -22,16 +22,31 @@
package auction.test.basic;
+import java.io.BufferedInputStream;
import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
+import java.lang.reflect.Constructor;
import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.persistence.ColumnResult;
import javax.persistence.SqlResultSetMapping;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathException;
+import javax.xml.xpath.XPathFactory;
+import org.apache.commons.dbutils.DbUtils;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseDataSourceConnection;
@@ -42,34 +57,43 @@
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.SessionFactory;
import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
+import org.jboss.jbossts.performance.persistence.vendor.AbstractConstraint;
import org.jboss.jbossts.performance.persistence.vendor.IntegrityConstraint;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
+import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
import auction.test.EJB3IntegrationTest;
import com.mentorgen.tools.profile.runtime.Profile;
+import com.sun.org.apache.xpath.internal.NodeSet;
+ at SqlResultSetMapping(name = "PriceSellerResult", columns =
+{@ColumnResult(name = "IP"), @ColumnResult(name = "SID")
- at SqlResultSetMapping(
- name = "PriceSellerResult",
- columns = {
- @ColumnResult(name = "IP"),
- @ColumnResult(name = "SID")
-
- }
- )
-
+})
public class ProfiledStateTransitions extends EJB3IntegrationTest
{
private static Logger logger = Logger.getLogger(ProfiledStateTransitions.class);
+
protected SessionFactory hSessionFactory;
+
private IntegrityConstraint integrityConstraint;
- protected String SESSION_FACTORY_JNDI ;
- @BeforeGroups(groups = "integration-warmup")
+ protected String SESSION_FACTORY_JNDI;
+
+ @Override
+ public void prepareDataSet(String location, String factory) throws Exception
+ {
+ //Overriden and replaced.
+ }
+
+ @BeforeClass(groups = "integration-warmup")
@Parameters(
{"basedata_location", "datatype_factory", "integrity-constraint"})
public void prepareDataSet(String location, String factory, String constraint) throws Exception
@@ -92,31 +116,32 @@
if (null != constraint && !"".equals(constraint.trim()))
{
- this.integrityConstraint = (IntegrityConstraint) Class.forName(constraint).newInstance();
+ AbstractConstraint abconstraint = (AbstractConstraint) Class.forName(constraint).newInstance();
+ abconstraint.setTableNames(getTableNames(location));
+ this.integrityConstraint = abconstraint;
}
-
}
-
- protected SessionFactory getSessionFactory()
+
+ protected SessionFactory getSessionFactory()
{
SessionFactory returnValue = null;
try
{
- logger.trace("Attempting to lookup session factory with jndi ["+this.SESSION_FACTORY_JNDI+"]");
- returnValue= (SessionFactory) jndi.lookup(SESSION_FACTORY_JNDI);
+ logger.trace("Attempting to lookup session factory with jndi [" + this.SESSION_FACTORY_JNDI + "]");
+ returnValue = (SessionFactory) jndi.lookup(SESSION_FACTORY_JNDI);
}
catch (NameNotFoundException nnfe)
{
logger.error(nnfe.getMessage(), nnfe);
}
- catch ( NamingException ne)
+ catch (NamingException ne)
{
logger.error(ne.getMessage(), ne);
}
return returnValue;
}
- @BeforeGroups(groups = "integration-persistence", dependsOnGroups="integration-warmup")
+ @BeforeGroups(groups = "integration-persistence", dependsOnGroups = "integration-warmup")
public void startProfiler() throws Exception
{
logger.info("Starting profiler.....");
@@ -140,10 +165,6 @@
/* Relax the constraints for this connection allowing bulk imports of data */
Connection con = dbUnitCon.getConnection();
- if (null != this.integrityConstraint)
- {
- this.integrityConstraint.relax(con);
- }
if (null != this.datatypeFactory)
{
@@ -158,62 +179,169 @@
{
beforeTestOperations.add(DatabaseOperation.CLEAN_INSERT);
}
-
-
+
@BeforeGroups(groups = "integration-warmup")
- @Parameters({"session_jndi"})
- public void prepareParameters( String jndiSessionFactory)
- throws Exception
+ @Parameters(
+ {"session_jndi"})
+ public void prepareParameters(String jndiSessionFactory) throws Exception
{
logger.debug("Setting Hibernate SessionFactory JNDI");
- this.SESSION_FACTORY_JNDI = jndiSessionFactory;
+ this.SESSION_FACTORY_JNDI = jndiSessionFactory;
}
+
@Override
@BeforeTest(groups = "integration-warmup")
- @Parameters({"deploy_beans_xml", "scan_classpath",
- "jndi_datasource", "jndi_name_emf", "jndi_name_usertx"})
- public void startContainer(String deployBeansXml, String scanClasspath,
- String jndiDatasource, String jndiNameEMF, String jndiNameUserTx)
- throws Exception {
- // Set configuration options from TestNG parameters
- JNDI_DATASOURCE = jndiDatasource;
- JNDI_NAME_EMF = jndiNameEMF;
- JNDI_NAME_USERTX = jndiNameUserTx;
+ @Parameters(
+ {"deploy_beans_xml", "scan_classpath", "jndi_datasource", "jndi_name_emf", "jndi_name_usertx"})
+ public void startContainer(String deployBeansXml, String scanClasspath, String jndiDatasource, String jndiNameEMF,
+ String jndiNameUserTx) throws Exception
+ {
+ // Set configuration options from TestNG parameters
+ JNDI_DATASOURCE = jndiDatasource;
+ JNDI_NAME_EMF = jndiNameEMF;
+ JNDI_NAME_USERTX = jndiNameUserTx;
- // Boot the JBoss Microcontainer with EJB3 settings, automatically
- // loads ejb3-interceptors-aop.xml and embedded-jboss-beans.xml
- EJB3StandaloneBootstrap.boot(null);
+ // Boot the JBoss Microcontainer with EJB3 settings, automatically
+ // loads ejb3-interceptors-aop.xml and embedded-jboss-beans.xml
+ EJB3StandaloneBootstrap.boot(null);
- // Deploy custom stateless beans (datasource, mostly)
- EJB3StandaloneBootstrap.deployXmlResource(deployBeansXml);
+ // Deploy custom stateless beans (datasource, mostly)
+ EJB3StandaloneBootstrap.deployXmlResource(deployBeansXml);
- // Deploy all EJBs found on classpath (slow, scans all)
- //EJB3StandaloneBootstrap.scanClasspath();
+ // Deploy all EJBs found on classpath (slow, scans all)
+ //EJB3StandaloneBootstrap.scanClasspath();
- // Deploy all EJBs found on classpath (fast, scans only build directory)
- // This is a relative location, matching the substring end of one of java.class.path locations!
- // Print out System.getProperty("java.class.path") to understand this...
- EJB3StandaloneBootstrap.scanClasspath(scanClasspath.replace("/", File.separator));
+ // Deploy all EJBs found on classpath (fast, scans only build directory)
+ // This is a relative location, matching the substring end of one of java.class.path locations!
+ // Print out System.getProperty("java.class.path") to understand this...
+ EJB3StandaloneBootstrap.scanClasspath(scanClasspath.replace("/", File.separator));
- // Create InitialContext from jndi.properties
- jndi = new InitialContext();
+ // Create InitialContext from jndi.properties
+ jndi = new InitialContext();
}
+
@BeforeGroups(groups = "integration-warmup")
- public void beforeTestMethod() throws Exception {
+ @Parameters ({"basedata_location"})
+ public void beforeTestMethod(String basedataLocation) throws Exception {
Profile.stop();
prepareSettings();
+
+ Connection connectionConstraintModify = null;
+ try
+ {
+ if (null != this.integrityConstraint)
+ {
+ connectionConstraintModify = getConnection().getConnection();
+ this.integrityConstraint.relax(connectionConstraintModify);
+ }
+ }
+ catch (SQLException sqle)
+ {
+ logger.error(sqle.getMessage(), sqle);
+ }
+ finally
+ {
+ DbUtils.closeQuietly(connectionConstraintModify );
+ connectionConstraintModify = null;
+ }
+
for (DatabaseOperation op : beforeTestOperations ) {
- op.execute(getConnection(), dataSet);
+ IDatabaseConnection idbConnection = null;
+ Connection connection = null;
+ try
+ {
+ op.execute(getConnection(), dataSet);
+ }
+ catch (SQLException sqle)
+ {
+ logger.error(sqle);
+ }
+ finally
+ {
+ DbUtils.closeQuietly(getConnection().getConnection());
+ }
}
+
+ try
+ {
+ connectionConstraintModify = getConnection().getConnection();
+ if (null != this.integrityConstraint)
+ {
+ this.integrityConstraint.constrain (connectionConstraintModify);
+ }
+ }
+ catch (SQLException sqle)
+ {
+ logger.error(sqle.getMessage(), sqle);
+ }
+ finally
+ {
+ DbUtils.closeQuietly(connectionConstraintModify );
+ }
+
Profile.start();
}
@AfterGroups(groups = "integration-integration")
- public void afterTestMethod() throws Exception {
+ public void afterTestMethod() throws Exception
+ {
Profile.stop();
- for (DatabaseOperation op : afterTestOperations ) {
- op.execute(getConnection(), dataSet);
- }
- Profile.start();
+ for (DatabaseOperation op : afterTestOperations)
+ {
+ IDatabaseConnection connection = null;
+ try
+ {
+ connection= getConnection();
+ op.execute(connection, dataSet);
+ }
+ catch (SQLException sqle)
+ {
+ logger.error(sqle);
+ }
+ finally
+ {
+ if (null != connection)
+ {
+ connection.getConnection().close();
+ }
+ }
+
+ }
+ Profile.start();
}
+
+ private Set<String> getTableNames(String basedataLocation)
+ {
+ Set<String> returnValue = new HashSet<String>();
+ if (null != basedataLocation)
+ {/* this is expected to be the path to a file containing the dbunit bulk
+ data import xml. Find each element named /dataset/* as these will be
+ the tables with data imported. */
+ InputStream iStream = null;
+ try
+ {
+ XPath xpath = XPathFactory.newInstance().newXPath();
+ iStream = new BufferedInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(basedataLocation));
+
+ Object result = xpath.evaluate ("/dataset/*", new InputSource(iStream), XPathConstants.NODESET);
+
+ NodeList resultTree = (NodeList)result;
+ logger.trace("Size of element list ["+resultTree.getLength()+"]");
+ for (int position = 0 ; position < resultTree.getLength() ; position += 1)
+ {
+ Node element = (Node )resultTree.item(position);
+ returnValue.add(element.getNodeName());
+ }
+ }
+ catch (XPathException xpe)
+ {
+ logger.error(xpe.getMessage(), xpe);
+ }
+ finally
+ {
+ IOUtils.closeQuietly(iStream );
+ }
+ }
+ return returnValue;
+ }
}
Modified: labs/jbosstm/workspace/whitingjr/trunk/performance/src/test/java/org/jboss/jbossts/performance/readonly/JPAExampleTests.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/performance/src/test/java/org/jboss/jbossts/performance/readonly/JPAExampleTests.java 2009-11-10 17:21:58 UTC (rev 30089)
+++ labs/jbosstm/workspace/whitingjr/trunk/performance/src/test/java/org/jboss/jbossts/performance/readonly/JPAExampleTests.java 2009-11-10 17:22:33 UTC (rev 30090)
@@ -23,23 +23,22 @@
package org.jboss.jbossts.performance.readonly;
import java.math.BigDecimal;
-import java.math.BigInteger;
import java.util.Calendar;
import java.util.Currency;
import java.util.GregorianCalendar;
import javax.persistence.EntityManager;
+import javax.persistence.PersistenceException;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
+import org.hibernate.ejb.EntityManagerImpl;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
-import com.mentorgen.tools.profile.runtime.Profile;
-
import auction.dao.ItemDAO;
import auction.dao.UserDAO;
import auction.dao.ejb3.GenericEJB3DAO;
@@ -50,6 +49,8 @@
import auction.model.User;
import auction.test.basic.ProfiledStateTransitions;
+import com.mentorgen.tools.profile.runtime.Profile;
+
public class JPAExampleTests extends ProfiledStateTransitions
{
private static final Logger logger = Logger.getLogger(JPAExampleTests.class);
@@ -97,12 +98,22 @@
// Start a unit of work (manually, no container)
//getUserTransaction().begin();
- Session session = getSessionFactory().openSession();
+ Session session = null;
Transaction transaction = null;
try
{
+ /*
+ session = getSessionFactory().openSession();
+
transaction = session.beginTransaction();
- EntityManager em = getEntityManagerFactory().createEntityManager();
+ */
+ EntityManager em = getEntityManagerFactory().createEntityManager();
+
+
+ EntityManagerImpl emImpl = (EntityManagerImpl)em;
+ logger.debug("Transaction status ["+emImpl.getSession().getTransaction().isActive()+"]");
+
+ em.joinTransaction();
// Prepare the DAOs (manually, no Seam)
ItemDAO itemDAO = new ItemDAOBean();
@@ -132,29 +143,70 @@
// Don't forget to take the return value, this is basically a merge()
newItem = itemDAO.makePersistent(newItem);
+ session.flush();
+ em.flush();
+
+ transaction.commit(); // End the unit of work
+
+ // Direct SQL query for database state in auto-commit mode
+ EntityManager emtest = null;
+ try
+ {
+
+ emtest = getEntityManagerFactory().createEntityManager();
+ Object[] result = (Object[])
+ emtest.createNativeQuery(
+ "select INITIAL_PRICE as IP," +
+ " SELLER_ID as SID from ITEM where ITEM_ID = :itemId")
+ .setParameter("itemId", newItem.getId())
+ .getSingleResult();
- // End the unit of work
-// getUserTransaction().commit();
-// em.close();
- transaction.commit();
-
- // Direct SQL query for database state in auto-commit mode
- em = getEntityManagerFactory().createEntityManager();
- Object[] result = (Object[])
- em.createNativeQuery(
- "select INITIAL_PRICE as IP," +
- " SELLER_ID as SID from ITEM where ITEM_ID = :itemId")
- .setParameter("itemId", newItem.getId())
- .getSingleResult();
-
- // Assert correctness of state
- Assert.assertEquals( result[0].getClass() , BigDecimal.class);
- Assert.assertEquals(0 , ((BigDecimal)result[0]).compareTo( ((newItem.getInitialPrice().getValue()))));
- // The SQL resultset mapping returns a BigInteger
- Assert.assertEquals(result[1], new BigInteger("1"));
+ // Assert correctness of state
+ Assert.assertEquals( result[0].getClass() , BigDecimal.class);
+ Assert.assertEquals(0 , ((BigDecimal)result[0]).compareTo( ((newItem.getInitialPrice().getValue()))));
+ // The SQL resultset mapping returns a BigInteger
+ Assert.assertEquals(result[1], new BigDecimal("1"));
+ }
+ catch (PersistenceException pe)
+ {
+ logger.error(pe.getMessage(), pe);
+ }
+ finally
+ {
+ if (null != em)
+ {
+ em.close();
+ }
+ if (null != emtest)
+ {
+ emtest.close();
+ }
+ }
+
}
catch (HibernateException e) {
logger.error(e.getMessage(), e);
+ if (null != transaction)
+ {
+ transaction.rollback();
+ }
+ throw e;
}
+ catch (PersistenceException pe)
+ {
+ logger.error(pe.getMessage(), pe);
+ if (null != transaction)
+ {
+ transaction.rollback();
+ }
+ throw pe;
+ }
+ finally {
+ if (null != session)
+ {
+ session.close();
+ }
+ }
}
}
+
More information about the jboss-svn-commits
mailing list