1 21 package com.db4o.test.legacy; 22 23 import java.util.*; 24 25 import com.db4o.*; 26 import com.db4o.ext.*; 27 import com.db4o.query.*; 28 import com.db4o.test.*; 29 import com.db4o.tools.*; 30 import com.db4o.types.*; 31 32 33 36 public class Db4oHashMap { 37 38 public static class Db4oHashMapHelper{ 39 public Db4oHashMapHelper i_child; 40 public List i_childList; 41 42 } 43 44 static final int COUNT = 10; 45 46 static final String [] DEFAULT = {"wow", "cool", "great"}; 47 static final String MORE = "more and more "; 48 49 Map i_map; 50 Db4oHashMapHelper i_helper; 51 52 public void storeOne(){ 53 i_map = Test.objectContainer().collections().newHashMap(10); 54 setDefaultValues(i_map); 55 i_helper = helper(10); 56 } 57 58 private static Db4oHashMapHelper helper(int a_depth){ 59 if(a_depth > 0){ 60 Db4oHashMapHelper helper = new Db4oHashMapHelper(); 61 helper.i_childList = Test.objectContainer().collections().newLinkedList(); 62 helper.i_childList.add("hi"); 63 helper.i_child = helper(a_depth - 1); 64 return helper; 65 } 66 return null; 67 } 68 69 private void setDefaultValues(Map a_map){ 70 for (int i = 0; i < DEFAULT.length; i++) { 71 a_map.put(DEFAULT[i], new Atom(DEFAULT[i])); 72 } 73 } 74 75 public void testOne(){ 76 77 ObjectContainer oc = Test.objectContainer(); 78 79 checkHelper(i_helper); 80 runElementTest(true); 81 82 oc = Test.objectContainer(); 83 oc.set(this); 84 oc.set(i_helper); 85 oc.commit(); 86 87 checkHelper(i_helper); 88 runElementTest(false); 89 90 91 boolean defrag = true; 92 93 if(defrag){ 94 95 Test.commit(); 96 97 close(); 98 99 try { 100 new Defragment().run(currentFileName(), true); 101 } finally { 102 103 reOpen(); 104 } 105 106 restoreMembers(); 107 checkHelper(i_helper); 108 runElementTest(false); 109 } 110 } 111 112 113 private void runElementTest(boolean onOriginal){ 114 115 Map otherMap = new HashMap(); 116 117 Atom atom = null; 118 119 tDefaultValues(); 120 121 int itCount = 0; 122 Iterator i = i_map.keySet().iterator(); 123 while(i.hasNext()){ 124 String str = (String )i.next(); 125 itCount ++; 126 atom = (Atom)i_map.get(str); 127 Test.ensure(atom.name.equals(str)); 128 otherMap.put(str, atom); 129 } 130 Test.ensure(itCount == DEFAULT.length); 131 132 133 Test.ensure(i_map.size() == DEFAULT.length); 134 Test.ensure(i_map.isEmpty() == false); 135 Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE); 136 i_map.get("great"); 137 Test.ensure(((Atom)i_map.get("great")).name.equals("great")); 138 Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE); 139 140 if(onOriginal){ 141 Query q = Test.query(); 142 Db4oHashMap template = new Db4oHashMap(); 143 template.i_map = Test.objectContainer().collections().newHashMap(1); 144 template.i_map.put("cool", new Atom("cool")); 145 q.constrain(template); 146 ObjectSet qResult = q.execute(); 147 Test.ensure(qResult.size() == 1); 148 Test.ensure(qResult.next() == this); 149 } 150 151 Test.ensure(i_map.keySet().containsAll(otherMap.keySet())); 152 153 154 Object [] arr = i_map.keySet().toArray(); 155 tDefaultArray(arr); 156 157 158 String [] cmp = new String [DEFAULT.length]; 159 System.arraycopy(DEFAULT,0, cmp, 0, DEFAULT.length); 160 161 i = i_map.keySet().iterator(); 162 while(i.hasNext()){ 163 String str = (String )i.next(); 164 boolean found = false; 165 for (int j = 0; j < cmp.length; j++) { 166 if(str.equals(cmp[j])){ 167 cmp[j] = null; 168 found = true; 169 } 170 } 171 Test.ensure(found); 172 } 173 174 for (int j = 0; j < cmp.length; j++) { 175 Test.ensure(cmp[j] == null); 176 } 177 178 Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE); 179 Test.ensure(i_map.isEmpty() == false); 180 Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE); 181 i_map.put("yup", new Atom("yup")); 182 183 Test.objectContainer().set(this); 184 Test.objectContainer().set(this); 185 Test.objectContainer().set(this); 186 Test.objectContainer().set(i_map); 187 Test.objectContainer().set(i_map); 188 Test.objectContainer().set(i_map); 189 Test.objectContainer().set(i_helper); 190 Test.objectContainer().set(i_helper); 191 Test.objectContainer().set(i_helper); 192 Test.objectContainer().commit(); 193 194 Test.ensure(i_map.size() == 4); 195 196 atom = (Atom)i_map.get("yup"); 197 Test.ensure(atom.name.equals("yup")); 198 199 Atom removed = (Atom)i_map.remove("great"); 200 201 Test.ensure(removed.name.equals("great")); 202 Test.ensure(i_map.remove("great") == null); 203 Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE); 204 Test.ensure(i_map.size() == 3); 205 206 Test.ensure(i_map.keySet().removeAll(otherMap.keySet())); 207 Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE); 208 Test.ensure(! i_map.keySet().removeAll(otherMap.keySet())); 209 Test.ensure(i_map.size() == 1); 210 i = i_map.keySet().iterator(); 211 String str = (String )i.next(); 212 Test.ensure(str.equals("yup")); 213 Test.ensure(! i.hasNext()); 214 215 i_map.clear(); 216 Test.ensure(i_map.isEmpty()); 217 Test.ensure(i_map.size() == 0); 218 219 setDefaultValues(i_map); 220 221 String [] strArr = new String [1]; 222 strArr = (String [])i_map.keySet().toArray(strArr); 223 tDefaultArray(strArr); 224 225 i_map.clear(); 226 i_map.put("zero", "zero"); 227 228 long start = System.currentTimeMillis(); 229 230 for (int j = 0; j < COUNT; j++) { 231 i_map.put(MORE + j, new Atom(MORE + j)); 232 } 233 long stop = System.currentTimeMillis(); 234 Test.ensure(i_map.size() == COUNT + 1); 236 lookupLast(); 237 238 Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE); 239 lookupLast(); 241 lookupLast(); 243 244 Test.reOpen(); 245 restoreMembers(); 246 lookupLast(); 248 249 atom = new Atom("double"); 250 251 i_map.put("double", atom); 252 253 int previousSize = i_map.size(); 254 255 Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE); 256 257 Atom doubleAtom = (Atom)i_map.put("double", new Atom("double")); 258 Test.ensure(atom == doubleAtom); 259 260 Test.ensure(i_map.size() == previousSize); 261 i_map.put("double", doubleAtom); 262 263 Test.commit(); 264 265 i_map.put("rollBack", "rollBack"); 266 i_map.put("double", new Atom("nono")); 267 268 Test.rollBack(); 269 Test.ensure(i_map.get("rollBack") == null); 270 Test.ensure(i_map.size() == previousSize); 271 atom = (Atom)i_map.get("double"); 272 Test.ensure(atom == doubleAtom); 273 Test.ensure(i_map.containsKey("double")); 274 Test.ensure(! i_map.containsKey("rollBack")); 275 276 otherMap.clear(); 277 otherMap.put("other1", doubleAtom); 278 otherMap.put("other2", doubleAtom); 279 280 i_map.putAll(otherMap); 281 Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE); 282 283 Test.ensure(i_map.get("other1") == doubleAtom); 284 Test.ensure(i_map.get("other2") == doubleAtom); 285 286 287 i_map.clear(); 288 Test.ensure(i_map.size() == 0); 289 setDefaultValues(i_map); 290 291 int j = 0; 292 i = i_map.keySet().iterator(); 293 while(i.hasNext()){ 294 String key = (String )i.next(); 295 if(key.equals("cool")){ 296 i.remove(); 297 } 298 j++; 299 } 300 Test.ensure(i_map.size() == 2); 301 Test.ensure(! i_map.containsKey("cool")); 302 Test.ensure(j == 3); 303 304 305 i_map.put("double", doubleAtom); 306 ((Db4oMap)i_map).deleteRemoved(true); 307 i_map.keySet().remove("double"); 308 Test.ensure(! Test.objectContainer().isStored(doubleAtom)); 309 ((Db4oMap)i_map).deleteRemoved(false); 310 311 i_map.clear(); 312 Test.ensure(i_map.size() == 0); 313 setDefaultValues(i_map); 314 } 315 316 private void tDefaultValues(){ 317 for (int i = 0; i < DEFAULT.length; i++) { 318 Atom atom = (Atom)i_map.get(DEFAULT[i]); 319 Test.ensure(atom.name.equals(DEFAULT[i])); 320 } 321 } 322 323 private void tDefaultArray(Object [] arr){ 324 Test.ensure(arr.length == DEFAULT.length); 325 String str[] = new String [DEFAULT.length]; 326 System.arraycopy(DEFAULT,0, str, 0, DEFAULT.length); 327 for (int i = 0; i < arr.length; i++) { 328 boolean found = false; 329 for (int j = 0; j < str.length; j++) { 330 if(arr[i].equals(str[j])){ 331 str[j] = null; 332 found = true; 333 } 334 } 335 Test.ensure(found); 336 } 337 for (int j = 0; j < str.length; j++) { 338 Test.ensure(str[j] == null); 339 } 340 } 341 342 343 private void restoreMembers(){ 344 Query q = Test.query(); 345 q.constrain(this.getClass()); 346 ObjectSet objectSet = q.execute(); 347 Db4oHashMap dll = (Db4oHashMap)objectSet.next(); 348 i_map = dll.i_map; 349 i_helper = dll.i_helper; 350 } 351 352 private void lookupLast(){ 353 long start = System.currentTimeMillis(); 354 Atom atom = (Atom)i_map.get(MORE + (COUNT - 1)); 355 long stop = System.currentTimeMillis(); 356 Test.ensure(atom.name. equals(MORE + (COUNT - 1))); 358 } 359 360 void checkHelper(Db4oHashMapHelper helper){ 361 ExtObjectContainer con = Test.objectContainer(); 362 if(con.isActive(helper)){ 363 Test.ensure(helper.i_childList.get(0).equals("hi")); 364 checkHelper(helper.i_child); 365 } 366 } 367 368 private String currentFileName() { 369 return Test.isClientServer() 370 ? Test.FILE_SERVER 371 : Test.FILE_SOLO; 372 } 373 374 private void close() { 375 Test.close(); 376 if (Test.isClientServer()) { 377 Test.server().close(); 378 } 379 } 380 381 private void reOpen() { 382 Test.reOpenServer(); 383 } 384 385 386 } 387 | Popular Tags |