1 22 package org.jboss.test; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.io.Serializable ; 29 import java.net.URL ; 30 import java.security.AccessController ; 31 import java.security.PrivilegedAction ; 32 import java.util.Arrays ; 33 import java.util.Collection ; 34 35 import junit.framework.AssertionFailedError; 36 import junit.framework.TestCase; 37 38 import org.jboss.logging.Logger; 39 40 46 public abstract class AbstractTestCase extends TestCase 47 { 48 49 long startTime; 50 51 56 public AbstractTestCase(String name) 57 { 58 super(name); 59 } 60 61 66 public abstract Logger getLog(); 67 68 public URL getResource(final String name) 69 { 70 return findResource(getClass(), name); 71 } 72 73 public static URL findResource(final Class clazz, final String name) 74 { 75 PrivilegedAction <URL > action = new PrivilegedAction <URL >() 76 { 77 public URL run() 78 { 79 return clazz.getResource(name); 80 } 81 }; 82 return AccessController.doPrivileged(action); 83 } 84 85 protected void setUp() throws Exception 86 { 87 log("Starting"); 88 startTime = System.currentTimeMillis(); 89 } 90 91 protected void tearDown() throws Exception 92 { 93 getLog().debug(getName() + " took " + (System.currentTimeMillis() - startTime) + "ms"); 94 log("Stopping"); 95 } 96 97 100 protected void configureLogging() 101 { 102 } 103 104 109 protected abstract void enableTrace(String name); 110 111 117 protected void assertEquals(float one, float two) 118 { 119 assertEquals(one, two, 0f); 120 } 121 122 128 protected void assertEquals(double one, double two) 129 { 130 assertEquals(one, two, 0f); 131 } 132 133 139 protected void assertEquals(Object [] expected, Object [] actual) 140 { 141 if (Arrays.equals(expected, actual) == false) 142 throw new AssertionFailedError("expected: " + Arrays.asList(expected) + " actual: " + Arrays.asList(actual)); 143 } 144 145 152 protected void assertEquals(String context, Object [] expected, Object [] actual) 153 { 154 if (Arrays.equals(expected, actual) == false) 155 throw new AssertionFailedError(context + " expected: " + Arrays.asList(expected) + " actual: " + Arrays.asList(actual)); 156 } 157 158 163 protected void assertEmpty(Collection c) 164 { 165 assertNotNull(c); 166 if (c.isEmpty() == false) 167 throw new AssertionFailedError("Expected empty collection " + c); 168 } 169 170 176 protected void assertEmpty(String context, Collection c) 177 { 178 assertNotNull(c); 179 if (c.isEmpty() == false) 180 throw new AssertionFailedError(context); 181 } 182 183 189 protected void checkThrowable(Class expected, Throwable throwable) 190 { 191 if (expected == null) 192 fail("Must provide an expected class"); 193 if (throwable == null) 194 fail("Must provide a throwable for comparison"); 195 if (throwable instanceof AssertionFailedError || throwable instanceof AssertionError ) 196 throw (Error ) throwable; 197 if (expected.equals(throwable.getClass()) == false) 198 { 199 getLog().error("Unexpected throwable", throwable); 200 fail("Unexpected throwable: " + throwable); 201 } 202 else 203 { 204 getLog().debug("Got expected " + expected.getName() + "(" + throwable + ")"); 205 } 206 } 207 208 215 protected byte[] serialize(Serializable object) throws Exception 216 { 217 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 218 ObjectOutputStream oos = new ObjectOutputStream (baos); 219 oos.writeObject(object); 220 oos.close(); 221 return baos.toByteArray(); 222 } 223 224 231 protected Object deserialize(byte[] bytes) throws Exception 232 { 233 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 234 ObjectInputStream ois = new ObjectInputStream (bais); 235 return ois.readObject(); 236 } 237 238 243 private void log(String context) 244 { 245 getLog().debug("==== " + context + " " + getName() + " ===="); 246 } 247 } 248 | Popular Tags |