001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.collection;
018:
019: import org.objectweb.speedo.SpeedoTestHelper;
020: import org.objectweb.speedo.pobjects.collection.Employee;
021: import org.objectweb.speedo.pobjects.collection.AMMB;
022: import org.objectweb.speedo.pobjects.collection.BMMB;
023: import org.objectweb.util.monolog.api.BasicLevel;
024:
025: import javax.jdo.PersistenceManager;
026:
027: import junit.framework.Assert;
028:
029: import java.util.List;
030: import java.util.ArrayList;
031: import java.util.Collection;
032: import java.util.Iterator;
033:
034: /**
035: *
036: * @author S.Chassande-Barrioz
037: */
038: public class POBuilder extends SpeedoTestHelper {
039:
040: public final static String EMPLOYEE_NAME = "POBuilder_testCreateEmployee";
041: public final static String EMPLOYEE_NAME2 = "POBuilder_testSetCollection";
042: public final static int NB_ELEMENTS = 20;
043: public final static int NB_ELEMENTS2 = 5;
044:
045: public static String getName(int i) {
046: return "testIterator_e" + i;
047: }
048:
049: public static String getName2(int i) {
050: return "testSetCollection_e" + i;
051: }
052:
053: public static List getElementNames() {
054: ArrayList res = new ArrayList(NB_ELEMENTS);
055: for (int i = 0; i < NB_ELEMENTS; i++) {
056: res.add(getName(i));
057: }
058: return res;
059: }
060:
061: public static List getElementNames2() {
062: ArrayList res = new ArrayList(NB_ELEMENTS2);
063: for (int i = 0; i < NB_ELEMENTS2; i++) {
064: res.add(getName2(i));
065: }
066: return res;
067: }
068:
069: public POBuilder(String s) {
070: super (s);
071: }
072:
073: protected String getLoggerName() {
074: return LOG_NAME + ".rt.collection.POBuilder";
075: }
076:
077: public void testCreateEmployee() {
078: logger.log(BasicLevel.DEBUG, "Start testCreateEmployee");
079: Employee e = new Employee(EMPLOYEE_NAME, null);
080:
081: PersistenceManager pm = pmf.getPersistenceManager();
082: pm.makePersistent(e);
083: Employee[] es = new Employee[NB_ELEMENTS];
084: for (int i = 0; i < NB_ELEMENTS; i++) {
085: es[i] = new Employee(getName(i));
086: e.addFriend(es[i]);
087: e.addInt(i);
088: }
089: Assert.assertEquals("Bad sise:", NB_ELEMENTS, e.friends.size());
090:
091: Collection elems = e.friends;
092: Assert.assertNotNull("Null collection field", elems);
093: Iterator elemIt = elems.iterator();
094: Assert.assertNotNull("Null iterator over elements", elemIt);
095: ArrayList elemNames = new ArrayList();
096: while (elemIt.hasNext()) {
097: Object elem = elemIt.next();
098: Assert.assertNotNull("Null element", elem);
099: Assert.assertEquals("bad elem type", Employee.class, elem
100: .getClass());
101: elemNames.add(((Employee) elem).name);
102: }
103: assertSameCollection("Bad element names: ", POBuilder
104: .getElementNames(), elemNames);
105:
106: pm.close();
107: }
108:
109: public void testSetCollection() {
110: logger.log(BasicLevel.DEBUG, "Start testSetCollection");
111: Employee e = new Employee(EMPLOYEE_NAME2, null);
112:
113: PersistenceManager pm = pmf.getPersistenceManager();
114: pm.currentTransaction().begin();
115: pm.makePersistent(e);
116: pm.currentTransaction().commit();
117:
118: ArrayList es = new ArrayList();
119:
120: pm.currentTransaction().begin();
121: for (int i = 0; i < NB_ELEMENTS2; i++) {
122: es.add(new Employee(getName2(i)));
123: }
124: e.setFriends(es);
125:
126: Collection elems = e.friends;
127: Assert.assertNotNull("Null collection field", elems);
128: Iterator elemIt = elems.iterator();
129: Assert.assertNotNull("Null iterator over elements", elemIt);
130: ArrayList elemNames = new ArrayList();
131: while (elemIt.hasNext()) {
132: Object elem = elemIt.next();
133: Assert.assertNotNull("Null element", elem);
134: Assert.assertEquals("bad elem type", Employee.class, elem
135: .getClass());
136: elemNames.add(((Employee) elem).name);
137: }
138: assertSameCollection("Bad element names: ", POBuilder
139: .getElementNames2(), elemNames);
140:
141: pm.currentTransaction().commit();
142: pm.close();
143: }
144:
145: public void testCreateXMMB() {
146: logger.log(BasicLevel.DEBUG, "Start testCreateXMMB");
147: long ida = 1230;
148: AMMB a = new AMMB(ida);
149: ArrayList bs = new ArrayList();
150: ArrayList as = new ArrayList(1);
151: as.add(a);
152: for (int i = 0; i < 5; i++) {
153: BMMB b = new BMMB(ida + i);
154: b.setAs(as);
155: bs.add(b);
156: }
157: a.setBs(bs);
158: PersistenceManager pm = pmf.getPersistenceManager();
159: pm.makePersistent(a);
160: pm.close();
161: }
162: }
|