KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > AbstractTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.security.AccessController JavaDoc;
31 import java.security.PrivilegedAction JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Collection JavaDoc;
34
35 import junit.framework.AssertionFailedError;
36 import junit.framework.TestCase;
37
38 import org.jboss.logging.Logger;
39
40 /**
41  * An abstract Test Case.
42  *
43  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 56645 $
45  */

46 public abstract class AbstractTestCase extends TestCase
47 {
48    /** The start time */
49    long startTime;
50    
51    /**
52     * Create a new abstract test case
53     *
54     * @param name the test name
55     */

56    public AbstractTestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60
61    /**
62     * Get the log for this test
63     *
64     * @return the log
65     */

66    public abstract Logger getLog();
67    
68    public URL JavaDoc getResource(final String JavaDoc name)
69    {
70       return findResource(getClass(), name);
71    }
72    
73    public static URL JavaDoc findResource(final Class JavaDoc clazz, final String JavaDoc name)
74    {
75       PrivilegedAction JavaDoc<URL JavaDoc> action = new PrivilegedAction JavaDoc<URL JavaDoc>()
76       {
77          public URL JavaDoc run()
78          {
79             return clazz.getResource(name);
80          }
81       };
82       return AccessController.doPrivileged(action);
83    }
84
85    protected void setUp() throws Exception JavaDoc
86    {
87       log("Starting");
88       startTime = System.currentTimeMillis();
89    }
90
91    protected void tearDown() throws Exception JavaDoc
92    {
93       getLog().debug(getName() + " took " + (System.currentTimeMillis() - startTime) + "ms");
94       log("Stopping");
95    }
96
97    /**
98     * Callback for configuring logging at the start of the test
99     */

100    protected void configureLogging()
101    {
102    }
103
104    /**
105     * Enable trace for a logging category
106     *
107     * @param name the logging category
108     */

109    protected abstract void enableTrace(String JavaDoc name);
110
111    /**
112     * Assert two float values are equal
113     *
114     * @param one the expected value
115     * @param two the actual value
116     */

117    protected void assertEquals(float one, float two)
118    {
119       assertEquals(one, two, 0f);
120    }
121
122    /**
123     * Assert two double values are equal
124     *
125     * @param one the expected value
126     * @param two the actual value
127     */

128    protected void assertEquals(double one, double two)
129    {
130       assertEquals(one, two, 0f);
131    }
132
133    /**
134     * Assert two arrays are equal
135     *
136     * @param expected the expected array
137     * @param actual the actual array
138     */

139    protected void assertEquals(Object JavaDoc[] expected, Object JavaDoc[] actual)
140    {
141       if (Arrays.equals(expected, actual) == false)
142          throw new AssertionFailedError("expected: " + Arrays.asList(expected) + " actual: " + Arrays.asList(actual));
143    }
144
145    /**
146     * Assert two arrays are equal
147     *
148     * @param context the context
149     * @param expected the expected array
150     * @param actual the actual array
151     */

152    protected void assertEquals(String JavaDoc context, Object JavaDoc[] expected, Object JavaDoc[] 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    /**
159     * Asserts a collection is empty
160     *
161     * @param c the collection
162     */

163    protected void assertEmpty(Collection JavaDoc c)
164    {
165       assertNotNull(c);
166       if (c.isEmpty() == false)
167          throw new AssertionFailedError("Expected empty collection " + c);
168    }
169
170    /**
171     * Asserts a collection is empty
172     *
173     * @param context the context
174     * @param c the collection
175     */

176    protected void assertEmpty(String JavaDoc context, Collection JavaDoc c)
177    {
178       assertNotNull(c);
179       if (c.isEmpty() == false)
180          throw new AssertionFailedError(context);
181    }
182    
183    /**
184     * Check we have the expected exception
185     *
186     * @param expected the excepted class of the exception
187     * @param throwable the real exception
188     */

189    protected void checkThrowable(Class JavaDoc expected, Throwable JavaDoc 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 JavaDoc)
196          throw (Error JavaDoc) 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    /**
209     * Serialize an object
210     *
211     * @param object the object
212     * @return the bytes
213     * @throws Exception for any error
214     */

215    protected byte[] serialize(Serializable JavaDoc object) throws Exception JavaDoc
216    {
217       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
218       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
219       oos.writeObject(object);
220       oos.close();
221       return baos.toByteArray();
222    }
223
224    /**
225     * Serialize an object
226     *
227     * @param bytes - the raw serialzied object data
228     * @return the bytes
229     * @throws Exception for any error
230     */

231    protected Object JavaDoc deserialize(byte[] bytes) throws Exception JavaDoc
232    {
233       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
234       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
235       return ois.readObject();
236    }
237    
238    /**
239     * Log an event with the given context
240     *
241     * @param context the context
242     */

243    private void log(String JavaDoc context)
244    {
245       getLog().debug("==== " + context + " " + getName() + " ====");
246    }
247 }
248
Popular Tags