KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.jboss.logging.Logger;
28
29 /**
30  * An extension of AbstractTestCase that adds AbstractTestDelegate and
31  * AbstractTestSetup delegate notions. The AbstractTestSetup integrates
32  * with the junit.extensions.TestSetup setUp/tearDown callbacks to
33  * create an AbstractTestDelegate. When a testcase is run as a class with
34  * all conforming unit test methods run, a single class wide
35  * AbstractTestDelegate is created by either the fist call to the setUp
36  * method, or the suite AbstractTestSetup wrapper created by suite(Class).
37  *
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 58481 $
41  */

42 public class AbstractTestCaseWithSetup extends AbstractTestCase
43 {
44    /** Single run setup */
45    private AbstractTestSetup setup;
46    
47    /**
48     * Create a new test case
49     *
50     * @param name the test name
51     */

52    public AbstractTestCaseWithSetup(String JavaDoc name)
53    {
54       super(name);
55    }
56
57    /**
58     * Get the jboss logger.
59     */

60    public Logger getLog()
61    {
62       return getDelegate().getLog();
63    }
64
65    /**
66     * Enable trace logging for the given category name.
67     * @param name - the logging category to enable trace level logging for.
68     */

69    protected void enableTrace(String JavaDoc name)
70    {
71       getDelegate().enableTrace(name);
72    }
73    
74    /**
75     * Get the delegate
76     *
77     * @return the delegate
78     */

79    protected AbstractTestDelegate getDelegate()
80    {
81       return AbstractTestSetup.delegate;
82    }
83
84    /**
85     * Create a AbstractTestSetup wrapper for this class/instance to initialize
86     * the AbstractTestDelegate if the AbstractTestSetup.delegate singleton
87     * has not been initialized.
88     *
89     * @throws Exception
90     */

91    protected void setUp() throws Exception JavaDoc
92    {
93       // This is a single test run
94
if (AbstractTestSetup.delegate == null)
95       {
96          setup = new AbstractTestSetup(this.getClass(), this);
97          setup.setUp();
98       }
99       super.setUp();
100    }
101
102    protected void tearDown() throws Exception JavaDoc
103    {
104       super.tearDown();
105       if (setup != null)
106          setup.tearDown();
107    }
108
109    /**
110     * Bootstrap the test for the case of running all tests in clazz.
111     * This creates an AbstractTestSetup wrapper around TestSuite(clazz) to
112     * initialize the AbstractTestDelegate once for the suite.
113     *
114     * @param clazz the test class
115     * @return the bootstrap wrapper test
116     */

117    public static Test suite(Class JavaDoc clazz)
118    {
119       TestSuite suite = new TestSuite(clazz);
120       return new AbstractTestSetup(clazz, suite);
121    }
122 }
123
Popular Tags