KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: F_Dass.java,v 1.3 2004/06/25 09:18:28 durieuxp Exp $
22  * --------------------------------------------------------------------------
23  */

24
25 package org.objectweb.jonas.jtests.clients.entity;
26
27 import java.rmi.RemoteException JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import javax.ejb.EJBObject JavaDoc;
31 import javax.ejb.FinderException JavaDoc;
32 import javax.ejb.RemoveException JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.rmi.PortableRemoteObject JavaDoc;
35 import junit.framework.Assert;
36 import junit.framework.Test;
37 import junit.framework.TestSuite;
38 import org.objectweb.jonas.jtests.beans.relation.dass.DassHome;
39 import org.objectweb.jonas.jtests.beans.relation.dass.P1Remote;
40 import org.objectweb.jonas.jtests.beans.relation.dass.P1HomeRemote;
41 import org.objectweb.jonas.jtests.beans.relation.dass.P2Remote;
42 import org.objectweb.jonas.jtests.beans.relation.dass.P2HomeRemote;
43 import org.objectweb.jonas.jtests.beans.relation.dass.P3Remote;
44 import org.objectweb.jonas.jtests.beans.relation.dass.P3HomeRemote;
45 import org.objectweb.jonas.jtests.beans.relation.dass.P4Remote;
46 import org.objectweb.jonas.jtests.beans.relation.dass.P4HomeRemote;
47 import org.objectweb.jonas.jtests.beans.relation.dass.ARemote;
48 import org.objectweb.jonas.jtests.beans.relation.dass.AHomeRemote;
49 import org.objectweb.jonas.jtests.beans.relation.dass.BRemote;
50 import org.objectweb.jonas.jtests.beans.relation.dass.BHomeRemote;
51 import org.objectweb.jonas.jtests.util.JTestCase;
52
53 /**
54  * This test suite tests specifics mapping used in some applications
55  * for example, cmr field and cmp field mapped on the same table column
56  * @author Philippe Durieux
57  */

58 public class F_Dass extends JTestCase {
59
60     /**
61      * Reference on the remote Home's
62      */

63     private static P1HomeRemote p1home = null;
64     private static P2HomeRemote p2home = null;
65     private static P3HomeRemote p3home = null;
66     private static P4HomeRemote p4home = null;
67     private static AHomeRemote ahome = null;
68     private static BHomeRemote bhome = null;
69
70     /**
71      * Standard constructor
72      */

73     public F_Dass(String JavaDoc name) {
74         super(name);
75     }
76
77     protected static boolean isInit = false;
78
79     /**
80      * setup is called before each test case.
81      * If not initialized, load the dass bean and lookup the Home.
82      */

83     protected void setUp() {
84         super.setUp();
85         if (!isInit) {
86             useBeans("dass", false);
87             try {
88                 p1home = (P1HomeRemote) PortableRemoteObject.narrow(ictx.lookup("relation_dass_P1Home"),
89                                                                     P1HomeRemote.class);
90                 p2home = (P2HomeRemote) PortableRemoteObject.narrow(ictx.lookup("relation_dass_P2Home"),
91                                                                     P2HomeRemote.class);
92                 p3home = (P3HomeRemote) PortableRemoteObject.narrow(ictx.lookup("relation_dass_P3Home"),
93                                                                     P3HomeRemote.class);
94                 p4home = (P4HomeRemote) PortableRemoteObject.narrow(ictx.lookup("relation_dass_P4Home"),
95                                                                     P4HomeRemote.class);
96                 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup("relation_dass_AHome"),
97                                                                   AHomeRemote.class);
98                 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup("relation_dass_BHome"),
99                                                                   BHomeRemote.class);
100             } catch (NamingException JavaDoc e) {
101                 fail("Cannot get bean home: " + e.getMessage());
102             }
103             isInit = true;
104         }
105     }
106
107     /**
108      * teardown is called after each test case.
109      * Notes for debugging :
110      * To see DataBase state after a test is passed :
111      * Replace the cleanall() by sync(false).
112      */

113     protected void tearDown() throws Exception JavaDoc {
114         cleanall();
115         //sync(false);
116
}
117
118     /**
119      * Cleanup all the dass after each test, to make tests independant.
120      * Use transaction to avoid known bugs during cleanup.
121      * (With no transaction -> 1 remove will fail)
122      */

123     private void cleanall() throws Exception JavaDoc {
124         debug("remove all beans");
125
126         // In case there is a problem during remove, we must unload
127
// the bean to restart properly.
128
try {
129             cleanAllBeans(ahome);
130             cleanAllBeans(bhome);
131             cleanAllBeans(p1home);
132             cleanAllBeans(p2home);
133             cleanAllBeans(p3home);
134             cleanAllBeans(p4home);
135         } catch (Exception JavaDoc e) {
136             error("Exception in cleanup: " + e);
137             unloadBeans("dass");
138             isInit = false;
139         }
140     }
141         
142     private void cleanAllBeans(DassHome home) throws RemoteException JavaDoc, RemoveException JavaDoc {
143         try {
144             Collection JavaDoc c = home.findAll();
145             for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ) {
146                 EJBObject JavaDoc p = (EJBObject JavaDoc) i.next();
147                 p.remove();
148             }
149         } catch (FinderException JavaDoc e) {
150         }
151     }
152
153     // ---------------------------------------------------------------
154
// Set of the tests that are passed automatically.
155
// ---------------------------------------------------------------
156

157     public void testCreateA() throws Exception JavaDoc {
158         ahome.create("A1");
159     }
160
161     public void testCreateB() throws Exception JavaDoc {
162         bhome.create("B1");
163     }
164
165     /**
166      * This test fails, normally, because:
167      * - setting the cmr field doesn't work because it's also a cmp field.
168      * - the cmp field is not changed, so this means that the cmr field is readonly.
169      * Jonas should throw an exception when we try to modify this kind of cmr field.
170      */

171     public void _testCreateA2B() throws Exception JavaDoc {
172         ARemote a1 = ahome.create("A1");
173         bhome.create("B1");
174         a1.assignB("B1");
175         String JavaDoc cmrval = a1.getBcmrvalue();
176         assertTrue("Bad cmr value:" + cmrval, "B1".equals(cmrval));
177     }
178
179     public void _testCreateA2BTx() throws Exception JavaDoc {
180         try {
181             utx.begin();
182             _testCreateA2B();
183         } finally {
184             try {
185                 utx.commit();
186             } catch (Exception JavaDoc ii) {
187             }
188         }
189     }
190
191     /**
192      * Simple example with 2 beans with the same pk and a cmr on this pk.
193      * looks like a bean mapped on 2 tables.
194      */

195     public void testCreateP1P2() throws Exception JavaDoc {
196         P1Remote p1 = p1home.create("P", "100");
197         P2Remote p2 = p2home.create("P");
198         String JavaDoc val = p2.getP1Value();
199         assertTrue("Cannot access bean P1", "100".equals(val));
200     }
201
202     /**
203      * Complex example of beans with cmr mapped on fields of the primary key.
204      * Note that the primary key is composite.
205      */

206     public void testCreateP3() throws Exception JavaDoc {
207         P1Remote p1 = p1home.create("P", "100");
208         P4Remote p4 = p4home.create("P", "102");
209         P3Remote p3 = p3home.create("P3", "P");
210         String JavaDoc val = p3.getP1Value();
211         assertTrue("Cannot access bean P1", "100".equals(val));
212         val = p3.getP4Value();
213         assertTrue("Cannot access bean P4", "102".equals(val));
214     }
215
216     /**
217      * Run all the tests of this suite.
218      */

219     public static Test suite() {
220         return new TestSuite(F_Dass.class);
221     }
222
223     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
224         String JavaDoc testtorun = null;
225         
226         // Get args
227
for (int argn = 0; argn < args.length; argn++) {
228             String JavaDoc sarg = args[argn];
229             if (sarg.equals("-n")) {
230                 testtorun = args[++argn];
231             }
232         }
233         if (testtorun == null) {
234             junit.textui.TestRunner.run(suite());
235         } else {
236             junit.textui.TestRunner.run(new F_Dass(testtorun));
237         }
238     }
239
240 }
241
Popular Tags