001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestComponentLifeCycle.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.framework;
030:
031: import com.sun.jbi.management.ComponentInstallationContext;
032:
033: import java.util.ArrayList;
034:
035: /**
036: * Tests for the ComponentLifeCycle class.
037: *
038: * @author Sun Microsystems, Inc.
039: */
040: public class TestComponentLifeCycle extends junit.framework.TestCase {
041: /**
042: * Current test name.
043: */
044: private String mTestName;
045:
046: /**
047: * Current SRCROOT path.
048: */
049: private String mSrcroot;
050:
051: /**
052: * Instance of the EnvironmentSetup helper class.
053: */
054: private EnvironmentSetup mSetup;
055:
056: /**
057: * Instance of the EnvironmentContext created by EnvironmentSetup.
058: */
059: private EnvironmentContext mEnvironment;
060:
061: /**
062: * Instance of the ComponentFramework created by EnvironmentSetup.
063: */
064: private ComponentFramework mCompFW;
065:
066: /**
067: * Instance of the ComponentRegistry creatd by EnvironmentSetup.
068: */
069: private ComponentRegistry mCompReg;
070:
071: /**
072: * Component instance created during setup.
073: */
074: private Component mComponent;
075:
076: /**
077: * ComponentInstallationContext instance created during setup.
078: */
079: private ComponentInstallationContext mInstallContext;
080:
081: /**
082: * ComponentLifeCycle instance created during setup.
083: */
084: private ComponentLifeCycle mLifeCycle;
085:
086: /**
087: * Result holder for threads.
088: */
089: private Throwable mResults[];
090:
091: /**
092: * Thread counter.
093: */
094: private int mThreadCount;
095:
096: /**
097: * Object for notification of thread completion.
098: */
099: private Boolean mWaitNotify;
100:
101: /**
102: * Constant for start operation.
103: */
104: private static final int START = 1;
105:
106: /**
107: * Constant for stop operation.
108: */
109: private static final int STOP = 2;
110:
111: /**
112: * Constant for shutDown operation.
113: */
114: private static final int SHUTDOWN = 3;
115:
116: /**
117: * Constant for shutDown operation with force set.
118: */
119: private static final int SHUTDOWNFORCE = 4;
120:
121: /**
122: * The constructor for this testcase, forwards the test name to
123: * the jUnit TestCase base class.
124: * @param aTestName String with the name of this test.
125: */
126: public TestComponentLifeCycle(String aTestName) {
127: super (aTestName);
128: mTestName = aTestName;
129: }
130:
131: /**
132: * Setup for the test. This creates the ComponentLifeCycle instance
133: * and other objects needed for the tests.
134: * @throws Exception when set up fails for any reason.
135: */
136: public void setUp() throws Exception {
137: super .setUp();
138: System.err.println("***** START of test " + mTestName);
139: mSrcroot = System.getProperty("junit.srcroot") + "/";
140:
141: // Create and initialize the EnvironmentContext. Create, initialize,
142: // and start up the Component Registry and Framework.
143:
144: mSetup = new EnvironmentSetup();
145: mEnvironment = mSetup.getEnvironmentContext();
146: mSetup.startup(true, true);
147: mCompReg = mEnvironment.getComponentRegistry();
148: mCompFW = mEnvironment.getComponentFramework();
149: }
150:
151: /**
152: * Cleanup for the test.
153: * @throws Exception when tearDown fails for any reason.
154: */
155: public void tearDown() throws Exception {
156: mSetup.shutdown(true, true);
157: System.err.println("***** END of test " + mTestName);
158: super .tearDown();
159: }
160:
161: /**
162: * Install the component for the test using the supplied component name.
163: * @param name the component name for this component.
164: * @throws Exception when set up fails for any reason.
165: */
166: private void install(String name) throws Exception {
167: // Create component class path lists
168:
169: ArrayList bootstrapClassPath = new ArrayList();
170: bootstrapClassPath.add(mSrcroot
171: + Constants.BC_BOOTSTRAP_CLASS_PATH);
172: ArrayList componentClassPath = new ArrayList();
173: componentClassPath.add(Constants.BC_LIFECYCLE_CLASS_PATH);
174:
175: // Create installation context.
176:
177: mInstallContext = new ComponentInstallationContext(name,
178: ComponentInstallationContext.BINDING,
179: Constants.BC_LIFECYCLE_CLASS_NAME, componentClassPath,
180: null);
181: mInstallContext.setInstallRoot(mSrcroot);
182: mInstallContext.setIsInstall(true);
183:
184: // Install the component.
185:
186: mCompFW.loadBootstrap(mInstallContext,
187: Constants.BC_BOOTSTRAP_CLASS_NAME, bootstrapClassPath,
188: null);
189: mCompFW.installComponent(mInstallContext);
190:
191: // Get the Component instance and create a ComponentLifeCycle instance.
192:
193: mComponent = mCompReg.getComponent(name);
194: mLifeCycle = new ComponentLifeCycle(mComponent, mCompFW);
195: }
196:
197: // ============================= test methods ================================
198:
199: /**
200: * Test the get method for the current state.
201: * @throws Exception if an unexpected error occurs.
202: */
203: public void testGetCurrentState() throws Exception {
204: install(Constants.BC_NAME);
205: assertEquals("Failure getting current state: ",
206: mLifeCycle.SHUTDOWN, mLifeCycle.getCurrentState());
207: }
208:
209: /**
210: * Test the get method for the DeployerMBean name.
211: * @throws Exception if an unexpected error occurs.
212: */
213: public void testGetDeploymentMBeanName() throws Exception {
214: install(Constants.BC_NAME);
215: assertNotNull("Failure getting DeploymentMBeanName: ",
216: mLifeCycle.getDeploymentMBeanName());
217: }
218:
219: /**
220: * Test the get method for the ExtensionMBean name.
221: * @throws Exception if an unexpected error occurs.
222: */
223: public void testGetExtensionMBeanName() throws Exception {
224: install(Constants.BC_NAME);
225: assertNull("Failure getting ExtensionMBeanName: ", mLifeCycle
226: .getExtensionMBeanName());
227: }
228:
229: /**
230: * Tests the start method with good result.
231: * @throws Exception if an unexpected error occurs.
232: */
233:
234: public void testStartGood() throws Exception {
235: install(Constants.BC_NAME);
236: mLifeCycle.start();
237: assertTrue("start failed: component state is "
238: + mComponent.getStatusAsString(), mComponent
239: .isStarted());
240: }
241:
242: /**
243: * Tests the stop method with good result.
244: * @throws Exception if an unexpected error occurs.
245: */
246:
247: public void testStopGood() throws Exception {
248: install(Constants.BC_NAME);
249: mLifeCycle.start();
250: mLifeCycle.stop();
251: assertTrue("stop failed: component state is "
252: + mComponent.getStatusAsString(), mComponent
253: .isStopped());
254: }
255:
256: /**
257: * Tests the shutDown method with good result.
258: * @throws Exception if an unexpected error occurs.
259: */
260:
261: public void testShutDownGood() throws Exception {
262: install(Constants.BC_NAME);
263: mLifeCycle.start();
264: mLifeCycle.stop();
265: mLifeCycle.shutDown();
266: assertTrue("shutDown failed: component state is "
267: + mComponent.getStatusAsString(), mComponent
268: .isInstalled());
269: }
270:
271: /**
272: * Tests the shutDownForce method with good result.
273: * @throws Exception if an unexpected error occurs.
274: */
275:
276: public void testShutDownForceGood() throws Exception {
277: install(Constants.BC_NAME_BAD_SHUTDOWN);
278: mLifeCycle.start();
279: mLifeCycle.stop();
280: mLifeCycle.shutDown(true);
281: assertTrue("shutDownForce failed: component state is "
282: + mComponent.getStatusAsString(), mComponent
283: .isInstalled());
284: }
285:
286: /**
287: * Tests the start method's multiple operation safeguards.
288: * @throws Exception if an unexpected error occurs.
289: */
290:
291: public void testStartBusy() throws Exception {
292: install(Constants.BC_NAME_SLOW_START);
293:
294: mWaitNotify = new Boolean(false);
295: mResults = new Throwable[2];
296:
297: mThreadCount = 0;
298: Thread t1 = new Thread(new TestThread(START, 0));
299: Thread t2 = new Thread(new TestThread(START, 1));
300:
301: t1.start();
302: t2.start();
303:
304: synchronized (mWaitNotify) {
305: mWaitNotify.wait();
306: }
307:
308: assertTrue("start failed: component state is "
309: + mComponent.getStatusAsString(), mComponent
310: .isStarted());
311:
312: if (null == mResults[0]) // thread t1 finished first
313: {
314: assertNotNull("start 2 succeeded, should have failed",
315: mResults[1]);
316: assertTrue("start 2 incorrect exception received: "
317: + mResults[1].toString(), (-1 < mResults[1]
318: .getMessage().indexOf("JBIFW2011")));
319: } else // thread t2 finished first
320: {
321: assertNull(
322: "start 2 failed, should have succeeded: exception is "
323: + mResults[1], mResults[1]);
324: assertTrue("start 1 incorrect exception received: "
325: + mResults[0].toString(), (-1 < mResults[0]
326: .getMessage().indexOf("JBIFW2011")));
327: }
328: }
329:
330: /**
331: * Tests the stop method's multiple operation safeguards.
332: * @throws Exception if an unexpected error occurs.
333: */
334:
335: public void testStopBusy() throws Exception {
336: install(Constants.BC_NAME_SLOW_STOP);
337: mLifeCycle.start();
338:
339: mWaitNotify = new Boolean(false);
340: mResults = new Throwable[2];
341:
342: mThreadCount = 0;
343: Thread t1 = new Thread(new TestThread(STOP, 0));
344: Thread t2 = new Thread(new TestThread(STOP, 1));
345:
346: t1.start();
347: t2.start();
348:
349: synchronized (mWaitNotify) {
350: mWaitNotify.wait();
351: }
352:
353: assertTrue("start failed: component state is "
354: + mComponent.getStatusAsString(), mComponent
355: .isStopped());
356:
357: if (null == mResults[0]) // thread t1 finished first
358: {
359: assertNotNull("stop 2 succeeded, should have failed",
360: mResults[1]);
361: assertTrue("stop 2 incorrect exception received: "
362: + mResults[1].toString(), (-1 < mResults[1]
363: .getMessage().indexOf("JBIFW2011")));
364: } else // thread t2 finished first
365: {
366: assertNull(
367: "stop 2 failed, should have succeeded: exception is "
368: + mResults[1], mResults[1]);
369: assertTrue("stop 1 incorrect exception received: "
370: + mResults[0].toString(), (-1 < mResults[0]
371: .getMessage().indexOf("JBIFW2011")));
372: }
373: }
374:
375: /**
376: * Tests the shutDown method's multiple operation safeguards.
377: * @throws Exception if an unexpected error occurs.
378: */
379:
380: public void testShutDownBusy() throws Exception {
381: install(Constants.BC_NAME_SLOW_SHUTDOWN);
382: mLifeCycle.start();
383:
384: mWaitNotify = new Boolean(false);
385: mResults = new Throwable[2];
386:
387: mThreadCount = 0;
388: Thread t1 = new Thread(new TestThread(SHUTDOWN, 0));
389: Thread t2 = new Thread(new TestThread(SHUTDOWN, 1));
390:
391: t1.start();
392: t2.start();
393:
394: synchronized (mWaitNotify) {
395: mWaitNotify.wait();
396: }
397:
398: assertTrue("shutDown failed: component state is "
399: + mComponent.getStatusAsString(), mComponent
400: .isInstalled());
401:
402: if (null == mResults[0]) // thread t1 finished first
403: {
404: assertNotNull("shutDown 2 succeeded, should have failed",
405: mResults[1]);
406: assertTrue("shutDown 2 incorrect exception received: "
407: + mResults[1].toString(), (-1 < mResults[1]
408: .getMessage().indexOf("JBIFW2011")));
409: } else // thread t2 finished first
410: {
411: assertNull(
412: "shutDown 2 failed, should have succeeded: exception is "
413: + mResults[1], mResults[1]);
414: assertTrue("shutDown 1 incorrect exception received: "
415: + mResults[0].toString(), (-1 < mResults[0]
416: .getMessage().indexOf("JBIFW2011")));
417: }
418: }
419:
420: /**
421: * Tests the shutDown method's multiple operation safeguards with force.
422: * @throws Exception if an unexpected error occurs.
423: */
424:
425: public void testShutDownForceBusy() throws Exception {
426: install(Constants.BC_NAME_SLOW_SHUTDOWN);
427: mLifeCycle.start();
428:
429: mWaitNotify = new Boolean(false);
430: mResults = new Throwable[2];
431:
432: mThreadCount = 0;
433: Thread t1 = new Thread(new TestThread(SHUTDOWNFORCE, 0));
434: Thread t2 = new Thread(new TestThread(SHUTDOWNFORCE, 1));
435:
436: t1.start();
437: t2.start();
438:
439: synchronized (mWaitNotify) {
440: mWaitNotify.wait();
441: }
442:
443: assertTrue("shutDown failed: component state is "
444: + mComponent.getStatusAsString(), mComponent
445: .isInstalled());
446: assertNull("shutDown 1 failed, exception is " + mResults[0],
447: mResults[0]);
448: assertNull("shutDown 2 failed, exception is " + mResults[1],
449: mResults[1]);
450: }
451:
452: /**
453: * Class to run on a separate thread to perform an operation.
454: */
455: class TestThread implements Runnable {
456: /**
457: * Operation to perform.
458: */
459: int mOperation;
460:
461: /**
462: * Sequence number of thread.
463: */
464: int mSequence;
465:
466: /**
467: * Constructor.
468: * @param operation the operation to perform.
469: */
470: public TestThread(int operation, int sequence) {
471: mOperation = operation;
472: mSequence = sequence;
473: ++mThreadCount;
474: }
475:
476: /**
477: * Separate thread to perform an operation on the component to test
478: * the "busy" flag implementation.
479: */
480: public final void run() {
481: try {
482: switch (mOperation) {
483: case START:
484: mLifeCycle.start();
485: break;
486: case STOP:
487: mLifeCycle.stop();
488: break;
489: case SHUTDOWN:
490: mLifeCycle.shutDown();
491: break;
492: case SHUTDOWNFORCE:
493: mLifeCycle.shutDown(true);
494: break;
495: }
496: mResults[mSequence] = null;
497: } catch (Throwable ex) {
498: mResults[mSequence] = ex;
499: ex.printStackTrace();
500: }
501: synchronized (mWaitNotify) {
502: if (--mThreadCount == 0) {
503: mWaitNotify.notifyAll();
504: }
505: }
506: }
507: }
508: }
|