KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > component > lifecycle > ComponentLifeCycleTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: ComponentLifeCycleTest.java 10:44:49 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.component.lifecycle;
23
24 import javax.jbi.JBIException;
25 import javax.management.MalformedObjectNameException JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.easymock.classextension.EasyMock;
31 import org.objectweb.petals.jbi.component.context.ComponentContextImpl;
32 import org.objectweb.petals.jbi.component.lifecycle.mock.MockComponentDescriptionBuilder;
33 import org.objectweb.petals.jbi.component.thread.ComponentLifeCycleThread;
34 import org.objectweb.petals.jbi.management.systemstate.SystemStateImpl;
35 import org.objectweb.petals.jbi.messaging.mock.MockLogger;
36 import org.objectweb.petals.jbi.routing.mock.ComponentMock;
37 import org.objectweb.petals.tools.jbicommon.descriptor.ComponentDescription;
38 import org.objectweb.petals.util.LoggingUtil;
39 import org.objectweb.util.monolog.api.Logger;
40
41 import static org.easymock.classextension.EasyMock.createMock;
42
43 /**
44  * Test of the ComponentLifeCycle
45  *
46  * @author ddesjardins - eBMWebsourcing
47  * @author wjoseph - eBMWebSourcing
48  */

49 public class ComponentLifeCycleTest extends TestCase {
50
51     private ComponentLifeCycle componentLifeCycle;
52     
53     private ComponentLifeCycleThread lifeCycleThread;
54
55     private ComponentMock componentMock;
56
57     private ComponentDescription componentDescription;
58
59     public void testDoInit() throws JBIException {
60         componentLifeCycle.doInit();
61         assertNotNull(componentMock.getContext());
62     }
63
64     public void testDoShutdown() throws JBIException {
65         componentLifeCycle.doShutdown();
66         assertEquals(componentMock.getState(), "Shutdown");
67     }
68
69     public void testDoStart() throws JBIException {
70         componentLifeCycle.doStart();
71         assertEquals(componentMock.getState(), "Started");
72     }
73
74     public void testDoStop() throws JBIException {
75         componentLifeCycle.doStop();
76         assertEquals(componentMock.getState(), "Stopped");
77     }
78
79     public void testGetComponent() {
80         assertEquals(componentLifeCycle.getComponent(), componentMock);
81     }
82
83     public void testGetComponentDescription() {
84         assertEquals(componentLifeCycle.getComponentDescription(),
85             componentDescription);
86     }
87
88     public void testGetExtensionMBeanName()
89         throws MalformedObjectNameException JavaDoc, NullPointerException JavaDoc, JBIException {
90         assertEquals(componentLifeCycle.getExtensionMBeanName(),
91             new ObjectName JavaDoc("test@test:name=test"));
92     }
93
94     public void testGetName() {
95         assertEquals(componentLifeCycle.getName(), "testComponentDescription");
96     }
97
98     public void testGetComponentContext() {
99         /*
100          * Test Case 1
101          */

102         ComponentContextImpl context = new ComponentContextImpl();
103         Logger logger = new MockLogger();
104         ObjectName JavaDoc name = null;
105         try {
106             name = new ObjectName JavaDoc("test@test:name=test");
107         } catch (Exception JavaDoc e) {
108             fail("Could not create Test parameters");
109         }
110         
111         lifeCycleThread = new ComponentLifeCycleThread(componentMock);
112         lifeCycleThread.start();
113         
114         componentLifeCycle = new ComponentLifeCycle(null, null, context, null,
115             name, lifeCycleThread, new LoggingUtil(logger));
116         /*
117          * Run Test Case 1
118          */

119         ComponentContextImpl result = componentLifeCycle.getComponentContext();
120         assertEquals("Wrong invokation result", result, context);
121     }
122
123     @Override JavaDoc
124     protected void setUp() throws MalformedObjectNameException JavaDoc,
125         NullPointerException JavaDoc {
126         componentDescription = MockComponentDescriptionBuilder
127             .buildComponentDescription();
128         componentMock = new ComponentMock();
129         ComponentContextImpl componentContextImpl = new ComponentContextImpl();
130         SystemStateImpl systemStateImpl = createMock(SystemStateImpl.class);
131         Logger logger = new MockLogger();
132         lifeCycleThread = new ComponentLifeCycleThread(componentMock);
133         lifeCycleThread.start();
134         componentLifeCycle = new ComponentLifeCycle(componentDescription,
135             componentMock, componentContextImpl, systemStateImpl,
136             new ObjectName JavaDoc("test@test:name=test"), lifeCycleThread, new LoggingUtil(logger));
137     }
138
139     public void testSetState() throws Exception JavaDoc {
140         /*
141          * Test Case 1 The state is properly set
142          */

143         /*
144          * Create Test parameters
145          */

146         String JavaDoc state = LifeCycleMBean.STARTED;
147         /*
148          * Run Test Case 1
149          */

150         try {
151             componentLifeCycle.setState(state);
152         } catch (Exception JavaDoc e) {
153             e.printStackTrace();
154             fail("Error during method invokation");
155         }
156         assertEquals("State was not properly set", componentLifeCycle
157             .getCurrentState(), state);
158
159         /*
160          * Test Case 2 An exception is raised, the state could not be persisted
161          */

162         componentDescription = MockComponentDescriptionBuilder
163             .buildComponentDescription();
164         componentMock = new ComponentMock();
165         ComponentContextImpl componentContextImpl = new ComponentContextImpl();
166         SystemStateImpl systemStateImpl = new SystemStateImpl() {
167             public void updateComponentLifeCycleState(String JavaDoc componentName,
168                 String JavaDoc lifecycleState) throws Exception JavaDoc {
169                 throw new Exception JavaDoc(
170                     "updateComponentLifeCycleState testing exception");
171             }
172         };
173         componentLifeCycle = new ComponentLifeCycle(componentDescription,
174             componentMock, componentContextImpl, systemStateImpl,
175             new ObjectName JavaDoc("test@test:name=test"), null, EasyMock
176                 .createMock(LoggingUtil.class));
177         /*
178          * Run Test Case 2
179          */

180         try {
181             componentLifeCycle.setState(state);
182             fail("No exception was raised");
183         } catch (Exception JavaDoc e) {
184             // Do nothing
185
}
186     }
187 }
188
Popular Tags