KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > installation > HotDeployTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.installation;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22
23 import javax.jbi.JBIException;
24 import javax.jbi.component.Bootstrap;
25 import javax.jbi.component.Component;
26 import javax.jbi.component.ComponentLifeCycle;
27 import javax.jbi.component.ServiceUnitManager;
28 import javax.jbi.management.LifeCycleMBean;
29 import javax.management.MBeanServerInvocationHandler JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.apache.servicemix.jbi.util.FileUtil;
33 import org.easymock.MockControl;
34
35 public class HotDeployTest extends AbstractManagementTest {
36
37     // Create mocks
38
protected ExtMockControl bootstrapMock;
39     protected Bootstrap bootstrap;
40     protected ExtMockControl componentMock;
41     protected Component component;
42     protected ExtMockControl lifecycleMock;
43     protected ComponentLifeCycle lifecycle;
44     protected ExtMockControl managerMock;
45     protected ServiceUnitManager manager;
46     
47     protected void setUp() throws Exception JavaDoc {
48         super.setUp();
49         // Create mocks
50
bootstrapMock = ExtMockControl.createControl(Bootstrap.class);
51         bootstrap = (Bootstrap) bootstrapMock.getMock();
52         Bootstrap1.setDelegate(bootstrap);
53         componentMock = ExtMockControl.createControl(Component.class);
54         component = (Component) componentMock.getMock();
55         Component1.setDelegate(component);
56         lifecycleMock = ExtMockControl.createControl(ComponentLifeCycle.class);
57         lifecycle = (ComponentLifeCycle) lifecycleMock.getMock();
58         managerMock = ExtMockControl.createControl(ServiceUnitManager.class);
59         manager = (ServiceUnitManager) managerMock.getMock();
60     }
61     
62     protected void reset() {
63         bootstrapMock.reset();
64         componentMock.reset();
65         lifecycleMock.reset();
66         managerMock.reset();
67     }
68     
69     protected void replay() {
70         bootstrapMock.replay();
71         componentMock.replay();
72         lifecycleMock.replay();
73         managerMock.replay();
74     }
75     
76     protected void verify() {
77         bootstrapMock.verify();
78         componentMock.verify();
79         lifecycleMock.verify();
80         managerMock.verify();
81     }
82     
83     protected void initContainer() {
84         container.setCreateMBeanServer(true);
85         container.setMonitorInstallationDirectory(true);
86         container.setMonitorDeploymentDirectory(true);
87         container.setMonitorInterval(1);
88     }
89     
90     
91     
92     public void testHotDeployComponent() throws Exception JavaDoc {
93         final Object JavaDoc lock = new Object JavaDoc();
94         // configure mocks
95
Bootstrap1.setDelegate(new BootstrapDelegate(bootstrap) {
96             public void cleanUp() throws JBIException {
97                 super.cleanUp();
98                 synchronized (lock) {
99                     lock.notify();
100                 }
101             }
102         });
103         reset();
104         bootstrap.init(null);
105         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
106         bootstrap.getExtensionMBeanName();
107         bootstrapMock.setReturnValue(null);
108         bootstrap.onInstall();
109         bootstrap.cleanUp();
110         component.getLifeCycle();
111         componentMock.setReturnValue(lifecycle);
112         lifecycle.init(null);
113         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
114         lifecycle.start();
115         replay();
116         // test component installation
117
startContainer(true);
118         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
119         File JavaDoc hdInstaller = new File JavaDoc(container.getEnvironmentContext().getInstallationDir(), new File JavaDoc(installJarUrl).getName());
120         synchronized (lock) {
121             FileUtil.copyInputStream(new FileInputStream JavaDoc(installJarUrl),
122                     new FileOutputStream JavaDoc(hdInstaller));
123             lock.wait(5000);
124         }
125         Thread.sleep(50);
126         ObjectName JavaDoc lifecycleName = container.getComponent("component1").getMBeanName();
127         assertNotNull(lifecycleName);
128         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
129         assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState());
130         // check mocks
131
verify();
132         
133         // Clean shutdown
134
reset();
135         component.getLifeCycle();
136         componentMock.setReturnValue(lifecycle);
137         lifecycle.stop();
138         lifecycle.shutDown();
139         replay();
140         shutdownContainer();
141     }
142     
143     public void testHotDeployUndeployComponent() throws Exception JavaDoc {
144         final Object JavaDoc lock = new Object JavaDoc();
145         // configure mocks
146
Bootstrap1.setDelegate(new BootstrapDelegate(bootstrap) {
147             public void cleanUp() throws JBIException {
148                 super.cleanUp();
149                 synchronized (lock) {
150                     lock.notify();
151                 }
152             }
153         });
154         reset();
155         bootstrap.init(null);
156         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
157         bootstrap.getExtensionMBeanName();
158         bootstrapMock.setReturnValue(null);
159         bootstrap.onInstall();
160         bootstrap.cleanUp();
161         component.getLifeCycle();
162         componentMock.setReturnValue(lifecycle);
163         lifecycle.init(null);
164         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
165         lifecycle.start();
166         replay();
167         // test component installation
168
startContainer(true);
169         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
170         File JavaDoc hdInstaller = new File JavaDoc(container.getEnvironmentContext().getInstallationDir(), new File JavaDoc(installJarUrl).getName());
171         synchronized (lock) {
172             FileUtil.copyInputStream(new FileInputStream JavaDoc(installJarUrl),
173                     new FileOutputStream JavaDoc(hdInstaller));
174             lock.wait(5000);
175         }
176         Thread.sleep(50);
177         ObjectName JavaDoc lifecycleName = container.getComponent("component1").getMBeanName();
178         assertNotNull(lifecycleName);
179         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
180         assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState());
181         // check mocks
182
verify();
183         
184         // Configure mocks
185
reset();
186         bootstrap.onUninstall();
187         bootstrap.cleanUp();
188         lifecycle.stop();
189         lifecycle.shutDown();
190         //manager.shutDown("su");
191
replay();
192         // test component uninstallation
193
synchronized (lock) {
194             assertTrue(hdInstaller.delete());
195             lock.wait(5000);
196         }
197         Thread.sleep(50);
198         assertNull(container.getComponent("component1"));
199         // check mocks
200
verify();
201         
202         // Clean shutdown
203
reset();
204         replay();
205         shutdownContainer();
206     }
207     
208     public void testDeploySAThenComponent() throws Exception JavaDoc {
209         final Object JavaDoc lock = new Object JavaDoc();
210         // configure mocks
211
Bootstrap1.setDelegate(new BootstrapDelegate(bootstrap) {
212             public void cleanUp() throws JBIException {
213                 super.cleanUp();
214                 synchronized (lock) {
215                     lock.notify();
216                 }
217             }
218         });
219         reset();
220         bootstrap.init(null);
221         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
222         bootstrap.getExtensionMBeanName();
223         bootstrapMock.setReturnValue(null);
224         bootstrap.onInstall();
225         bootstrap.cleanUp();
226         component.getLifeCycle();
227         componentMock.setReturnValue(lifecycle);
228         lifecycle.init(null);
229         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
230         lifecycle.start();
231         component.getServiceUnitManager();
232         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
233         manager.deploy(null, null);
234         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
235         managerMock.setReturnValue(null);
236         manager.init(null, null);
237         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
238         manager.start("su");
239         replay();
240         // test component installation
241
startContainer(true);
242         File JavaDoc saJarUrl = createServiceAssemblyArchive("sa", "su", "component1");
243         File JavaDoc hdSa = new File JavaDoc(container.getEnvironmentContext().getDeploymentDir(), saJarUrl.getName());
244         FileUtil.copyInputStream(new FileInputStream JavaDoc(saJarUrl),
245                 new FileOutputStream JavaDoc(hdSa));
246         Thread.sleep(2000);
247         
248         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
249         File JavaDoc hdInstaller = new File JavaDoc(container.getEnvironmentContext().getInstallationDir(), new File JavaDoc(installJarUrl).getName());
250         synchronized (lock) {
251             FileUtil.copyInputStream(new FileInputStream JavaDoc(installJarUrl),
252                     new FileOutputStream JavaDoc(hdInstaller));
253             lock.wait(5000);
254         }
255         Thread.sleep(2000);
256         ObjectName JavaDoc lifecycleName = container.getComponent("component1").getMBeanName();
257         assertNotNull(lifecycleName);
258         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
259         assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState());
260         // check mocks
261
verify();
262         
263         // Clean shutdown
264
reset();
265         component.getLifeCycle();
266         componentMock.setReturnValue(lifecycle);
267         component.getServiceUnitManager();
268         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
269         lifecycle.stop();
270         manager.shutDown("su");
271         lifecycle.shutDown();
272         replay();
273         shutdownContainer();
274     }
275 }
276
Popular Tags