1 10 11 package com.triactive.jdo.test; 12 13 import java.util.Arrays ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 import javax.jdo.InstanceCallbacks; 21 import javax.jdo.JDOHelper; 22 import javax.jdo.PersistenceManager; 23 import junit.framework.Assert; 24 25 26 32 33 public class InverseMapFieldTester extends TestObject implements InstanceCallbacks 34 { 35 private Set mapValues = new HashSet (); 36 private Map inverseMap = new HashMap (); 37 38 39 42 43 public InverseMapFieldTester() 44 { 45 } 46 47 48 public Object clone() 49 { 50 InverseMapFieldTester mft = (InverseMapFieldTester)super.clone(); 51 52 HashSet mv = new HashSet (); 53 HashMap im = new HashMap (); 54 55 Iterator i = mapValues.iterator(); 56 57 while (i.hasNext()) 58 { 59 InverseMapValue imv = (InverseMapValue)((InverseMapValue)i.next()).clone(); 60 61 imv.setOwner(mft); 62 mv.add(imv); 63 im.put(imv.getKey(), imv); 64 } 65 66 mft.mapValues = mv; 67 mft.inverseMap = im; 68 69 return mft; 70 } 71 72 73 public void fillRandom() 74 { 75 throw new UnsupportedOperationException (); 76 } 77 78 79 public boolean compareTo(Object obj) 80 { 81 throw new UnsupportedOperationException (); 82 } 83 84 85 public void jdoPreStore() {} 86 public void jdoPostLoad() {} 87 public void jdoPreClear() {} 88 89 public void jdoPreDelete() 90 { 91 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 92 93 94 pm.deletePersistentAll(mapValues); 95 } 96 97 98 public void addMapValue(InverseMapValue imv) 99 { 100 String key = imv.getKey(); 101 102 Assert.assertTrue("inverseMap already contains key: " + key, !inverseMap.containsKey(key)); 103 Assert.assertTrue("inverseMap already contains value: " + imv, !inverseMap.containsValue(imv)); 104 105 Assert.assertTrue("add() returned false adding value: " + imv, mapValues.add(imv)); 106 Object oldimv = inverseMap.put(key, imv); 107 108 if (JDOHelper.isPersistent(this)) 109 Assert.assertSame("put() should have returned object just persisted with key: " + key, imv, oldimv); 110 else 111 Assert.assertNull("put() returned non-null adding key: " + key, oldimv); 112 113 Assert.assertTrue("inverseMap does not contain key: " + key, inverseMap.containsKey(key)); 114 Assert.assertTrue("inverseMap does not contain value: " + imv, inverseMap.containsValue(imv)); 115 } 116 117 118 public void removeMapValue(InverseMapValue imv) 119 { 120 Assert.assertSame(this, imv.getOwner()); 121 122 String key = imv.getKey(); 123 124 if (JDOHelper.isPersistent(imv)) 125 JDOHelper.getPersistenceManager(imv).deletePersistent(imv); 126 else 127 { 128 inverseMap.remove(key); 129 mapValues.remove(imv); 130 } 131 132 Assert.assertTrue("inverseMap still contains key: " + key, !inverseMap.containsKey(key)); 133 Assert.assertTrue("inverseMap still contains value", !inverseMap.containsValue(imv)); 134 } 135 136 137 public void assertMapsEqual(List values) 138 { 139 Assert.assertEquals(values.size(), mapValues.size()); 140 Assert.assertEquals(mapValues.size(), inverseMap.size()); 141 142 InverseMapValue[] pValues = new InverseMapValue[values.size()]; 143 HashMap pValueMap = new HashMap (); 144 145 for (int i = 0; i < values.size(); ++i) 146 { 147 InverseMapValue expected = (InverseMapValue)values.get(i); 148 String key = expected.getKey(); 149 Assert.assertTrue("inverseMap does not contain key: " + key, inverseMap.containsKey(key)); 150 151 pValues[i] = (InverseMapValue)inverseMap.get(expected.getKey()); 152 expected.assertEquals(pValues[i]); 153 154 Assert.assertTrue("inverseMap does not contain value: " + pValues[i], inverseMap.containsValue(pValues[i])); 155 156 pValueMap.put(pValues[i].getKey(), pValues[i]); 157 } 158 159 163 Assert.assertTrue(inverseMap.keySet().containsAll(pValueMap.keySet())); 164 Assert.assertTrue(inverseMap.values().containsAll(Arrays.asList(pValues))); 165 166 170 Iterator i = inverseMap.entrySet().iterator(); 171 172 while (i.hasNext()) 173 { 174 Map.Entry entry = (Map.Entry )i.next(); 175 176 InverseMapValue imv = (InverseMapValue)pValueMap.get(entry.getKey()); 177 178 Assert.assertEquals(entry.getKey(), imv.getKey()); 179 Assert.assertEquals(entry.getValue(), imv); 180 181 pValueMap.remove(entry.getKey()); 182 } 183 184 Assert.assertEquals(0, pValueMap.size()); 185 } 186 } 187 | Popular Tags |