001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: * --------------------------------------------------------------------------
021: * $Id: F_Dass.java 7533 2005-10-19 15:55:05Z durieuxp $
022: * --------------------------------------------------------------------------
023: */
024:
025: package org.objectweb.jonas.jtests.clients.entity;
026:
027: import java.rmi.RemoteException;
028: import java.util.Collection;
029: import java.util.Iterator;
030: import javax.ejb.EJBObject;
031: import javax.ejb.FinderException;
032: import javax.ejb.RemoveException;
033: import javax.naming.NamingException;
034: import javax.rmi.PortableRemoteObject;
035: import junit.framework.Assert;
036: import junit.framework.Test;
037: import junit.framework.TestSuite;
038: import org.objectweb.jonas.jtests.beans.relation.dass.DassHome;
039: import org.objectweb.jonas.jtests.beans.relation.dass.P1Remote;
040: import org.objectweb.jonas.jtests.beans.relation.dass.P1HomeRemote;
041: import org.objectweb.jonas.jtests.beans.relation.dass.P2Remote;
042: import org.objectweb.jonas.jtests.beans.relation.dass.P2HomeRemote;
043: import org.objectweb.jonas.jtests.beans.relation.dass.P3Remote;
044: import org.objectweb.jonas.jtests.beans.relation.dass.P3HomeRemote;
045: import org.objectweb.jonas.jtests.beans.relation.dass.P4Remote;
046: import org.objectweb.jonas.jtests.beans.relation.dass.P4HomeRemote;
047: import org.objectweb.jonas.jtests.beans.relation.dass.ARemote;
048: import org.objectweb.jonas.jtests.beans.relation.dass.AHomeRemote;
049: import org.objectweb.jonas.jtests.beans.relation.dass.BRemote;
050: import org.objectweb.jonas.jtests.beans.relation.dass.BHomeRemote;
051: import org.objectweb.jonas.jtests.util.JTestCase;
052:
053: /**
054: * This test suite tests specifics mapping used in some applications
055: * for example, cmr field and cmp field mapped on the same table column
056: * @author Philippe Durieux
057: */
058: public class F_Dass extends JTestCase {
059:
060: /**
061: * Reference on the remote Home's
062: */
063: private static P1HomeRemote p1home = null;
064: private static P2HomeRemote p2home = null;
065: private static P3HomeRemote p3home = null;
066: private static P4HomeRemote p4home = null;
067: private static AHomeRemote ahome = null;
068: private static BHomeRemote bhome = null;
069:
070: /**
071: * Standard constructor
072: */
073: public F_Dass(String name) {
074: super (name);
075: }
076:
077: protected boolean isInit = false;
078:
079: /**
080: * setup is called before each test case.
081: * If not initialized, load the dass bean and lookup the Home.
082: */
083: protected void setUp() {
084: super .setUp();
085: if (!isInit) {
086: useBeans("dass", false);
087: try {
088: p1home = (P1HomeRemote) PortableRemoteObject.narrow(
089: ictx.lookup("relation_dass_P1Home"),
090: P1HomeRemote.class);
091: p2home = (P2HomeRemote) PortableRemoteObject.narrow(
092: ictx.lookup("relation_dass_P2Home"),
093: P2HomeRemote.class);
094: p3home = (P3HomeRemote) PortableRemoteObject.narrow(
095: ictx.lookup("relation_dass_P3Home"),
096: P3HomeRemote.class);
097: p4home = (P4HomeRemote) PortableRemoteObject.narrow(
098: ictx.lookup("relation_dass_P4Home"),
099: P4HomeRemote.class);
100: ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx
101: .lookup("relation_dass_AHome"),
102: AHomeRemote.class);
103: bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx
104: .lookup("relation_dass_BHome"),
105: BHomeRemote.class);
106: } catch (NamingException e) {
107: fail("Cannot get bean home: " + e.getMessage());
108: }
109: isInit = true;
110: }
111: }
112:
113: /**
114: * teardown is called after each test case.
115: * Notes for debugging :
116: * To see DataBase state after a test is passed :
117: * Replace the cleanall() by sync(false).
118: */
119: protected void tearDown() throws Exception {
120: cleanall();
121: //sync(false);
122: }
123:
124: /**
125: * Cleanup all the dass after each test, to make tests independant.
126: * Use transaction to avoid known bugs during cleanup.
127: * (With no transaction -> 1 remove will fail)
128: */
129: private void cleanall() throws Exception {
130: debug("remove all beans");
131:
132: // In case there is a problem during remove, we must unload
133: // the bean to restart properly.
134: try {
135: cleanAllBeans(ahome);
136: cleanAllBeans(bhome);
137: cleanAllBeans(p1home);
138: cleanAllBeans(p2home);
139: cleanAllBeans(p3home);
140: cleanAllBeans(p4home);
141: } catch (Exception e) {
142: error("Exception in cleanup: " + e);
143: unloadBeans("dass");
144: isInit = false;
145: }
146: }
147:
148: private void cleanAllBeans(DassHome home) throws RemoteException,
149: RemoveException {
150: try {
151: Collection c = home.findAll();
152: for (Iterator i = c.iterator(); i.hasNext();) {
153: EJBObject p = (EJBObject) i.next();
154: p.remove();
155: }
156: } catch (FinderException e) {
157: }
158: }
159:
160: // ---------------------------------------------------------------
161: // Set of the tests that are passed automatically.
162: // ---------------------------------------------------------------
163:
164: public void testCreateA() throws Exception {
165: ahome.create("A1");
166: }
167:
168: public void testCreateB() throws Exception {
169: bhome.create("B1");
170: }
171:
172: /**
173: * This test fails, normally, because:
174: * - setting the cmr field doesn't work because it's also a cmp field.
175: * - the cmp field is not changed, so this means that the cmr field is readonly.
176: * Jonas should throw an exception when we try to modify this kind of cmr field.
177: */
178: public void _testCreateA2B() throws Exception {
179: ARemote a1 = ahome.create("A1");
180: bhome.create("B1");
181: a1.assignB("B1");
182: String cmrval = a1.getBcmrvalue();
183: assertTrue("Bad cmr value:" + cmrval, "B1".equals(cmrval));
184: }
185:
186: public void _testCreateA2BTx() throws Exception {
187: try {
188: utx.begin();
189: _testCreateA2B();
190: } finally {
191: try {
192: utx.commit();
193: } catch (Exception ii) {
194: }
195: }
196: }
197:
198: /**
199: * Simple example with 2 beans with the same pk and a cmr on this pk.
200: * looks like a bean mapped on 2 tables.
201: */
202: public void testCreateP1P2() throws Exception {
203: P1Remote p1 = p1home.create("P", "100");
204: P2Remote p2 = p2home.create("P");
205: String val = p2.getP1Value();
206: assertTrue("Cannot access bean P1", "100".equals(val));
207: }
208:
209: /**
210: * Complex example of beans with cmr mapped on fields of the primary key.
211: * Note that the primary key is composite.
212: */
213: public void testCreateP3() throws Exception {
214: P1Remote p1 = p1home.create("P", "100");
215: P4Remote p4 = p4home.create("P", "102");
216: P3Remote p3 = p3home.create("P3", "P");
217: String val = p3.getP1Value();
218: assertTrue("Cannot access bean P1", "100".equals(val));
219: val = p3.getP4Value();
220: assertTrue("Cannot access bean P4", "102".equals(val));
221: }
222:
223: /**
224: * Run all the tests of this suite.
225: */
226: public static Test suite() {
227: return new TestSuite(F_Dass.class);
228: }
229:
230: public static void main(String args[]) throws Exception {
231: String testtorun = null;
232:
233: // Get args
234: for (int argn = 0; argn < args.length; argn++) {
235: String sarg = args[argn];
236: if (sarg.equals("-n")) {
237: testtorun = args[++argn];
238: }
239: }
240: if (testtorun == null) {
241: junit.textui.TestRunner.run(suite());
242: } else {
243: junit.textui.TestRunner.run(new F_Dass(testtorun));
244: }
245: }
246:
247: }
|