001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
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 of the License, or (at your option) 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 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.runtime.map;
027:
028: import org.objectweb.speedo.SpeedoTestHelper;
029: import org.objectweb.speedo.pobjects.map.Registry;
030: import org.objectweb.speedo.pobjects.map.A;
031: import org.objectweb.speedo.pobjects.map.B;
032:
033: import javax.jdo.PersistenceManager;
034: import javax.jdo.JDOException;
035:
036: import junit.framework.Assert;
037:
038: import java.util.Collection;
039:
040: public class TestMap extends SpeedoTestHelper {
041:
042: public TestMap(String s) {
043: super (s);
044: }
045:
046: protected String getLoggerName() {
047: return LOG_NAME + ".rt.map.TestMap";
048: }
049:
050: public void testCreation1() {
051: Registry r1 = new Registry("r1");
052: Registry r2 = new Registry("r2");
053: Registry r3 = new Registry("r3");
054: r1.bind("a", r2);
055: r1.bind("b", r3);
056:
057: PersistenceManager pm = pmf.getPersistenceManager();
058: pm.makePersistent(r1);
059: Object oid1 = pm.getObjectId(r1);
060: Assert.assertNotNull("null object identifier of r1", r1);
061: pm.close();
062:
063: r1 = null;
064: r2 = null;
065: r3 = null;
066: pm = pmf.getPersistenceManager();
067: pm.evictAll();
068: r1 = (Registry) pm.getObjectById(oid1, true);
069: Assert.assertNotNull(
070: "null instance returned by getObjectById(oid1)", r1);
071: r2 = (Registry) r1.lookup("a");
072: Assert.assertNotNull("null instance returned by r1.lookup(a)",
073: r2);
074: r3 = (Registry) r1.lookup("b");
075: Assert.assertNotNull("null instance returned by r1.lookup(b)",
076: r3);
077: pm.currentTransaction().begin();
078: pm.deletePersistent(r1);
079: pm.deletePersistent(r2);
080: pm.deletePersistent(r3);
081: pm.currentTransaction().commit();
082: pm.close();
083: }
084:
085: public void testA() {
086:
087: //Make persitent the a instance
088: PersistenceManager pm = pmf.getPersistenceManager();
089: A a = new A(0);
090: pm.makePersistent(a);
091: pm.close();
092:
093: //Add B instances into the map
094: final int NB_B = 10;
095: final String IDX_PREFIX = "index2B";
096: final String F1_PREFIX = "toto";
097: pm = pmf.getPersistenceManager();
098: for (int i = 0; i < NB_B; i++) {
099: a.add(IDX_PREFIX + i, new B(i, F1_PREFIX + i));
100: }
101: pm.close();
102:
103: //Clear the cache
104: a = null; //permit the GC to garbage instances
105: pm = pmf.getPersistenceManager();
106: pm.evictAll();
107: pm.close();
108:
109: //Check the instance existing
110: pm = pmf.getPersistenceManager();
111: try {
112: a = (A) pm.getObjectById(pm.newObjectIdInstance(A.class,
113: "" + 0), false);
114: } catch (JDOException e) {
115: fail("The A instance is not found on the data support");
116: }
117: Collection names = a.names();
118: assertNotNull("name collection is null", names);
119: assertEquals("Bad name collection size", NB_B, names.size());
120: B[] bs = new B[NB_B];
121: for (int i = 0; i < NB_B; i++) {
122: String idx = IDX_PREFIX + i;
123: assertTrue(
124: "The name collection does not contain the index: "
125: + idx, names.contains(idx));
126: bs[i] = a.getB(idx);
127: assertNotNull(
128: "Null element assocaited to the index " + idx,
129: bs[i]);
130: assertEquals(
131: "Bad F1 value for the element assocaited to the index "
132: + idx, F1_PREFIX + i, bs[i].getF1());
133: }
134: a.clear();
135: pm.currentTransaction().begin();
136: pm.deletePersistentAll(bs);
137: pm.deletePersistent(a);
138: pm.currentTransaction().commit();
139: pm.close();
140: }
141: }
|