1 22 package org.jboss.test.system.controller; 23 24 import java.lang.reflect.UndeclaredThrowableException ; 25 import java.net.URL ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import javax.management.MBeanServer ; 33 import javax.management.MBeanServerFactory ; 34 import javax.management.ObjectName ; 35 36 import junit.framework.AssertionFailedError; 37 38 import org.jboss.deployment.IncompleteDeploymentException; 39 import org.jboss.mx.server.ServerConstants; 40 import org.jboss.system.ServiceContext; 41 import org.jboss.system.ServiceControllerMBean; 42 import org.jboss.test.AbstractSystemTest; 43 import org.jboss.test.AbstractTestDelegate; 44 import org.jboss.test.system.controller.support.Order; 45 46 52 public abstract class ControllerTestDelegate extends AbstractTestDelegate 53 { 54 private MBeanServer server; 55 56 private ServiceControllerMBean serviceController; 57 58 private SimpleSARDeployer deployer; 59 60 public ControllerTestDelegate(Class clazz) 61 { 62 super(clazz); 63 } 64 65 public MBeanServer getServer() 66 { 67 return server; 68 } 69 70 public ServiceControllerMBean getController() 71 { 72 return serviceController; 73 } 74 75 public void setUp() throws Exception 76 { 77 super.setUp(); 78 79 System.setProperty(ServerConstants.MBEAN_SERVER_BUILDER_CLASS_PROPERTY, ServerConstants.DEFAULT_MBEAN_SERVER_BUILDER_CLASS); 80 server = MBeanServerFactory.newMBeanServer("jboss"); 81 serviceController = createServiceController(); 82 server.registerMBean(serviceController, ServiceControllerMBean.OBJECT_NAME); 83 84 deployer = new SimpleSARDeployer(server, serviceController); 85 86 Order.reset(); 87 88 deploy(); 89 90 if (isValidateAtSetUp()) 91 validate(); 92 } 93 94 protected boolean isValidateAtSetUp() 96 { 97 return true; 98 } 99 100 public abstract ServiceControllerMBean createServiceController() throws Exception ; 101 102 public void tearDown() throws Exception 103 { 104 deployer.uninstall(); 105 super.tearDown(); 106 } 107 108 protected void uninstallTemporary() throws Exception 109 { 110 deployer.uninstallTemporary(); 111 } 112 113 protected void validate() throws Exception 114 { 115 Collection <ServiceContext> waitingForClasses = new HashSet <ServiceContext>(); 116 Collection <ServiceContext> waitingForDepends = serviceController.listIncompletelyDeployed(); 117 Collection <ServiceContext> allServices = serviceController.listDeployed(); 118 119 Collection <ServiceContext> rootCause = new HashSet <ServiceContext>(waitingForDepends); 121 Collection <ServiceContext> missing = new HashSet <ServiceContext>(); 122 for (Iterator i = rootCause.iterator(); i.hasNext();) 123 { 124 ServiceContext ctx = (ServiceContext) i.next(); 125 for (Iterator j = ctx.iDependOn.iterator(); j.hasNext(); ) 126 { 127 ServiceContext dependee = (ServiceContext) j.next(); 128 if (dependee.state != ServiceContext.RUNNING) 129 { 130 if (allServices.contains(dependee) == false) 132 missing.add(dependee); 133 i.remove(); 135 break; 136 } 137 } 138 } 139 rootCause.addAll(missing); 141 142 IncompleteDeploymentException ide = new IncompleteDeploymentException( 143 waitingForClasses, 144 waitingForDepends, 145 rootCause, 146 Collections.emptyList(), 147 Collections.emptyList()); 148 149 if (ide.isEmpty() == false) 150 throw ide; 151 } 152 153 protected void deploy() throws Exception 154 { 155 String testName = clazz.getName(); 156 testName = testName.replace('.', '/'); 157 158 int index = testName.indexOf("NewUnitTestCase"); 159 if (index != -1) 160 testName = testName.substring(0, index); 161 index = testName.indexOf("OldUnitTestCase"); 162 if (index != -1) 163 testName = testName.substring(0, index); 164 165 testName += ".xml"; 166 167 URL url = clazz.getClassLoader().getResource(testName); 168 if (url != null) 169 deploy(url, false); 170 else 171 log.debug("No test specific deployment " + testName); 172 } 173 174 protected List <ObjectName > deploy(URL url, boolean temporary) throws Exception 175 { 176 return deployer.deploy(url, temporary); 177 } 178 179 protected void undeploy(List <ObjectName > objectNames) 180 { 181 deployer.undeploy(objectNames); 182 } 183 184 protected List <ObjectName > install(URL url) throws Exception 185 { 186 return deployer.install(url); 187 } 188 189 protected void uninstall(List <ObjectName > objectNames) 190 { 191 deployer.uninstall(objectNames); 192 } 193 194 public abstract void assertMBeanFailed(ObjectName name, boolean registered) throws Exception ; 195 196 protected IncompleteDeploymentException assertInvalidDeployments() throws Exception 197 { 198 try 199 { 200 validate(); 201 throw new AssertionFailedError("Deployments should not be valid!"); 202 } 203 catch (IncompleteDeploymentException expected) 204 { 205 log.debug("Got expected " + expected.getClass().getName()); 206 return expected; 207 } 208 } 209 210 protected void assertInitialDeployFailure(URL url, ObjectName name, Class <? extends Throwable > expected) throws Exception 211 { 212 try 213 { 214 deploy(url, true); 215 throw new AssertionFailedError("Should have got a " + expected.getName()); 216 } 217 catch (Throwable t) 218 { 219 AbstractSystemTest.checkThrowableDeep(expected, t); 220 } 221 if (name != null) 222 assertNoService(name); 223 } 224 225 public List <ObjectName > assertDeployFailure(URL url, ObjectName name, Class <? extends Throwable > expected) throws Exception 226 { 227 List <ObjectName > result = deploy(url, true); 228 IncompleteDeploymentException e = assertInvalidDeployments(); 229 checkIncomplete(e, name, expected); 230 return result; 231 } 232 233 public List <ObjectName > assertMaybeDeployFailure(URL url, ObjectName name, Class <? extends Throwable > expected) throws Exception 234 { 235 return assertDeployFailure(url, name, expected); 236 } 237 238 public void assertMaybeParseFailure(URL url, ObjectName name, Class <? extends Throwable > expected) throws Exception 239 { 240 assertDeployFailure(url, name, expected); 241 assertServiceFailed(name); 242 } 243 244 protected void checkIncomplete(IncompleteDeploymentException e, ObjectName name, Class <? extends Throwable > expected) throws Exception 245 { 246 Collection incomplete = e.getMbeansWaitingForDepends(); 247 for (Iterator i = incomplete.iterator(); i.hasNext();) 248 { 249 ServiceContext ctx = (ServiceContext) i.next(); 250 if (name.equals(ctx.objectName)) 251 { 252 if (e != null || expected != null) 253 { 254 if (expected != null && ctx.problem == null) 255 throw new AssertionFailedError("Did not get expected " + expected.getName() + " for " + ctx); 256 if (expected == null && ctx.problem != null) 257 { 258 if (ctx.problem instanceof Exception ) 259 throw (Exception ) ctx.problem; 260 if (ctx.problem instanceof Error ) 261 throw (Error ) ctx.problem; 262 throw new UndeclaredThrowableException (ctx.problem); 263 } 264 if (expected != null) 265 AbstractSystemTest.checkThrowableDeep(expected, ctx.problem); 266 } 267 return; 268 } 269 } 270 271 throw new AssertionFailedError("Did not find " + name + " in incomplete deployments " + incomplete); 272 } 273 274 protected ServiceContext getServiceContext(ObjectName name) throws Exception 275 { 276 return getController().getServiceContext(name); 277 } 278 279 protected void assertNoService(ObjectName name) throws Exception 280 { 281 ServiceContext ctx = getServiceContext(name); 282 if (ctx != null && ctx.state != ServiceContext.NOTYETINSTALLED) 283 throw new AssertionFailedError("Should not be a service context for " + ctx); 284 } 285 286 protected void assertServiceFailed(ObjectName name) throws Exception 287 { 288 assertServiceFailed(name, AbstractControllerTest.OLD_REGISTERED); 289 } 290 291 protected void assertServiceFailed(ObjectName name, boolean registered) throws Exception 292 { 293 assertServiceState(name, ServiceContext.FAILED, registered); 294 assertMBeanFailed(name, registered); 295 } 296 297 protected void assertServiceState(ObjectName name, int expectedState, boolean registered) throws Exception 298 { 299 ServiceContext ctx = getServiceContext(name); 300 if (registered == false && ctx == null) 301 return; 302 if (ctx == null) 303 throw new AssertionFailedError("Incorrect state for " + name + " expected " + ServiceContext.getStateString(expectedState) + " but there is no context/state"); 304 if (expectedState != ctx.state) 305 throw new AssertionFailedError("Incorrect state for " + name + " expected " + ServiceContext.getStateString(expectedState) + " got " + ctx.getStateString()); 306 } 307 } 308 | Popular Tags |