1 22 package org.jboss.test; 23 24 import java.lang.reflect.Method ; 25 26 import org.jboss.logging.Logger; 27 import org.jboss.test.logging.LoggingPlugin; 28 import org.jboss.test.security.PolicyPlugin; 29 30 36 public class AbstractTestDelegate 37 { 38 39 protected Class clazz; 40 41 42 public boolean enableSecurity = false; 43 44 45 protected PolicyPlugin policy; 46 47 48 protected LoggingPlugin logging; 49 50 51 protected Logger log; 52 53 62 protected static AbstractTestDelegate getDelegate(Class clazz) throws Exception 63 { 64 NoSuchMethodException original = null; 65 while (true) 66 { 67 try 68 { 69 Method method = clazz.getMethod("getDelegate", new Class [] { Class .class }); 70 AbstractTestDelegate delegate = (AbstractTestDelegate) method.invoke(null, new Object [] { clazz }); 71 return delegate; 72 } 73 catch (NoSuchMethodException 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 89 public AbstractTestDelegate(Class clazz) 90 { 91 this.clazz = clazz; 92 } 93 94 99 protected Logger getLog() 100 { 101 return log; 102 } 103 104 109 protected void enableTrace(String name) 110 { 111 logging.enableTrace(name); 112 } 113 114 120 public void setUp() throws Exception 121 { 122 setUpLogging(); 123 log("setUp"); 124 if (enableSecurity) 125 setUpSecurity(); 126 } 127 128 134 public void tearDown() throws Exception 135 { 136 try 137 { 138 if (enableSecurity) 139 tearDownSecurity(); 140 } 141 finally 142 { 143 tearDownLogging(); 144 } 145 log("tornDown"); 146 } 147 148 153 public void setUpLogging() throws Exception 154 { 155 logging = LoggingPlugin.getInstance(); 156 logging.setUp(); 157 log = Logger.getLogger(clazz); 158 } 159 160 165 public void tearDownLogging() throws Exception 166 { 167 logging.tearDown(); 168 } 169 170 175 protected void setUpSecurity() throws Exception 176 { 177 policy = PolicyPlugin.getInstance(clazz); 178 PolicyPlugin.setPolicy(policy); 179 System.setSecurityManager(new SecurityManager ()); 180 } 181 182 187 public void tearDownSecurity() throws Exception 188 { 189 System.setSecurityManager(null); 190 } 191 192 197 protected void log(String context) 198 { 199 getLog().debug("==== " + context + " " + clazz.getName() + " ===="); 200 } 201 } 202 | Popular Tags |