KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > jbas1361 > JBAS1361UnitTestCase


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.cmp2.jbas1361;
23
24 import javax.naming.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.transaction.Transaction JavaDoc;
27 import javax.transaction.TransactionManager JavaDoc;
28 import org.jboss.test.JBossTestCase;
29 import org.jboss.test.util.ejb.EJBTestCase;
30 import junit.framework.Test;
31
32 /**
33  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
34  * @version <tt>$Revision: 41901 $</tt>
35  */

36 public class JBAS1361UnitTestCase
37    extends EJBTestCase
38 {
39    public static Test suite() throws Exception JavaDoc
40    {
41       return JBossTestCase.getDeploySetup(JBAS1361UnitTestCase.class, "cmp2-jbas1361.jar");
42    }
43
44    public JBAS1361UnitTestCase(String JavaDoc methodName)
45    {
46       super(methodName);
47    }
48
49    protected void setUp() throws Exception JavaDoc
50    {
51       // NOTE: setUp, tearDown and tests appear to run in THE SAME transaction!
52
TransactionManager JavaDoc tm = getTM();
53       Transaction JavaDoc oldTx = tm.getTransaction();
54       if(oldTx != null)
55       {
56          tm.suspend();
57       }
58       tm.begin();
59
60       try
61       {
62          //System.out.println("setUp: " + tm.getTransaction());
63
ALocal a = getALocalHome().create(new Integer JavaDoc(1), "a");
64          BLocalHome bh = getBLocalHome();
65          for(int i = 1; i <= 3; ++i)
66          {
67             bh.create(new Integer JavaDoc(i), "b").setA(a);
68          }
69
70          tm.commit();
71       }
72       catch(Throwable JavaDoc t)
73       {
74          tm.rollback();
75       }
76
77       if(oldTx != null)
78       {
79          tm.resume(oldTx);
80       }
81    }
82
83    protected void tearDown() throws Exception JavaDoc
84    {
85       //System.out.println("tearDown: " + (getTM()).getTransaction());
86
getALocalHome().remove(new Integer JavaDoc(1));
87       BLocalHome bh = getBLocalHome();
88       for(int i = 1; i <= 3; ++i)
89       {
90          bh.remove(new BPK(new Integer JavaDoc(i), "b"));
91       }
92    }
93
94    public void testJBAS1361() throws Throwable JavaDoc
95    {
96       TransactionManager JavaDoc tm = getTM();
97       Transaction JavaDoc oldTx = tm.getTransaction();
98       if(oldTx != null)
99       {
100          tm.suspend();
101       }
102       tm.begin();
103
104       try
105       {
106          //System.out.println("test: " + (getTM()).getTransaction());
107

108          ALocal a = getALocalHome().findByPrimaryKey(new Integer JavaDoc(1));
109
110          a.getB().clear();
111
112          BLocalHome bh = getBLocalHome();
113
114          BPK bpk = new BPK();
115          bpk.name = "b";
116          for(int i = 1; i <= 3; ++i)
117          {
118             bpk.id = new Integer JavaDoc(i);
119             BLocal b = bh.findByPrimaryKey(bpk);
120             assertTrue(a.getB().add(b));
121          }
122
123          assertEquals(3, a.getB().size());
124
125          tm.commit();
126       }
127       catch(Throwable JavaDoc t)
128       {
129          tm.rollback();
130          throw t;
131       }
132       finally
133       {
134          if(oldTx != null)
135          {
136             tm.resume(oldTx);
137          }
138       }
139    }
140
141    private TransactionManager JavaDoc getTM()
142       throws NamingException JavaDoc
143    {
144       return (TransactionManager JavaDoc)lookup("java:/TransactionManager");
145    }
146
147    private ALocalHome getALocalHome()
148       throws NamingException JavaDoc
149    {
150       return (ALocalHome)lookup(ALocalHome.JNDI_NAME);
151    }
152
153    private BLocalHome getBLocalHome()
154       throws NamingException JavaDoc
155    {
156       return (BLocalHome)lookup(BLocalHome.JNDI_NAME);
157    }
158
159    private Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc
160    {
161       InitialContext JavaDoc ic = new InitialContext JavaDoc();
162       return ic.lookup(name);
163    }
164 }
165
Popular Tags