KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > inheritance > TestDatastorelid


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.inheritance;
19
20 import org.objectweb.speedo.SpeedoTestHelper;
21 import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.RootClass;
22 import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass1;
23 import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass12;
24 import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass2;
25 import org.objectweb.util.monolog.api.BasicLevel;
26 import javax.jdo.Extent;
27 import javax.jdo.Query;
28 import javax.jdo.PersistenceManager;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  *
35  * @author S.Chassande-Barrioz
36  */

37 public class TestDatastorelid extends SpeedoTestHelper {
38
39     public TestDatastorelid(String JavaDoc s) {
40         super(s);
41     }
42
43     protected String JavaDoc getLoggerName() {
44         return LOG_NAME + "rt.inheritance.TestDatastorelid";
45     }
46
47     public void testA() {
48         PersistenceManager pm = pmf.getPersistenceManager();
49         pm.currentTransaction().begin();
50         ArrayList JavaDoc rcs = new ArrayList JavaDoc();
51         rcs.add(new RootClass("rf1"));
52         rcs.add(new RootClass("rf2"));
53         rcs.add(new Subclass1("rf3", "f1_3"));
54         rcs.add(new Subclass12("rf4", "f1_4", "f12_4"));
55         rcs.add(new Subclass2("rf5", "f2_5"));
56         pm.makePersistentAll(rcs);
57         pm.currentTransaction().commit();
58
59         pm.currentTransaction().begin();
60         checkNumberOfInstanceWithExtent(RootClass.class, false, 2, pm);
61         checkNumberOfInstanceWithExtent(RootClass.class, true, 5, pm);
62
63         checkNumberOfInstanceWithExtent(Subclass1.class, false, 1, pm);
64         checkNumberOfInstanceWithExtent(Subclass1.class, true, 2, pm);
65
66         checkNumberOfInstanceWithExtent(Subclass12.class, true, 1, pm);
67         checkNumberOfInstanceWithExtent(Subclass12.class, false, 1, pm);
68
69         checkNumberOfInstanceWithExtent(Subclass2.class, false, 1, pm);
70         checkNumberOfInstanceWithExtent(Subclass2.class, true, 1, pm);
71         pm.currentTransaction().commit();
72
73         pm.currentTransaction().begin();
74         for(int i=0; i<rcs.size(); i++) {
75             rcs.set(i, pm.getObjectId(rcs.get(i)));
76         }
77         pm.currentTransaction().commit();
78         pm.evictAll();
79
80         pm.currentTransaction().begin();
81         for(int i=0; i<rcs.size(); i++) {
82             pm.getObjectById(rcs.get(i), false);
83         }
84         pm.currentTransaction().commit();
85
86         pm.evictAll();
87
88         pm.currentTransaction().begin();
89         Query query = pm.newQuery(RootClass.class);
90         Collection JavaDoc col = (Collection JavaDoc) query.execute();
91         Iterator JavaDoc it = col.iterator();
92         int size = 0;
93         while(it.hasNext()) {
94             RootClass rc = (RootClass) it.next();
95             size ++;
96             logger.log(BasicLevel.DEBUG, "rc.rootField=" + rc.getRootField());
97             if ("rf1".equals(rc.getRootField())) {
98             } else if ("rf2".equals(rc.getRootField())) {
99             } else if ("rf3".equals(rc.getRootField())) {
100                 assertTrue("Bad class instance" + rc.getClass().getName(), rc instanceof Subclass1);
101                 assertEquals("Bad f1 value", "f1_3", ((Subclass1) rc).getF1());
102             } else if ("rf4".equals(rc.getRootField())) {
103                 assertTrue("Bad class instance" + rc.getClass().getName(), rc instanceof Subclass12);
104                 assertEquals("Bad f1 value", "f1_4", ((Subclass12) rc).getF1());
105                 assertEquals("Bad f1 value", "f12_4", ((Subclass12) rc).getF12());
106             } else if ("rf5".equals(rc.getRootField())) {
107                 assertTrue("Bad class instance" + rc.getClass().getName(), rc instanceof Subclass2);
108                 assertEquals("Bad f1 value", "f2_5", ((Subclass2) rc).getF2());
109             } else {
110                 fail("Unmanaged rootField value: " + rc.getRootField());
111             }
112         }
113         query.closeAll();
114         assertEquals("bad query result size", 5, size);
115         pm.currentTransaction().commit();
116         
117         pm.currentTransaction().begin();
118         for(int i=0; i<rcs.size(); i++) {
119             pm.deletePersistent(pm.getObjectById(rcs.get(i), false));
120         }
121         pm.currentTransaction().commit();
122         pm.close();
123
124     }
125
126     private void checkNumberOfInstanceWithExtent(Class JavaDoc clazz,
127                                                  boolean withSubClass,
128                                                  int expected,
129                                                  PersistenceManager pm) {
130         Extent e = pm.getExtent(clazz, withSubClass);
131         Iterator JavaDoc it = e.iterator();
132         int cpt = 0;
133         ArrayList JavaDoc rfs = new ArrayList JavaDoc(expected);
134         while(it.hasNext()) {
135             rfs.add(((RootClass) it.next()).getRootField());
136             cpt ++;
137         }
138         e.closeAll();
139         assertEquals("Bad number of " + clazz.getName()
140             + " instance (withSubclass: " + withSubClass + "): rfs=" + rfs,
141             expected, cpt);
142     }
143 }
144
Popular Tags