001: /*
002: * Copyright 1999-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 com.google.gwt.emultest.java.util.EmulTestBase;
019:
020: /**
021: * Tests base {@link java.lang.Object} methods and contracts.
022: * <p>
023: * To use, simply extend this class, and implement
024: * the {@link #makeObject()} method.
025: * <p>
026: * If your {@link Object} fails one of these tests by design,
027: * you may still use this base set of cases. Simply override the
028: * test case (method) your {@link Object} fails.
029: *
030: * @author Rodney Waldhoff
031: * @version $Id: TestObject.java,v 1.13.2.1 2004/05/22 12:14:05 scolebourne Exp $
032: */
033: public abstract class TestObject extends EmulTestBase {
034:
035: // current major release for Collections
036: public static final int COLLECTIONS_MAJOR_VERSION = 2;
037:
038: /**
039: * This constant makes it possible for TestMap (and other subclasses,
040: * if necessary) to automatically check CVS for a versionX copy of a
041: * Serialized object, so we can make sure that compatibility is maintained.
042: * See, for example, TestMap.getCanonicalFullMapName(Map map).
043: * Subclasses can override this variable, indicating compatibility
044: * with earlier Collections versions.
045: * Defaults to 1, the earliest Collections version. (Note: some
046: * collections did not even exist in this version).
047: *
048: * @return 1
049: */
050: public int getCompatibilityVersion() {
051: return 1;
052: }
053:
054: /**
055: * Return a new, empty {@link Object} to used for testing.
056: */
057: public abstract Object makeObject();
058:
059: public void testObjectEqualsSelf() {
060: Object obj = makeObject();
061: assertEquals("A Object should equal itself", obj, obj);
062: }
063:
064: public void testObjectHashCodeEqualsSelfHashCode() {
065: Object obj = makeObject();
066: assertEquals("hashCode should be repeatable", obj.hashCode(),
067: obj.hashCode());
068: }
069:
070: public void testObjectHashCodeEqualsContract() {
071: Object obj1 = makeObject();
072: if (obj1.equals(obj1)) {
073: assertEquals(
074: "[1] When two objects are equal, their hashCodes should be also.",
075: obj1.hashCode(), obj1.hashCode());
076: }
077: Object obj2 = makeObject();
078: if (obj1.equals(obj2)) {
079: assertEquals(
080: "[2] When two objects are equal, their hashCodes should be also.",
081: obj1.hashCode(), obj2.hashCode());
082: assertTrue(
083: "When obj1.equals(obj2) is true, then obj2.equals(obj1) should also be true",
084: obj2.equals(obj1));
085: }
086: }
087:
088: public String getCanonicalEmptyCollectionName(Object object) {
089: StringBuffer retval = new StringBuffer();
090: retval.append("data/test/");
091: String colName = object.getClass().getName();
092:
093: colName = colName.substring(colName.lastIndexOf(".") + 1,
094: colName.length());
095: retval.append(colName);
096: retval.append(".emptyCollection.version");
097: retval.append(getCompatibilityVersion());
098: retval.append(".obj");
099: return retval.toString();
100: }
101:
102: public String getCanonicalFullCollectionName(Object object) {
103: StringBuffer retval = new StringBuffer();
104: retval.append("data/test/");
105: String colName = object.getClass().getName();
106: colName = colName.substring(colName.lastIndexOf(".") + 1,
107: colName.length());
108: retval.append(colName);
109: retval.append(".fullCollection.version");
110: retval.append(getCompatibilityVersion());
111: retval.append(".obj");
112: return retval.toString();
113: }
114:
115: /**
116: * Override this method if a subclass is testing a
117: * Collections that cannot serialize an "empty" Collection
118: * (e.g. Comparators have no contents)
119: *
120: * @return true
121: */
122: public boolean supportsEmptyCollections() {
123: return true;
124: }
125:
126: /**
127: * Override this method if a subclass is testing a
128: * Collections that cannot serialize a "full" Collection
129: * (e.g. Comparators have no contents)
130: *
131: * @return true
132: */
133: public boolean supportsFullCollections() {
134: return true;
135: }
136:
137: }
|