KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.system.ServiceMBeanState;
25 import org.jboss.test.JBossTestCase;
26 import org.jboss.util.state.IllegalTransitionException;
27 import org.jboss.util.state.State;
28 import org.jboss.util.state.StateMachine;
29
30 /**
31  * ServiceMBean StateMachine tests
32  *
33  * @author <a HREF="dimitris@jboss.org">Dimitris Andreadis</a>
34  * @version $Revision: 41230 $
35 **/

36 public class ServiceMBeanStateMachineUnitTestCase extends JBossTestCase
37 {
38    /** The ServiceMBean StateMachine */
39    private StateMachine sm = ServiceMBeanState.createStateMachine("A test state machine");
40    
41    public ServiceMBeanStateMachineUnitTestCase(String JavaDoc name)
42    {
43       super(name);
44    }
45
46    public void setUp()
47    {
48       sm.reset();
49    }
50    
51    public void testRegisterUnregister() throws Exception JavaDoc
52    {
53       getLog().debug("+++ testRegisterUnregister");
54       
55       String JavaDoc[] script = {
56             "register",
57             "unregister"
58       };
59       tryActionScript(script, false);
60    }
61    
62    public void testCreateDestroy() throws Exception JavaDoc
63    {
64       getLog().debug("+++ testCreateDestroy");
65       
66       String JavaDoc[] script = {
67             "register",
68             "create",
69             "destroy"
70       };
71       tryActionScript(script, false);
72    }
73    
74    public void testCreateDestroyCreate() throws Exception JavaDoc
75    {
76       getLog().debug("+++ testCreateDestroyCreate");
77       
78       String JavaDoc[] script = {
79             "register",
80             "create",
81             "destroy",
82             "create"
83       };
84       tryActionScript(script, false);
85    }
86    
87    public void testStartStopStart() throws Exception JavaDoc
88    {
89       getLog().debug("+++ testStartStopStart");
90       
91       String JavaDoc[] script = {
92             "register",
93             "create",
94             "startBegin",
95             "startEnd",
96             "stopBegin",
97             "stopEnd",
98             "startBegin",
99             "startEnd"
100       };
101       tryActionScript(script, false);
102    }
103    
104    public void testFullLifecycle() throws Exception JavaDoc
105    {
106       getLog().debug("+++ testFullLifecycle");
107       
108       String JavaDoc[] script = {
109             "register",
110             "create",
111             "startBegin",
112             "startEnd",
113             "stopBegin",
114             "stopEnd",
115             "destroy",
116             "unregister"
117       };
118       tryActionScript(script, false);
119    }
120    
121    public void testFailedCreate() throws Exception JavaDoc
122    {
123       getLog().debug("+++ testFailedCreate");
124       
125       String JavaDoc[] script = {
126             "register",
127             "create",
128             "fail"
129       };
130       tryActionScript(script, false);
131    }
132    
133    public void testFailedStart() throws Exception JavaDoc
134    {
135       getLog().debug("+++ testFailedStart");
136       
137       String JavaDoc[] script = {
138             "register",
139             "create",
140             "startBegin",
141             "fail"
142       };
143       tryActionScript(script, false);
144    }
145    
146    public void testFailedStop() throws Exception JavaDoc
147    {
148       getLog().debug("+++ testFailedStop");
149       
150       String JavaDoc[] script = {
151             "register",
152             "create",
153             "startBegin",
154             "startEnd",
155             "stopBegin",
156             "fail"
157       };
158       tryActionScript(script, false);
159    }
160    
161    public void testFailedDestroy() throws Exception JavaDoc
162    {
163       getLog().debug("+++ testFailedDestroy");
164       
165       String JavaDoc[] script = {
166             "register",
167             "create",
168             "startBegin",
169             "startEnd",
170             "stopBegin",
171             "stopEnd",
172             "destroy",
173             "fail"
174       };
175       tryActionScript(script, false);
176    }
177    
178    public void testDontCreateBeforeRegister() throws Exception JavaDoc
179    {
180       getLog().debug("+++ testDontCreateBeforeRegister");
181       
182       String JavaDoc[] script = {
183             "create"
184       };
185       tryActionScript(script, true);
186    }
187    
188    public void testDontStartBeforeCreate() throws Exception JavaDoc
189    {
190       getLog().debug("+++ testDontStartBeforeCreate");
191       
192       String JavaDoc[] script = {
193             "register",
194             "startBegin"
195       };
196       tryActionScript(script, true);
197    }
198    
199    public void testDontDestroyBeforeStop() throws Exception JavaDoc
200    {
201       getLog().debug("+++ testDontDestroyBeforeStop");
202       
203       String JavaDoc[] script = {
204             "register",
205             "create",
206             "startBegin",
207             "startEnd",
208             "destroy"
209       };
210       tryActionScript(script, true);
211    }
212    
213    private void tryActionScript(String JavaDoc[] script, boolean exceptionExpected) throws Exception JavaDoc
214    {
215       for (int i = 0; i < script.length; i++)
216       {
217          getLog().debug("Current State: " + sm.getCurrentState().getName() + ", Action: " + script[i]);
218          try
219          {
220             State stateAfter = sm.nextState(script[i]);
221          }
222          catch (IllegalTransitionException e)
223          {
224             if (exceptionExpected)
225             {
226                getLog().debug("Caught Excepted IllegalTransitionException: " + e.getMessage());
227             }
228             else
229             {
230                getLog().debug("Caught Unexcepted IllegalTransitionException: " + e.getMessage());
231                
232                throw e;
233             }
234          }
235       }
236       getLog().debug("Final State: " + sm.getCurrentState().getName());
237    }
238 }
239
Popular Tags