001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.OutputStream;
028: import java.io.Serializable;
029:
030: /**
031: * Abstract test class for {@link java.lang.Object} methods and contracts.
032: * <p>
033: * To use, simply extend this class, and implement
034: * the {@link #makeObject()} method.
035: * <p>
036: * If your {@link Object} fails one of these tests by design,
037: * you may still use this base set of cases. Simply override the
038: * test case (method) your {@link Object} fails.
039: *
040: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
041: *
042: * @author Rodney Waldhoff
043: * @author Stephen Colebourne
044: * @author Anonymous
045: */
046: public abstract class AbstractTestObject extends BulkTest {
047:
048: /** Current major release for Collections */
049: public static final int COLLECTIONS_MAJOR_VERSION = 3;
050:
051: /**
052: * JUnit constructor.
053: *
054: * @param testName the test class name
055: */
056: public AbstractTestObject(String testName) {
057: super (testName);
058: }
059:
060: //-----------------------------------------------------------------------
061: /**
062: * Implement this method to return the object to test.
063: *
064: * @return the object to test
065: */
066: public abstract Object makeObject();
067:
068: /**
069: * Override this method if a subclass is testing an object
070: * that cannot serialize an "empty" Collection.
071: * (e.g. Comparators have no contents)
072: *
073: * @return true
074: */
075: public boolean supportsEmptyCollections() {
076: return true;
077: }
078:
079: /**
080: * Override this method if a subclass is testing an object
081: * that cannot serialize a "full" Collection.
082: * (e.g. Comparators have no contents)
083: *
084: * @return true
085: */
086: public boolean supportsFullCollections() {
087: return true;
088: }
089:
090: /**
091: * Is serialization testing supported.
092: * Default is true.
093: */
094: public boolean isTestSerialization() {
095: return true;
096: }
097:
098: /**
099: * Returns true to indicate that the collection supports equals() comparisons.
100: * This implementation returns true;
101: */
102: public boolean isEqualsCheckable() {
103: return true;
104: }
105:
106: //-----------------------------------------------------------------------
107: public void testObjectEqualsSelf() {
108: Object obj = makeObject();
109: assertEquals("A Object should equal itself", obj, obj);
110: }
111:
112: public void testEqualsNull() {
113: Object obj = makeObject();
114: assertEquals(false, obj.equals(null)); // make sure this doesn't throw NPE either
115: }
116:
117: public void testObjectHashCodeEqualsSelfHashCode() {
118: Object obj = makeObject();
119: assertEquals("hashCode should be repeatable", obj.hashCode(),
120: obj.hashCode());
121: }
122:
123: public void testObjectHashCodeEqualsContract() {
124: Object obj1 = makeObject();
125: if (obj1.equals(obj1)) {
126: assertEquals(
127: "[1] When two objects are equal, their hashCodes should be also.",
128: obj1.hashCode(), obj1.hashCode());
129: }
130: Object obj2 = makeObject();
131: if (obj1.equals(obj2)) {
132: assertEquals(
133: "[2] When two objects are equal, their hashCodes should be also.",
134: obj1.hashCode(), obj2.hashCode());
135: assertTrue(
136: "When obj1.equals(obj2) is true, then obj2.equals(obj1) should also be true",
137: obj2.equals(obj1));
138: }
139: }
140:
141: public void testSerializeDeserializeThenCompare() throws Exception {
142: Object obj = makeObject();
143: if (obj instanceof Serializable && isTestSerialization()) {
144: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
145: ObjectOutputStream out = new ObjectOutputStream(buffer);
146: out.writeObject(obj);
147: out.close();
148:
149: ObjectInputStream in = new ObjectInputStream(
150: new ByteArrayInputStream(buffer.toByteArray()));
151: Object dest = in.readObject();
152: in.close();
153: if (isEqualsCheckable()) {
154: assertEquals("obj != deserialize(serialize(obj))", obj,
155: dest);
156: }
157: }
158: }
159:
160: /**
161: * Sanity check method, makes sure that any Serializable
162: * class can be serialized and de-serialized in memory,
163: * using the handy makeObject() method
164: *
165: * @throws IOException
166: * @throws ClassNotFoundException
167: */
168: public void testSimpleSerialization() throws Exception {
169: Object o = makeObject();
170: if (o instanceof Serializable && isTestSerialization()) {
171: byte[] objekt = writeExternalFormToBytes((Serializable) o);
172: Object p = readExternalFormFromBytes(objekt);
173: }
174: }
175:
176: /**
177: * Tests serialization by comparing against a previously stored version in CVS.
178: * If the test object is serializable, confirm that a canonical form exists.
179: */
180: public void testCanonicalEmptyCollectionExists() {
181: if (supportsEmptyCollections() && isTestSerialization()
182: && !skipSerializedCanonicalTests()) {
183: Object object = makeObject();
184: if (object instanceof Serializable) {
185: String name = getCanonicalEmptyCollectionName(object);
186: assertTrue("Canonical empty collection (" + name
187: + ") is not in CVS", new File(name).exists());
188: }
189: }
190: }
191:
192: /**
193: * Tests serialization by comparing against a previously stored version in CVS.
194: * If the test object is serializable, confirm that a canonical form exists.
195: */
196: public void testCanonicalFullCollectionExists() {
197: if (supportsFullCollections() && isTestSerialization()
198: && !skipSerializedCanonicalTests()) {
199: Object object = makeObject();
200: if (object instanceof Serializable) {
201: String name = getCanonicalFullCollectionName(object);
202: assertTrue("Canonical full collection (" + name
203: + ") is not in CVS", new File(name).exists());
204: }
205: }
206: }
207:
208: // protected implementation
209: //-----------------------------------------------------------------------
210: /**
211: * Get the version of Collections that this object tries to
212: * maintain serialization compatibility with. Defaults to 1, the
213: * earliest Collections version. (Note: some collections did not
214: * even exist in this version).
215: *
216: * This constant makes it possible for TestMap (and other subclasses,
217: * if necessary) to automatically check CVS for a versionX copy of a
218: * Serialized object, so we can make sure that compatibility is maintained.
219: * See, for example, TestMap.getCanonicalFullMapName(Map map).
220: * Subclasses can override this variable, indicating compatibility
221: * with earlier Collections versions.
222: *
223: * @return The version, or <code>null</code> if this object shouldn't be
224: * tested for compatibility with previous versions.
225: */
226: public String getCompatibilityVersion() {
227: return "1";
228: }
229:
230: protected String getCanonicalEmptyCollectionName(Object object) {
231: StringBuffer retval = new StringBuffer();
232: retval.append("data/test/");
233: String colName = object.getClass().getName();
234: colName = colName.substring(colName.lastIndexOf(".") + 1,
235: colName.length());
236: retval.append(colName);
237: retval.append(".emptyCollection.version");
238: retval.append(getCompatibilityVersion());
239: retval.append(".obj");
240: return retval.toString();
241: }
242:
243: protected String getCanonicalFullCollectionName(Object object) {
244: StringBuffer retval = new StringBuffer();
245: retval.append("data/test/");
246: String colName = object.getClass().getName();
247: colName = colName.substring(colName.lastIndexOf(".") + 1,
248: colName.length());
249: retval.append(colName);
250: retval.append(".fullCollection.version");
251: retval.append(getCompatibilityVersion());
252: retval.append(".obj");
253: return retval.toString();
254: }
255:
256: /**
257: * Write a Serializable or Externalizable object as
258: * a file at the given path. NOT USEFUL as part
259: * of a unit test; this is just a utility method
260: * for creating disk-based objects in CVS that can become
261: * the basis for compatibility tests using
262: * readExternalFormFromDisk(String path)
263: *
264: * @param o Object to serialize
265: * @param path path to write the serialized Object
266: * @exception IOException
267: */
268: protected void writeExternalFormToDisk(Serializable o, String path)
269: throws IOException {
270: FileOutputStream fileStream = new FileOutputStream(path);
271: writeExternalFormToStream(o, fileStream);
272: }
273:
274: /**
275: * Converts a Serializable or Externalizable object to
276: * bytes. Useful for in-memory tests of serialization
277: *
278: * @param o Object to convert to bytes
279: * @return serialized form of the Object
280: * @exception IOException
281: */
282: protected byte[] writeExternalFormToBytes(Serializable o)
283: throws IOException {
284: ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
285: writeExternalFormToStream(o, byteStream);
286: return byteStream.toByteArray();
287: }
288:
289: /**
290: * Reads a Serialized or Externalized Object from disk.
291: * Useful for creating compatibility tests between
292: * different CVS versions of the same class
293: *
294: * @param path path to the serialized Object
295: * @return the Object at the given path
296: * @exception IOException
297: * @exception ClassNotFoundException
298: */
299: protected Object readExternalFormFromDisk(String path)
300: throws IOException, ClassNotFoundException {
301: FileInputStream stream = new FileInputStream(path);
302: return readExternalFormFromStream(stream);
303: }
304:
305: /**
306: * Read a Serialized or Externalized Object from bytes.
307: * Useful for verifying serialization in memory.
308: *
309: * @param b byte array containing a serialized Object
310: * @return Object contained in the bytes
311: * @exception IOException
312: * @exception ClassNotFoundException
313: */
314: protected Object readExternalFormFromBytes(byte[] b)
315: throws IOException, ClassNotFoundException {
316: ByteArrayInputStream stream = new ByteArrayInputStream(b);
317: return readExternalFormFromStream(stream);
318: }
319:
320: protected boolean skipSerializedCanonicalTests() {
321: return Boolean
322: .getBoolean("org.apache.commons.collections:with-clover");
323: }
324:
325: // private implementation
326: //-----------------------------------------------------------------------
327: private Object readExternalFormFromStream(InputStream stream)
328: throws IOException, ClassNotFoundException {
329: ObjectInputStream oStream = new ObjectInputStream(stream);
330: return oStream.readObject();
331: }
332:
333: private void writeExternalFormToStream(Serializable o,
334: OutputStream stream) throws IOException {
335: ObjectOutputStream oStream = new ObjectOutputStream(stream);
336: oStream.writeObject(o);
337: }
338:
339: }
|