1 8 package org.ozoneDB.collections; 9 10 import java.lang.reflect.InvocationTargetException ; 11 import java.lang.reflect.Method ; 12 import java.io.IOException ; 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collection ; 16 import java.util.Iterator ; 17 import java.util.LinkedList ; 18 import junit.framework.TestCase; 19 import org.ozoneDB.OzoneObjectFactory; 20 21 30 public abstract class OzoneCollectionTest extends TestCase { 31 32 protected static Class [] PARAM_OBJECT = new Class [] {Object .class}; 33 protected static Class [] PARAM_COLLECTION = new Class [] {Collection .class}; 34 35 public static final String [] VALUES = new String [] { 36 "0", "4", "1", "3", "5", 37 }; 38 39 public static final String [] COLLECTIONVALUES = new String [] { 40 "6", "8", "9", "0", "7", "5", 41 }; 42 43 public static final Collection COLLECTION = new LinkedList (); 44 45 static { 46 for (int i = 0; i < COLLECTIONVALUES.length; i++) { 47 COLLECTION.add(COLLECTIONVALUES[i]); 48 } 49 } 50 51 protected Collection ref; 52 protected Collection cmp; 53 54 public OzoneCollectionTest(String name) { 55 super(name); 56 } 57 58 public void compare(String methodName) { 59 compare(methodName, null, null); 60 } 61 62 public void compare(String methodName, Object [] params, Class [] paramTypes) { 63 Method refMethod; 64 Method cmpMethod; 65 try { 66 refMethod = ref.getClass().getMethod(methodName, paramTypes); 67 cmpMethod = cmp.getClass().getMethod(methodName, paramTypes); 68 } catch (NoSuchMethodException e) { 69 throw new RuntimeException (e); 70 } 71 Object refResult = null; 72 Object cmpResult = null; 73 Exception refException = null; 74 Exception cmpException = null; 75 try { 76 refResult = refMethod.invoke(ref, params); 77 } catch (Exception e) { 78 if (e instanceof InvocationTargetException ) { 79 refException = (Exception ) e.getCause(); 80 } else throw (RuntimeException ) e; 81 } 83 try { 84 cmpResult = cmpMethod.invoke(cmp, params); 85 } catch (Exception e) { 86 if (e instanceof InvocationTargetException ) { 87 cmpException = (Exception ) e.getCause(); 88 } else throw (RuntimeException ) e; 89 } 91 compareResults(refResult, cmpResult); 92 assertTrue("methods should throw same exception; ref: " + refException + ", cmp: " + cmpException, 93 refException == null ? cmpException == null : refException.getClass().equals(cmpException.getClass())); 94 compare(); 95 } 96 97 public void compareResults(Object refResult, Object cmpResult) { 98 if (refResult instanceof Object []) { 99 assertTrue("methods should return same value; ref: " + refResult + ", cmp: " + cmpResult, 100 Arrays.equals((Object []) refResult, (Object []) cmpResult)); 101 } else { 102 assertTrue("methods should return same value; ref: " + refResult + ", cmp: " + cmpResult, 103 refResult == null ? refResult == null : cmpResult.equals(refResult)); 104 } 105 } 106 107 public void compare() { 108 assertTrue("should have same size; ref: " + ref.size() + ", cmp: " + cmp.size(), ref.size() == cmp.size()); 109 assertTrue("all in ref should be in cmp", cmp.containsAll(ref)); 110 assertTrue("all in cmp should be in ref", ref.containsAll(cmp)); 111 } 112 113 public void compareIterator(Iterator refIterator, Iterator cmpIterator) { 114 Object refObject = null; 115 Object cmpObject = null; 116 Exception refException = null; 117 Exception cmpException = null; 118 for(boolean hasNext = true; hasNext; ) { 119 hasNext = refIterator.hasNext(); 120 assertTrue(refIterator.hasNext() == cmpIterator.hasNext()); 121 try { 122 refObject = refIterator.next(); 123 } catch (Exception e) { 124 refException = e; 125 } 127 try { 128 cmpObject = cmpIterator.next(); 129 } catch (Exception e) { 130 cmpException = e; 131 } 133 assertTrue("iterators should throw same exception; ref: " + refException + ", cmp: " + cmpException, 134 refException == null ? cmpException == null: refException.getClass().equals(cmpException.getClass())); 135 assertTrue("iterators should return equal objects; ref: " + refObject + ", cmp: " + cmpObject, 136 refObject == null ? cmpObject == null: refObject.equals(cmpObject)); 137 } 138 } 139 140 public java.util.Collection getRef() { 141 return ref; 142 } 143 144 public void setRef(java.util.Collection ref) { 145 this.ref = ref; 146 } 147 148 public java.util.Collection getCmp() { 149 return cmp; 150 } 151 152 public void setCmp(java.util.Collection cmp) { 153 this.cmp = cmp; 154 } 155 156 public void testAdd() { 157 compare("clear"); 158 for (int i = 0; i < VALUES.length; i++) { 159 compare("add", new Object [] {VALUES[i]}, PARAM_OBJECT); 160 } 161 for (int i = 0; i < VALUES.length; i++) { 162 compare("add", new Object [] {VALUES[i]}, PARAM_OBJECT); 163 } 164 } 165 166 public void testAddAll() { 167 compare("clear"); 168 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 169 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 170 } 171 172 public void testClear() { 173 compare("clear"); 174 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 175 compare("clear"); 176 } 177 178 public void testContains() { 179 compare("clear"); 180 for (int i = 0; i < VALUES.length; i += 2) { 181 compare("add", new Object [] {VALUES[i]}, PARAM_OBJECT); 182 } 183 for (int i = 0; i < VALUES.length; i++) { 184 compare("contains", new Object [] {VALUES[i]}, PARAM_OBJECT); 185 } 186 } 187 188 public void testContainsAll() { 189 compare("clear"); 190 compare("containsAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 191 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 192 compare("containsAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 193 compare("remove", new Object [] {COLLECTIONVALUES[0]}, PARAM_OBJECT); 194 compare("containsAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 195 } 196 197 public void testEquals() { 198 } 200 201 public void testIterator() { 202 compare("clear"); 203 compareIterator(ref.iterator(), cmp.iterator()); 204 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 205 compareIterator(ref.iterator(), cmp.iterator()); 206 } 207 208 public void testIsEmpty() { 209 compare("clear"); 210 compare("isEmpty"); 211 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 212 compare("isEmpty"); 213 } 214 215 public void testRemove() { 216 compare("clear"); 217 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 218 compare("remove", new Object [] {COLLECTIONVALUES[0]}, PARAM_OBJECT); 219 } 220 221 public void testRemoveAll() { 222 compare("clear"); 223 for (int i = 0; i < VALUES.length; i++ ) { 224 compare("add", new Object [] {VALUES[i]}, PARAM_OBJECT); 225 } 226 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 227 compare("removeAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 228 compare("removeAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 229 } 230 231 public void testRetainAll() { 232 compare("clear"); 233 for (int i = 0; i < VALUES.length; i++ ) { 234 compare("add", new Object [] {VALUES[i]}, PARAM_OBJECT); 235 } 236 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 237 compare("retainAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 238 } 239 240 public void testSize() { 241 compare("clear"); 242 compare("size"); 243 for (int i = 0; i < VALUES.length; i++ ) { 244 compare("add", new Object [] {VALUES[i]}, PARAM_OBJECT); 245 compare("size"); 246 } 247 for (int i = 0; i < VALUES.length; i++ ) { 248 compare("add", new Object [] {VALUES[i]}, PARAM_OBJECT); 249 compare("size"); 250 } 251 for (int i = 0; i < VALUES.length; i++ ) { 252 compare("remove", new Object [] {VALUES[i]}, PARAM_OBJECT); 253 compare("size"); 254 } 255 } 256 257 public void testToArray() { 258 compare("clear"); 259 compare("toArray"); 260 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 261 compare("toArray"); 262 } 263 264 public void testToArray_array() { 265 compare("clear"); 266 compare("toArray", new Object [] {new String [] {}}, new Class [] {Object [].class}); 267 compare("toArray", new Object [] {new Collection [] {}}, new Class [] {Object [].class}); 268 compare("addAll", new Object [] {COLLECTION}, PARAM_COLLECTION); 269 compare("toArray", new Object [] {new String [] {}}, new Class [] {Object [].class}); 270 } 272 273 protected abstract Collection createRef(); 274 275 protected abstract Collection createCmp() throws Exception ; 276 278 protected abstract Collection createCmp(String name) throws Exception ; 279 280 public void testDatabaseRestart() { 281 ArrayList refs = new ArrayList (); 282 ArrayList names = new ArrayList (); 283 284 Method [] allMethods = getClass().getMethods(); 285 for (int i = 0; i < allMethods.length; i++) { 286 Method method = allMethods[i]; 287 if (method.getName().startsWith("test") && (method.getParameterTypes() == null || method.getParameterTypes().length == 0) 288 && !method.getName().equals("testDatabaseRestart")) { 289 setRef(createRef()); 290 refs.add(getRef()); 291 String name = getRef().getClass().getName() + "." + method.getName(); 292 names.add(name); 293 try { 294 Collection c = createCmp(name); 295 setCmp(c); 296 } catch (Exception e) { 297 throw new RuntimeException (e); 298 } 299 try { 300 System.out.println("invoking: " + method.getName()); 301 method.invoke(this, null); 302 } catch (Exception e) { 303 throw new RuntimeException (e); 304 } 305 } 306 } 307 try { 308 OzoneObjectFactory.getDefault().closeDatabase(); 309 } catch (Exception e) { 310 throw new RuntimeException (e); 311 } 312 System.out.println("Please shut down the database and start it up again; after that, press <enter>."); 313 try { 314 System.in.read(); 315 } catch (IOException e) { 316 throw new RuntimeException (e); 317 } 318 for (int i = 0; i < refs.size(); i++) { 319 setRef((Collection ) refs.get(i)); 320 try { 321 setCmp((Collection ) OzoneObjectFactory.getDefault().objectForName((String ) names.get(i))); 322 } catch (Exception e) { 323 throw new RuntimeException (e); 324 } 325 compare(); 326 } 327 } 328 329 protected void setUp() throws java.lang.Exception { 330 super.setUp(); 331 if (OzoneObjectFactory.getDefaultDatabaseUrl() == null) { 332 OzoneObjectFactory.setDefaultDatabaseUrl("ozonedb:remote://localhost:3333"); 333 } 334 setRef(createRef()); 335 try { 336 setCmp(createCmp()); 337 } catch (Exception e) { 338 throw new RuntimeException (e); 339 } 340 } 341 342 protected void tearDown() throws java.lang.Exception { 343 super.tearDown(); 344 } 345 346 } 347 | Popular Tags |