KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > system > controller > ControllerTestDelegate


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.system.controller;
23
24 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.MBeanServerFactory JavaDoc;
34 import javax.management.ObjectName JavaDoc;
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 /**
47  * ControllerTestDelegate.
48  *
49  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
50  * @version $Revision: 1.1 $
51  */

52 public abstract class ControllerTestDelegate extends AbstractTestDelegate
53 {
54    private MBeanServer JavaDoc server;
55    
56    private ServiceControllerMBean serviceController;
57
58    private SimpleSARDeployer deployer;
59    
60    public ControllerTestDelegate(Class JavaDoc clazz)
61    {
62       super(clazz);
63    }
64
65    public MBeanServer JavaDoc getServer()
66    {
67       return server;
68    }
69
70    public ServiceControllerMBean getController()
71    {
72       return serviceController;
73    }
74    
75    public void setUp() throws Exception JavaDoc
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    // TODO Temporary until the IncompleteDeploymentDescription is integrated
95
protected boolean isValidateAtSetUp()
96    {
97       return true;
98    }
99    
100    public abstract ServiceControllerMBean createServiceController() throws Exception JavaDoc;
101    
102    public void tearDown() throws Exception JavaDoc
103    {
104       deployer.uninstall();
105       super.tearDown();
106    }
107
108    protected void uninstallTemporary() throws Exception JavaDoc
109    {
110       deployer.uninstallTemporary();
111    }
112    
113    protected void validate() throws Exception JavaDoc
114    {
115       Collection JavaDoc<ServiceContext> waitingForClasses = new HashSet JavaDoc<ServiceContext>();
116       Collection JavaDoc<ServiceContext> waitingForDepends = serviceController.listIncompletelyDeployed();
117       Collection JavaDoc<ServiceContext> allServices = serviceController.listDeployed();
118       
119       // Weed services that are waiting for other deployments
120
Collection JavaDoc<ServiceContext> rootCause = new HashSet JavaDoc<ServiceContext>(waitingForDepends);
121       Collection JavaDoc<ServiceContext> missing = new HashSet JavaDoc<ServiceContext>();
122       for (Iterator JavaDoc i = rootCause.iterator(); i.hasNext();)
123       {
124          ServiceContext ctx = (ServiceContext) i.next();
125          for (Iterator JavaDoc j = ctx.iDependOn.iterator(); j.hasNext(); )
126          {
127             ServiceContext dependee = (ServiceContext) j.next();
128             if (dependee.state != ServiceContext.RUNNING)
129             {
130                // Add missing mbean
131
if (allServices.contains(dependee) == false)
132                   missing.add(dependee);
133                // We are not a root cause
134
i.remove();
135                break;
136             }
137          }
138       }
139       // Add missing mbeans to the root cause
140
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 JavaDoc
154    {
155       String JavaDoc 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 JavaDoc 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 JavaDoc<ObjectName JavaDoc> deploy(URL JavaDoc url, boolean temporary) throws Exception JavaDoc
175    {
176       return deployer.deploy(url, temporary);
177    }
178    
179    protected void undeploy(List JavaDoc<ObjectName JavaDoc> objectNames)
180    {
181       deployer.undeploy(objectNames);
182    }
183
184    protected List JavaDoc<ObjectName JavaDoc> install(URL JavaDoc url) throws Exception JavaDoc
185    {
186       return deployer.install(url);
187    }
188    
189    protected void uninstall(List JavaDoc<ObjectName JavaDoc> objectNames)
190    {
191       deployer.uninstall(objectNames);
192    }
193    
194    public abstract void assertMBeanFailed(ObjectName JavaDoc name, boolean registered) throws Exception JavaDoc;
195    
196    protected IncompleteDeploymentException assertInvalidDeployments() throws Exception JavaDoc
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 JavaDoc url, ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
211    {
212       try
213       {
214          deploy(url, true);
215          throw new AssertionFailedError("Should have got a " + expected.getName());
216       }
217       catch (Throwable JavaDoc t)
218       {
219          AbstractSystemTest.checkThrowableDeep(expected, t);
220       }
221       if (name != null)
222          assertNoService(name);
223    }
224
225    public List JavaDoc<ObjectName JavaDoc> assertDeployFailure(URL JavaDoc url, ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
226    {
227       List JavaDoc<ObjectName JavaDoc> result = deploy(url, true);
228       IncompleteDeploymentException e = assertInvalidDeployments();
229       checkIncomplete(e, name, expected);
230       return result;
231    }
232    
233    public List JavaDoc<ObjectName JavaDoc> assertMaybeDeployFailure(URL JavaDoc url, ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
234    {
235       return assertDeployFailure(url, name, expected);
236    }
237    
238    public void assertMaybeParseFailure(URL JavaDoc url, ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
239    {
240       assertDeployFailure(url, name, expected);
241       assertServiceFailed(name);
242    }
243    
244    protected void checkIncomplete(IncompleteDeploymentException e, ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
245    {
246       Collection JavaDoc incomplete = e.getMbeansWaitingForDepends();
247       for (Iterator JavaDoc 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 JavaDoc)
259                      throw (Exception JavaDoc) ctx.problem;
260                   if (ctx.problem instanceof Error JavaDoc)
261                      throw (Error JavaDoc) ctx.problem;
262                   throw new UndeclaredThrowableException JavaDoc(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 JavaDoc name) throws Exception JavaDoc
275    {
276       return getController().getServiceContext(name);
277    }
278    
279    protected void assertNoService(ObjectName JavaDoc name) throws Exception JavaDoc
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 JavaDoc name) throws Exception JavaDoc
287    {
288       assertServiceFailed(name, AbstractControllerTest.OLD_REGISTERED);
289    }
290    
291    protected void assertServiceFailed(ObjectName JavaDoc name, boolean registered) throws Exception JavaDoc
292    {
293       assertServiceState(name, ServiceContext.FAILED, registered);
294       assertMBeanFailed(name, registered);
295    }
296    
297    protected void assertServiceState(ObjectName JavaDoc name, int expectedState, boolean registered) throws Exception JavaDoc
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