KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > collections > OzoneCollectionTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// This file is
5
// Copyright (C) 2003-@year@ Leo Mekenkamp. All rights reserved.
6
// $Id: OzoneCollectionTest.java,v 1.2 2003/02/18 21:03:10 leomekenkamp Exp $
7

8 package org.ozoneDB.collections;
9
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.LinkedList JavaDoc;
18 import junit.framework.TestCase;
19 import org.ozoneDB.OzoneObjectFactory;
20
21 /**
22  * Provides for basic testing of the <code>OzoneCollection</code>
23  * classes, or any non-java.util collection. What is tested is only a
24  * comparison against <code>java.util.Collection</code> classes; we consider
25  * ozone collections to be behaving properly if they mimic their java.util
26  * counterparts.
27  *
28  * @author <a HREF="mailto:ozoneATmekenkampD0Tcom">Leo Mekenkamp (mind the anti-sp@m)</a>
29  */

30 public abstract class OzoneCollectionTest extends TestCase {
31     
32     protected static Class JavaDoc[] PARAM_OBJECT = new Class JavaDoc[] {Object JavaDoc.class};
33     protected static Class JavaDoc[] PARAM_COLLECTION = new Class JavaDoc[] {Collection JavaDoc.class};
34     
35     public static final String JavaDoc[] VALUES = new String JavaDoc[] {
36         "0", "4", "1", "3", "5",
37     };
38
39     public static final String JavaDoc[] COLLECTIONVALUES = new String JavaDoc[] {
40         "6", "8", "9", "0", "7", "5",
41     };
42     
43     public static final Collection JavaDoc COLLECTION = new LinkedList JavaDoc();
44     
45     static {
46         for (int i = 0; i < COLLECTIONVALUES.length; i++) {
47             COLLECTION.add(COLLECTIONVALUES[i]);
48         }
49     }
50
51     protected Collection JavaDoc ref;
52     protected Collection JavaDoc cmp;
53     
54     public OzoneCollectionTest(String JavaDoc name) {
55         super(name);
56     }
57     
58     public void compare(String JavaDoc methodName) {
59         compare(methodName, null, null);
60     }
61         
62     public void compare(String JavaDoc methodName, Object JavaDoc[] params, Class JavaDoc[] paramTypes) {
63         Method JavaDoc refMethod;
64         Method JavaDoc cmpMethod;
65         try {
66             refMethod = ref.getClass().getMethod(methodName, paramTypes);
67             cmpMethod = cmp.getClass().getMethod(methodName, paramTypes);
68         } catch (NoSuchMethodException JavaDoc e) {
69             throw new RuntimeException JavaDoc(e);
70         }
71         Object JavaDoc refResult = null;
72         Object JavaDoc cmpResult = null;
73         Exception JavaDoc refException = null;
74         Exception JavaDoc cmpException = null;
75         try {
76             refResult = refMethod.invoke(ref, params);
77         } catch (Exception JavaDoc e) {
78             if (e instanceof InvocationTargetException JavaDoc) {
79                 refException = (Exception JavaDoc) e.getCause();
80             } else throw (RuntimeException JavaDoc) e;
81 // e.printStackTrace(System.out);
82
}
83         try {
84             cmpResult = cmpMethod.invoke(cmp, params);
85         } catch (Exception JavaDoc e) {
86             if (e instanceof InvocationTargetException JavaDoc) {
87                 cmpException = (Exception JavaDoc) e.getCause();
88             } else throw (RuntimeException JavaDoc) e;
89 // e.printStackTrace(System.out);
90
}
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 JavaDoc refResult, Object JavaDoc cmpResult) {
98         if (refResult instanceof Object JavaDoc[]) {
99             assertTrue("methods should return same value; ref: " + refResult + ", cmp: " + cmpResult,
100                     Arrays.equals((Object JavaDoc[]) refResult, (Object JavaDoc[]) 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 JavaDoc refIterator, Iterator JavaDoc cmpIterator) {
114         Object JavaDoc refObject = null;
115         Object JavaDoc cmpObject = null;
116         Exception JavaDoc refException = null;
117         Exception JavaDoc 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 JavaDoc e) {
124                 refException = e;
125 // e.printStackTrace(System.out);
126
}
127             try {
128                 cmpObject = cmpIterator.next();
129             } catch (Exception JavaDoc e) {
130                 cmpException = e;
131 // e.printStackTrace(System.out);
132
}
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 JavaDoc getRef() {
141         return ref;
142     }
143     
144     public void setRef(java.util.Collection JavaDoc ref) {
145         this.ref = ref;
146     }
147     
148     public java.util.Collection JavaDoc getCmp() {
149         return cmp;
150     }
151     
152     public void setCmp(java.util.Collection JavaDoc 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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
160         }
161         for (int i = 0; i < VALUES.length; i++) {
162             compare("add", new Object JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
163         }
164     }
165     
166     public void testAddAll() {
167         compare("clear");
168         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
169         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
170     }
171     
172     public void testClear() {
173         compare("clear");
174         compare("addAll", new Object JavaDoc[] {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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
182         }
183         for (int i = 0; i < VALUES.length; i++) {
184             compare("contains", new Object JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
185         }
186     }
187
188     public void testContainsAll() {
189         compare("clear");
190         compare("containsAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
191         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
192         compare("containsAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
193         compare("remove", new Object JavaDoc[] {COLLECTIONVALUES[0]}, PARAM_OBJECT);
194         compare("containsAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
195     }
196     
197     public void testEquals() {
198         // don't know what to put here...
199
}
200
201     public void testIterator() {
202         compare("clear");
203         compareIterator(ref.iterator(), cmp.iterator());
204         compare("addAll", new Object JavaDoc[] {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 JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
212         compare("isEmpty");
213     }
214     
215     public void testRemove() {
216         compare("clear");
217         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
218         compare("remove", new Object JavaDoc[] {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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
225         }
226         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
227         compare("removeAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
228         compare("removeAll", new Object JavaDoc[] {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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
235         }
236         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
237         compare("retainAll", new Object JavaDoc[] {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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
245             compare("size");
246         }
247         for (int i = 0; i < VALUES.length; i++ ) {
248             compare("add", new Object JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
249             compare("size");
250         }
251         for (int i = 0; i < VALUES.length; i++ ) {
252             compare("remove", new Object JavaDoc[] {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 JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
261         compare("toArray");
262     }
263     
264     public void testToArray_array() {
265         compare("clear");
266         compare("toArray", new Object JavaDoc[] {new String JavaDoc[] {}}, new Class JavaDoc[] {Object JavaDoc[].class});
267         compare("toArray", new Object JavaDoc[] {new Collection JavaDoc[] {}}, new Class JavaDoc[] {Object JavaDoc[].class});
268         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
269         compare("toArray", new Object JavaDoc[] {new String JavaDoc[] {}}, new Class JavaDoc[] {Object JavaDoc[].class});
270 // compare("toArray", new Object[] {new Collection[] {}}, new Class[] {Object[].class});
271
}
272     
273     protected abstract Collection JavaDoc createRef();
274     
275     protected abstract Collection JavaDoc createCmp() throws Exception JavaDoc;
276 // protected abstract Collection createCmp1();
277

278     protected abstract Collection JavaDoc createCmp(String JavaDoc name) throws Exception JavaDoc;
279     
280     public void testDatabaseRestart() {
281         ArrayList JavaDoc refs = new ArrayList JavaDoc();
282         ArrayList JavaDoc names = new ArrayList JavaDoc();
283
284         Method JavaDoc[] allMethods = getClass().getMethods();
285         for (int i = 0; i < allMethods.length; i++) {
286             Method JavaDoc 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 JavaDoc name = getRef().getClass().getName() + "." + method.getName();
292                 names.add(name);
293                 try {
294                     Collection JavaDoc c = createCmp(name);
295                     setCmp(c);
296                 } catch (Exception JavaDoc e) {
297                     throw new RuntimeException JavaDoc(e);
298                 }
299                 try {
300                     System.out.println("invoking: " + method.getName());
301                     method.invoke(this, null);
302                 } catch (Exception JavaDoc e) {
303                     throw new RuntimeException JavaDoc(e);
304                 }
305             }
306         }
307         try {
308             OzoneObjectFactory.getDefault().closeDatabase();
309         } catch (Exception JavaDoc e) {
310             throw new RuntimeException JavaDoc(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 JavaDoc e) {
316             throw new RuntimeException JavaDoc(e);
317         }
318         for (int i = 0; i < refs.size(); i++) {
319             setRef((Collection JavaDoc) refs.get(i));
320             try {
321                 setCmp((Collection JavaDoc) OzoneObjectFactory.getDefault().objectForName((String JavaDoc) names.get(i)));
322             } catch (Exception JavaDoc e) {
323                 throw new RuntimeException JavaDoc(e);
324             }
325             compare();
326         }
327     }
328     
329     protected void setUp() throws java.lang.Exception JavaDoc {
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 JavaDoc e) {
338             throw new RuntimeException JavaDoc(e);
339         }
340     }
341     
342     protected void tearDown() throws java.lang.Exception JavaDoc {
343         super.tearDown();
344     }
345
346 }
347
Popular Tags