1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertSame; 36 import static org.testng.Assert.assertTrue; 37 38 import java.io.ByteArrayInputStream; 39 import java.io.ByteArrayOutputStream; 40 import java.io.DataInputStream; 41 import java.io.FileInputStream; 42 import java.io.IOException; 43 import java.io.ObjectInputStream; 44 import java.io.ObjectOutputStream; 45 import java.io.ObjectStreamConstants; 46 import java.lang.reflect.Constructor; 47 import java.lang.reflect.Field; 48 import java.lang.reflect.Modifier; 49 50 /** 51 * Base test class. 52 */ 53 public abstract class AbstractTest { 54 55 private static final String SERIALISATION_DATA_FOLDER = "src/test/resources/"; 56 isIsoLeap(long year)57 protected static boolean isIsoLeap(long year) { 58 if (year % 4 != 0) { 59 return false; 60 } 61 if (year % 100 == 0 && year % 400 != 0) { 62 return false; 63 } 64 return true; 65 } 66 assertSerializable(Object o)67 protected static void assertSerializable(Object o) throws IOException, ClassNotFoundException { 68 Object deserialisedObject = writeThenRead(o); 69 assertEquals(deserialisedObject, o); 70 } 71 assertSerializableAndSame(Object o)72 protected static void assertSerializableAndSame(Object o) throws IOException, ClassNotFoundException { 73 Object deserialisedObject = writeThenRead(o); 74 assertSame(deserialisedObject, o); 75 } 76 writeThenRead(Object o)77 protected static Object writeThenRead(Object o) throws IOException, ClassNotFoundException { 78 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 79 ObjectOutputStream oos = null; 80 try { 81 oos = new ObjectOutputStream(baos); 82 oos.writeObject(o); 83 } finally { 84 if (oos != null) { 85 oos.close(); 86 } 87 } 88 89 ObjectInputStream ois = null; 90 try { 91 ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); 92 return ois.readObject(); 93 } finally { 94 if (ois != null) { 95 ois.close(); 96 } 97 } 98 } 99 assertEqualsSerialisedForm(Object objectSerialised)100 protected static void assertEqualsSerialisedForm(Object objectSerialised) throws IOException, ClassNotFoundException { 101 assertEqualsSerialisedForm(objectSerialised, objectSerialised.getClass()); 102 } 103 assertEqualsSerialisedForm(Object objectSerialised, Class<?> cls)104 protected static void assertEqualsSerialisedForm(Object objectSerialised, Class<?> cls) throws IOException, ClassNotFoundException { 105 String className = cls.getSimpleName(); 106 // try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(SERIALISATION_DATA_FOLDER + className + ".bin"))) { 107 // out.writeObject(objectSerialised); 108 // } 109 110 ObjectInputStream in = null; 111 try { 112 in = new ObjectInputStream(new FileInputStream(SERIALISATION_DATA_FOLDER + className + ".bin")); 113 Object objectFromFile = in.readObject(); 114 assertEquals(objectFromFile, objectSerialised); 115 } finally { 116 if (in != null) { 117 in.close(); 118 } 119 } 120 } 121 assertImmutable(Class<?> cls)122 protected static void assertImmutable(Class<?> cls) { 123 assertTrue(Modifier.isPublic(cls.getModifiers())); 124 assertTrue(Modifier.isFinal(cls.getModifiers())); 125 Field[] fields = cls.getDeclaredFields(); 126 for (Field field : fields) { 127 if (field.getName().contains("$") == false) { 128 if (Modifier.isStatic(field.getModifiers())) { 129 assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName()); 130 } else { 131 assertTrue(Modifier.isPrivate(field.getModifiers()), "Field:" + field.getName()); 132 assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName()); 133 } 134 } 135 } 136 Constructor<?>[] cons = cls.getDeclaredConstructors(); 137 for (Constructor<?> con : cons) { 138 assertTrue(Modifier.isPrivate(con.getModifiers())); 139 } 140 } 141 assertSerializedBySer(Object object, byte[] expectedBytes, byte[]... matches)142 protected static void assertSerializedBySer(Object object, byte[] expectedBytes, byte[]... matches) throws Exception { 143 String serClass = object.getClass().getPackage().getName() + ".Ser"; 144 Class<?> serCls = Class.forName(serClass); 145 Field field = serCls.getDeclaredField("serialVersionUID"); 146 field.setAccessible(true); 147 long serVer = (Long) field.get(null); 148 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 149 ObjectOutputStream oos = null; 150 try { 151 oos = new ObjectOutputStream(baos); 152 oos.writeObject(object); 153 } finally { 154 if (oos != null) { 155 oos.close(); 156 } 157 } 158 byte[] bytes = baos.toByteArray(); 159 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 160 DataInputStream dis = null; 161 try { 162 dis = new DataInputStream(bais); 163 assertEquals(dis.readShort(), ObjectStreamConstants.STREAM_MAGIC); 164 assertEquals(dis.readShort(), ObjectStreamConstants.STREAM_VERSION); 165 assertEquals(dis.readByte(), ObjectStreamConstants.TC_OBJECT); 166 assertEquals(dis.readByte(), ObjectStreamConstants.TC_CLASSDESC); 167 assertEquals(dis.readUTF(), serClass); 168 assertEquals(dis.readLong(), serVer); 169 assertEquals(dis.readByte(), ObjectStreamConstants.SC_EXTERNALIZABLE | ObjectStreamConstants.SC_BLOCK_DATA); 170 assertEquals(dis.readShort(), 0); // number of fields 171 assertEquals(dis.readByte(), ObjectStreamConstants.TC_ENDBLOCKDATA); // end of classdesc 172 assertEquals(dis.readByte(), ObjectStreamConstants.TC_NULL); // no superclasses 173 if (expectedBytes.length < 256) { 174 assertEquals(dis.readByte(), ObjectStreamConstants.TC_BLOCKDATA); 175 assertEquals(dis.readUnsignedByte(), expectedBytes.length); 176 } else { 177 assertEquals(dis.readByte(), ObjectStreamConstants.TC_BLOCKDATALONG); 178 assertEquals(dis.readInt(), expectedBytes.length); 179 } 180 byte[] input = new byte[expectedBytes.length]; 181 dis.readFully(input); 182 assertEquals(input, expectedBytes); 183 if (matches.length > 0) { 184 for (byte[] match : matches) { 185 boolean matched = false; 186 while (matched == false) { 187 try { 188 dis.mark(1000); 189 byte[] possible = new byte[match.length]; 190 dis.readFully(possible); 191 assertEquals(possible, match); 192 matched = true; 193 } catch (AssertionError ex) { 194 dis.reset(); 195 dis.readByte(); // ignore 196 } 197 } 198 } 199 } else { 200 assertEquals(dis.readByte(), ObjectStreamConstants.TC_ENDBLOCKDATA); // end of blockdata 201 assertEquals(dis.read(), -1); 202 } 203 } finally { 204 if (dis != null) { 205 dis.close(); 206 } 207 } 208 } 209 210 } 211