1 22 package org.jboss.test.kernel.junit; 23 24 import java.net.URL ; 25 26 import org.jboss.dependency.spi.ControllerState; 27 import org.jboss.kernel.Kernel; 28 import org.jboss.kernel.plugins.bootstrap.AbstractBootstrap; 29 import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap; 30 import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer; 31 import org.jboss.kernel.spi.dependency.KernelController; 32 import org.jboss.kernel.spi.dependency.KernelControllerContext; 33 import org.jboss.kernel.spi.deployment.KernelDeployment; 34 import org.jboss.test.AbstractTestDelegate; 35 36 42 public class MicrocontainerTestDelegate extends AbstractTestDelegate 43 { 44 45 protected Kernel kernel; 46 47 48 protected BasicXMLDeployer deployer; 49 50 56 public MicrocontainerTestDelegate(Class clazz) throws Exception 57 { 58 super(clazz); 59 } 60 61 public void setUp() throws Exception 62 { 63 super.setUp(); 64 65 try 66 { 67 AbstractBootstrap bootstrap = getBootstrap(); 69 bootstrap.run(); 70 kernel = bootstrap.getKernel(); 71 72 deployer = new BasicXMLDeployer(kernel); 74 75 deploy(); 77 } 78 catch (RuntimeException e) 79 { 80 throw e; 81 } 82 catch (Exception e) 83 { 84 throw e; 85 } 86 catch (Error e) 87 { 88 throw e; 89 } 90 catch (Throwable e) 91 { 92 throw new RuntimeException (e); 93 } 94 } 95 96 public void tearDown() throws Exception 97 { 98 super.tearDown(); 99 undeploy(); 100 } 101 102 108 protected AbstractBootstrap getBootstrap() throws Exception 109 { 110 return new BasicBootstrap(); 111 } 112 113 121 protected Object getBean(final Object name, final ControllerState state) 122 { 123 KernelControllerContext context = getControllerContext(name, state); 124 return context.getTarget(); 125 } 126 127 135 protected KernelControllerContext getControllerContext(final Object name, final ControllerState state) 136 { 137 KernelController controller = kernel.getController(); 138 KernelControllerContext context = (KernelControllerContext) controller.getContext(name, state); 139 if (context == null) 140 throw new IllegalStateException ("Bean not found " + name + " at state " + state); 141 return context; 142 } 143 144 149 protected void validate() throws Exception 150 { 151 try 152 { 153 deployer.validate(); 154 } 155 catch (RuntimeException e) 156 { 157 throw e; 158 } 159 catch (Exception e) 160 { 161 throw e; 162 } 163 catch (Error e) 164 { 165 throw e; 166 } 167 catch (Throwable t) 168 { 169 throw new RuntimeException (t); 170 } 171 } 172 173 180 protected KernelDeployment deploy(URL url) throws Exception 181 { 182 try 183 { 184 log.debug("Deploying " + url); 185 KernelDeployment deployment = deployer.deploy(url); 186 log.trace("Deployed " + url); 187 return deployment; 188 } 189 catch (RuntimeException e) 190 { 191 throw e; 192 } 193 catch (Exception e) 194 { 195 throw e; 196 } 197 catch (Error e) 198 { 199 throw e; 200 } 201 catch (Throwable t) 202 { 203 throw new RuntimeException (t); 204 } 205 } 206 207 212 protected void undeploy(KernelDeployment deployment) 213 { 214 log.debug("Undeploying " + deployment.getName()); 215 try 216 { 217 deployer.undeploy(deployment); 218 log.trace("Undeployed " + deployment.getName()); 219 } 220 catch (Throwable t) 221 { 222 log.warn("Error during undeployment: " + deployment.getName(), t); 223 } 224 } 225 226 231 protected void undeploy(URL url) 232 { 233 log.debug("Undeploying " + url); 234 try 235 { 236 deployer.undeploy(url); 237 log.trace("Undeployed " + url); 238 } 239 catch (Throwable t) 240 { 241 log.warn("Error during undeployment: " + url, t); 242 } 243 } 244 245 250 protected void deploy() throws Exception 251 { 252 String testName = clazz.getName(); 253 testName = testName.replace('.', '/') + ".xml"; 254 URL url = clazz.getClassLoader().getResource(testName); 255 if (url != null) 256 deploy(url); 257 else 258 log.debug("No test specific deployment " + testName); 259 } 260 261 264 protected void undeploy() 265 { 266 log.debug("Undeploying " + deployer.getDeploymentNames()); 267 deployer.shutdown(); 268 } 269 } 270 | Popular Tags |