KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > entityexc > test > EntityExcUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.entityexc.test;
23
24
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.ejb.Handle JavaDoc;
29 import javax.ejb.HomeHandle JavaDoc;
30 import javax.ejb.CreateException JavaDoc;
31 import javax.ejb.FinderException JavaDoc;
32 import javax.ejb.EJBException JavaDoc;
33
34 import javax.naming.InitialContext JavaDoc;
35
36 import javax.transaction.TransactionRolledbackException JavaDoc;
37
38 import javax.rmi.PortableRemoteObject JavaDoc;
39
40 import org.jboss.test.entityexc.interfaces.EntityExcHome;
41 import org.jboss.test.entityexc.interfaces.EntityExc;
42 import org.jboss.test.entityexc.interfaces.MyAppException;
43
44 import junit.framework.Test;
45 import junit.framework.AssertionFailedError;
46
47 import org.jboss.test.JBossTestCase;
48
49
50 /**
51  * Test case for testing transactions and exceptions
52  * in entity beans.
53  *
54  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
55  * @version $Revision: 37406 $
56  */

57 public class EntityExcUnitTestCase
58    extends JBossTestCase
59 {
60    /**
61     * Create a new test case instance.
62     */

63    public EntityExcUnitTestCase(String JavaDoc name)
64    {
65       super(name);
66    }
67
68    /**
69     * Re-deploy and reset the database.
70     */

71    private void reset()
72       throws Exception JavaDoc
73    {
74       getLog().debug(" Resetting...");
75       undeploy("entityexc.jar");
76       deploy("entityexc.jar");
77       getHome().resetDatabase();
78       getLog().debug(" ...reset done OK");
79    }
80
81    /**
82     * Get a bean home interface.
83     */

84    private EntityExcHome getHome()
85       throws Exception JavaDoc
86    {
87       return (EntityExcHome)getInitialContext().lookup(EntityExcHome.JNDI_NAME);
88    }
89
90
91    /**
92     * Test for bug #463548: Bug in cache / setRollbackOnly bug.
93     */

94    public void testBug463548()
95       throws Exception JavaDoc
96    {
97       getLog().debug(
98          "**************************************************************");
99       getLog().debug(" testBug463548()");
100
101       reset();
102
103       boolean gotException = false;
104
105       try {
106          EntityExc bean = getHome().create(new Integer JavaDoc(1), EntityExc.EXC_CREATEEXCEPTION|EntityExc.F_FAIL_POSTCREATE|EntityExc.F_SETROLLBACKONLY);
107
108          // No cache problem if failure happens in ejbCreate
109
// EntityExc bean = getHome().create(new Integer(1), EntityExc.EXC_CREATEEXCEPTION|EntityExc.F_SETROLLBACKONLY);
110
} catch (TransactionRolledbackException JavaDoc ex) {
111          gotException = true;
112          getLog().error("Got unexpected TransactionRolledbackException", ex);
113          getLog().error("Container started the transaction, so we should get the CreateException");
114          fail("EJB2.0 section 17.6.2.8 violation.");
115       } catch (CreateException JavaDoc ex) {
116          gotException = true;
117          getLog().debug("Got expected CreateException", ex);
118       } catch (Exception JavaDoc ex) {
119          gotException = true;
120          getLog().error("Unexpected exception", ex);
121 // Check if the bean instance was removed from the cache
122
// It isn't in the database, since the transaction was marked for rollback only
123
try {
124    EntityExc bean = getHome().findByPrimaryKey(new Integer JavaDoc(1), 0);
125    fail("Rolled back bean creation, but still in cache.");
126 } catch (AssertionFailedError ex2) {
127    // Just re-throw this
128
throw ex2;
129 } catch (FinderException JavaDoc ex2) {
130          //ex2.printStackTrace();
131
getLog().error("Got expected FinderException", ex2);
132 } catch (Throwable JavaDoc ex2) {
133          //ex2.printStackTrace();
134
getLog().error("Got unexpected exception", ex2);
135 }
136          fail("Unexpected exception: " + ex);
137       }
138
139       if (!gotException)
140          fail("Did not get expected CreateException.");
141
142 // Check if the bean instance was removed from the cache
143
// It isn't in the database, since the transaction was marked for rollback only
144
try {
145    EntityExc bean = getHome().findByPrimaryKey(new Integer JavaDoc(1), 0);
146    fail("Rolled back bean creation, but still in cache 2.");
147 } catch (AssertionFailedError ex2) {
148    // Just re-throw this
149
throw ex2;
150 } catch (FinderException JavaDoc ex2) {
151          //ex2.printStackTrace();
152
getLog().error("Got expected FinderException", ex2);
153 } catch (Throwable JavaDoc ex2) {
154          //ex2.printStackTrace();
155
getLog().error("Got unexpected exception", ex2);
156 }
157       getLog().debug(
158          "**************************************************************");
159    }
160
161    public static Test suite() throws Exception JavaDoc
162    {
163       return getDeploySetup(EntityExcUnitTestCase.class, "entityexc.jar");
164    }
165 }
166
Popular Tags