KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > test > ServiceLifecycleUnitTestCase


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.jmx.test;
23
24 import javax.management.MBeanServerConnection JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26
27 import junit.framework.Test;
28
29 import org.jboss.test.JBossTestCase;
30
31 /**
32  * Tests implicit call of create() upon start() and stop() upon destroy()
33  *
34  * [JBAS-2022]
35  *
36  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
37  * @version $Revision: 37406 $
38  */

39 public class ServiceLifecycleUnitTestCase extends JBossTestCase
40 {
41    public static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[] {};
42    public static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[] {};
43    
44    public ServiceLifecycleUnitTestCase(String JavaDoc test)
45    {
46       super(test);
47    }
48
49    public static Test suite() throws Exception JavaDoc
50    {
51       return getDeploySetup(ServiceLifecycleUnitTestCase.class, "jmx-simpleservice.sar");
52    }
53    
54    public void testCreateStopImplicitlyCalledJBossInternalLifecyleExposed() throws Exception JavaDoc
55    {
56       ObjectName JavaDoc target = new ObjectName JavaDoc("jboss.test:service=SimpleService");
57       MBeanServerConnection JavaDoc server = super.getServer();
58
59       // call 'destroy' directly to see if 'stop' was called
60
server.invoke(target, "destroy", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
61       Boolean JavaDoc stopCalled = (Boolean JavaDoc)server.getAttribute(target, "StopCalled");
62       assertTrue("stop() not called upon direct destroy", stopCalled.booleanValue());
63       
64       // reset the service memory
65
server.invoke(target, "resetLifecycleMemory", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
66       
67       // call 'start' directly to see if 'create' will be called
68
server.invoke(target, "start", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
69       Boolean JavaDoc createCalled = (Boolean JavaDoc)server.getAttribute(target, "CreateCalled");
70       assertTrue("create() not called upon direct start()", createCalled.booleanValue());
71    }
72    
73    public void testCreateStopImplicitlyCalledJBossInternalLifecycleHidden() throws Exception JavaDoc
74    {
75       ObjectName JavaDoc target = new ObjectName JavaDoc("jboss.test:service=SimpleService2");
76       MBeanServerConnection JavaDoc server = super.getServer();
77
78       // call 'destroy' directly to see if 'stop' was called
79
server.invoke(target, "destroy", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
80       Boolean JavaDoc stopCalled = (Boolean JavaDoc)server.getAttribute(target, "StopCalled");
81       assertTrue("stop() not called upon direct destroy()", stopCalled.booleanValue());
82       
83       // reset the service memory
84
server.invoke(target, "resetLifecycleMemory", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
85       
86       // call 'start' directly to see if 'create' will be called
87
server.invoke(target, "start", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
88       Boolean JavaDoc createCalled = (Boolean JavaDoc)server.getAttribute(target, "CreateCalled");
89       assertTrue("create() not called upon direct start()", createCalled.booleanValue());
90    }
91    
92    public void testStopCreateImplicitlyCalledWithDependencyAndLifecycleExposed() throws Exception JavaDoc
93    {
94       // target1 depends on target2
95
ObjectName JavaDoc target1 = new ObjectName JavaDoc("jboss.test:service=SimpleService3");
96       ObjectName JavaDoc target2 = new ObjectName JavaDoc("jboss.test:service=SimpleService4");
97       MBeanServerConnection JavaDoc server = super.getServer();
98       
99       // call 'destroy' directly on target2 to see if 'stop' is called on target1
100
server.invoke(target2, "destroy", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
101       Boolean JavaDoc stopCalled = (Boolean JavaDoc)server.getAttribute(target1, "StopCalled");
102       assertTrue("stop() not called on target1 upon indirect destroy() of target2", stopCalled.booleanValue());
103       
104       // reset the services memory
105
server.invoke(target1, "resetLifecycleMemory", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
106       server.invoke(target2, "resetLifecycleMemory", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
107       
108       // call 'start' directly on target2 to see if 'create' is called on target1
109
server.invoke(target2, "start", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
110       Boolean JavaDoc createCalled = (Boolean JavaDoc)server.getAttribute(target1, "CreateCalled");
111       assertTrue("create() not called on target1 upon indirect start() of target2", createCalled.booleanValue());
112    }
113    
114    public void testStopCreateImplicitlyCalledWithDependencyAndLifecycleHidden() throws Exception JavaDoc
115    {
116       // target1 depends on target2
117
// target1 hides jbossInternalLifecycle, however, target2 must expose
118
// jbossInternalLifecycle, otherwise the ServiceController is not
119
// invoked to alter the state of target1 in tandem with target2
120
ObjectName JavaDoc target1 = new ObjectName JavaDoc("jboss.test:service=SimpleService5");
121       ObjectName JavaDoc target2 = new ObjectName JavaDoc("jboss.test:service=SimpleService6");
122       MBeanServerConnection JavaDoc server = super.getServer();
123       
124       // call 'destroy' directly on target2 to see if 'stop' is called on target1
125
server.invoke(target2, "destroy", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
126       Boolean JavaDoc stopCalled = (Boolean JavaDoc)server.getAttribute(target1, "StopCalled");
127       assertTrue("stop() not called on target1 upon indirect destroy() of target2", stopCalled.booleanValue());
128       
129       // reset the services memory
130
server.invoke(target1, "resetLifecycleMemory", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
131       server.invoke(target2, "resetLifecycleMemory", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
132       
133       // call 'start' directly on target2 to see if 'create' is called on target1
134
server.invoke(target2, "start", EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY);
135       Boolean JavaDoc createCalled = (Boolean JavaDoc)server.getAttribute(target1, "CreateCalled");
136       assertTrue("create() not called on target1 upon indirect start() of target2", createCalled.booleanValue());
137    }
138 }
Popular Tags