You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by devu213 <de...@gmail.com> on 2008/08/14 12:43:49 UTC
openJPA not working with DBUnit....
I'm really having problems trying to make openJPA work with DBUnit. Here is a
snippet of my test class. The moment I have a situation where DBUnit does
some sort of initializing operations on the database, openJPA starts
behaving really strangely. Specifically the second test method which uses
inheritance fails to fire a query for the bae class.
The moment I stop using DBUnit, everything starts working fine and openJPA
fires the queries for the base class and derived class in the proper order.
I've tried using two separate connections for DBUnit and openJPA and then
finally passing a connection from openJPA to DBUnit.
I really don't understand what is going wrong....Can someone please give me
some pointers?
public class BaseDAOTest extends BaseDBUnitTestCase {
@Override
protected DatabaseOperation getSetUpOperation() {
return DatabaseOperation.DELETE;
//return DatabaseOperation.NONE; //This works....
}
@Override
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSet(
ClassLoader.getSystemResourceAsStream("BaseDAOTestClean.xml"));
}
protected IDataSet getDataSet(String datasetName) throws Exception {
return new FlatXmlDataSet(
ClassLoader.getSystemResourceAsStream(datasetName));
}
public void testCreate() {
System.out.println("testCreate");
EntityManager manager = null;
Customer customer = new Customer(new Double(123), new Double(123),
new String("DK1"), new Date(), new Date(),
new Date(), "KMD", new Date());
Customer anotherCustomer = new Customer(new Double(124), new Double(124),
new String("DK2"), new Date(), new Date(),
new Date(), "ATP", new Date());
try {
//Invocations to the processing logic
manager = PersistenceBootstrapper.getEntityManager();
EntityTransaction tx = manager.getTransaction();
tx.begin();
BaseDAO.create(manager, customer);
BaseDAO.create(manager, anotherCustomer);
tx.commit();
//Do the asserts... Result verification.
ITable addedData = getConnection().createQueryTable(
"customer",
"select pbs_no, cvr_no, postal_cd," +
" update_by from customer where" +
" pbs_no in (123, 124) order by pbs_no");
Assertion.assertEquals(getDataSet("BaseDAOTest1.xml"),
new DefaultDataSet(addedData));
} catch (Exception e) {
e.printStackTrace();
assertEquals("Failed", "Normal flow failed for BaseDAOTest",
e.getMessage());
} finally {
//Do this instead of closing the manager directly on your own.
manager.close();
}
}
public void testCreateInheritance() {
System.out.println("testInheritacne");
//EntityManagerFactory factory =
Persistence.createEntityManagerFactory("refimpl-nocontainer");
EntityManager manager = null;
Creditor creditor = new Creditor(new Double(125), new Double(125),
"DK3", new Date(), new Date(), new Date(), "PROG", new Date());
try {
//Note the new dataset
IDataSet expectedDataset = getDataSet("BaseDAOTest2.xml");
//Invocations to the processing logic
manager = PersistenceBootstrapper.getEntityManager();
EntityTransaction transaction = manager.getTransaction();
transaction.begin();
BaseDAO.create(manager, creditor);
transaction.commit();
//Do the asserts... Result verification.
//Note the way the actual dataset is created.
QueryDataSet actual = new QueryDataSet(getConnection());
actual.addTable("customer", "select pbs_no, cvr_no, " +
"postal_cd, update_by from customer where" +
" pbs_no = 125");
actual.addTable("creditor", "select pbs_no, cred_type, " +
"billing_type, update_by from creditor where" +
" pbs_no = 125");
//compare with expected dataset.
Assertion.assertEquals(actual, expectedDataset);
} catch (Exception e) {
e.printStackTrace();
assertEquals("Failed", "Normal flow failed for BaseDAOTest",
e.getMessage());
} finally {
//Do this instead of closing the manager directly on your own.
//PersistenceBootstrapper.closeManager();
manager.close();
}
}
public void testCreateManyToOne() {
System.out.println("manytoone");
EntityManager manager = null;
Creditor creditor = new Creditor(new Double(126), new Double(126),
"DK3", new Date(), new Date(), new Date(), "PROG", new Date());
Country creditorsCountry = new Country("DK", "Denmark", "PROG");
try {
IDataSet dataset = new FlatXmlDataSet(
ClassLoader.getSystemResourceAsStream("BaseDAOTestClean.xml"));
//Invocations to the processing logic
manager = PersistenceBootstrapper.getEntityManager();
EntityTransaction transaction = manager.getTransaction();
transaction.begin();
BaseDAO.create(manager, creditor);
creditor.setCountry(creditorsCountry);
transaction.commit();
//Do the asserts... Result verification.
QueryDataSet actual = new QueryDataSet(getConnection());
actual.addTable("customer", "select pbs_no, cvr_no, " +
"postal_cd, update_by from customer where" +
" pbs_no = 125");
actual.addTable("creditor", "select pbs_no, cred_type, " +
"billing_type, update_by from creditor where" +
" pbs_no = 125");
actual.addTable("country_master", "select country_cd, country, " +
"update_by from country_master where country_cd = \'DK\'");
Assertion.assertEquals(actual, dataset);
} catch (Exception e) {
e.printStackTrace();
assertEquals("Failed", "Normal flow failed for BaseDAOTest",
e.getMessage());
} finally {
//Do this instead of closing the manager directly on your own.
//PersistenceBootstrapper.closeManager();
manager.close();
}
}
}
http://n2.nabble.com/file/n723864/src.zip src.zip
--
View this message in context: http://n2.nabble.com/openJPA-not-working-with-DBUnit....-tp723864p723864.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: openJPA not working with DBUnit....
Posted by devu213 <de...@gmail.com>.
I have since realized that this problem has nothing to do with DBUnit. I was
trying to test an inheritance hierarchy with openJPA and that is failing.
I have added to a separate post that already mentions a similar issue with
version 1.1.0
devu213 wrote:
>
> I'm really having problems trying to make openJPA work with DBUnit. Here
> is a snippet of my test class. The moment I have a situation where DBUnit
> does some sort of initializing operations on the database, openJPA starts
> behaving really strangely. Specifically the second test method which uses
> inheritance fails to fire a query for the bae class.
>
> The moment I stop using DBUnit, everything starts working fine and openJPA
> fires the queries for the base class and derived class in the proper
> order.
>
> I've tried using two separate connections for DBUnit and openJPA and then
> finally passing a connection from openJPA to DBUnit.
>
> I really don't understand what is going wrong....Can someone please give
> me some pointers?
>
>
> public class BaseDAOTest extends BaseDBUnitTestCase {
>
> @Override
> protected DatabaseOperation getSetUpOperation() {
> return DatabaseOperation.DELETE;
> //return DatabaseOperation.NONE; //This
> works....
>
> }
>
> @Override
> protected IDataSet getDataSet() throws Exception {
> return new FlatXmlDataSet(
> ClassLoader.getSystemResourceAsStream("BaseDAOTestClean.xml"));
> }
>
> protected IDataSet getDataSet(String datasetName) throws Exception {
> return new FlatXmlDataSet(
> ClassLoader.getSystemResourceAsStream(datasetName));
> }
>
>
> public void testCreate() {
> System.out.println("testCreate");
> EntityManager manager = null;
> Customer customer = new Customer(new Double(123), new Double(123),
> new String("DK1"), new Date(), new Date(),
> new Date(), "KMD", new Date());
> Customer anotherCustomer = new Customer(new Double(124), new
> Double(124),
> new String("DK2"), new Date(), new Date(),
> new Date(), "ATP", new Date());
> try {
> //Invocations to the processing logic
> manager = PersistenceBootstrapper.getEntityManager();
> EntityTransaction tx = manager.getTransaction();
> tx.begin();
> BaseDAO.create(manager, customer);
> BaseDAO.create(manager, anotherCustomer);
> tx.commit();
>
> //Do the asserts... Result verification.
> ITable addedData = getConnection().createQueryTable(
> "customer",
> "select pbs_no, cvr_no, postal_cd," +
> " update_by from customer where" +
> " pbs_no in (123, 124) order by pbs_no");
> Assertion.assertEquals(getDataSet("BaseDAOTest1.xml"),
> new DefaultDataSet(addedData));
> } catch (Exception e) {
> e.printStackTrace();
> assertEquals("Failed", "Normal flow failed for BaseDAOTest",
> e.getMessage());
> } finally {
> //Do this instead of closing the manager directly on your own.
> manager.close();
> }
> }
>
>
> public void testCreateInheritance() {
> System.out.println("testInheritacne");
> //EntityManagerFactory factory =
> Persistence.createEntityManagerFactory("refimpl-nocontainer");
> EntityManager manager = null;
> Creditor creditor = new Creditor(new Double(125), new Double(125),
> "DK3", new Date(), new Date(), new Date(), "PROG", new Date());
> try {
> //Note the new dataset
> IDataSet expectedDataset = getDataSet("BaseDAOTest2.xml");
>
> //Invocations to the processing logic
> manager = PersistenceBootstrapper.getEntityManager();
> EntityTransaction transaction = manager.getTransaction();
> transaction.begin();
> BaseDAO.create(manager, creditor);
> transaction.commit();
>
> //Do the asserts... Result verification.
> //Note the way the actual dataset is created.
> QueryDataSet actual = new QueryDataSet(getConnection());
> actual.addTable("customer", "select pbs_no, cvr_no, " +
> "postal_cd, update_by from customer where" +
> " pbs_no = 125");
> actual.addTable("creditor", "select pbs_no, cred_type, " +
> "billing_type, update_by from creditor where" +
> " pbs_no = 125");
>
> //compare with expected dataset.
> Assertion.assertEquals(actual, expectedDataset);
> } catch (Exception e) {
> e.printStackTrace();
> assertEquals("Failed", "Normal flow failed for BaseDAOTest",
> e.getMessage());
> } finally {
> //Do this instead of closing the manager directly on your own.
> //PersistenceBootstrapper.closeManager();
> manager.close();
> }
> }
>
>
>
> public void testCreateManyToOne() {
> System.out.println("manytoone");
> EntityManager manager = null;
>
> Creditor creditor = new Creditor(new Double(126), new Double(126),
> "DK3", new Date(), new Date(), new Date(), "PROG", new Date());
>
> Country creditorsCountry = new Country("DK", "Denmark", "PROG");
>
>
> try {
>
> IDataSet dataset = new FlatXmlDataSet(
> ClassLoader.getSystemResourceAsStream("BaseDAOTestClean.xml"));
>
> //Invocations to the processing logic
> manager = PersistenceBootstrapper.getEntityManager();
> EntityTransaction transaction = manager.getTransaction();
> transaction.begin();
> BaseDAO.create(manager, creditor);
> creditor.setCountry(creditorsCountry);
> transaction.commit();
>
> //Do the asserts... Result verification.
> QueryDataSet actual = new QueryDataSet(getConnection());
> actual.addTable("customer", "select pbs_no, cvr_no, " +
> "postal_cd, update_by from customer where" +
> " pbs_no = 125");
> actual.addTable("creditor", "select pbs_no, cred_type, " +
> "billing_type, update_by from creditor where" +
> " pbs_no = 125");
> actual.addTable("country_master", "select country_cd, country, " +
> "update_by from country_master where country_cd = \'DK\'");
>
> Assertion.assertEquals(actual, dataset);
> } catch (Exception e) {
> e.printStackTrace();
> assertEquals("Failed", "Normal flow failed for BaseDAOTest",
> e.getMessage());
> } finally {
> //Do this instead of closing the manager directly on your own.
> //PersistenceBootstrapper.closeManager();
> manager.close();
> }
> }
> }
> http://n2.nabble.com/file/n723864/src.zip src.zip
>
--
View this message in context: http://n2.nabble.com/openJPA-not-working-with-DBUnit....-tp723864p781444.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: openJPA not working with DBUnit....
Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
> where DBUnit does some sort of initializing operations on the database
1. What sort of operations?
2. Does OpenJPA define the database schema for entity tables or DBUnit?
3. Switch on the trace
<property name="openjpa.Log" value="DefaultLevel=TRACE"/>
or
<property name="openjpa.Log" value="SQL=TRACE"/>
and post the trace as well as the error stack trace for the failed test.
4. There is a SQL listener facility in OpenJPA that will let you test which
SQL has been issued by OpenJPA and has been extensively used by OpenJPA's
own test corpus of nearly 3000 tests. Did you consider that as an option?
--
View this message in context: http://n2.nabble.com/openJPA-not-working-with-DBUnit....-tp723864p724786.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.