001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test;
023:
024: import java.lang.reflect.Method;
025:
026: import org.jboss.logging.Logger;
027: import org.jboss.test.logging.LoggingPlugin;
028: import org.jboss.test.security.PolicyPlugin;
029:
030: /**
031: * An AbstractTestDelegate.
032: *
033: * @author <a href="adrian@jboss.com">Adrian Brock</a>
034: * @version $Revision: 57204 $
035: */
036: public class AbstractTestDelegate {
037: /** The class */
038: protected Class clazz;
039:
040: /** Whether security is enabled */
041: public boolean enableSecurity = false;
042:
043: /** The policy plugin */
044: protected PolicyPlugin policy;
045:
046: /** The logging plugin */
047: protected LoggingPlugin logging;
048:
049: /** The log */
050: protected Logger log;
051:
052: /**
053: * Get the test delegate
054: *
055: * @param clazz the test class
056: */
057: protected static AbstractTestDelegate getDelegate(Class clazz)
058: throws Exception {
059: NoSuchMethodException original = null;
060: while (true) {
061: try {
062: Method method = clazz.getMethod("getDelegate",
063: new Class[] { Class.class });
064: AbstractTestDelegate delegate = (AbstractTestDelegate) method
065: .invoke(null, new Object[] { clazz });
066: return delegate;
067: } catch (NoSuchMethodException e) {
068: if (original == null)
069: original = e;
070: clazz = clazz.getSuperclass();
071: if (clazz == null)
072: throw original;
073: }
074: }
075: }
076:
077: /**
078: * Create a new test delegate
079: *
080: * @param clazz the class
081: */
082: public AbstractTestDelegate(Class clazz) {
083: this .clazz = clazz;
084: }
085:
086: /**
087: * Get the log
088: *
089: * @return the log
090: */
091: protected Logger getLog() {
092: return log;
093: }
094:
095: /**
096: * Enable trace
097: *
098: * @param name the logging context
099: */
100: protected void enableTrace(String name) {
101: logging.enableTrace(name);
102: }
103:
104: /**
105: * Setup
106: *
107: * @throws Exception for any error
108: */
109: public void setUp() throws Exception {
110: setUpLogging();
111: log("setUp");
112: if (enableSecurity)
113: setUpSecurity();
114: }
115:
116: /**
117: * Teardown
118: *
119: * @throws Exception for any error
120: */
121: public void tearDown() throws Exception {
122: try {
123: if (enableSecurity)
124: tearDownSecurity();
125: } finally {
126: tearDownLogging();
127: }
128: log("tornDown");
129: }
130:
131: /**
132: * Setup the logging
133: *
134: * @throws Exception for any error
135: */
136: public void setUpLogging() throws Exception {
137: logging = LoggingPlugin.getInstance();
138: logging.setUp();
139: log = Logger.getLogger(clazz);
140: }
141:
142: /**
143: * Teardown the logging
144: *
145: * @throws Exception for any error
146: */
147: public void tearDownLogging() throws Exception {
148: logging.tearDown();
149: }
150:
151: /**
152: * Setup the security
153: *
154: * @throws Exception for any error
155: */
156: protected void setUpSecurity() throws Exception {
157: policy = PolicyPlugin.getInstance();
158: PolicyPlugin.setPolicy(policy);
159: System.setSecurityManager(new SecurityManager());
160: }
161:
162: /**
163: * Teardown the security
164: *
165: * @throws Exception for any error
166: */
167: public void tearDownSecurity() throws Exception {
168: System.setSecurityManager(null);
169: }
170:
171: /**
172: * Log an event with the given context
173: *
174: * @param context the context
175: */
176: protected void log(String context) {
177: getLog().debug(
178: "==== " + context + " " + clazz.getName() + " ====");
179: }
180: }
|