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_RcycleEC2.java 7533 2005-10-19 15:55:05Z durieuxp $
022: * --------------------------------------------------------------------------
023: */
024:
025: package org.objectweb.jonas.jtests.clients.entity;
026:
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.naming.NamingException;
031: import javax.rmi.PortableRemoteObject;
032:
033: import junit.framework.Test;
034: import junit.framework.TestSuite;
035:
036: import org.objectweb.jonas.jtests.beans.relation.rcycle.PersonRemote;
037: import org.objectweb.jonas.jtests.beans.relation.rcycle.PersonHomeRemote;
038: import org.objectweb.jonas.jtests.util.JTestCase;
039:
040: /**
041: * This is a test suite on CMP 2 : legacy, cycle in relations, relations between
042: * the same bean.
043: * @author Helene Joanin
044: */
045: public class F_RcycleEC2 extends JTestCase {
046:
047: private static final int ID_L_ERIC = 1;
048:
049: private static final int ID_JL_HELENE = 2;
050:
051: private static final int ID_L_GUILHEM = 3;
052:
053: private static final int ID_L_MALVA = 4;
054:
055: private static PersonHomeRemote personhome = null;
056:
057: public F_RcycleEC2(String name) {
058: super (name);
059: }
060:
061: protected boolean isInit = false;
062:
063: protected void setUp() {
064: super .setUp();
065: if (!isInit) {
066: useBeans("rcycle", true);
067: try {
068: personhome = (PersonHomeRemote) PortableRemoteObject
069: .narrow(ictx.lookup("RcyclePersonHome"),
070: PersonHomeRemote.class);
071: } catch (NamingException e) {
072: fail("Cannot get bean home: " + e.getMessage());
073: }
074: isInit = true;
075: }
076: }
077:
078: /**
079: * Test the findAll method.
080: * @throws Exception
081: */
082: public void testFindAll() throws Exception {
083: Collection cp = personhome.findAll();
084: assertEquals("Number of Persons: ", 4, cp.size());
085: }
086:
087: /**
088: * (Bug #300533) Test the findQuery1 method.
089: * @throws Exception
090: */
091: public void testFindQuery1() throws Exception {
092: Collection cp = personhome.findQuery1();
093: assertEquals("Number of Persons: ", 0, cp.size());
094: }
095:
096: /**
097: * Test the findQuery2 method.
098: * @throws Exception
099: */
100: public void testFindQuery2() throws Exception {
101: Collection cp = personhome.findQuery2();
102: assertEquals("Number of Persons: ", 0, cp.size());
103: }
104:
105: /**
106: * (Bug #300526) Test the findSpouse3 method.
107: * @throws Exception
108: */
109: public void testFindSpouse3() throws Exception {
110: PersonRemote p = personhome.findSpouse3();
111: assertNull("Laurent Guilhem spouse: ", p);
112: }
113:
114: /**
115: * Test the spouse relation.
116: * @throws Exception
117: */
118: public void testSpouseRelation() throws Exception {
119: PersonRemote ph = personhome.findByPrimaryKey(new Integer(
120: ID_L_ERIC));
121: Integer iw = ph.retrieveSpouse();
122: assertEquals("Wife of Laurent Eric: ", ID_JL_HELENE, iw
123: .intValue());
124: PersonRemote pw = personhome.findByPrimaryKey(iw);
125: Integer ih = pw.retrieveSpouse();
126: assertEquals("Husband of Joanin-Laurent Helene: ", ID_L_ERIC,
127: ih.intValue());
128: PersonRemote pc = personhome.findByPrimaryKey(new Integer(
129: ID_L_GUILHEM));
130: Integer in = pc.retrieveSpouse();
131: assertNull("Laurent Guilhem spouse: ", in);
132: }
133:
134: /**
135: * Test the guardian/is-guardian-of relation.
136: * @throws Exception
137: */
138: public void testGuardianRelation() throws Exception {
139: Integer iLEric = new Integer(ID_L_ERIC);
140: PersonRemote ph = personhome.findByPrimaryKey(iLEric);
141: Collection c = ph.retrieveGuardianOf();
142: assertEquals("Laurent Eric guardian of: ", 2, c.size());
143: for (Iterator i = c.iterator(); i.hasNext();) {
144: Integer ip = (Integer) i.next();
145: PersonRemote p = personhome.findByPrimaryKey(ip);
146: assertEquals("Guardian of " + p.getName() + ": ", iLEric, p
147: .retrieveGuardian());
148: }
149: }
150:
151: /**
152: * Test the parents/children relation.
153: * @throws Exception
154: */
155: public void testParentsChildrenRelation() throws Exception {
156: Integer iLEric = new Integer(ID_L_ERIC);
157: PersonRemote ph = personhome.findByPrimaryKey(iLEric);
158: Collection cc = ph.retrieveChildren();
159: assertEquals("Laurent Eric father of: ", 2, cc.size());
160: for (Iterator i = cc.iterator(); i.hasNext();) {
161: Integer ip = (Integer) i.next();
162: PersonRemote p = personhome.findByPrimaryKey(ip);
163: Collection cp = p.retrieveParents();
164: assertTrue("Father of " + p.getName() + ": ", cp
165: .contains(iLEric));
166: }
167: }
168:
169: /**
170: * Test if a cmr null is 'null'
171: * @throws Exception
172: */
173: public void testCmrNull() throws Exception {
174: PersonRemote ph = personhome.findByPrimaryKey(new Integer(
175: ID_L_ERIC));
176: assertTrue("CMR null is NOT null", ph.testCmrNull());
177: }
178:
179: /**
180: * Verify the spy trace to check less database access are done
181: * @throws Exception
182: */
183: public void testPrefetch() throws Exception {
184: //sleep(15000);
185: try {
186: utx.begin();
187: Integer iLEric = new Integer(ID_L_ERIC);
188: PersonRemote ph = personhome.findByPrimaryKey(iLEric);
189: // If there is CMR prefetching, this may imply less database access
190: Collection cc = ph.retrieveChildrenNames();
191: assertEquals("Laurent Eric father of: ", 2, cc.size());
192: } finally {
193: utx.commit();
194: }
195: }
196:
197: protected boolean initStateOK() throws Exception {
198: return true;
199: }
200:
201: public static Test suite() {
202: return new TestSuite(F_RcycleEC2.class);
203: }
204:
205: public static void main(String args[]) {
206: String testtorun = null;
207: // Get args
208: for (int argn = 0; argn < args.length; argn++) {
209: String sarg = args[argn];
210: if (sarg.equals("-n")) {
211: testtorun = args[++argn];
212: }
213: }
214: if (testtorun == null) {
215: junit.textui.TestRunner.run(suite());
216: } else {
217: junit.textui.TestRunner.run(new F_RcycleEC2(testtorun));
218: }
219: }
220: }
|