hello all,
i am running a set of java test involving some Entity CMP beans.
I am currently running test using openEJB, and my tests populates a
database using Dbunit.
The problem that i have is that the rows created by using an openEJB
entity bean don't get deleted using dbUnit.
Could anyone explain me why? the two calls to db.insert and db.delete
done using dbunit are done correctly,but the two rows inserted via
the EJB.create method simply don't get deleted..
anyone could help? #
I attach both test cases..
thanx and regards
marco
package com.myapp.ejb;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Date;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import junit.framework.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;
import java.io.*;
import java.sql.*;
import junit.extensions.*;
import org.dbunit.*;
import org.dbunit.operation.*;
import org.dbunit.database.*;
import org.dbunit.dataset.*;
import org.xml.sax.InputSource;
import org.dbunit.dataset.xml.*;
/**
* This is the common TestClass which uses DbUnit to
* set up the database.
* All tests should extend this class.
*/
public abstract class MenagerieTestCase extends TestCase
{
protected IDatabaseConnection connection;
public void setUp() throws Exception{
try {
System.out.println("-------- calling menagerie..setup..");
insertFileIntoDb();
}catch(Exception e) {
System.err.println("Exception in MenagerieTest.setup()\n" +
e);
e.printStackTrace();
}
}
public void tearDown() {
System.err.println("menagerie...teardown...");
try {
emptyTable();
} catch(Exception e) {
System.err.println("MenagerieTEst. exception in teardown!!!" + e);
e.printStackTrace();
}
}
/**
* This method inserts the contents of a FlatXmlDataSet file
* into the connection
*/
protected void insertFileIntoDb() throws Exception
{
connection = getConnection();
DatabaseOperation.INSERT.execute(connection,getDataSet());
connection.close();
}
/** Empty a table */
protected void emptyTable() throws Exception
{
connection = getConnection();
System.out.println("--- DELETING TABLE..");
DatabaseOperation.DELETE.execute(connection, getDataSet());
connection.close();
}
protected IDatabaseConnection getConnection() throws Exception
{
Class driverClass = Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConnection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root",
"mypass");
return new DatabaseConnection(jdbcConnection);
}
protected IDataSet getDataSet() throws Exception
{
InputStream stream =
this.getClass().getResourceAsStream("/budget-seed.xml");
return new FlatXmlDataSet(new InputSource(stream));
}
}
package com.myapp.ejb;
import java.util.Collection;
import java.util.Properties;
import java.util.Vector;
import java.util.Date;
import java.text.*;
import javax.naming.InitialContext;
import junit.framework.*;
public class EntryTest extends MenagerieTestCase {
private AdminFacadeHome ejbHome;
private AdminFacade ejbObject;
private EntryHome entryHome;
private DateFormat formatter = new SimpleDateFormat("ddMMyyyy");
public void setUp() throws Exception{
super.setUp();
InitialContext context = new InitialContext(System.getProperties());
Object obj = context.lookup("AdminFacade");
ejbHome = (AdminFacadeHome)javax.rmi.PortableRemoteObject.narrow( obj,
AdminFacadeHome.class);
ejbObject = ejbHome.create();
Object homeObj = context.lookup("Entry");
entryHome = (EntryHome)javax.rmi.PortableRemoteObject.narrow( homeObj,
EntryHome.class);
}
public static Test suite() {
return new TestSuite(EntryTest.class);
}
/*-------------------------------------------------*/
/* String */
/*-------------------------------------------------*/
public void Xtest_returnStringObject() {
try{
Collection collection = ejbObject.getExpenseTypes();
assertEquals("We should have 2 items", 2, collection.size());
} catch (Exception e){
fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
}
}
public void testCreateEntry() throws Exception {
System.err.println("----- creating entry.... ");
Entry entry = entryHome.create(null,"Test", (double)5.0, 1, new
java.util.Date(),"aa");
//System.err.println("----- entry created..now we need to find it....
");
//Collection entries = entryHome.findByTypeUser(1,"aa");
//assertEquals("We should have only 2 entries", 2, entries.size());
}
public void XtestCreateAnother() throws Exception {
System.err.println("----- creating entry.... ");
Entry entry = entryHome.create(new Integer("100"),"Testa", (double)2.0,
1, new java.util.Date(),
"aa");
}
public void XtestFindByTypeUser() throws Exception {
Collection entries = entryHome.findByTypeUser(99,"TestUser");
assertEquals("We should have only 2 entries", 2, entries.size());
}
public void XtestFindByDateUser() throws Exception {
Date startDate = formatter.parse("31121999");
Date endDate = formatter.parse("01082001");
Collection entries = entryHome.findByDateUser(startDate, endDate,
"TestUser");
assertEquals("we should have only 2 entries", 2, entries.size());
}
public void XtestFindByDateTypeUser() throws Exception {
Date startDate = formatter.parse("31121999");
Date endDate = formatter.parse("01082002");
Collection entries =
entryHome.findEntryByDateTypeUserDate(99,startDate, endDate, "TestUser");
assertEquals("we should have only 2 entries", 2, entries.size());
}
}