KickJava   Java API By Example, From Geeks To Geeks.

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


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.G;
22 import org.objectweb.speedo.pobjects.collection.H;
23
24 import javax.jdo.PersistenceManager;
25 import javax.jdo.Query;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  *
33  * @author S.Chassande-Barrioz
34  */

35 public class TestGH extends SpeedoTestHelper {
36
37     public TestGH(String JavaDoc s) {
38         super(s);
39     }
40
41     protected String JavaDoc getLoggerName() {
42         return LOG_NAME + ".rt.collection.TestGH";
43     }
44
45     public void testA() {
46         int NBOBJ = 10;
47         PersistenceManager pm = pmf.getPersistenceManager();
48         pm.currentTransaction().begin();
49         G g = new G("g1_testA");
50         Collection JavaDoc hs = g.getHs();
51         for(int i=0; i<NBOBJ; i++) {
52             H h = new H("hid_" + i);
53             h.setH1("h1_testA");
54             hs.add(h);
55         }
56         pm.makePersistent(g);
57         g = null;
58         hs = null;
59         pm.currentTransaction().commit();
60         pm.evictAll();
61         pm.close();
62         pm = pmf.getPersistenceManager();
63         pm.evictAll();
64         pm.currentTransaction().begin();
65         H h0 = (H) pm.getObjectById(pm.newObjectIdInstance(H.class, "hid_0"), false);
66         assertNotNull("Null h0", h0);
67         g = h0.getG();
68         assertNotNull("Null g", g);
69         pm.deletePersistentAll(g.getHs());
70         pm.deletePersistent(g);
71         pm.currentTransaction().commit();
72         pm.close();
73
74     }
75     public void testCollectionElementLoadingWithPrefetch() {
76         int NBOBJ = 2;
77         PersistenceManager pm = pmf.getPersistenceManager();
78         //create persistent instances
79
pm.currentTransaction().begin();
80         G g = new G("g1_testCollectionElementLoadingWithPrefetch");
81         Collection JavaDoc hs = g.getHs();
82         for(int i=0; i<NBOBJ; i++) {
83             H h = new H("hid_" + i);
84             h.setH1("h1_testCollectionElementLoadingWithPrefetch");
85             hs.add(h);
86         }
87         hs = null;
88         pm.makePersistent(g);
89         Object JavaDoc g_oid = pm.getObjectId(g);
90         g = null;
91         pm.currentTransaction().commit();
92
93         //evict all instances
94
pm.evictAll();
95
96         pm.currentTransaction().begin();
97         Query q = pm.newQuery(H.class);
98         q.declareParameters("String p1");
99         q.setFilter("hid == p1");
100         Object JavaDoc o = new ArrayList JavaDoc(((Collection JavaDoc) q.execute("hid_" + (NBOBJ-1))));
101         q.closeAll();
102         
103         g = (G) pm.getObjectById(g_oid, false);
104         for (Iterator JavaDoc iter = g.getHs().iterator(); iter.hasNext();) {
105             iter.next();
106         }
107         pm.currentTransaction().commit();
108
109         //Clean up
110
pm.currentTransaction().begin();
111         pm.deletePersistentAll(g.getHs());
112         pm.deletePersistent(g);
113         pm.currentTransaction().commit();
114         pm.close();
115
116     }
117 }
118
Popular Tags