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: * @(#)TestComponentContext.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.StringTranslator;
032:
033: import java.io.File;
034: import java.util.logging.Logger;
035:
036: import javax.jbi.management.MBeanNames;
037: import javax.jbi.messaging.DeliveryChannel;
038: import javax.management.MBeanServer;
039: import javax.naming.InitialContext;
040:
041: /**
042: * Tests for the ComponentContext class.
043: *
044: * @author Sun Microsystems, Inc.
045: */
046: public class TestComponentContext extends junit.framework.TestCase {
047: /**
048: * EnvironmentContext
049: */
050: private EnvironmentContext mEnvironmentContext;
051:
052: /**
053: * EnvironmentSetup
054: */
055: private EnvironmentSetup mEnvironmentSetup;
056:
057: /**
058: * Component
059: */
060: private Component mComponent;
061:
062: /**
063: * ComponentContext
064: */
065: private ComponentContext mComponentContext;
066:
067: /**
068: * Component install root directory
069: */
070: private String mInstallRoot;
071:
072: /**
073: * Component work root directory
074: */
075: private String mWorkspaceRoot;
076:
077: /**
078: * JBI root directory
079: */
080: private String mJbiRoot;
081:
082: /**
083: * Current test name
084: */
085: private String mTestName;
086:
087: /**
088: * Constant for component name
089: */
090: private static final String COMPONENT_NAME = "TestComponent";
091:
092: /**
093: * Constant for package name
094: */
095: private static final String PACKAGE_NAME = "com.sun.jbi.framework";
096:
097: /**
098: * The constructor for this testcase, forwards the test name to
099: * the jUnit TestCase base class.
100: * @param aTestName String with the name of this test.
101: */
102: public TestComponentContext(String aTestName) {
103: super (aTestName);
104: mTestName = aTestName;
105: }
106:
107: /**
108: * Setup for the test. This creates the ComponentContext instance
109: * and other objects needed for the tests.
110: * @throws Exception when set up fails for any reason.
111: */
112: public void setUp() throws Exception {
113: super .setUp();
114: System.err.println("***** START of test " + mTestName);
115:
116: // Create an EnvironmentContext
117: mEnvironmentSetup = new EnvironmentSetup();
118: mEnvironmentContext = mEnvironmentSetup.getEnvironmentContext();
119:
120: // Set JBI and component roots
121: mJbiRoot = mEnvironmentContext.getJbiInstanceRoot();
122: mInstallRoot = mJbiRoot + "/" + COMPONENT_NAME;
123: mWorkspaceRoot = mJbiRoot + "/" + COMPONENT_NAME + "/"
124: + "workspace";
125:
126: // Create a Component
127: Binding instance = new Binding();
128: mComponent = new Component();
129: mComponent.setName(COMPONENT_NAME);
130: mComponent.setComponentInstance(instance);
131: mComponent.setInstallRoot(mInstallRoot);
132: mComponent.setWorkspaceRoot(mWorkspaceRoot);
133: mComponent.setStatisticsInstance(new ComponentStatistics(
134: COMPONENT_NAME));
135:
136: // Create a ComponentContext
137: mComponentContext = new ComponentContext(mComponent,
138: mEnvironmentContext);
139: }
140:
141: /**
142: * Cleanup for the test.
143: * @throws Exception when tearDown fails for any reason.
144: */
145: public void tearDown() throws Exception {
146: super .tearDown();
147: System.err.println("***** END of test " + mTestName);
148: }
149:
150: // ============================= test methods ================================
151:
152: /**
153: * Test the method for creating a custom component MBean name.
154: * @throws Exception if an unexpected error occurs.
155: */
156: public void testCreateCustomComponentMBeanName() throws Exception {
157: javax.management.ObjectName mbn;
158:
159: mComponent.setComponentTypeBinding();
160: mbn = mComponentContext
161: .createCustomComponentMBeanName("InstallerConfigurationMBean");
162: assertNotNull("Failure on createCustomComponentMBeanName(): "
163: + "expected a non-null value, got a null", mbn);
164: System.out.println("MBean name is " + mbn);
165: mComponent.setComponentTypeEngine();
166: mbn = mComponentContext
167: .createCustomComponentMBeanName("InstallerConfigurationMBean");
168: assertNotNull("Failure on createCustomComponentMBeanName(): "
169: + "expected a non-null value, got a null", mbn);
170: System.out.println("MBean name is " + mbn);
171: }
172:
173: /**
174: * Test the get method for the delivery channel.
175: * @throws Exception if an unexpected error occurs.
176: */
177: public void testGetDeliveryChannel() throws Exception {
178: DeliveryChannel dc1 = mComponentContext
179: .getDeliveryChannel(false);
180: DeliveryChannel dc2 = mComponentContext
181: .getDeliveryChannel(true);
182: DeliveryChannel dc3 = mComponentContext
183: .getDeliveryChannel(false);
184: DeliveryChannel dc4 = mComponentContext.getDeliveryChannel();
185: assertNull("Failure on getDeliveryChannel(false): "
186: + "expected a null, got a non-null value", dc1);
187: assertNotNull("Failure on getDeliveryChannel(true): "
188: + "expected a non-null value, got a null", dc2);
189: assertSame("Failure on getDeliveryChannel(false): ", dc3, dc2);
190: assertSame("Failure on getDeliveryChannel(): ", dc4, dc2);
191: }
192:
193: /**
194: * Test the get method for the component name.
195: * @throws Exception if an unexpected error occurs.
196: */
197: public void testGetComponentName() throws Exception {
198: String name = mComponentContext.getComponentName();
199: assertEquals("Failure on getComponentName(): ", name,
200: COMPONENT_NAME);
201: }
202:
203: /**
204: * Test the get method for the component install root directory.
205: * @throws Exception if an unexpected error occurs.
206: */
207: public void testGetInstallRoot() throws Exception {
208: String ir = mComponentContext.getInstallRoot();
209: assertEquals("Failure on getInstallRoot(): ", mInstallRoot
210: .replace('/', File.separatorChar), ir);
211: }
212:
213: /**
214: * Test the get method for the JMX domain name.
215: * @throws Exception if an unexpected error occurs.
216: */
217: public void testGetJmxDomainName() throws Exception {
218: String dn = mComponentContext.getJmxDomainName();
219: assertEquals("Failure on getJmxDomainName(): ", dn,
220: mEnvironmentContext.getMBeanNames().getJmxDomainName());
221: }
222:
223: /**
224: * Test the get method for component loggers.
225: * @throws Exception if an unexpected error occurs.
226: */
227: public void testGetLogger() throws Exception {
228: // Get a main logger with no name suffix and no resource bundle.
229:
230: Logger l1 = mComponentContext.getLogger("", null);
231: assertTrue("Main logger has wrong name: " + l1.getName(), l1
232: .getName().equals(COMPONENT_NAME));
233:
234: // Get a secondary logger with the suffix "secondary" and no resource
235: // bundle.
236:
237: String suffix = "secondary";
238: Logger l2 = mComponentContext.getLogger(suffix, null);
239: assertTrue("Secondary logger has wrong name: " + l2.getName(),
240: l2.getName().startsWith(COMPONENT_NAME));
241: assertTrue("Secondary logger has wrong name: " + l2.getName(),
242: l2.getName().endsWith(suffix));
243:
244: // Make sure that a repeated call for the same logger returns the
245: // same Logger instance.
246:
247: Logger l3 = mComponentContext.getLogger(suffix, null);
248: assertSame("Duplicate logger created: " + l3.getName(), l2, l3);
249:
250: // Get a secondary logger with suffix "com.sun.jbi.TestBinding"
251: // and no resource bundle.
252:
253: suffix = "com.sun.jbi.TestBinding";
254: Logger l4 = mComponentContext.getLogger(suffix, null);
255: assertTrue("Secondary logger has wrong name: " + l4.getName(),
256: l4.getName().startsWith(COMPONENT_NAME));
257: assertTrue("Secondary logger has wrong name: " + l4.getName(),
258: l4.getName().endsWith(suffix));
259: }
260:
261: /**
262: * Test the get method for component loggers with a default main logger.
263: * @throws Exception if an unexpected error occurs.
264: */
265: public void testGetLoggerDefaultMain() throws Exception {
266: // Get a secondary logger with the suffix "secondary" and no resource
267: // bundle. This will also create the main logger by default.
268:
269: String suffix = "secondary";
270: Logger l1 = mComponentContext.getLogger(suffix, null);
271: assertTrue("Secondary logger has wrong name: " + l1.getName(),
272: l1.getName().startsWith(COMPONENT_NAME));
273: assertTrue("Secondary logger has wrong name: " + l1.getName(),
274: l1.getName().endsWith(suffix));
275:
276: Logger l2 = l1.getParent();
277: assertNotNull("Main logger not created automatically: ", l2);
278: assertTrue("Main logger has wrong name: ", l2.getName().equals(
279: COMPONENT_NAME));
280:
281: // Get the main logger and verify that it is the one created by the
282: // first call to getLogger().
283:
284: Logger l3 = mComponentContext.getLogger("", null);
285: assertSame("Duplicate main logger created: " + l3.getName(),
286: l2, l3);
287: }
288:
289: /**
290: * Test the get method for component loggers and make sure the parents
291: * are set properly.
292: * @throws Exception if an unexpected error occurs.
293: */
294: public void testGetLoggerCheckParent() throws Exception {
295: Logger jbi = Logger.getLogger("com.sun.jbi");
296:
297: // Get a main logger with no name suffix and no resource bundle and
298: // make sure its parent is set to the top-level JBI logger.
299:
300: Logger l1 = mComponentContext.getLogger("", null);
301: assertSame("Main logger has wrong parent: ", l1.getParent(),
302: jbi);
303:
304: // Get a sub-logger with no resource bundle and make sure its parent
305: // is set to the main logger.
306:
307: Logger l2 = mComponentContext.getLogger("another", null);
308: assertSame("Sub-logger has wrong parent: ", l2.getParent(), l1);
309: }
310:
311: /**
312: * Test the get method for component loggers with resource bundles.
313: * @throws Exception if an unexpected error occurs.
314: */
315: public void testGetLoggerWithResourceBundle() throws Exception {
316: // Create resource bundle for loggers.
317: String rbName = this .getClass().getPackage().getName()
318: + ".TestBundle1";
319:
320: // Get a main logger with no name suffix and a resource bundle.
321:
322: Logger l1 = mComponentContext.getLogger("", rbName);
323: assertTrue("Failure on getLogger(null, rbName): ", l1.getName()
324: .startsWith(COMPONENT_NAME));
325: assertEquals("Failure on getLogger(null, rbName): ", l1
326: .getResourceBundleName(), rbName);
327:
328: // Get a secondary logger with the suffix "secondary" and a resource
329: // bundle.
330:
331: String suffix = "secondary";
332: Logger l2 = mComponentContext.getLogger(suffix, rbName);
333: assertTrue("Failure on getLogger(suffix, rbName): ", l2
334: .getName().startsWith(COMPONENT_NAME));
335: assertTrue("Failure on getLogger(suffix, rbName): ", l2
336: .getName().endsWith(suffix));
337: assertEquals("Failure on getLogger(suffix, rbName): ", l2
338: .getResourceBundleName(), rbName);
339: }
340:
341: /**
342: * Test the getLogger method with a failure due to an invalid "suffix"
343: * argument.
344: * @throws Exception if an unexpected error occurs.
345: */
346: public void testGetLoggerBadNullSuffix() throws Exception {
347: try {
348: Logger l1 = mComponentContext.getLogger(null, null);
349: fail("Expected exception not received");
350: } catch (java.lang.IllegalArgumentException ex) {
351: // Verification
352: assertTrue(
353: "Incorrect exception received: " + ex.toString(),
354: (ex.getMessage().startsWith("JBIFW1356")));
355: }
356: }
357:
358: /**
359: * Test the getLogger method with a failure due to a missing resource
360: * bundle. An exception is expected.
361: * @throws Exception if an unexpected error occurs.
362: */
363: public void testGetLoggerBadMissingResource() throws Exception {
364: // Resource bundle name for loggers.
365: String rbName = this .getClass().getPackage().getName()
366: + ".BadBundle1";
367:
368: // Note: due to the behavior of the Logger class, this test has to
369: // create its logger with a different name from the previous test
370: // to avoid an IllegalArgumentException.
371:
372: String suffix = "missingresource";
373: try {
374: Logger l1 = mComponentContext.getLogger(suffix, rbName);
375: fail("Expected exception not received");
376: } catch (java.util.MissingResourceException ex) {
377: // Verification
378: assertTrue(
379: "Incorrect exception received: " + ex.toString(),
380: (-1 < ex.getMessage().indexOf(rbName)));
381: }
382: }
383:
384: /**
385: * Test the getLogger method with a failure due to an existing logger
386: * using a different resource bundle. An exception is expected.
387: * @throws Exception if an unexpected error occurs.
388: */
389: public void testGetLoggerBadDifferentResource() throws Exception {
390: // Resource bundle names for loggers.
391: String rbName1 = this .getClass().getPackage().getName()
392: + ".TestBundle1";
393: String rbName2 = this .getClass().getPackage().getName()
394: + ".TestBundle2";
395:
396: // Note: due to the behavior of the Logger class, this test has to
397: // create its logger with a different name from the previous test
398: // to avoid an IllegalArgumentException.
399:
400: String suffix = "differentresource";
401: Logger l1 = mComponentContext.getLogger("", rbName1);
402: try {
403: Logger l2 = mComponentContext.getLogger("", rbName2);
404: fail("Expected exception not received");
405: } catch (javax.jbi.JBIException ex) {
406: // Verification
407: assertTrue(
408: "Incorrect exception received: " + ex.toString(),
409: ((-1 < ex.getMessage().indexOf(rbName1)) && (-1 < ex
410: .getMessage().indexOf(rbName2))));
411: }
412: }
413:
414: /**
415: * Test the get method for the MBeanNames service.
416: * @throws Exception if an unexpected error occurs.
417: */
418: public void testGetMBeanNames() throws Exception {
419: MBeanNames mbn = mComponentContext.getMBeanNames();
420: assertNotNull("Failure on getMBeanNames(): "
421: + "expected a non-null value, got a null", mbn);
422: assertTrue("Failure on getMBeanNames(): "
423: + "expected a javax.jbi.management.MBeanNames "
424: + "instance, got a " + mbn.getClass().getName(),
425: mbn instanceof javax.jbi.management.MBeanNames);
426: }
427:
428: /**
429: * Test the get method for the MBean server.
430: * @throws Exception if an unexpected error occurs.
431: */
432: public void testGetMBeanServer() throws Exception {
433: MBeanServer mbs = mComponentContext.getMBeanServer();
434: assertNotNull("Failure on getMBeanServer(): "
435: + "expected a non-null value, got a null", mbs);
436: }
437:
438: /**
439: * Test the get method for the naming context.
440: * @throws Exception if an unexpected error occurs.
441: */
442: public void testGetNamingContext() throws Exception {
443: InitialContext nc = mComponentContext.getNamingContext();
444: assertNotNull("Failure on getNamingContext(): "
445: + "expected a non-null value, got a null", nc);
446: }
447:
448: /**
449: * Test the get method for the StringTranslator for a package name.
450: * @throws Exception if an unexpected error occurs.
451: */
452: public void testGetStringTranslator() throws Exception {
453: StringTranslator st1 = mComponentContext
454: .getStringTranslator(PACKAGE_NAME);
455: StringTranslator st2 = mComponentContext
456: .getStringTranslator(PACKAGE_NAME);
457: assertNotNull("Failure on getStringTranslator(PACKAGE_NAME): "
458: + "expected a non-null value, got a null", st1);
459: assertSame(
460: "Received different instances of StringTranslator: ",
461: st1, st2);
462: }
463:
464: /**
465: * Test the get method for the StringTranslator for an object.
466: * @throws Exception if an unexpected error occurs.
467: */
468: public void testGetStringTranslatorFor() throws Exception {
469: StringTranslator st1 = mComponentContext
470: .getStringTranslatorFor(this );
471: StringTranslator st2 = mComponentContext
472: .getStringTranslatorFor(this );
473: assertNotNull("Failure on getStringTranslatorFor(this): "
474: + "expected a non-null value, got a null", st1);
475: assertSame(
476: "Received different instances of StringTranslator: ",
477: st1, st2);
478: }
479:
480: /**
481: * Test the get method for the component work root directory.
482: * @throws Exception if an unexpected error occurs.
483: */
484: public void testGetWorkspaceRoot() throws Exception {
485: String wr = mComponentContext.getWorkspaceRoot();
486: assertEquals("Failure on getWorkspaceRoot(): ", mWorkspaceRoot
487: .replace('/', File.separatorChar), wr);
488: }
489:
490: }
|