📄 ozonecollectiontest.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// This file is// Copyright (C) 2003-@year@ Leo Mekenkamp. All rights reserved.// $Id: OzoneCollectionTest.java,v 1.2 2003/02/18 21:03:10 leomekenkamp Exp $package org.ozoneDB.collections;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Iterator;import java.util.LinkedList;import junit.framework.TestCase;import org.ozoneDB.OzoneObjectFactory;/** * Provides for basic testing of the <code>OzoneCollection</code> * classes, or any non-java.util collection. What is tested is only a * comparison against <code>java.util.Collection</code> classes; we consider * ozone collections to be behaving properly if they mimic their java.util * counterparts. * * @author <a href="mailto:ozoneATmekenkampD0Tcom">Leo Mekenkamp (mind the anti-sp@m)</a> */public abstract class OzoneCollectionTest extends TestCase { protected static Class[] PARAM_OBJECT = new Class[] {Object.class}; protected static Class[] PARAM_COLLECTION = new Class[] {Collection.class}; public static final String[] VALUES = new String[] { "0", "4", "1", "3", "5", }; public static final String[] COLLECTIONVALUES = new String[] { "6", "8", "9", "0", "7", "5", }; public static final Collection COLLECTION = new LinkedList(); static { for (int i = 0; i < COLLECTIONVALUES.length; i++) { COLLECTION.add(COLLECTIONVALUES[i]); } } protected Collection ref; protected Collection cmp; public OzoneCollectionTest(String name) { super(name); } public void compare(String methodName) { compare(methodName, null, null); } public void compare(String methodName, Object[] params, Class[] paramTypes) { Method refMethod; Method cmpMethod; try { refMethod = ref.getClass().getMethod(methodName, paramTypes); cmpMethod = cmp.getClass().getMethod(methodName, paramTypes); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Object refResult = null; Object cmpResult = null; Exception refException = null; Exception cmpException = null; try { refResult = refMethod.invoke(ref, params); } catch (Exception e) { if (e instanceof InvocationTargetException) { refException = (Exception) e.getCause(); } else throw (RuntimeException) e;// e.printStackTrace(System.out); } try { cmpResult = cmpMethod.invoke(cmp, params); } catch (Exception e) { if (e instanceof InvocationTargetException) { cmpException = (Exception) e.getCause(); } else throw (RuntimeException) e;// e.printStackTrace(System.out); } compareResults(refResult, cmpResult); assertTrue("methods should throw same exception; ref: " + refException + ", cmp: " + cmpException, refException == null ? cmpException == null : refException.getClass().equals(cmpException.getClass())); compare(); } public void compareResults(Object refResult, Object cmpResult) { if (refResult instanceof Object[]) { assertTrue("methods should return same value; ref: " + refResult + ", cmp: " + cmpResult, Arrays.equals((Object[]) refResult, (Object[]) cmpResult)); } else { assertTrue("methods should return same value; ref: " + refResult + ", cmp: " + cmpResult, refResult == null ? refResult == null : cmpResult.equals(refResult)); } } public void compare() { assertTrue("should have same size; ref: " + ref.size() + ", cmp: " + cmp.size(), ref.size() == cmp.size()); assertTrue("all in ref should be in cmp", cmp.containsAll(ref)); assertTrue("all in cmp should be in ref", ref.containsAll(cmp)); } public void compareIterator(Iterator refIterator, Iterator cmpIterator) { Object refObject = null; Object cmpObject = null; Exception refException = null; Exception cmpException = null; for(boolean hasNext = true; hasNext; ) { hasNext = refIterator.hasNext(); assertTrue(refIterator.hasNext() == cmpIterator.hasNext()); try { refObject = refIterator.next(); } catch (Exception e) { refException = e;// e.printStackTrace(System.out); } try { cmpObject = cmpIterator.next(); } catch (Exception e) { cmpException = e;// e.printStackTrace(System.out); } assertTrue("iterators should throw same exception; ref: " + refException + ", cmp: " + cmpException, refException == null ? cmpException == null: refException.getClass().equals(cmpException.getClass())); assertTrue("iterators should return equal objects; ref: " + refObject + ", cmp: " + cmpObject, refObject == null ? cmpObject == null: refObject.equals(cmpObject)); } } public java.util.Collection getRef() { return ref; } public void setRef(java.util.Collection ref) { this.ref = ref; } public java.util.Collection getCmp() { return cmp; } public void setCmp(java.util.Collection cmp) { this.cmp = cmp; } public void testAdd() { compare("clear"); for (int i = 0; i < VALUES.length; i++) { compare("add", new Object[] {VALUES[i]}, PARAM_OBJECT); } for (int i = 0; i < VALUES.length; i++) { compare("add", new Object[] {VALUES[i]}, PARAM_OBJECT); } } public void testAddAll() { compare("clear"); compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); } public void testClear() { compare("clear"); compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("clear"); } public void testContains() { compare("clear"); for (int i = 0; i < VALUES.length; i += 2) { compare("add", new Object[] {VALUES[i]}, PARAM_OBJECT); } for (int i = 0; i < VALUES.length; i++) { compare("contains", new Object[] {VALUES[i]}, PARAM_OBJECT); } } public void testContainsAll() { compare("clear"); compare("containsAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("containsAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("remove", new Object[] {COLLECTIONVALUES[0]}, PARAM_OBJECT); compare("containsAll", new Object[] {COLLECTION}, PARAM_COLLECTION); } public void testEquals() { // don't know what to put here... } public void testIterator() { compare("clear"); compareIterator(ref.iterator(), cmp.iterator()); compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compareIterator(ref.iterator(), cmp.iterator()); } public void testIsEmpty() { compare("clear"); compare("isEmpty"); compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("isEmpty"); } public void testRemove() { compare("clear"); compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("remove", new Object[] {COLLECTIONVALUES[0]}, PARAM_OBJECT); } public void testRemoveAll() { compare("clear"); for (int i = 0; i < VALUES.length; i++ ) { compare("add", new Object[] {VALUES[i]}, PARAM_OBJECT); } compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("removeAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("removeAll", new Object[] {COLLECTION}, PARAM_COLLECTION); } public void testRetainAll() { compare("clear"); for (int i = 0; i < VALUES.length; i++ ) { compare("add", new Object[] {VALUES[i]}, PARAM_OBJECT); } compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("retainAll", new Object[] {COLLECTION}, PARAM_COLLECTION); } public void testSize() { compare("clear"); compare("size"); for (int i = 0; i < VALUES.length; i++ ) { compare("add", new Object[] {VALUES[i]}, PARAM_OBJECT); compare("size"); } for (int i = 0; i < VALUES.length; i++ ) { compare("add", new Object[] {VALUES[i]}, PARAM_OBJECT); compare("size"); } for (int i = 0; i < VALUES.length; i++ ) { compare("remove", new Object[] {VALUES[i]}, PARAM_OBJECT); compare("size"); } } public void testToArray() { compare("clear"); compare("toArray"); compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("toArray"); } public void testToArray_array() { compare("clear"); compare("toArray", new Object[] {new String[] {}}, new Class[] {Object[].class}); compare("toArray", new Object[] {new Collection[] {}}, new Class[] {Object[].class}); compare("addAll", new Object[] {COLLECTION}, PARAM_COLLECTION); compare("toArray", new Object[] {new String[] {}}, new Class[] {Object[].class});// compare("toArray", new Object[] {new Collection[] {}}, new Class[] {Object[].class}); } protected abstract Collection createRef(); protected abstract Collection createCmp() throws Exception;// protected abstract Collection createCmp1(); protected abstract Collection createCmp(String name) throws Exception; public void testDatabaseRestart() { ArrayList refs = new ArrayList(); ArrayList names = new ArrayList(); Method[] allMethods = getClass().getMethods(); for (int i = 0; i < allMethods.length; i++) { Method method = allMethods[i]; if (method.getName().startsWith("test") && (method.getParameterTypes() == null || method.getParameterTypes().length == 0) && !method.getName().equals("testDatabaseRestart")) { setRef(createRef()); refs.add(getRef()); String name = getRef().getClass().getName() + "." + method.getName(); names.add(name); try { Collection c = createCmp(name); setCmp(c); } catch (Exception e) { throw new RuntimeException(e); } try { System.out.println("invoking: " + method.getName()); method.invoke(this, null); } catch (Exception e) { throw new RuntimeException(e); } } } try { OzoneObjectFactory.getDefault().closeDatabase(); } catch (Exception e) { throw new RuntimeException(e); } System.out.println("Please shut down the database and start it up again; after that, press <enter>."); try { System.in.read(); } catch (IOException e) { throw new RuntimeException(e); } for (int i = 0; i < refs.size(); i++) { setRef((Collection) refs.get(i)); try { setCmp((Collection) OzoneObjectFactory.getDefault().objectForName((String) names.get(i))); } catch (Exception e) { throw new RuntimeException(e); } compare(); } } protected void setUp() throws java.lang.Exception { super.setUp(); if (OzoneObjectFactory.getDefaultDatabaseUrl() == null) { OzoneObjectFactory.setDefaultDatabaseUrl("ozonedb:remote://localhost:3333"); } setRef(createRef()); try { setCmp(createCmp()); } catch (Exception e) { throw new RuntimeException(e); } } protected void tearDown() throws java.lang.Exception { super.tearDown(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -