001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: InverseMapFieldTester.java,v 1.3 2002/11/08 05:06:27 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import java.util.Arrays;
014: import java.util.HashMap;
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.Set;
020: import javax.jdo.InstanceCallbacks;
021: import javax.jdo.JDOHelper;
022: import javax.jdo.PersistenceManager;
023: import junit.framework.Assert;
024:
025: /**
026: * A test object for testing inverse <code>java.util.Map</code> fields.
027: *
028: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
029: * @version $Revision: 1.3 $
030: */
031:
032: public class InverseMapFieldTester extends TestObject implements
033: InstanceCallbacks {
034: private Set mapValues = new HashSet();
035: private Map inverseMap = new HashMap();
036:
037: /**
038: * Constructs an empty map field tester object.
039: */
040:
041: public InverseMapFieldTester() {
042: }
043:
044: public Object clone() {
045: InverseMapFieldTester mft = (InverseMapFieldTester) super
046: .clone();
047:
048: HashSet mv = new HashSet();
049: HashMap im = new HashMap();
050:
051: Iterator i = mapValues.iterator();
052:
053: while (i.hasNext()) {
054: InverseMapValue imv = (InverseMapValue) ((InverseMapValue) i
055: .next()).clone();
056:
057: imv.setOwner(mft);
058: mv.add(imv);
059: im.put(imv.getKey(), imv);
060: }
061:
062: mft.mapValues = mv;
063: mft.inverseMap = im;
064:
065: return mft;
066: }
067:
068: public void fillRandom() {
069: throw new UnsupportedOperationException();
070: }
071:
072: public boolean compareTo(Object obj) {
073: throw new UnsupportedOperationException();
074: }
075:
076: public void jdoPreStore() {
077: }
078:
079: public void jdoPostLoad() {
080: }
081:
082: public void jdoPreClear() {
083: }
084:
085: public void jdoPreDelete() {
086: PersistenceManager pm = JDOHelper.getPersistenceManager(this );
087:
088: /* Delete all dependent objects. */
089: pm.deletePersistentAll(mapValues);
090: }
091:
092: public void addMapValue(InverseMapValue imv) {
093: String key = imv.getKey();
094:
095: Assert.assertTrue("inverseMap already contains key: " + key,
096: !inverseMap.containsKey(key));
097: Assert.assertTrue("inverseMap already contains value: " + imv,
098: !inverseMap.containsValue(imv));
099:
100: Assert.assertTrue("add() returned false adding value: " + imv,
101: mapValues.add(imv));
102: Object oldimv = inverseMap.put(key, imv);
103:
104: if (JDOHelper.isPersistent(this ))
105: Assert.assertSame(
106: "put() should have returned object just persisted with key: "
107: + key, imv, oldimv);
108: else
109: Assert.assertNull("put() returned non-null adding key: "
110: + key, oldimv);
111:
112: Assert.assertTrue("inverseMap does not contain key: " + key,
113: inverseMap.containsKey(key));
114: Assert.assertTrue("inverseMap does not contain value: " + imv,
115: inverseMap.containsValue(imv));
116: }
117:
118: public void removeMapValue(InverseMapValue imv) {
119: Assert.assertSame(this , imv.getOwner());
120:
121: String key = imv.getKey();
122:
123: if (JDOHelper.isPersistent(imv))
124: JDOHelper.getPersistenceManager(imv).deletePersistent(imv);
125: else {
126: inverseMap.remove(key);
127: mapValues.remove(imv);
128: }
129:
130: Assert.assertTrue("inverseMap still contains key: " + key,
131: !inverseMap.containsKey(key));
132: Assert.assertTrue("inverseMap still contains value",
133: !inverseMap.containsValue(imv));
134: }
135:
136: public void assertMapsEqual(List values) {
137: Assert.assertEquals(values.size(), mapValues.size());
138: Assert.assertEquals(mapValues.size(), inverseMap.size());
139:
140: InverseMapValue[] pValues = new InverseMapValue[values.size()];
141: HashMap pValueMap = new HashMap();
142:
143: for (int i = 0; i < values.size(); ++i) {
144: InverseMapValue expected = (InverseMapValue) values.get(i);
145: String key = expected.getKey();
146: Assert.assertTrue(
147: "inverseMap does not contain key: " + key,
148: inverseMap.containsKey(key));
149:
150: pValues[i] = (InverseMapValue) inverseMap.get(expected
151: .getKey());
152: expected.assertEquals(pValues[i]);
153:
154: Assert.assertTrue("inverseMap does not contain value: "
155: + pValues[i], inverseMap.containsValue(pValues[i]));
156:
157: pValueMap.put(pValues[i].getKey(), pValues[i]);
158: }
159:
160: /*
161: * Use containsAll() to assert that the collections returned by keySet()
162: * and values() contain the expected elements.
163: */
164: Assert.assertTrue(inverseMap.keySet().containsAll(
165: pValueMap.keySet()));
166: Assert.assertTrue(inverseMap.values().containsAll(
167: Arrays.asList(pValues)));
168:
169: /*
170: * Iterate over the entire entry set of inverseMap and assert that
171: * every expected entry is returned.
172: */
173: Iterator i = inverseMap.entrySet().iterator();
174:
175: while (i.hasNext()) {
176: Map.Entry entry = (Map.Entry) i.next();
177:
178: InverseMapValue imv = (InverseMapValue) pValueMap.get(entry
179: .getKey());
180:
181: Assert.assertEquals(entry.getKey(), imv.getKey());
182: Assert.assertEquals(entry.getValue(), imv);
183:
184: pValueMap.remove(entry.getKey());
185: }
186:
187: Assert.assertEquals(0, pValueMap.size());
188: }
189: }
|