1 22 package org.jboss.test.system.controller.integration.test; 23 24 import java.net.URL ; 25 26 import org.jboss.dependency.spi.ControllerContext; 27 import org.jboss.dependency.spi.ControllerState; 28 import org.jboss.kernel.Kernel; 29 import org.jboss.kernel.plugins.bootstrap.AbstractBootstrap; 30 import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap; 31 import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer; 32 import org.jboss.kernel.spi.dependency.KernelController; 33 import org.jboss.kernel.spi.deployment.KernelDeployment; 34 import org.jboss.system.ServiceController; 35 import org.jboss.system.ServiceControllerMBean; 36 import org.jboss.test.system.controller.NewControllerTestDelegate; 37 38 44 public class IntegrationTestDelegate extends NewControllerTestDelegate 45 { 46 47 protected Kernel kernel; 48 49 50 protected BasicXMLDeployer beanDeployer; 51 52 public IntegrationTestDelegate(Class clazz) 53 { 54 super(clazz); 55 } 56 57 public void setUp() throws Exception 58 { 59 super.setUp(); 60 61 try 62 { 63 beanDeployer = new BasicXMLDeployer(kernel); 65 66 deployMC(); 68 69 validateMC(); 70 } 71 catch (RuntimeException e) 72 { 73 throw e; 74 } 75 catch (Exception e) 76 { 77 throw e; 78 } 79 catch (Error e) 80 { 81 throw e; 82 } 83 catch (Throwable e) 84 { 85 throw new RuntimeException (e); 86 } 87 } 88 89 protected boolean isValidateAtSetUp() 91 { 92 return false; 93 } 94 95 public ServiceControllerMBean createServiceController() throws Exception 96 { 97 try 98 { 99 AbstractBootstrap bootstrap = getBootstrap(); 101 bootstrap.run(); 102 kernel = bootstrap.getKernel(); 103 104 ServiceController result = new ServiceController(); 106 result.setKernel(kernel); 107 return result; 108 } 109 catch (RuntimeException e) 110 { 111 throw e; 112 } 113 catch (Exception e) 114 { 115 throw e; 116 } 117 catch (Error e) 118 { 119 throw e; 120 } 121 catch (Throwable e) 122 { 123 throw new RuntimeException (e); 124 } 125 } 126 127 public void tearDown() throws Exception 128 { 129 undeployMC(); 130 super.tearDown(); 131 } 132 133 138 protected AbstractBootstrap getBootstrap() throws Exception 139 { 140 return new BasicBootstrap(); 141 } 142 143 151 protected Object getBean(final Object name, final ControllerState state) 152 { 153 ControllerContext context = getControllerContext(name, state); 154 return context.getTarget(); 155 } 156 157 165 protected ControllerContext getControllerContext(final Object name, final ControllerState state) 166 { 167 KernelController controller = kernel.getController(); 168 ControllerContext context = controller.getContext(name, state); 169 if (context == null) 170 throw new IllegalStateException ("Context not found: " + name); 171 return context; 172 } 173 174 177 protected void validateMC() throws Exception 178 { 179 try 180 { 181 beanDeployer.validate(); 182 } 183 catch (RuntimeException e) 184 { 185 throw e; 186 } 187 catch (Exception e) 188 { 189 throw e; 190 } 191 catch (Error e) 192 { 193 throw e; 194 } 195 catch (Throwable t) 196 { 197 throw new RuntimeException (t); 198 } 199 } 200 201 208 protected KernelDeployment deployMC(URL url) throws Exception 209 { 210 try 211 { 212 log.debug("Deploying " + url); 213 KernelDeployment deployment = beanDeployer.deploy(url); 214 log.trace("Deployed " + url); 215 return deployment; 216 } 217 catch (RuntimeException e) 218 { 219 throw e; 220 } 221 catch (Exception e) 222 { 223 throw e; 224 } 225 catch (Error e) 226 { 227 throw e; 228 } 229 catch (Throwable t) 230 { 231 throw new RuntimeException (t); 232 } 233 } 234 235 240 protected void undeployMC(URL url) 241 { 242 log.debug("Undeploying " + url); 243 try 244 { 245 beanDeployer.undeploy(url); 246 log.trace("Undeployed " + url); 247 } 248 catch (Throwable t) 249 { 250 log.warn("Error during undeployment: " + url, t); 251 } 252 } 253 254 259 protected void undeployMC(KernelDeployment deployment) 260 { 261 log.debug("Undeploying " + deployment.getName()); 262 try 263 { 264 beanDeployer.undeploy(deployment); 265 log.trace("Undeployed " + deployment.getName()); 266 } 267 catch (Throwable t) 268 { 269 log.warn("Error during undeployment: " + deployment.getName(), t); 270 } 271 } 272 273 278 protected void deployMC() throws Exception 279 { 280 String testName = clazz.getName(); 281 testName = testName.replace('.', '/') + "-mc.xml"; 282 URL url = clazz.getClassLoader().getResource(testName); 283 if (url != null) 284 deployMC(url); 285 else 286 log.debug("No test specific deployment " + testName); 287 } 288 289 292 protected void undeployMC() 293 { 294 log.debug("Undeploying " + beanDeployer.getDeploymentNames()); 295 beanDeployer.shutdown(); 296 } 297 298 } 299 | Popular Tags |