KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
25
26 import org.jboss.logging.Logger;
27 import org.jboss.test.logging.LoggingPlugin;
28 import org.jboss.test.security.PolicyPlugin;
29
30 /**
31  * An AbstractTestDelegate.
32  *
33  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
34  * @version $Revision: 56504 $
35  */

36 public class AbstractTestDelegate
37 {
38    /** The class */
39    protected Class JavaDoc clazz;
40
41    /** Whether security is enabled */
42    public boolean enableSecurity = false;
43
44    /** The policy plugin */
45    protected PolicyPlugin policy;
46
47    /** The logging plugin */
48    protected LoggingPlugin logging;
49
50    /** The log */
51    protected Logger log;
52
53    /**
54     * Get the test delegate. This queries the clazz, and its superclasses
55     * for a static getDelegate(Class) method to allow for testclass specific
56     * delegates.
57     *
58     * @param clazz the test class
59     * @return the delegate
60     * @throws Exception for any error
61     */

62    protected static AbstractTestDelegate getDelegate(Class JavaDoc clazz) throws Exception JavaDoc
63    {
64       NoSuchMethodException JavaDoc original = null;
65       while (true)
66       {
67          try
68          {
69             Method JavaDoc method = clazz.getMethod("getDelegate", new Class JavaDoc[] { Class JavaDoc.class });
70             AbstractTestDelegate delegate = (AbstractTestDelegate) method.invoke(null, new Object JavaDoc[] { clazz });
71             return delegate;
72          }
73          catch (NoSuchMethodException JavaDoc e)
74          {
75             if (original == null)
76                original = e;
77             clazz = clazz.getSuperclass();
78             if (clazz == null)
79                throw original;
80          }
81       }
82    }
83
84    /**
85     * Create a new test delegate
86     *
87     * @param clazz the class
88     */

89    public AbstractTestDelegate(Class JavaDoc clazz)
90    {
91       this.clazz = clazz;
92    }
93
94    /**
95     * Get the log
96     *
97     * @return the log
98     */

99    protected Logger getLog()
100    {
101       return log;
102    }
103
104    /**
105     * Enable trace
106     *
107     * @param name the logging context
108     */

109    protected void enableTrace(String JavaDoc name)
110    {
111       logging.enableTrace(name);
112    }
113
114    /**
115     * Setup callback. This calls setUpLogging and if enableSecurity is true,
116     * setUpSecurity.
117     *
118     * @throws Exception for any error
119     */

120    public void setUp() throws Exception JavaDoc
121    {
122       setUpLogging();
123       log("setUp");
124       if (enableSecurity)
125          setUpSecurity();
126    }
127
128    /**
129     * Teardown. If enableSecurity is true, this calls tearDownSecurity. The
130     * tearDownLogging is then called regardless.
131     *
132     * @throws Exception for any error
133     */

134    public void tearDown() throws Exception JavaDoc
135    {
136       try
137       {
138          if (enableSecurity)
139             tearDownSecurity();
140       }
141       finally
142       {
143          tearDownLogging();
144       }
145       log("tornDown");
146    }
147
148    /**
149     * Setup the logging
150     *
151     * @throws Exception for any error
152     */

153    public void setUpLogging() throws Exception JavaDoc
154    {
155       logging = LoggingPlugin.getInstance();
156       logging.setUp();
157       log = Logger.getLogger(clazz);
158    }
159
160    /**
161     * Teardown the logging
162     *
163     * @throws Exception for any error
164     */

165    public void tearDownLogging() throws Exception JavaDoc
166    {
167       logging.tearDown();
168    }
169
170    /**
171     * Setup the security
172     *
173     * @throws Exception for any error
174     */

175    protected void setUpSecurity() throws Exception JavaDoc
176    {
177       policy = PolicyPlugin.getInstance(clazz);
178       PolicyPlugin.setPolicy(policy);
179       System.setSecurityManager(new SecurityManager JavaDoc());
180    }
181
182    /**
183     * Teardown the security
184     *
185     * @throws Exception for any error
186     */

187    public void tearDownSecurity() throws Exception JavaDoc
188    {
189       System.setSecurityManager(null);
190    }
191
192    /**
193     * Log an event with the given context
194     *
195     * @param context the context
196     */

197    protected void log(String JavaDoc context)
198    {
199       getLog().debug("==== " + context + " " + clazz.getName() + " ====");
200    }
201 }
202
Popular Tags