You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by "J. B. Rainsberger" <jb...@rogers.com> on 2003/10/21 00:04:01 UTC

Cactus and DBUnit tests

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



RE: Reporting dir.

Posted by Vincent Massol <vm...@pivolis.com>.
Are you using the Maven plugin or directly the <cactus> task?

If you're using the <cactus> task, I was going to RTFM you but I've just
noticed the doc is not up to date and missing the explanation of the
"todir" attribute
(http://jakarta.apache.org/cactus/integration/ant/task_cactus.html).

However you can have a look at the sample application provided in the
Cactus distribution. Here's an extract:

    <cactus warfile="${target.dir}/test.war" fork="yes"
        failureproperty="tests.failed" haltonerror="true">
[...]
      <containerset>
        <jboss3x if="cactus.home.jboss3x"
            dir="${cactus.home.jboss3x}"
            output="${target.testreports.dir}/jboss3x.out"
            todir="${target.testreports.dir}/jboss3x"/>
[...]

So the answer is: use the todir attribute.

If you're using the maven plugin, here's also an extract of
plugin.jelly:

[...]
        <jboss3x if="cactus.home.jboss3x"
            dir="${cactus.home.jboss3x}"
            output="${cactus.reports.dir}/jboss3x.out"
            todir="${cactus.reports.dir}/jboss3x"
            config="${cactus.jboss3x.config.name}">
[...]

Thus you can override the cactus.reports.dir property but the reports
will go in ${cactus.reports.dir}/[container name].

-Vincent

> -----Original Message-----
> From: Bret Kumler [mailto:bkumler@firstam.com]
> Sent: 21 October 2003 00:46
> To: Cactus Users List
> Subject: Reporting dir.
> 
> Is there a way to make the TESTxx.xml written to a different
directory?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org



Reporting dir.

Posted by Bret Kumler <bk...@firstam.com>.
Is there a way to make the TESTxx.xml written to a different directory?


RE: Cactus and DBUnit tests

Posted by Vincent Massol <vm...@pivolis.com>.
Yep. There was a bug in cactus 1.5b1 where the setUp() method was not
called if you were using ServletTestSuite (instead of extending
ServletTestCase). It has been fixed in CVS.

Thanks
-Vincent

> -----Original Message-----
> From: J. B. Rainsberger [mailto:jbrains@rogers.com]
> Sent: 21 October 2003 00:04
> To: cactus-user@jakarta.apache.org
> Subject: Cactus and DBUnit tests
> 
> 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
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org