KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > F_Relation2_mouEC2


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_Relation2_mouEC2.java,v 1.2 2004/03/11 15:45:43 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34 import org.objectweb.jonas.jtests.beans.relation.mou.AHomeRemote;
35 import org.objectweb.jonas.jtests.beans.relation.mou.ARemote;
36 import org.objectweb.jonas.jtests.beans.relation.mou.BHomeRemote;
37 import org.objectweb.jonas.jtests.beans.relation.mou.BRemote;
38 import org.objectweb.jonas.jtests.util.JTestCase;
39
40 /**
41  * For testing many-to-one unidirectional relationships
42  * with relations created in the ejbPostCreate, as the EJB spec recommands it.
43  * @author Philippe Durieux
44  **/

45 public class F_Relation2_mouEC2 extends JTestCase {
46
47     private static String JavaDoc BEAN_HOME_A = "relation_mou_A1Home";
48     protected static AHomeRemote ahome = null;
49     private static String JavaDoc BEAN_HOME_B = "relation_mou_B1Home";
50     protected static BHomeRemote bhome = null;
51
52     public F_Relation2_mouEC2(String JavaDoc name) {
53         super(name);
54     }
55
56     protected static boolean isInit = false;
57
58     protected void setUp() {
59         super.setUp();
60         if (!isInit) {
61             useBeans("mou", true);
62             try {
63                 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A),
64                                                                   AHomeRemote.class);
65                 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B),
66                                                                   BHomeRemote.class);
67             } catch (NamingException JavaDoc e) {
68                 fail("Cannot get bean home: " + e.getMessage());
69             }
70             isInit = true;
71         }
72     }
73
74     /**
75      * teardown is called after each test case.
76      * Notes for debugging :
77      * To see DataBase state after a test is passed :
78      * Replace the cleanall() by sync(false).
79      */

80     protected void tearDown() throws Exception JavaDoc {
81         cleanall();
82     }
83
84     private void cleanall() throws Exception JavaDoc {
85         debug("remove all beans");
86
87         // Start the Transaction. In case a Transaction is still running,
88
// roll it back first and start a new one.
89
try {
90             utx.begin();
91         } catch (Exception JavaDoc e) {
92             debug("rollback first and start a new tx");
93             utx.rollback();
94             utx.begin();
95         }
96         // In case there is a problem during remove, we must unload
97
// the bean to restart properly.
98
try {
99             Collection JavaDoc c = ahome.findAll();
100             for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ) {
101                 ARemote a = (ARemote) i.next();
102                 a.remove();
103             }
104             c = bhome.findAll();
105             for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ) {
106                 BRemote b = (BRemote) i.next();
107                 b.remove();
108             }
109             utx.commit();
110         } catch (Exception JavaDoc e) {
111             error("Exception in cleanup: " + e);
112             try {
113                 utx.rollback();
114             } finally {
115                 unloadBeans("mou");
116                 isInit = false;
117             }
118         }
119     }
120
121     public void testCreateB() throws Exception JavaDoc {
122         bhome.create("b0");
123     }
124
125     public void testCreateBA() throws Exception JavaDoc {
126         bhome.create("b0");
127         ahome.create("a0", "b0");
128     }
129
130     public static Test suite() {
131         return new TestSuite(F_Relation2_mouEC2.class);
132     }
133
134     public static void main(String JavaDoc args[]) {
135         String JavaDoc testtorun = null;
136         // Get args
137
for (int argn = 0; argn < args.length; argn++) {
138             String JavaDoc s_arg = args[argn];
139             Integer JavaDoc i_arg;
140             if (s_arg.equals("-n")) {
141                 testtorun = args[++argn];
142             }
143         }
144         if (testtorun == null) {
145             junit.textui.TestRunner.run(suite());
146         } else {
147             junit.textui.TestRunner.run(new F_Relation2_mouEC2(testtorun));
148         }
149     }
150
151 }
152
Popular Tags