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: * @(#)TestServiceUnitOperation.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: import javax.jbi.component.ServiceUnitManager;
036:
037: /**
038: * Tests for the ServiceUnitOperation.
039: *
040: * @author Sun Microsystems, Inc.
041: */
042: public class TestServiceUnitOperation extends junit.framework.TestCase {
043: /**
044: * Current test name.
045: */
046: private String mTestName;
047:
048: /**
049: * Current SRCROOT path.
050: */
051: private String mSrcroot;
052:
053: /**
054: * Helper class to setup environment for testing.
055: */
056: private EnvironmentSetup mSetup;
057:
058: /**
059: * Instance of the EnvironmentContext class.
060: */
061: private EnvironmentContext mContext;
062:
063: /**
064: * Instance of Component.
065: */
066: private Component mComponent;
067:
068: /**
069: * Instance of ServiceUnitManager.
070: */
071: private ServiceUnitManager mServiceUnitManager;
072:
073: /**
074: * Local instance of the ComponentInstallationContext class.
075: */
076: private ComponentInstallationContext mInstallContext;
077:
078: /**
079: * Instance of the ComponentFramework class.
080: */
081: private ComponentFramework mCompFW;
082:
083: /**
084: * Local instance of the ComponentRegistry class.
085: */
086: private ComponentRegistry mCompReg;
087:
088: /**
089: * Arguments for ServiceUnitOperation.
090: */
091: private Object mArgs[];
092:
093: /**
094: * Local instance of the OperationCounter class.
095: */
096: private OperationCounter mCounter;
097:
098: /**
099: * Local instance of the ServiceUnitOperation class.
100: */
101: private ServiceUnitOperation mOperation;
102:
103: /**
104: * Service Unit root file path.
105: */
106: private String mServiceUnitRoot;
107:
108: /**
109: * Constant for invalid operation value.
110: */
111: private static final int BADOPER = 999;
112:
113: /**
114: * Constant for Service Unit name.
115: */
116: private static final String SERVICE_UNIT_NAME = "TestServiceUnit";
117:
118: /**
119: * Constant for Service Unit root path.
120: */
121: private static final String SERVICE_UNIT_ROOT = "framework/regress/TestServiceUnit.zip";
122:
123: /**
124: * The constructor for this testcase, forwards the test name to
125: * the jUnit TestCase base class.
126: * @param aTestName String with the name of this test.
127: */
128: public TestServiceUnitOperation(String aTestName) {
129: super (aTestName);
130: mTestName = aTestName;
131: }
132:
133: /**
134: * Setup for the test. This creates the EnvironmentContext instance
135: * and other objects needed for the tests.
136: * @throws Exception when set up fails for any reason.
137: */
138: public void setUp() throws Exception {
139: super .setUp();
140: System.err.println("***** START of test " + mTestName);
141: mSrcroot = System.getProperty("junit.srcroot") + "/";
142:
143: // Create and initialize the EnvironmentContext. Create, initialize,
144: // and start up the Component Registry and Component Framework.
145:
146: mSetup = new EnvironmentSetup();
147: mContext = mSetup.getEnvironmentContext();
148: mSetup.startup(true, true);
149: mCompReg = mContext.getComponentRegistry();
150: mCompFW = mContext.getComponentFramework();
151:
152: // Create component class path lists
153:
154: ArrayList bootstrapClassPath = new ArrayList();
155: bootstrapClassPath.add(mSrcroot
156: + Constants.BC_BOOTSTRAP_CLASS_PATH);
157: ArrayList componentClassPath = new ArrayList();
158: componentClassPath.add(Constants.BC_LIFECYCLE_CLASS_PATH);
159:
160: // Create installation context
161:
162: mInstallContext = new ComponentInstallationContext(
163: Constants.BC_NAME,
164: ComponentInstallationContext.BINDING,
165: Constants.BC_LIFECYCLE_CLASS_NAME, componentClassPath,
166: null);
167: mInstallContext.setInstallRoot(mSrcroot);
168: mInstallContext.setIsInstall(true);
169:
170: // Install the component and start it
171:
172: mCompFW.loadBootstrap(mInstallContext,
173: Constants.BC_BOOTSTRAP_CLASS_NAME, bootstrapClassPath,
174: null);
175: mCompFW.installComponent(mInstallContext);
176: mComponent = mCompReg.getComponent(Constants.BC_NAME);
177: mCompFW.startComponent(mComponent);
178:
179: // Get the ServiceUnitManager instance.
180: mServiceUnitManager = mComponent.getServiceUnitManager();
181:
182: // Set up argument list for the ServiceUnitOperation
183: mArgs = new Object[2];
184: mArgs[0] = (Object) SERVICE_UNIT_NAME;
185: mArgs[1] = (Object) mServiceUnitRoot;
186: mCounter = new OperationCounter();
187:
188: // Set the service unit root path
189: mServiceUnitRoot = mSrcroot + SERVICE_UNIT_ROOT;
190: }
191:
192: /**
193: * Cleanup for the test.
194: * @throws Exception when tearDown fails for any reason.
195: */
196: public void tearDown() throws Exception {
197: super .tearDown();
198: mSetup.shutdown(true, true);
199: System.err.println("***** END of test " + mTestName);
200: }
201:
202: // ============================= test methods ================================
203:
204: /**
205: * Tests constructor with bad operation parameter. An exception is expected.
206: * @throws Exception if an unexpected error occurs.
207: */
208: public void testConstructorBadOperation() throws Exception {
209: try {
210: mOperation = new ServiceUnitOperation(mCounter, mComponent
211: .getName(), mServiceUnitManager, BADOPER, mArgs);
212: fail("Expected exception not received");
213: } catch (java.lang.IllegalArgumentException ex) {
214: // Verification
215: assertTrue("Unexpected exception received: "
216: + ex.toString(), (-1 < ex.getMessage().indexOf(
217: "JBIFW0062")));
218: }
219: }
220:
221: /**
222: * Tests constructor with null Service Unit manager parameter. An exception
223: * is expected.
224: * @throws Exception if an unexpected error occurs.
225: */
226: public void testConstructorNullSuManager() throws Exception {
227: try {
228: mOperation = new ServiceUnitOperation(mCounter, mComponent
229: .getName(), null, ServiceUnitOperation.DEPLOY,
230: mArgs);
231: fail("Expected exception not received");
232: } catch (java.lang.IllegalArgumentException ex) {
233: // Verification
234: assertTrue("Unexpected exception received: "
235: + ex.toString(), (-1 < ex.getMessage().indexOf(
236: "JBIFW0063")));
237: }
238: }
239:
240: /**
241: * Tests process method calling deploy on the ServiceUnitManager.
242: * @throws Exception if an unexpected error occurs.
243: * @throws Throwable if an unexpected error occurs.
244: */
245:
246: public void testProcessDeploy() throws Exception, Throwable {
247: mOperation = new ServiceUnitOperation(mCounter, mComponent
248: .getName(), mServiceUnitManager,
249: ServiceUnitOperation.DEPLOY, mArgs);
250: String msg = (String) mOperation.process(mArgs);
251: assertNotNull("Failure deploying Service Unit", msg);
252: assertTrue("Failure deploying Service Unit", (-1 < msg
253: .indexOf(SERVICE_UNIT_NAME)));
254: }
255:
256: /**
257: * Tests process method calling init on the ServiceUnitManager. The test
258: * binding does no special processing, so if no exception occurs, the
259: * test was successful.
260: * @throws Exception if an unexpected error occurs.
261: * @throws Throwable if an unexpected error occurs.
262: */
263:
264: public void testProcessInit() throws Exception, Throwable {
265: mOperation = new ServiceUnitOperation(mCounter, mComponent
266: .getName(), mServiceUnitManager,
267: ServiceUnitOperation.INIT, mArgs);
268: mOperation.process(mArgs);
269: }
270:
271: /**
272: * Tests process method calling start on the ServiceUnitManager. The test
273: * binding does no special processing, so if no exception occurs, the
274: * test was successful.
275: * @throws Exception if an unexpected error occurs.
276: * @throws Throwable if an unexpected error occurs.
277: */
278:
279: public void testProcessStart() throws Exception, Throwable {
280: mOperation = new ServiceUnitOperation(mCounter, mComponent
281: .getName(), mServiceUnitManager,
282: ServiceUnitOperation.START, mArgs);
283: mOperation.process(mArgs);
284: }
285:
286: /**
287: * Tests process method calling stop on the ServiceUnitManager. The test
288: * binding does no special processing, so if no exception occurs, the
289: * test was successful.
290: * @throws Exception if an unexpected error occurs.
291: * @throws Throwable if an unexpected error occurs.
292: */
293:
294: public void testProcessStop() throws Exception, Throwable {
295: mOperation = new ServiceUnitOperation(mCounter, mComponent
296: .getName(), mServiceUnitManager,
297: ServiceUnitOperation.STOP, mArgs);
298: mOperation.process(mArgs);
299: }
300:
301: /**
302: * Tests process method calling shutDown on the ServiceUnitManager. The test
303: * binding does no special processing, so if no exception occurs, the
304: * test was successful.
305: * @throws Exception if an unexpected error occurs.
306: * @throws Throwable if an unexpected error occurs.
307: */
308:
309: public void testProcessShutDown() throws Exception, Throwable {
310: mOperation = new ServiceUnitOperation(mCounter, mComponent
311: .getName(), mServiceUnitManager,
312: ServiceUnitOperation.SHUTDOWN, mArgs);
313: mOperation.process(mArgs);
314: }
315:
316: /**
317: * Tests undeploy with a good result.
318: * @throws Exception if an unexpected error occurs.
319: * @throws Throwable if an unexpected error occurs.
320: */
321:
322: public void testProcessUndeploy() throws Exception, Throwable {
323: mOperation = new ServiceUnitOperation(mCounter, mComponent
324: .getName(), mServiceUnitManager,
325: ServiceUnitOperation.UNDEPLOY, mArgs);
326: String msg = (String) mOperation.process(mArgs);
327: assertNotNull("Failure undeploying Service Unit", msg);
328: assertTrue("Failure undeploying Service Unit", (-1 < msg
329: .indexOf(SERVICE_UNIT_NAME)));
330: }
331:
332: }
|