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 javax.jdo.InstanceCallbacks; 20 import javax.jdo.JDOHelper; 21 import javax.jdo.PersistenceManager; 22 import junit.framework.Assert; 23 24 25 31 32 public class MapFieldTester implements InstanceCallbacks 33 { 34 private Map personsByNumber = new HashMap (); 35 private Map husbandsByWife = new HashMap (); 36 private Map wivesByHusband = new HashMap (); 37 38 39 42 43 public MapFieldTester() 44 { 45 } 46 47 48 59 60 public void addPair(Person husband, Person wife) 61 { 62 Long hNum = new Long (husband.getPersonNum()); 63 Long wNum = new Long (wife.getPersonNum()); 64 65 Assert.assertEquals(null, personsByNumber.put(hNum, husband)); 66 Assert.assertEquals(null, personsByNumber.put(wNum, wife)); 67 Assert.assertTrue(personsByNumber.containsKey(hNum)); 68 Assert.assertTrue(personsByNumber.containsKey(wNum)); 69 Assert.assertTrue(personsByNumber.containsValue(husband)); 70 Assert.assertTrue(personsByNumber.containsValue(wife)); 71 72 Assert.assertEquals(null, husbandsByWife.put(wife, husband)); 73 Assert.assertEquals(null, wivesByHusband.put(husband, wife)); 74 Assert.assertTrue(husbandsByWife.containsKey(wife)); 75 Assert.assertTrue(husbandsByWife.containsValue(husband)); 76 Assert.assertTrue(wivesByHusband.containsKey(husband)); 77 Assert.assertTrue(wivesByHusband.containsValue(wife)); 78 } 79 80 81 92 93 public void removePair(Person husband, Person wife) 94 { 95 Long hNum = new Long (husband.getPersonNum()); 96 Long wNum = new Long (wife.getPersonNum()); 97 98 Assert.assertEquals(husband, personsByNumber.remove(hNum)); 99 Assert.assertEquals(wife, personsByNumber.remove(wNum)); 100 Assert.assertTrue(!personsByNumber.containsKey(hNum)); 101 Assert.assertTrue(!personsByNumber.containsKey(wNum)); 102 Assert.assertTrue(!personsByNumber.containsValue(husband)); 103 Assert.assertTrue(!personsByNumber.containsValue(wife)); 104 105 Assert.assertEquals(husband, husbandsByWife.remove(wife)); 106 Assert.assertEquals(wife, wivesByHusband.remove(husband)); 107 Assert.assertTrue(!husbandsByWife.containsKey(wife)); 108 Assert.assertTrue(!husbandsByWife.containsValue(husband)); 109 Assert.assertTrue(!wivesByHusband.containsKey(husband)); 110 Assert.assertTrue(!wivesByHusband.containsValue(wife)); 111 112 if (JDOHelper.isPersistent(husband)) 113 JDOHelper.getPersistenceManager(husband).deletePersistent(husband); 114 115 if (JDOHelper.isPersistent(wife)) 116 JDOHelper.getPersistenceManager(wife).deletePersistent(wife); 117 } 118 119 120 133 134 public void assertMapsEqual(List husbands, List wives) 135 { 136 Assert.assertEquals(husbands.size(), wives.size()); 137 Assert.assertEquals(husbands.size() + wives.size(), personsByNumber.size()); 138 Assert.assertEquals(husbands.size(), husbandsByWife.size()); 139 Assert.assertEquals(wives.size(), wivesByHusband.size()); 140 141 Person[] pHusbands = new Person[husbands.size()]; 142 Person[] pWives = new Person[wives.size()]; 143 HashMap personsByNumber1 = new HashMap (); 144 145 for (int i = 0; i < husbands.size(); ++i) 146 { 147 Person expected; 148 149 expected = (Person)husbands.get(i); 150 pHusbands[i] = (Person)personsByNumber.get(new Long (expected.getPersonNum())); 151 expected.assertEquals(pHusbands[i]); 152 Assert.assertTrue(husbandsByWife.containsValue(pHusbands[i])); 153 Assert.assertTrue(wivesByHusband.containsKey(pHusbands[i])); 154 155 expected = (Person)wives.get(i); 156 pWives[i] = (Person)personsByNumber.get(new Long (expected.getPersonNum())); 157 expected.assertEquals(pWives[i]); 158 Assert.assertTrue(husbandsByWife.containsKey(pWives[i])); 159 Assert.assertTrue(wivesByHusband.containsValue(pWives[i])); 160 161 Assert.assertEquals(JDOHelper.getObjectId(pHusbands[i]), 162 JDOHelper.getObjectId(husbandsByWife.get(pWives[i]))); 163 164 Assert.assertEquals(JDOHelper.getObjectId(pWives[i]), 165 JDOHelper.getObjectId(wivesByHusband.get(pHusbands[i]))); 166 167 personsByNumber1.put(new Long (pHusbands[i].getPersonNum()), pHusbands[i]); 168 personsByNumber1.put(new Long (pWives[i].getPersonNum()), pWives[i]); 169 } 170 171 175 Assert.assertTrue(husbandsByWife.keySet().containsAll(Arrays.asList(pWives))); 176 Assert.assertTrue(husbandsByWife.values().containsAll(Arrays.asList(pHusbands))); 177 Assert.assertTrue(wivesByHusband.keySet().containsAll(Arrays.asList(pHusbands))); 178 Assert.assertTrue(wivesByHusband.values().containsAll(Arrays.asList(pWives))); 179 180 Assert.assertEquals(personsByNumber, personsByNumber1); 181 182 186 Iterator i = personsByNumber.entrySet().iterator(); 187 188 while (i.hasNext()) 189 { 190 Map.Entry entry = (Map.Entry )i.next(); 191 192 Person p = (Person)personsByNumber1.get(entry.getKey()); 193 194 Assert.assertEquals(entry.getKey(), new Long (p.getPersonNum())); 195 Assert.assertEquals(entry.getValue(), p); 196 197 personsByNumber1.remove(entry.getKey()); 198 } 199 200 Assert.assertEquals(0, personsByNumber1.size()); 201 } 202 203 public int hashCode() 204 { 205 Object id = JDOHelper.getObjectId(this); 206 207 return id == null ? super.hashCode() : id.hashCode(); 208 } 209 210 public boolean equals(Object o) 211 { 212 if (o == this) 213 return true; 214 215 Object id = JDOHelper.getObjectId(this); 216 217 return id == null ? super.equals(o) : id.equals(JDOHelper.getObjectId(o)); 218 } 219 220 public void jdoPreStore() {} 221 public void jdoPostLoad() {} 222 public void jdoPreClear() {} 223 224 public void jdoPreDelete() 225 { 226 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 227 228 229 HashSet persons = new HashSet (personsByNumber.values()); 230 231 personsByNumber.clear(); 232 husbandsByWife.clear(); 233 wivesByHusband.clear(); 234 235 pm.deletePersistentAll(persons); 236 } 237 } 238 | Popular Tags |