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