jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "J. B. Rainsberger" <jbra...@rogers.com>
Subject Cactus and DBUnit tests
Date Mon, 20 Oct 2003 22:04:01 GMT
Everyone:

I am trying to use DBUnit and Cactus together to test an entity bean in 
a live container.

PROBLEM. The CLEAN_INSERT operation that DBUnit is supposed to perform 
in setUp() does not occur.

FAILURE. When my entity bean tries to create a new order, an order 
already exists with that ID, having not been cleaned up during test case 
setup.

QUESTION. Is something causing my setUp() not to be invoked on the 
server side? I had other, related problems: when I obtain the EJB home 
in setUp(), that appears to be null when the test executes. I suspect 
there is a larger problem that my test fixture is not being set up, I 
guess, on the server side.

INFO. If I run the test locally (that is, not with Cactus), everything 
works.

PLATFORM. JBoss 3.2.2rc4, Cactus 1.5b1, DBUnit 1.5.5, Windows 2000.

I have attached the source of my test, in case you find it helpful. You 
will notice that I have hardcoded my data set in the test to avoid 
problems due to being unable to load a file from disk.

Thanks for any help you can provide.

---------------------------------------------------------------------
package junit.cookbook.coffee.model.ejb.test;

import java.io.StringReader;
import java.rmi.RemoteException;
import java.sql.*;

import javax.ejb.FinderException;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;

import junit.cookbook.coffee.model.ejb.*;
import junit.framework.Test;

import org.apache.cactus.ServletTestSuite;
import org.dbunit.DatabaseTestCase;
import org.dbunit.database.*;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;

import com.diasparsoftware.jdbc.JdbcResourceRegistry;
import com.mimer.jdbc.MimerDataSource;

public class AddOrderTest extends DatabaseTestCase {
     private JdbcResourceRegistry jdbcResourceRegistry;
     private MimerDataSource dataSource;
     private OrderHome home;

     public AddOrderTest(String name) {
         super(name);
     }

     protected IDatabaseConnection getConnection() throws Exception {
         Connection connection = makeJdbcConnection();
         return new DatabaseConnection(connection);
     }

     protected IDataSet getDataSet() throws Exception {
         return new FlatXmlDataSet(
             new StringReader(
                 "<?xml version=\"1.0\" ?>"
                     + "<dataset>"
                     + "<people.customer customerId=\"762\" name=\"J. B. 
Rainsberger\" />"
                     + "<orders.orders />"
                     + "</dataset>"));
     }

     public void testCreateEmptyOrder() throws Exception {
         Context rootContext = new InitialContext();
         Object homeAsObject = rootContext.lookup("ejb/Order");

         home =
             (OrderHome) PortableRemoteObject.narrow(
                 homeAsObject,
                 OrderHome.class);

         home.create(new Integer(001), new Integer(762));
         assertOrderExists(001, 762);
     }

     private void assertOrderExists(int orderId, int customerId)
         throws FinderException, RemoteException {

         Order newOrder = home.findByPrimaryKey(new Integer(orderId));
         assertEquals(new Integer(customerId), newOrder.getCustomerId());
     }

     protected void setUp() throws Exception {
         System.setProperty("dbunit.qualified.table.names", "true");
         jdbcResourceRegistry = new JdbcResourceRegistry();
         super.setUp();
     }

     protected void tearDown() throws Exception {
         jdbcResourceRegistry.cleanUp();
         super.tearDown();
     }

     private DataSource getDataSource() {
         if (dataSource == null) {
             dataSource = new MimerDataSource();
             dataSource.setDatabaseName("coffeeShopData");
             dataSource.setUser("admin");
             dataSource.setPassword("adm1n");
         }

         return dataSource;
     }

     private Connection makeJdbcConnection() throws SQLException {
         Connection connection = getDataSource().getConnection();
         jdbcResourceRegistry.registerConnection(connection);
         return connection;
     }

     public static Test suite() {
         return new ServletTestSuite(AddOrderTest.class);
     }
}

-- 
J. B. Rainsberger,
Diaspar Software Services
http://www.diasparsoftware.com :: +1 416 791-8603
Let's write software that people understand



Mime
View raw message