1 22 package org.jboss.test.system.controller; 23 24 import java.io.IOException ; 25 import java.net.URL ; 26 import java.util.Collections ; 27 import java.util.List ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 32 import org.jboss.system.ServiceContext; 33 import org.jboss.system.ServiceControllerMBean; 34 import org.jboss.test.AbstractSystemTest; 35 import org.jboss.test.AbstractTestDelegate; 36 import org.jboss.test.system.controller.support.Order; 37 import org.jboss.test.system.controller.support.Simple; 38 import org.jboss.test.system.controller.support.SimpleMBean; 39 40 46 public abstract class AbstractControllerTest extends AbstractSystemTest 47 { 48 public static boolean OLD_NOT_REGISTERED = false; 49 public static boolean OLD_REGISTERED = true; 50 51 56 public AbstractControllerTest(String name) 57 { 58 super(name); 59 } 60 61 public static AbstractTestDelegate getNewControllerDelegate(Class clazz) throws Exception 62 { 63 ControllerTestDelegate delegate = new NewControllerTestDelegate(clazz); 64 return delegate; 66 } 67 68 public static AbstractTestDelegate getOldControllerDelegate(Class clazz) throws Exception 69 { 70 ControllerTestDelegate delegate = new OldControllerTestDelegate(clazz); 71 return delegate; 73 } 74 75 protected void setUp() throws Exception 76 { 77 super.setUp(); 78 Order.reset(); 79 } 80 81 protected void tearDown() throws Exception 82 { 83 getControllerDelegate().uninstallTemporary(); 84 super.tearDown(); 85 } 86 87 protected ControllerTestDelegate getControllerDelegate() 88 { 89 return (ControllerTestDelegate) getDelegate(); 90 } 91 92 protected MBeanServer getServer() 93 { 94 return getControllerDelegate().getServer(); 95 } 96 97 protected ServiceControllerMBean getController() 98 { 99 return getControllerDelegate().getController(); 100 } 101 102 protected List <ObjectName > deploy(URL url) throws Exception 103 { 104 return getControllerDelegate().deploy(url, true); 105 } 106 107 protected List <ObjectName > deploy(String resource) throws Exception 108 { 109 URL url = getResourceURL(resource); 110 return deploy(url); 111 } 112 113 protected void undeploy(List <ObjectName > objectNames) 114 { 115 getControllerDelegate().undeploy(objectNames); 116 } 117 118 protected List <ObjectName > install(String resource) throws Exception 119 { 120 URL url = getResourceURL(resource); 121 return install(url); 122 } 123 124 protected List <ObjectName > install(URL url) throws Exception 125 { 126 return getControllerDelegate().install(url); 127 } 128 129 protected void uninstall(List <ObjectName > objectNames) 130 { 131 getControllerDelegate().uninstall(objectNames); 132 } 133 134 protected void assertInstall(ObjectName name) throws Exception 135 { 136 137 String resource = getName(); 138 resource = resource.substring(4) + "_install.xml"; 139 install(resource); 140 141 assertServiceConfigured(name); 142 assertRegistered(name); 143 } 144 145 protected void assertUninstall(ObjectName name) throws Exception 146 { 147 uninstall(Collections.singletonList(name)); 148 assertNoService(name); 149 assertNotRegistered(name); 150 } 151 152 protected List <ObjectName > assertDeploy(ObjectName name) throws Exception 153 { 154 155 String resource = getName(); 156 resource = resource.substring(4) + "_install.xml"; 157 List <ObjectName > result = deploy(resource); 158 159 assertServiceRunning(name); 160 assertRegistered(name); 161 162 return result; 163 } 164 165 protected void assertUndeploy(ObjectName name) throws Exception 166 { 167 assertUndeploy(name, Collections.singletonList(name)); 168 } 169 170 protected void assertUndeploy(ObjectName name, List <ObjectName > names) throws Exception 171 { 172 uninstall(names); 173 assertNoService(name); 174 assertNotRegistered(name); 175 } 176 177 protected void validate() throws Exception 178 { 179 getControllerDelegate().validate(); 180 } 181 182 protected void assertInvalidDeployments() throws Exception 183 { 184 getControllerDelegate().assertInvalidDeployments(); 185 } 186 187 protected void assertInitialDeployFailure(String resource, ObjectName name, Class <? extends Throwable > expected) throws Exception 188 { 189 URL url = getResourceURL(resource); 190 getControllerDelegate().assertInitialDeployFailure(url, name, expected); 191 } 192 193 protected List <ObjectName > assertDeployFailure(ObjectName name, Class <? extends Throwable > expected) throws Exception 194 { 195 return assertDeployFailure(name, ServiceContext.FAILED, expected); 196 } 197 198 protected List <ObjectName > assertDeployFailure(ObjectName name, int expectedState, Class <? extends Throwable > expected) throws Exception 199 { 200 String resource = getName(); 201 resource = resource.substring(4) + "_bad.xml"; 202 return assertDeployFailure(resource, name, expectedState, expected); 203 } 204 205 protected List <ObjectName > assertDeployFailure(String resource, ObjectName name, Class <? extends Throwable > expected) throws Exception 206 { 207 return assertDeployFailure(resource, name, ServiceContext.FAILED, expected); 208 } 209 210 protected List <ObjectName > assertDeployFailure(String resource, ObjectName name, int expectedState, Class <? extends Throwable > expected) throws Exception 211 { 212 URL url = getResourceURL(resource); 213 List <ObjectName > result = getControllerDelegate().assertDeployFailure(url, name, expected); 214 if (expectedState == ServiceContext.FAILED) 215 assertServiceFailed(name, OLD_REGISTERED); 216 else 217 assertServiceState(name, expectedState); 218 return result; 219 } 220 221 protected void redeployAfterDeployFailure(ObjectName name, Class <? extends Throwable > expected) throws Exception 222 { 223 String root = getName(); 224 root = root.substring(4); 225 226 List <ObjectName > names = assertDeployFailure(root + "_bad.xml", SimpleMBean.OBJECT_NAME, expected); 227 undeploy(names); 228 deploy(root + "_good.xml"); 229 assertServiceRunning(name); 230 } 231 232 protected void redeployAfterUndeployFailure(ObjectName name) throws Exception 233 { 234 String root = getName(); 235 root = root.substring(4); 236 237 List <ObjectName > names = deploy(root + "_bad.xml"); 238 assertServiceRunning(name); 239 undeploy(names); 240 deploy(root + "_good.xml"); 241 assertServiceRunning(name); 242 } 243 244 protected List <ObjectName > assertMaybeDeployFailure(ObjectName name, Class <? extends Throwable > expected) throws Exception 245 { 246 String resource = getName(); 247 resource = resource.substring(4) + "_bad.xml"; 248 return assertMaybeDeployFailure(resource, name, expected); 249 } 250 251 protected List <ObjectName > assertMaybeDeployFailure(String resource, ObjectName name, Class <? extends Throwable > expected) throws Exception 252 { 253 URL url = getResourceURL(resource); 254 List <ObjectName > result = getControllerDelegate().assertMaybeDeployFailure(url, name, expected); 255 assertServiceFailed(name, OLD_NOT_REGISTERED); 256 return result; 257 } 258 259 protected void redeployAfterMaybeDeployFailure(ObjectName name, Class <? extends Throwable > expected) throws Exception 260 { 261 String root = getName(); 262 root = root.substring(4); 263 264 List <ObjectName > names = assertMaybeDeployFailure(root + "_bad.xml", SimpleMBean.OBJECT_NAME, expected); 265 undeploy(names); 266 deploy(root + "_good.xml"); 267 assertServiceRunning(name); 268 } 269 270 protected void assertMaybeParseFailure(ObjectName name, Class <? extends Throwable > expected) throws Exception 271 { 272 String resource = getName(); 273 resource = resource.substring(4) + "_bad.xml"; 274 assertMaybeParseFailure(resource, name, expected); 275 } 276 277 protected void assertMaybeParseFailure(String resource, ObjectName name, Class <? extends Throwable > expected) throws Exception 278 { 279 URL url = getResourceURL(resource); 280 getControllerDelegate().assertMaybeParseFailure(url, name, expected); 281 assertServiceFailed(name, OLD_NOT_REGISTERED); 282 } 283 284 protected ServiceContext getServiceContext(ObjectName name) throws Exception 285 { 286 assertNotNull(name); 287 return getControllerDelegate().getServiceContext(name); 288 } 289 290 protected void assertServiceFailed(ObjectName name) throws Exception 291 { 292 assertServiceFailed(name, OLD_REGISTERED); 293 } 294 295 protected void assertServiceFailed(ObjectName name, boolean registered) throws Exception 296 { 297 getControllerDelegate().assertServiceFailed(name, registered); 298 } 299 300 protected void assertServiceInstalled(ObjectName name) throws Exception 301 { 302 assertServiceState(name, ServiceContext.INSTALLED, true); 303 } 304 305 protected void assertServiceConfigured(ObjectName name) throws Exception 306 { 307 assertServiceState(name, ServiceContext.CONFIGURED, true); 308 } 309 310 protected void assertServiceCreated(ObjectName name) throws Exception 311 { 312 assertServiceState(name, ServiceContext.CREATED, true); 313 } 314 315 protected void assertServiceRunning(ObjectName name) throws Exception 316 { 317 assertServiceState(name, ServiceContext.RUNNING, true); 318 } 319 320 protected void assertServiceStopped(ObjectName name) throws Exception 321 { 322 assertServiceState(name, ServiceContext.STOPPED, true); 323 } 324 325 protected void assertServiceDestroyed(ObjectName name) throws Exception 326 { 327 assertServiceState(name, ServiceContext.DESTROYED, true); 328 } 329 330 protected void assertServiceState(ObjectName name, int expectedState) throws Exception 331 { 332 ServiceContext ctx = getServiceContext(name); 333 assertTrue("Incorrect state for " + name + " expected " + ServiceContext.getStateString(expectedState) + " got " + ctx.getStateString(), expectedState == ctx.state); 334 } 335 336 protected void assertServiceState(ObjectName name, int expectedState, boolean registered) throws Exception 337 { 338 getControllerDelegate().assertServiceState(name, expectedState, registered); 339 } 340 341 protected void assertNoService(ObjectName name) throws Exception 342 { 343 ServiceContext ctx = getServiceContext(name); 344 assertNull("Should not be a service context for " + name, ctx); 345 } 346 347 protected URL getResourceURL(String resource) throws Exception 348 { 349 URL url = getClass().getResource(resource); 350 if (url == null) 351 throw new IOException (resource + " not found"); 352 return url; 353 } 354 355 protected void assertRegistered(ObjectName name) throws Exception 356 { 357 MBeanServer server = getServer(); 358 assertTrue(name + " should be registered in the MBeanServer", server.isRegistered(name)); 359 } 360 361 protected void assertNotRegistered(ObjectName name) throws Exception 362 { 363 MBeanServer server = getServer(); 364 assertFalse(name + " should NOT be registered in the MBeanServer", server.isRegistered(name)); 365 } 366 367 protected <T> T getMBean(Class <T> expected, ObjectName name, String attribute) throws Exception 368 { 369 MBeanServer server = getServer(); 370 Object object = server.getAttribute(name, attribute); 371 assertNotNull(object); 372 return assertInstanceOf(expected, object); 373 } 374 375 protected Simple getSimple() throws Exception 376 { 377 return getMBean(Simple.class, SimpleMBean.OBJECT_NAME, "Instance"); 378 } 379 380 protected void FAILS_IN_OLD() 381 { 382 getLog().debug("This test fails with the old service controller, ignoring."); 383 } 384 } 385 | Popular Tags |