KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > collection > POBuilder


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.runtime.collection;
19
20 import org.objectweb.speedo.SpeedoTestHelper;
21 import org.objectweb.speedo.pobjects.collection.Employee;
22 import org.objectweb.speedo.pobjects.collection.AMMB;
23 import org.objectweb.speedo.pobjects.collection.BMMB;
24 import org.objectweb.util.monolog.api.BasicLevel;
25
26 import javax.jdo.PersistenceManager;
27
28 import junit.framework.Assert;
29
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  *
37  * @author S.Chassande-Barrioz
38  */

39 public class POBuilder extends SpeedoTestHelper {
40
41     public final static String JavaDoc EMPLOYEE_NAME = "POBuilder_testCreateEmployee";
42     public final static String JavaDoc EMPLOYEE_NAME2 = "POBuilder_testSetCollection";
43     public final static int NB_ELEMENTS = 20;
44     public final static int NB_ELEMENTS2 = 5;
45
46     public static String JavaDoc getName(int i) {
47         return "testIterator_e" + i;
48     }
49
50     public static String JavaDoc getName2(int i) {
51         return "testSetCollection_e" + i;
52     }
53
54     public static List JavaDoc getElementNames() {
55         ArrayList JavaDoc res = new ArrayList JavaDoc(NB_ELEMENTS);
56         for(int i=0; i<NB_ELEMENTS; i++) {
57             res.add(getName(i));
58         }
59         return res;
60     }
61
62     public static List JavaDoc getElementNames2() {
63         ArrayList JavaDoc res = new ArrayList JavaDoc(NB_ELEMENTS2);
64         for(int i=0; i<NB_ELEMENTS2; i++) {
65             res.add(getName2(i));
66         }
67         return res;
68     }
69
70     public POBuilder(String JavaDoc s) {
71         super(s);
72     }
73
74     protected String JavaDoc getLoggerName() {
75         return LOG_NAME + ".rt.collection.POBuilder";
76     }
77
78     public void testCreateEmployee() {
79         logger.log(BasicLevel.DEBUG, "Start testCreateEmployee");
80         Employee e = new Employee(EMPLOYEE_NAME, null);
81
82         PersistenceManager pm = pmf.getPersistenceManager();
83         pm.makePersistent(e);
84         Employee[] es = new Employee[NB_ELEMENTS];
85         for(int i=0; i<NB_ELEMENTS; i++) {
86             es[i]= new Employee(getName(i));
87             e.addFriend(es[i]);
88             e.addInt(i);
89         }
90         Assert.assertEquals("Bad sise:" , NB_ELEMENTS, e.friends.size());
91
92         Collection JavaDoc elems = e.friends;
93         Assert.assertNotNull("Null collection field", elems);
94         Iterator JavaDoc elemIt = elems.iterator();
95         Assert.assertNotNull("Null iterator over elements", elemIt);
96         ArrayList JavaDoc elemNames = new ArrayList JavaDoc();
97         while(elemIt.hasNext()) {
98             Object JavaDoc elem = elemIt.next();
99             Assert.assertNotNull("Null element", elem);
100             Assert.assertEquals("bad elem type", Employee.class, elem.getClass());
101             elemNames.add(((Employee) elem).name);
102         }
103         assertSameCollection("Bad element names: ", POBuilder.getElementNames(), elemNames);
104
105         pm.close();
106     }
107
108     public void testSetCollection() {
109         logger.log(BasicLevel.DEBUG, "Start testSetCollection");
110         Employee e = new Employee(EMPLOYEE_NAME2, null);
111
112         PersistenceManager pm = pmf.getPersistenceManager();
113         pm.currentTransaction().begin();
114         pm.makePersistent(e);
115         pm.currentTransaction().commit();
116
117         ArrayList JavaDoc es = new ArrayList JavaDoc();
118
119         pm.currentTransaction().begin();
120         for(int i=0; i<NB_ELEMENTS2; i++) {
121             es.add(new Employee(getName2(i)));
122         }
123         e.setFriends(es);
124
125         Collection JavaDoc elems = e.friends;
126         Assert.assertNotNull("Null collection field", elems);
127         Iterator JavaDoc elemIt = elems.iterator();
128         Assert.assertNotNull("Null iterator over elements", elemIt);
129         ArrayList JavaDoc elemNames = new ArrayList JavaDoc();
130         while(elemIt.hasNext()) {
131             Object JavaDoc elem = elemIt.next();
132             Assert.assertNotNull("Null element", elem);
133             Assert.assertEquals("bad elem type", Employee.class, elem.getClass());
134             elemNames.add(((Employee) elem).name);
135         }
136         assertSameCollection("Bad element names: ", POBuilder.getElementNames2(), elemNames);
137
138         pm.currentTransaction().commit();
139         pm.close();
140     }
141
142     public void testCreateXMMB() {
143         logger.log(BasicLevel.DEBUG, "Start testCreateXMMB");
144         long ida = 1230;
145         AMMB a = new AMMB(ida);
146         ArrayList JavaDoc bs = new ArrayList JavaDoc();
147         ArrayList JavaDoc as = new ArrayList JavaDoc(1);
148         as.add(a);
149         for(int i=0; i<5; i++) {
150             BMMB b = new BMMB(ida + i);
151             b.setAs(as);
152             bs.add(b);
153         }
154         a.setBs(bs);
155         PersistenceManager pm = pmf.getPersistenceManager();
156         pm.makePersistent(a);
157         pm.close();
158     }
159 }
160
Popular Tags