1 16 package org.apache.commons.collections; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.OutputStream ; 28 import java.io.Serializable ; 29 30 46 public abstract class AbstractTestObject extends BulkTest { 47 48 49 public static final int COLLECTIONS_MAJOR_VERSION = 3; 50 51 56 public AbstractTestObject(String testName) { 57 super(testName); 58 } 59 60 66 public abstract Object makeObject(); 67 68 75 public boolean supportsEmptyCollections() { 76 return true; 77 } 78 79 86 public boolean supportsFullCollections() { 87 return true; 88 } 89 90 94 public boolean isTestSerialization() { 95 return true; 96 } 97 98 102 public boolean isEqualsCheckable() { 103 return true; 104 } 105 106 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)); } 116 117 public void testObjectHashCodeEqualsSelfHashCode() { 118 Object obj = makeObject(); 119 assertEquals("hashCode should be repeatable", obj.hashCode(), obj.hashCode()); 120 } 121 122 public void testObjectHashCodeEqualsContract() { 123 Object obj1 = makeObject(); 124 if (obj1.equals(obj1)) { 125 assertEquals( 126 "[1] When two objects are equal, their hashCodes should be also.", 127 obj1.hashCode(), obj1.hashCode()); 128 } 129 Object obj2 = makeObject(); 130 if (obj1.equals(obj2)) { 131 assertEquals( 132 "[2] When two objects are equal, their hashCodes should be also.", 133 obj1.hashCode(), obj2.hashCode()); 134 assertTrue( 135 "When obj1.equals(obj2) is true, then obj2.equals(obj1) should also be true", 136 obj2.equals(obj1)); 137 } 138 } 139 140 public void testSerializeDeserializeThenCompare() throws Exception { 141 Object obj = makeObject(); 142 if (obj instanceof Serializable && isTestSerialization()) { 143 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 144 ObjectOutputStream out = new ObjectOutputStream (buffer); 145 out.writeObject(obj); 146 out.close(); 147 148 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (buffer.toByteArray())); 149 Object dest = in.readObject(); 150 in.close(); 151 if (isEqualsCheckable()) { 152 assertEquals("obj != deserialize(serialize(obj))", obj, dest); 153 } 154 } 155 } 156 157 165 public void testSimpleSerialization() throws Exception { 166 Object o = makeObject(); 167 if (o instanceof Serializable && isTestSerialization()) { 168 byte[] objekt = writeExternalFormToBytes((Serializable ) o); 169 Object p = readExternalFormFromBytes(objekt); 170 } 171 } 172 173 177 public void testCanonicalEmptyCollectionExists() { 178 if (supportsEmptyCollections() && isTestSerialization() && !skipSerializedCanonicalTests()) { 179 Object object = makeObject(); 180 if (object instanceof Serializable ) { 181 String name = getCanonicalEmptyCollectionName(object); 182 assertTrue( 183 "Canonical empty collection (" + name + ") is not in CVS", 184 new File (name).exists()); 185 } 186 } 187 } 188 189 193 public void testCanonicalFullCollectionExists() { 194 if (supportsFullCollections() && isTestSerialization() && !skipSerializedCanonicalTests()) { 195 Object object = makeObject(); 196 if (object instanceof Serializable ) { 197 String name = getCanonicalFullCollectionName(object); 198 assertTrue( 199 "Canonical full collection (" + name + ") is not in CVS", 200 new File (name).exists()); 201 } 202 } 203 } 204 205 223 public String getCompatibilityVersion() { 224 return "1"; 225 } 226 227 protected String getCanonicalEmptyCollectionName(Object object) { 228 StringBuffer retval = new StringBuffer (); 229 retval.append("data/test/"); 230 String colName = object.getClass().getName(); 231 colName = colName.substring(colName.lastIndexOf(".") + 1, colName.length()); 232 retval.append(colName); 233 retval.append(".emptyCollection.version"); 234 retval.append(getCompatibilityVersion()); 235 retval.append(".obj"); 236 return retval.toString(); 237 } 238 239 protected String getCanonicalFullCollectionName(Object object) { 240 StringBuffer retval = new StringBuffer (); 241 retval.append("data/test/"); 242 String colName = object.getClass().getName(); 243 colName = colName.substring(colName.lastIndexOf(".") + 1, colName.length()); 244 retval.append(colName); 245 retval.append(".fullCollection.version"); 246 retval.append(getCompatibilityVersion()); 247 retval.append(".obj"); 248 return retval.toString(); 249 } 250 251 263 protected void writeExternalFormToDisk(Serializable o, String path) throws IOException { 264 FileOutputStream fileStream = new FileOutputStream (path); 265 writeExternalFormToStream(o, fileStream); 266 } 267 268 276 protected byte[] writeExternalFormToBytes(Serializable o) throws IOException { 277 ByteArrayOutputStream byteStream = new ByteArrayOutputStream (); 278 writeExternalFormToStream(o, byteStream); 279 return byteStream.toByteArray(); 280 } 281 282 292 protected Object readExternalFormFromDisk(String path) throws IOException , ClassNotFoundException { 293 FileInputStream stream = new FileInputStream (path); 294 return readExternalFormFromStream(stream); 295 } 296 297 306 protected Object readExternalFormFromBytes(byte[] b) throws IOException , ClassNotFoundException { 307 ByteArrayInputStream stream = new ByteArrayInputStream (b); 308 return readExternalFormFromStream(stream); 309 } 310 311 protected boolean skipSerializedCanonicalTests() { 312 return Boolean.getBoolean("org.apache.commons.collections:with-clover"); 313 } 314 315 private Object readExternalFormFromStream(InputStream stream) throws IOException , ClassNotFoundException { 318 ObjectInputStream oStream = new ObjectInputStream (stream); 319 return oStream.readObject(); 320 } 321 322 private void writeExternalFormToStream(Serializable o, OutputStream stream) throws IOException { 323 ObjectOutputStream oStream = new ObjectOutputStream (stream); 324 oStream.writeObject(o); 325 } 326 327 } 328 | Popular Tags |