KickJava   Java API By Example, From Geeks To Geeks.

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


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: FullTreeMap.java,v 1.3 2003/02/02 17:21:41 leomekenkamp Exp $
7

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

32 public abstract class OzoneMapTest extends TestCase {
33     
34     protected static Class JavaDoc[] PARAM_OBJECT = new Class JavaDoc[] {Object JavaDoc.class};
35     protected static Class JavaDoc[] PARAM_COLLECTION = new Class JavaDoc[] {Collection JavaDoc.class};
36     
37     public static final String JavaDoc[] VALUES = new String JavaDoc[] {
38         "0", "4", "1", "3", "2",
39     };
40
41     public static final String JavaDoc[] COLLECTIONVALUES = new String JavaDoc[] {
42         "6", "8", "9", "0", "7", "5",
43     };
44     
45     public static final Collection JavaDoc COLLECTION = new LinkedList JavaDoc();
46     
47     static {
48         for (int i = 0; i < COLLECTIONVALUES.length; i++) {
49             COLLECTION.add(COLLECTIONVALUES[i]);
50         }
51     }
52
53     protected Collection JavaDoc ref;
54     protected Collection JavaDoc cmp;
55     
56     public OzoneMapTest(String JavaDoc name) {
57         super(name);
58     }
59     
60     public void compare(String JavaDoc methodName) {
61         compare(methodName, null, null);
62     }
63         
64     public void compare(String JavaDoc methodName, Object JavaDoc[] params, Class JavaDoc[] paramTypes) {
65         Method JavaDoc refMethod;
66         Method JavaDoc cmpMethod;
67         try {
68             refMethod = ref.getClass().getMethod(methodName, paramTypes);
69             cmpMethod = cmp.getClass().getMethod(methodName, paramTypes);
70         } catch (NoSuchMethodException JavaDoc e) {
71             throw new RuntimeException JavaDoc(e);
72         }
73         Object JavaDoc refResult = null;
74         Object JavaDoc cmpResult = null;
75         Exception JavaDoc refException = null;
76         Exception JavaDoc cmpException = null;
77         try {
78             refResult = refMethod.invoke(ref, params);
79         } catch (Exception JavaDoc e) {
80             refException = e;
81 // e.printStackTrace(System.out);
82
}
83         try {
84             cmpResult = cmpMethod.invoke(cmp, params);
85         } catch (Exception JavaDoc e) {
86             cmpException = e;
87 // e.printStackTrace(System.out);
88
}
89         compare();
90         if (refResult instanceof Object JavaDoc[]) {
91             assertTrue("methods should return same value; ref: " + refResult + ", cmp: " + cmpResult,
92                     Arrays.equals((Object JavaDoc[]) refResult, (Object JavaDoc[]) 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 JavaDoc refIterator, Iterator JavaDoc cmpIterator) {
108         Object JavaDoc refObject = null;
109         Object JavaDoc cmpObject = null;
110         Exception JavaDoc refException = null;
111         Exception JavaDoc 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 JavaDoc e) {
118                 refException = e;
119 // e.printStackTrace(System.out);
120
}
121             try {
122                 cmpObject = cmpIterator.next();
123             } catch (Exception JavaDoc e) {
124                 cmpException = e;
125 // e.printStackTrace(System.out);
126
}
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 JavaDoc getRef() {
135         return ref;
136     }
137     
138     public void setRef(java.util.Collection JavaDoc ref) {
139         this.ref = ref;
140     }
141     
142     public java.util.Collection JavaDoc getCmp() {
143         return cmp;
144     }
145     
146     public void setCmp(java.util.Collection JavaDoc 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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
154         }
155         for (int i = 0; i < VALUES.length; i++) {
156             compare("add", new Object JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
157         }
158     }
159     
160     public void testAddAll() {
161         compare("clear");
162         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
163         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
164     }
165     
166     public void testClear() {
167         compare("clear");
168         compare("addAll", new Object JavaDoc[] {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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
176         }
177         for (int i = 0; i < VALUES.length; i++) {
178             compare("contains", new Object JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
179         }
180     }
181
182     public void testContainsAll() {
183         compare("clear");
184         compare("containsAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
185         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
186         compare("containsAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
187         compare("remove", new Object JavaDoc[] {COLLECTIONVALUES[0]}, PARAM_OBJECT);
188         compare("containsAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
189     }
190     
191     public void testEquals() {
192         // don't know what to put here...
193
}
194
195 // public void testIterator() {
196
// compare("clear");
197
// compareIterator(ref.iterator(), cmp.iterator());
198
// compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION);
199
// compareIterator(ref.iterator(), cmp.iterator());
200
// }
201

202     public void testIsEmpty() {
203         compare("clear");
204         compare("isEmpty");
205         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
206         compare("isEmpty");
207     }
208     
209     public void testRemove() {
210         compare("clear");
211         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
212         compare("remove", new Object JavaDoc[] {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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
219         }
220         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
221         compare("removeAll", new Object JavaDoc[] {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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
228         }
229         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
230         compare("retainAll", new Object JavaDoc[] {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 JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
238             compare("size");
239         }
240         for (int i = 0; i < VALUES.length; i++ ) {
241             compare("add", new Object JavaDoc[] {VALUES[i]}, PARAM_OBJECT);
242             compare("size");
243         }
244         for (int i = 0; i < VALUES.length; i++ ) {
245             compare("remove", new Object JavaDoc[] {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 JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
254         compare("toArray");
255     }
256     
257     public void testToArray_array() {
258         compare("clear");
259         compare("toArray", new Object JavaDoc[] {new String JavaDoc[] {}}, new Class JavaDoc[] {Object JavaDoc[].class});
260         compare("toArray", new Object JavaDoc[] {new Collection JavaDoc[] {}}, new Class JavaDoc[] {Object JavaDoc[].class});
261         compare("addAll", new Object JavaDoc[] {COLLECTION}, PARAM_COLLECTION);
262         compare("toArray", new Object JavaDoc[] {new String JavaDoc[] {}}, new Class JavaDoc[] {Object JavaDoc[].class});
263 // compare("toArray", new Object[] {new Collection[] {}}, new Class[] {Object[].class});
264
}
265     
266     protected abstract Collection JavaDoc createRef();
267     
268     protected abstract Collection JavaDoc createCmp() throws Exception JavaDoc;
269
270     protected abstract Collection JavaDoc createCmp(String JavaDoc name) throws Exception JavaDoc;
271     
272     public void teztDatabaseRestart() {
273         ArrayList JavaDoc refs = new ArrayList JavaDoc();
274         ArrayList JavaDoc names = new ArrayList JavaDoc();
275
276         Method JavaDoc[] allMethods = getClass().getMethods();
277         for (int i = 0; i < allMethods.length; i++) {
278             Method JavaDoc method = allMethods[i];
279             if (method.getName().startsWith("test") && method.getParameterTypes() == null) {
280                 setRef(createRef());
281                 refs.add(getRef());
282                 String JavaDoc name = getCmp().getClass().getName() + "." + method.getName();
283                 names.add(name);
284                 try {
285                     setCmp(createCmp(name));
286                 } catch (Exception JavaDoc e) {
287                     throw new RuntimeException JavaDoc(e);
288                 }
289                 try {
290                     method.invoke(this, null);
291                 } catch (Exception JavaDoc e) {
292                     throw new RuntimeException JavaDoc(e);
293                 }
294             }
295         }
296         try {
297             OzoneObjectFactory.getDefault().closeDatabase();
298         } catch (Exception JavaDoc e) {
299             throw new RuntimeException JavaDoc(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 JavaDoc e) {
305             throw new RuntimeException JavaDoc(e);
306         }
307         for (int i = 0; i < refs.size(); i++) {
308             setRef((Collection JavaDoc) refs.get(i));
309             try {
310                 setCmp((Collection JavaDoc) OzoneObjectFactory.getDefault().objectForName((String JavaDoc) names.get(i)));
311             } catch (Exception JavaDoc e) {
312                 throw new RuntimeException JavaDoc(e);
313             }
314             compare();
315         }
316     }
317     
318     protected void setUp() throws java.lang.Exception JavaDoc {
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 JavaDoc e) {
327             throw new RuntimeException JavaDoc(e);
328         }
329     }
330     
331     protected void tearDown() throws java.lang.Exception JavaDoc {
332         super.tearDown();
333     }
334
335 }
336
Popular Tags