KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, 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.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
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 /**
41  * A Controller Test.
42  *
43  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 1.2 $
45  */

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    /**
52     * Create a new ContainerTest.
53     *
54     * @param name the test name
55     */

56    public AbstractControllerTest(String JavaDoc name)
57    {
58       super(name);
59    }
60    
61    public static AbstractTestDelegate getNewControllerDelegate(Class JavaDoc clazz) throws Exception JavaDoc
62    {
63       ControllerTestDelegate delegate = new NewControllerTestDelegate(clazz);
64       // @todo delegate.enableSecurity = true;
65
return delegate;
66    }
67    
68    public static AbstractTestDelegate getOldControllerDelegate(Class JavaDoc clazz) throws Exception JavaDoc
69    {
70       ControllerTestDelegate delegate = new OldControllerTestDelegate(clazz);
71       // @todo delegate.enableSecurity = true;
72
return delegate;
73    }
74    
75    protected void setUp() throws Exception JavaDoc
76    {
77       super.setUp();
78       Order.reset();
79    }
80
81    protected void tearDown() throws Exception JavaDoc
82    {
83       getControllerDelegate().uninstallTemporary();
84       super.tearDown();
85    }
86
87    protected ControllerTestDelegate getControllerDelegate()
88    {
89       return (ControllerTestDelegate) getDelegate();
90    }
91    
92    protected MBeanServer JavaDoc getServer()
93    {
94       return getControllerDelegate().getServer();
95    }
96    
97    protected ServiceControllerMBean getController()
98    {
99       return getControllerDelegate().getController();
100    }
101    
102    protected List JavaDoc<ObjectName JavaDoc> deploy(URL JavaDoc url) throws Exception JavaDoc
103    {
104       return getControllerDelegate().deploy(url, true);
105    }
106    
107    protected List JavaDoc<ObjectName JavaDoc> deploy(String JavaDoc resource) throws Exception JavaDoc
108    {
109       URL JavaDoc url = getResourceURL(resource);
110       return deploy(url);
111    }
112    
113    protected void undeploy(List JavaDoc<ObjectName JavaDoc> objectNames)
114    {
115       getControllerDelegate().undeploy(objectNames);
116    }
117    
118    protected List JavaDoc<ObjectName JavaDoc> install(String JavaDoc resource) throws Exception JavaDoc
119    {
120       URL JavaDoc url = getResourceURL(resource);
121       return install(url);
122    }
123    
124    protected List JavaDoc<ObjectName JavaDoc> install(URL JavaDoc url) throws Exception JavaDoc
125    {
126       return getControllerDelegate().install(url);
127    }
128    
129    protected void uninstall(List JavaDoc<ObjectName JavaDoc> objectNames)
130    {
131       getControllerDelegate().uninstall(objectNames);
132    }
133
134    protected void assertInstall(ObjectName JavaDoc name) throws Exception JavaDoc
135    {
136       
137       String JavaDoc 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 JavaDoc name) throws Exception JavaDoc
146    {
147       uninstall(Collections.singletonList(name));
148       assertNoService(name);
149       assertNotRegistered(name);
150    }
151
152    protected List JavaDoc<ObjectName JavaDoc> assertDeploy(ObjectName JavaDoc name) throws Exception JavaDoc
153    {
154       
155       String JavaDoc resource = getName();
156       resource = resource.substring(4) + "_install.xml";
157       List JavaDoc<ObjectName JavaDoc> result = deploy(resource);
158       
159       assertServiceRunning(name);
160       assertRegistered(name);
161       
162       return result;
163    }
164
165    protected void assertUndeploy(ObjectName JavaDoc name) throws Exception JavaDoc
166    {
167       assertUndeploy(name, Collections.singletonList(name));
168    }
169
170    protected void assertUndeploy(ObjectName JavaDoc name, List JavaDoc<ObjectName JavaDoc> names) throws Exception JavaDoc
171    {
172       uninstall(names);
173       assertNoService(name);
174       assertNotRegistered(name);
175    }
176    
177    protected void validate() throws Exception JavaDoc
178    {
179       getControllerDelegate().validate();
180    }
181    
182    protected void assertInvalidDeployments() throws Exception JavaDoc
183    {
184       getControllerDelegate().assertInvalidDeployments();
185    }
186    
187    protected void assertInitialDeployFailure(String JavaDoc resource, ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
188    {
189       URL JavaDoc url = getResourceURL(resource);
190       getControllerDelegate().assertInitialDeployFailure(url, name, expected);
191    }
192    
193    protected List JavaDoc<ObjectName JavaDoc> assertDeployFailure(ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
194    {
195       return assertDeployFailure(name, ServiceContext.FAILED, expected);
196    }
197    
198    protected List JavaDoc<ObjectName JavaDoc> assertDeployFailure(ObjectName JavaDoc name, int expectedState, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
199    {
200       String JavaDoc resource = getName();
201       resource = resource.substring(4) + "_bad.xml";
202       return assertDeployFailure(resource, name, expectedState, expected);
203    }
204    
205    protected List JavaDoc<ObjectName JavaDoc> assertDeployFailure(String JavaDoc resource, ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
206    {
207       return assertDeployFailure(resource, name, ServiceContext.FAILED, expected);
208    }
209    
210    protected List JavaDoc<ObjectName JavaDoc> assertDeployFailure(String JavaDoc resource, ObjectName JavaDoc name, int expectedState, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
211    {
212       URL JavaDoc url = getResourceURL(resource);
213       List JavaDoc<ObjectName JavaDoc> 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 JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
222    {
223       String JavaDoc root = getName();
224       root = root.substring(4);
225
226       List JavaDoc<ObjectName JavaDoc> 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 JavaDoc name) throws Exception JavaDoc
233    {
234       String JavaDoc root = getName();
235       root = root.substring(4);
236
237       List JavaDoc<ObjectName JavaDoc> names = deploy(root + "_bad.xml");
238       assertServiceRunning(name);
239       undeploy(names);
240       deploy(root + "_good.xml");
241       assertServiceRunning(name);
242    }
243    
244    protected List JavaDoc<ObjectName JavaDoc> assertMaybeDeployFailure(ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
245    {
246       String JavaDoc resource = getName();
247       resource = resource.substring(4) + "_bad.xml";
248       return assertMaybeDeployFailure(resource, name, expected);
249    }
250    
251    protected List JavaDoc<ObjectName JavaDoc> assertMaybeDeployFailure(String JavaDoc resource, ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
252    {
253       URL JavaDoc url = getResourceURL(resource);
254       List JavaDoc<ObjectName JavaDoc> result = getControllerDelegate().assertMaybeDeployFailure(url, name, expected);
255       assertServiceFailed(name, OLD_NOT_REGISTERED);
256       return result;
257    }
258
259    protected void redeployAfterMaybeDeployFailure(ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
260    {
261       String JavaDoc root = getName();
262       root = root.substring(4);
263
264       List JavaDoc<ObjectName JavaDoc> 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 JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
271    {
272       String JavaDoc resource = getName();
273       resource = resource.substring(4) + "_bad.xml";
274       assertMaybeParseFailure(resource, name, expected);
275    }
276    
277    protected void assertMaybeParseFailure(String JavaDoc resource, ObjectName JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
278    {
279       URL JavaDoc url = getResourceURL(resource);
280       getControllerDelegate().assertMaybeParseFailure(url, name, expected);
281       assertServiceFailed(name, OLD_NOT_REGISTERED);
282    }
283    
284    protected ServiceContext getServiceContext(ObjectName JavaDoc name) throws Exception JavaDoc
285    {
286       assertNotNull(name);
287       return getControllerDelegate().getServiceContext(name);
288    }
289    
290    protected void assertServiceFailed(ObjectName JavaDoc name) throws Exception JavaDoc
291    {
292       assertServiceFailed(name, OLD_REGISTERED);
293    }
294    
295    protected void assertServiceFailed(ObjectName JavaDoc name, boolean registered) throws Exception JavaDoc
296    {
297       getControllerDelegate().assertServiceFailed(name, registered);
298    }
299    
300    protected void assertServiceInstalled(ObjectName JavaDoc name) throws Exception JavaDoc
301    {
302       assertServiceState(name, ServiceContext.INSTALLED, true);
303    }
304    
305    protected void assertServiceConfigured(ObjectName JavaDoc name) throws Exception JavaDoc
306    {
307       assertServiceState(name, ServiceContext.CONFIGURED, true);
308    }
309    
310    protected void assertServiceCreated(ObjectName JavaDoc name) throws Exception JavaDoc
311    {
312       assertServiceState(name, ServiceContext.CREATED, true);
313    }
314    
315    protected void assertServiceRunning(ObjectName JavaDoc name) throws Exception JavaDoc
316    {
317       assertServiceState(name, ServiceContext.RUNNING, true);
318    }
319    
320    protected void assertServiceStopped(ObjectName JavaDoc name) throws Exception JavaDoc
321    {
322       assertServiceState(name, ServiceContext.STOPPED, true);
323    }
324    
325    protected void assertServiceDestroyed(ObjectName JavaDoc name) throws Exception JavaDoc
326    {
327       assertServiceState(name, ServiceContext.DESTROYED, true);
328    }
329    
330    protected void assertServiceState(ObjectName JavaDoc name, int expectedState) throws Exception JavaDoc
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 JavaDoc name, int expectedState, boolean registered) throws Exception JavaDoc
337    {
338       getControllerDelegate().assertServiceState(name, expectedState, registered);
339    }
340    
341    protected void assertNoService(ObjectName JavaDoc name) throws Exception JavaDoc
342    {
343       ServiceContext ctx = getServiceContext(name);
344       assertNull("Should not be a service context for " + name, ctx);
345    }
346    
347    protected URL JavaDoc getResourceURL(String JavaDoc resource) throws Exception JavaDoc
348    {
349       URL JavaDoc url = getClass().getResource(resource);
350       if (url == null)
351          throw new IOException JavaDoc(resource + " not found");
352       return url;
353    }
354    
355    protected void assertRegistered(ObjectName JavaDoc name) throws Exception JavaDoc
356    {
357       MBeanServer JavaDoc server = getServer();
358       assertTrue(name + " should be registered in the MBeanServer", server.isRegistered(name));
359    }
360    
361    protected void assertNotRegistered(ObjectName JavaDoc name) throws Exception JavaDoc
362    {
363       MBeanServer JavaDoc server = getServer();
364       assertFalse(name + " should NOT be registered in the MBeanServer", server.isRegistered(name));
365    }
366
367    protected <T> T getMBean(Class JavaDoc<T> expected, ObjectName JavaDoc name, String JavaDoc attribute) throws Exception JavaDoc
368    {
369       MBeanServer JavaDoc server = getServer();
370       Object JavaDoc object = server.getAttribute(name, attribute);
371       assertNotNull(object);
372       return assertInstanceOf(expected, object);
373    }
374
375    protected Simple getSimple() throws Exception JavaDoc
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