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: * @(#)TestServiceUnit.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.framework.ScaffoldPlatformContext;
032: import java.io.ByteArrayInputStream;
033: import java.util.Properties;
034:
035: import com.sun.jbi.JBIProvider;
036: import com.sun.jbi.ServiceUnitState;
037:
038: /**
039: * Tests the ServiceUnit class.
040: *
041: * @author Sun Microsystems, Inc.
042: */
043: public class TestServiceUnit extends junit.framework.TestCase {
044: /**
045: * Local instance of the EnvironmentContext
046: */
047: private EnvironmentContext mContext;
048:
049: /**
050: * Local instance of the ServiceUnit class
051: */
052: private ServiceUnit mSU;
053:
054: /**
055: * Constant for bad state.
056: */
057: static final int BAD_STATE = 409;
058:
059: /**
060: * Constant for Component name
061: */
062: static final String COMP_NAME = "ElroyJetson";
063:
064: /**
065: * Constant for Service Assembly name
066: */
067: static final String SA_NAME = "GeorgeJetson";
068:
069: /**
070: * Constant for Service Unit name
071: */
072: static final String SU_NAME = "SpacelySprockets";
073:
074: /**
075: * Constant for Service Unit file path
076: */
077: static final String SU_PATH = "/as8/domains/JBIdomain/jbi/system/deployment/SpacelySprockets";
078:
079: /**
080: * The constructor for this testcase, forwards the test name to
081: * the jUnit TestCase base class.
082: * @param aTestName String with the name of this test.
083: */
084: public TestServiceUnit(String aTestName) {
085: super (aTestName);
086: }
087:
088: /**
089: * Setup for the test. This creates the ServiceUnit instances
090: * and other objects needed for the tests. It also serves to test
091: * the constructor for the class.
092: * @throws Exception when set up fails for any reason.
093: */
094: public void setUp() throws Exception {
095: super .setUp();
096: mContext = new EnvironmentContext(
097: new ScaffoldPlatformContext(), new JBIFramework(),
098: new Properties());
099:
100: // Create a ServiceUnit with a single-element class path
101:
102: mSU = new ServiceUnit(SA_NAME, SU_NAME, SU_PATH);
103: }
104:
105: /**
106: * Cleanup for the test.
107: * @throws Exception when tearDown fails for any reason.
108: */
109: public void tearDown() throws Exception {
110: super .tearDown();
111: }
112:
113: // ============================= test methods ================================
114:
115: /**
116: * Tests getEquals with good results.
117: * @throws Exception if an unexpected error occurs.
118: */
119: public void testEquals() throws Exception {
120: ServiceUnit su;
121:
122: // Null object:
123: assertFalse("Got wrong result with null: ", mSU.equals(null));
124:
125: // Different object types:
126: assertFalse("Got wrong result different object type: ", mSU
127: .equals(this ));
128:
129: // Different SA name values:
130: su = new ServiceUnit("dork", SU_NAME, SU_PATH);
131: assertFalse("Got wrong result with different SA names: ", mSU
132: .equals(su));
133:
134: // Different name values:
135: su = new ServiceUnit(SA_NAME, "nerd", SU_PATH);
136: assertFalse("Got wrong result with different names: ", mSU
137: .equals(su));
138:
139: // Different file path values:
140: su = new ServiceUnit(SA_NAME, SU_NAME, "geek");
141: assertFalse("Got wrong result with different file paths: ", mSU
142: .equals(su));
143:
144: // Equal:
145: su = new ServiceUnit(SA_NAME, SU_NAME, SU_PATH);
146: assertTrue("Got wrong result with identical objects: ", mSU
147: .equals(su));
148:
149: // Equal with different state values:
150: su = new ServiceUnit(SA_NAME, SU_NAME, SU_PATH);
151: su.setState(ServiceUnitState.STARTED);
152: assertTrue("Got wrong result with different states: ", mSU
153: .equals(su));
154:
155: // Equal with different desired state values:
156: su = new ServiceUnit(SA_NAME, SU_NAME, SU_PATH);
157: su.setDesiredState(ServiceUnitState.STARTED);
158: assertTrue(
159: "Got wrong result with different states at shutdown: ",
160: mSU.equals(su));
161: }
162:
163: /**
164: * Tests getName with good results.
165: * @throws Exception if an unexpected error occurs.
166: */
167: public void testGetName() throws Exception {
168: String name = mSU.getName();
169:
170: assertEquals("Got incorrect name: ", SU_NAME, name);
171: }
172:
173: /**
174: * Tests getFilePath with good results.
175: * @throws Exception if an unexpected error occurs.
176: */
177: public void testGetFilePath() throws Exception {
178: String path = mSU.getFilePath();
179:
180: assertEquals("Got incorrect file path: ", SU_PATH, path);
181: }
182:
183: /**
184: * Tests getServiceAssemblyName with good results.
185: * @throws Exception if an unexpected error occurs.
186: */
187: public void testGetServiceAssemblyName() throws Exception {
188: String name = mSU.getServiceAssemblyName();
189:
190: assertEquals("Got incorrect name: ", SA_NAME, name);
191: }
192:
193: /**
194: * Tests getState and setState with good results.
195: * @throws Exception if an unexpected error occurs.
196: */
197: public void testGetSetState() throws Exception {
198: mSU.setState(ServiceUnitState.SHUTDOWN);
199: assertEquals("Got wrong result with SHUTDOWN state: ",
200: ServiceUnitState.SHUTDOWN, mSU.getState());
201:
202: mSU.setState(ServiceUnitState.STOPPED);
203: assertEquals("Got wrong result with STOPPED state: ",
204: ServiceUnitState.STOPPED, mSU.getState());
205:
206: mSU.setState(ServiceUnitState.STARTED);
207: assertEquals("Got wrong result with STARTED state: ",
208: ServiceUnitState.STARTED, mSU.getState());
209: }
210:
211: /**
212: * Tests getStateAsString.
213: * @throws Exception if an unexpected error occurs.
214: */
215: public void testGetStateAsString() throws Exception {
216: mSU.setState(ServiceUnitState.SHUTDOWN);
217: assertEquals("Got wrong result with SHUTDOWN state: ",
218: ServiceUnitState.SHUTDOWN.toString(), mSU
219: .getStateAsString());
220:
221: mSU.setState(ServiceUnitState.STOPPED);
222: assertEquals("Got wrong result with STOPPED state: ",
223: ServiceUnitState.STOPPED.toString(), mSU
224: .getStateAsString());
225:
226: mSU.setState(ServiceUnitState.STARTED);
227: assertEquals("Got wrong result with STARTED state: ",
228: ServiceUnitState.STARTED.toString(), mSU
229: .getStateAsString());
230:
231: mSU.setState(ServiceUnitState.UNKNOWN);
232: assertEquals("Got wrong result with invalid state: ",
233: ServiceUnitState.UNKNOWN.toString(), mSU
234: .getStateAsString());
235: }
236:
237: /**
238: * Tests getDesiredState and setDesiredState with good results.
239: * @throws Exception if an unexpected error occurs.
240: */
241: public void testGetSetDesiredState() throws Exception {
242: mSU.setDesiredState(ServiceUnitState.SHUTDOWN);
243: assertEquals("Got wrong result with SHUTDOWN state: ",
244: ServiceUnitState.SHUTDOWN, mSU.getDesiredState());
245:
246: mSU.setDesiredState(ServiceUnitState.STOPPED);
247: assertEquals("Got wrong result with STOPPED state: ",
248: ServiceUnitState.STOPPED, mSU.getDesiredState());
249:
250: mSU.setDesiredState(ServiceUnitState.STARTED);
251: assertEquals("Got wrong result with STARTED state: ",
252: ServiceUnitState.STARTED, mSU.getDesiredState());
253: }
254:
255: /**
256: * Tests get/setTargetComponent with good results.
257: * @throws Exception if an unexpected error occurs.
258: */
259: public void testGetSetTargetComponent() throws Exception {
260: mSU.setTargetComponent(COMP_NAME);
261: assertEquals("Got incorrect Target Component: ", COMP_NAME, mSU
262: .getTargetComponent());
263: }
264:
265: /**
266: * Tests hashCode() with good results.
267: * @throws Exception if an unexpected error occurs.
268: */
269: public void testHashCode() throws Exception {
270: ServiceUnit su = new ServiceUnit("This one is", "different",
271: "/from/the/other/one");
272:
273: assertEquals("Got wrong result with same objects: ", mSU
274: .hashCode(), mSU.hashCode());
275: assertFalse("Got wrong result with different objects: "
276: + "\nmSU.hashCode() returned "
277: + new Integer(mSU.hashCode())
278: + "\nsu.hashCode() returned "
279: + new Integer(su.hashCode()), (mSU.hashCode() == su
280: .hashCode()));
281: }
282:
283: /**
284: * Tests isShutdown() with good results.
285: * @throws Exception if an unexpected error occurs.
286: */
287: public void testIsShutdown() throws Exception {
288: mSU.setState(ServiceUnitState.SHUTDOWN);
289: assertTrue("Got wrong result with SHUTDOWN state: ", mSU
290: .isShutdown());
291: mSU.setState(ServiceUnitState.STOPPED);
292: assertFalse("Got wrong result with STOPPED state: ", mSU
293: .isShutdown());
294: mSU.setState(ServiceUnitState.STARTED);
295: assertFalse("Got wrong result with STARTED state: ", mSU
296: .isShutdown());
297: }
298:
299: /**
300: * Tests isStarted() with good results.
301: * @throws Exception if an unexpected error occurs.
302: */
303: public void testIsStarted() throws Exception {
304: mSU.setState(ServiceUnitState.SHUTDOWN);
305: assertFalse("Got wrong result with SHUTDOWN state: ", mSU
306: .isStarted());
307: mSU.setState(ServiceUnitState.STOPPED);
308: assertFalse("Got wrong result with STOPPED state: ", mSU
309: .isStarted());
310: mSU.setState(ServiceUnitState.STARTED);
311: assertTrue("Got wrong result with STARTED state: ", mSU
312: .isStarted());
313: }
314:
315: /**
316: * Tests isStopped() with good results.
317: * @throws Exception if an unexpected error occurs.
318: */
319: public void testIsStopped() throws Exception {
320: mSU.setState(ServiceUnitState.SHUTDOWN);
321: assertFalse("Got wrong result with SHUTDOWN state: ", mSU
322: .isStopped());
323: mSU.setState(ServiceUnitState.STOPPED);
324: assertTrue("Got wrong result with STOPPED state: ", mSU
325: .isStopped());
326: mSU.setState(ServiceUnitState.STARTED);
327: assertFalse("Got wrong result with STARTED state: ", mSU
328: .isStopped());
329: }
330:
331: /**
332: * Tests setShutdown with good results.
333: * @throws Exception if an unexpected error occurs.
334: */
335: public void testSetShutdown() throws Exception {
336: // Set STOPPED service unit to SHUTDOWN. This is allowed.
337: mSU.setState(ServiceUnitState.STOPPED);
338: mSU.setShutdown();
339: assertEquals("Failure in setShutdown from STOPPED state: ",
340: ServiceUnitState.SHUTDOWN, mSU.getState());
341:
342: // Set STARTED service unit to SHUTDOWN. This is not allowed.
343: try {
344: mSU.setState(ServiceUnitState.STARTED);
345: mSU.setShutdown();
346: fail("Expected exception not received on setShutdown() from "
347: + "STARTED state");
348: } catch (java.lang.IllegalStateException ex) {
349: // Verification
350: assertTrue(
351: "Incorrect exception received: " + ex.toString(),
352: (-1 < ex.getMessage()
353: .indexOf("state cannot change")));
354: }
355: }
356:
357: /**
358: * Tests setStarted with good results.
359: * @throws Exception if an unexpected error occurs.
360: */
361: public void testSetStarted() throws Exception {
362: // Set STOPPED service unit to STARTED. This is allowed.
363: mSU.setState(ServiceUnitState.STOPPED);
364: mSU.setStarted();
365: assertEquals("Failure in setStarted from STOPPED state: ",
366: ServiceUnitState.STARTED, mSU.getState());
367:
368: // Set SHUTDOWN service unit to STARTED. This is not allowed.
369: try {
370: mSU.setState(ServiceUnitState.SHUTDOWN);
371: mSU.setStarted();
372: fail("Expected exception not received on setStarted() from "
373: + "SHUTDOWN state");
374: } catch (java.lang.IllegalStateException ex) {
375: // Verification
376: assertTrue(
377: "Incorrect exception received: " + ex.toString(),
378: (-1 < ex.getMessage()
379: .indexOf("state cannot change")));
380: }
381: }
382:
383: /**
384: * Tests setStopped with good results.
385: * @throws Exception if an unexpected error occurs.
386: */
387: public void testSetStopped() throws Exception {
388: // Set SHUTDOWN service unit to STOPPED. This is allowed.
389: mSU.setState(ServiceUnitState.SHUTDOWN);
390: mSU.setStopped();
391: assertEquals("Failure in setStopped from SHUTDOWN state: ",
392: ServiceUnitState.STOPPED, mSU.getState());
393:
394: // Set STARTED service unit to STOPPED. This is allowed.
395: mSU.setState(ServiceUnitState.STARTED);
396: mSU.setStopped();
397: assertEquals("Failure in setStopped from STARTED state: ",
398: ServiceUnitState.STOPPED, mSU.getState());
399: }
400:
401: /**
402: * Tests toString with good results.
403: * @throws Exception if an unexpected error occurs.
404: */
405: public void testToString() throws Exception {
406: // Identical instances should produce identical output:
407: ServiceUnit su = new ServiceUnit(SA_NAME, SU_NAME, SU_PATH);
408: assertEquals("Failure in toString with equal objects: ", mSU
409: .toString(), su.toString());
410:
411: // Different instances should produce different output:
412: su.setState(ServiceUnitState.STARTED);
413: assertFalse("Failure in toString with different objects: "
414: + "\nmSU.toString() returned " + mSU.toString()
415: + "\nsu.toString() returned " + su.toString(), mSU
416: .toString().equals(su.toString()));
417: }
418: }
|