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.map;
018:
019: import org.objectweb.speedo.SpeedoTestHelper;
020: import org.objectweb.speedo.pobjects.map.C;
021: import org.objectweb.speedo.pobjects.map.D;
022:
023: import java.util.ArrayList;
024: import java.util.Map;
025: import javax.jdo.PersistenceManager;
026:
027: /**
028: *
029: * @author S.Chassande-Barrioz
030: */
031: public class TestKeyFieldMap extends SpeedoTestHelper {
032:
033: public TestKeyFieldMap(String s) {
034: super (s);
035: }
036:
037: protected String getLoggerName() {
038: return LOG_NAME + ".rt.map.TestKeyFieldMap";
039: }
040:
041: public void test1() {
042: int MAP_SIZE = 10;
043: String F1_PREFIX = "f1_";
044: PersistenceManager pm = pmf.getPersistenceManager();
045:
046: //Create instances
047: pm.currentTransaction().begin();
048: C c = new C(1);
049: ArrayList ds = new ArrayList(MAP_SIZE * 2);
050: for (int i = 0; i < MAP_SIZE; i++) {
051: String f1 = F1_PREFIX + i;
052: ds.add(new D(i, f1));
053: }
054: pm.makePersistent(c);
055: pm.makePersistentAll(ds);
056: pm.currentTransaction().commit();
057:
058: //Add into the map
059: pm.currentTransaction().begin();
060: Map m = c.getDkey2d();
061: for (int i = 0; i < MAP_SIZE; i++) {
062: D d = (D) ds.get(i);
063: m.put(d.getF1(), d);
064: }
065: c = null;
066: ds = null;
067: pm.currentTransaction().commit();
068: finishTest(pm, MAP_SIZE, F1_PREFIX);
069: }
070:
071: public void test2() {
072: int MAP_SIZE = 10;
073: String F1_PREFIX = "f1_";
074: PersistenceManager pm = pmf.getPersistenceManager();
075: pm.currentTransaction().begin();
076: C c = new C(1);
077: Map m = c.getDkey2d();
078: for (int i = 0; i < MAP_SIZE; i++) {
079: String f1 = "f1_" + i;
080: D d = new D(i, f1);
081: m.put(f1, d);
082: }
083: pm.makePersistent(c);
084: pm.currentTransaction().commit();
085: finishTest(pm, MAP_SIZE, F1_PREFIX);
086: }
087:
088: public void test3() {
089: int MAP_SIZE = 10;
090: String F1_PREFIX = "f1_";
091: PersistenceManager pm = pmf.getPersistenceManager();
092: pm.currentTransaction().begin();
093: C c = new C(1);
094: for (int i = 0; i < MAP_SIZE; i++) {
095: String f1 = "f1_" + i;
096: D d = new D(i, f1);
097: d.setMyc(c);
098: pm.makePersistent(d);
099: }
100: pm.currentTransaction().commit();
101: finishTest(pm, MAP_SIZE, F1_PREFIX);
102: }
103:
104: private void finishTest(PersistenceManager pm, final int MAP_SIZE,
105: final String F1_PREFIX) {
106: pm.currentTransaction().begin();
107: long id = MAP_SIZE / 2;
108: String f1 = F1_PREFIX + id;
109: C c = (C) pm.getObjectById(
110: pm.newObjectIdInstance(C.class, "1"), false);
111: Object d = c.getDkey2d().get(f1);
112: assertNotNull("No Object associated to the key '" + f1 + "'", d);
113: assertTrue("Object associated to the key '" + f1 + "'",
114: d instanceof D);
115: assertEquals("Bad D identifier ", id, ((D) d).getMyid());
116: assertNotNull("Bad D identifier ", ((D) d).getMyc());
117: assertEquals("Bad C identifier ", 1, ((D) d).getMyc().getMyid());
118: c = ((D) d).getMyc();
119: ((D) d).setMyc(null);
120: assertNull("The map contains again the D instance", c
121: .getDkey2d().get(f1));
122: pm.currentTransaction().commit();
123:
124: //Delete
125: pm.currentTransaction().begin();
126: pm.deletePersistent(d);
127: pm.deletePersistentAll(c.getDkey2d().values());
128: pm.deletePersistent(c);
129: pm.currentTransaction().commit();
130: pm.close();
131: }
132: }
|