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: * @(#)TestEnvironmentContext.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.JBIProvider;
032: import com.sun.jbi.platform.PlatformContext;
033: import com.sun.jbi.framework.ScaffoldPlatformContext;
034:
035: import javax.naming.InitialContext;
036:
037: /**
038: * Tests for the EnvironmentContext class.
039: *
040: * @author Sun Microsystems, Inc.
041: */
042: public class TestEnvironmentContext extends junit.framework.TestCase {
043: /**
044: * The EnvironmentContext.
045: */
046: private EnvironmentContext mContext;
047:
048: /**
049: * AS8 install root directory.
050: */
051: private String mAS8InstallRoot;
052:
053: /**
054: * AS8 instance root directory.
055: */
056: private String mAS8InstanceRoot;
057:
058: /**
059: * JBI install root directory.
060: */
061: private String mJbiRoot;
062:
063: /**
064: * JBI instance root directory.
065: */
066: private String mJbiInstanceRoot;
067:
068: /**
069: * MBean server.
070: */
071: private javax.management.MBeanServer mMBeanServer;
072:
073: /**
074: * Initial properties.
075: */
076: private java.util.Properties mProperties;
077:
078: /**
079: * Constant for package name.
080: */
081: private static final String PACKAGE_NAME = "com.sun.jbi.framework";
082:
083: /**
084: * The appserver domain root directory.
085: */
086: private static final String DOMAIN_ROOT = "domains/domain1";
087:
088: /**
089: * The JBI installation root directory.
090: */
091: private static final String JBI_INSTALL_ROOT = "/jbi";
092:
093: /**
094: * The JNDI naming prefix
095: */
096: private static final String NAMING_PREFIX = "jbi";
097:
098: /**
099: * Fake property key.
100: */
101: private static final String PROPERTY_KEY1 = "nerbunge";
102:
103: /**
104: * Fake property value.
105: */
106: private static final String PROPERTY_VALUE1 = "false";
107:
108: /**
109: * Fake property key.
110: */
111: private static final String PROPERTY_KEY2 = "kegwheat";
112:
113: /**
114: * Fake property value.
115: */
116: private static final String PROPERTY_VALUE2 = "4096";
117:
118: /**
119: * Property not found value.
120: */
121: private static final String PROPERTY_NOT_FOUND = "PROPERTY_NOT_FOUND";
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 TestEnvironmentContext(String aTestName) {
129: super (aTestName);
130: }
131:
132: /**
133: * Setup for the test. This creates the data items need to create
134: * the EnvironmentContext in the tests.
135: * @throws Exception when set up fails for any reason.
136: */
137: public void setUp() throws Exception {
138: super .setUp();
139:
140: // Create an MBean server
141:
142: mMBeanServer = javax.management.MBeanServerFactory
143: .createMBeanServer();
144:
145: // Set the AppServer and JBI install roots
146:
147: mAS8InstallRoot = System.getProperty("junit.as8base") + "/";
148: mAS8InstanceRoot = mAS8InstallRoot + DOMAIN_ROOT;
149: mJbiInstanceRoot = mAS8InstanceRoot + JBI_INSTALL_ROOT;
150:
151: // Create the initial properties
152:
153: mProperties = new java.util.Properties();
154:
155: // Create a new instance of the framework with state = ready
156: JBIFramework framework = new JBIFramework();
157: framework.setFrameworkReady();
158:
159: // Create a platform context
160: ScaffoldPlatformContext platform = new ScaffoldPlatformContext();
161: platform.setInstallRoot(mAS8InstallRoot);
162: platform.setInstanceRoot(mAS8InstanceRoot);
163:
164: // Create and initialize the EnvironmentContext.
165: mContext = new EnvironmentContext(platform, framework,
166: mProperties);
167:
168: mContext.setRegistry(new ScaffoldRegistry(mContext));
169: }
170:
171: /**
172: * Cleanup for the test.
173: * @throws Exception when tearDown fails for any reason.
174: */
175: public void tearDown() throws Exception {
176: super .tearDown();
177: }
178:
179: // ========================= test static methods =============================
180:
181: /**
182: * Test the destroyInstance method. This also tests the getInstance()
183: * method for the failure case when the EnvironmentContext has not been
184: * cretaed (or in this case, has been destroyed).
185: * @throws Exception if an unexpected error occurs.
186: */
187: public void testDestroyInstance() throws Exception {
188: try {
189: mContext.destroyInstance();
190: EnvironmentContext.getInstance();
191: fail("Expected exception not received");
192: } catch (java.lang.IllegalStateException ex) {
193: // Verification
194: assertTrue("Unexpected exception received: "
195: + ex.toString(), (-1 < ex.getMessage().indexOf(
196: "not yet been created")));
197: }
198: }
199:
200: /**
201: * Test the get method for the EnvironmentContext singleton instance.
202: * @throws Exception if an unexpected error occurs.
203: */
204: public void testGetInstance() throws Exception {
205: assertSame("Failed to get EnvironmentContext instance: ",
206: mContext, EnvironmentContext.getInstance());
207: }
208:
209: // =================== test framework-internal methods =======================
210:
211: /**
212: * Test the get method for the ComponentFramework.
213: * @throws Exception if an unexpected error occurs.
214: */
215: public void testGetComponentFramework() throws Exception {
216: ComponentFramework cf = mContext.getComponentFramework();
217: assertNotNull("Failure on getComponentFramework(): "
218: + "expected a non-null value, got a null", cf);
219: }
220:
221: /**
222: * Test the get method for the ComponentRegistry.
223: * @throws Exception if an unexpected error occurs.
224: */
225: public void testGetComponentRegistry() throws Exception {
226: ComponentRegistry cr = mContext.getComponentRegistry();
227: assertNotNull("Failure on getComponentRegistry(): "
228: + "expected a non-null value, got a null", cr);
229: }
230:
231: /**
232: * Test the get method for the Logger.
233: * @throws Exception if an unexpected error occurs.
234: */
235: public void testGetLogger() throws Exception {
236: java.util.logging.Logger log = mContext.getLogger();
237: assertNotNull("Failure on getLogger(): "
238: + "expected a non-null value, got a null", log);
239: assertEquals("Failure on getLogger(): ", log.getName(),
240: PACKAGE_NAME);
241: }
242:
243: /**
244: * Test the get method for the top-level Logger.
245: * @throws Exception if an unexpected error occurs.
246: */
247: public void testGetJbiLogger() throws Exception {
248: java.util.logging.Logger log = mContext.getJbiLogger();
249: assertNotNull("Failure on getLogger(): "
250: + "expected a non-null value, got a null", log);
251: assertEquals("Failure on getLogger(): ", log.getName(),
252: "com.sun.jbi");
253: }
254:
255: // ========================= test public methods =============================
256:
257: /**
258: * Test the get method for the AppServer installation root directory.
259: * @throws Exception if an unexpected error occurs.
260: public void testGetAppServerInstallRoot()
261: throws Exception
262: {
263: assertSame("Failure on getAppServerInstallRoot(): ",
264: mAS8Root, mContext.getAppServerInstallRoot());
265: }
266: */
267:
268: /**
269: * Test the get method for the AppServer instance root directory.
270: * @throws Exception if an unexpected error occurs.
271: */
272: public void testGetAppServerInstanceRoot() throws Exception {
273: assertSame("Failure on getAppServerInstanceRoot(): ",
274: mAS8InstanceRoot, mContext.getAppServerInstanceRoot());
275: }
276:
277: /**
278: * Test the get method for the ComponentManager.
279: * @throws Exception if an unexpected error occurs.
280: */
281: public void testGetComponentManager() throws Exception {
282: com.sun.jbi.ComponentManager cm = mContext
283: .getComponentManager();
284: assertNotNull("Failure on getComponentManager(): "
285: + "expected a non-null value, got a null", cm);
286: }
287:
288: /**
289: * Test the get method for the ComponentQuery.
290: * @throws Exception if an unexpected error occurs.
291: */
292: public void testGetComponentQuery() throws Exception {
293: com.sun.jbi.ComponentQuery cq = mContext.getComponentQuery();
294: assertNotNull("Failure on getComponentQuery(): "
295: + "expected a non-null value, got a null", cq);
296: }
297:
298: /**
299: * Test the get method for the ConnectionManager. This is a pass-through
300: * to the NMR so a null value is expected, because the NMR is not active
301: * for this test.
302: * @throws Exception if an unexpected error occurs.
303: */
304: public void testGetConnectionManager() throws Exception {
305: com.sun.jbi.messaging.ConnectionManager cm = mContext
306: .getConnectionManager();
307: assertNull("Failure on getConnectionManager(): "
308: + "expected a null value, got a non-null value", cm);
309: }
310:
311: /**
312: * Test the get method for the default log level.
313: * @throws Exception if an unexpected error occurs.
314: */
315: public void testGetDefaultLogLevel() throws Exception {
316: java.util.logging.Level ll = mContext.getDefaultLogLevel();
317: assertEquals("Failure on getDefaultLogLevel(): ",
318: java.util.logging.Level.INFO, ll);
319: }
320:
321: /**
322: * Test the get method for the initial properties.
323: * @throws Exception if an unexpected error occurs.
324: */
325: public void testGetInitialProperties() throws Exception {
326: assertSame("Failure on getInitialProperties(): ", mProperties,
327: mContext.getInitialProperties());
328: }
329:
330: /**
331: * Test the get and set methods for the JBI installation root directory.
332: * @throws Exception if an unexpected error occurs.
333: */
334: public void testGetSetJbiInstallRoot() throws Exception {
335: mContext.setJbiInstallRoot(mJbiRoot);
336: assertSame("Failure on set/getJbiInstallRoot(): ", mJbiRoot,
337: mContext.getJbiInstallRoot());
338: }
339:
340: /**
341: * Test the get and set methods for the JBI instance root directory.
342: * @throws Exception if an unexpected error occurs.
343: */
344: public void testGetSetJbiInstanceRoot() throws Exception {
345: mContext.setJbiInstanceRoot(mJbiInstanceRoot);
346: assertSame("Failure on set/getJbiInstanceRoot(): ",
347: mJbiInstanceRoot, mContext.getJbiInstanceRoot());
348: }
349:
350: /**
351: * Test the get method for the ManagementClass.
352: * @throws Exception if an unexpected error occurs.
353: */
354: public void testGetManagementClass() throws Exception {
355: assertNotNull(
356: "Failure on getManagementClass(): "
357: + "expected a non-null value, got a null",
358: mContext
359: .getManagementClass(com.sun.jbi.management.MBeanNames.SERVICE_NAME_MESSAGE_SERVICE));
360: }
361:
362: /**
363: * Test the get method for the ManagementMessageFactory.
364: * @throws Exception if an unexpected error occurs.
365: */
366: public void testGetManagementMessageFactory() throws Exception {
367: com.sun.jbi.management.ManagementMessageFactory mmf = mContext
368: .getManagementMessageFactory();
369: assertNotNull("Failure on getManagementMessageFactory(): "
370: + "expected a non-null value, got a null", mmf);
371: }
372:
373: /**
374: * Test the get method for the ManagementService.
375: * @throws Exception if an unexpected error occurs.
376: */
377: public void testGetManagementService() throws Exception {
378: com.sun.jbi.management.system.ManagementService ms = mContext
379: .getManagementService();
380: assertNotNull("Failure on getManagementService(): "
381: + "expected a non-null value, got a null", ms);
382: }
383:
384: /**
385: * Test the get method for the MBeanHelper.
386: * @throws Exception if an unexpected error occurs.
387: */
388: public void testGetMBeanHelper() throws Exception {
389: com.sun.jbi.management.MBeanHelper mbh = mContext
390: .getMBeanHelper();
391: assertNotNull("Failure on getMBeanHelper(): "
392: + "expected a non-null value, got a null", mbh);
393: }
394:
395: /**
396: * Test the get method for the MBeanNames.
397: * @throws Exception if an unexpected error occurs.
398: */
399: public void testGetMBeanNames() throws Exception {
400: javax.jbi.management.MBeanNames mbn = mContext.getMBeanNames();
401: assertNotNull("Failure on getMBeanNames(): "
402: + "expected a non-null value, got a null", mbn);
403: }
404:
405: /**
406: * Test the get and set methods for the JNDI Naming Context.
407: * @throws Exception if an unexpected error occurs.
408: */
409: public void testGetSetNamingContext() throws Exception {
410: javax.naming.InitialContext nc = new javax.naming.InitialContext();
411: mContext.setNamingContext(nc);
412: assertSame("Failure on get/setNamingContext(): ", nc, mContext
413: .getNamingContext());
414: }
415:
416: /**
417: * Test the get and set methods for the JNDI Naming prefix.
418: * @throws Exception if an unexpected error occurs.
419: */
420: public void testGetSetNamingPrefix() throws Exception {
421: mContext.setNamingPrefix(NAMING_PREFIX);
422: assertSame("Failure on get/setNamingPrefix(): ", NAMING_PREFIX,
423: mContext.getNamingPrefix());
424: }
425:
426: /**
427: * Test the get method for the Normalized Message Service.
428: * @throws Exception if an unexpected error occurs.
429: */
430: public void testGetNormalizedMessageService() throws Exception {
431: com.sun.jbi.messaging.MessageService nms = mContext
432: .getNormalizedMessageService();
433: assertNotNull("Failure on getNormalizedMessageService(): "
434: + "expected a non-null value, got a null", nms);
435: }
436:
437: /**
438: * Test the get method for the notification MBean.
439: * @throws Exception if an unexpected error occurs.
440: */
441: public void testGetNotifier() throws Exception {
442: EventNotifierCommon fn = mContext.getNotifier();
443: assertNotNull("Failure on getNotifier(): "
444: + "expected a non-null value, got a null", fn);
445: }
446:
447: /**
448: * Test the get method for the JBI provider type.
449: * @throws Exception if an unexpected error occurs.
450: */
451: public void testGetProvider() throws Exception {
452: JBIProvider provider = mContext.getProvider();
453: assertEquals("Failure on getProvider(): ", JBIProvider.OTHER,
454: provider);
455: }
456:
457: /**
458: * Test the get method for the ServiceUnitRegistration.
459: * @throws Exception if an unexpected error occurs.
460: */
461: public void testGetServiceUnitRegistration() throws Exception {
462: com.sun.jbi.ServiceUnitRegistration sur = mContext
463: .getServiceUnitRegistration();
464: assertNotNull("Failure on getServiceUnitRegistration(): "
465: + "expected a non-null value, got a null", sur);
466: }
467:
468: /**
469: * Test the get method for the StringTranslator for a package name.
470: * @throws Exception if an unexpected error occurs.
471: */
472: public void testGetStringTranslator() throws Exception {
473: com.sun.jbi.StringTranslator st1 = mContext
474: .getStringTranslator(PACKAGE_NAME);
475: com.sun.jbi.StringTranslator st2 = mContext
476: .getStringTranslator(PACKAGE_NAME);
477: com.sun.jbi.StringTranslator st3 = mContext
478: .getStringTranslator("some.other.package");
479:
480: assertNotNull("Failure on getStringTranslator(PACKAGE_NAME): "
481: + "expected a non-null value, got a null", st1);
482: assertNotNull(
483: "Failure on getStringTranslator(some.other.package): "
484: + "expected a non-null value, got a null", st3);
485: assertSame("Received different instances of StringTranslator, "
486: + "should have been the same: ", st1, st2);
487: assertNotSame("Received same instances of StringTranslator, "
488: + "should have been different: ", st1, st3);
489: }
490:
491: /**
492: * Test the get method for the StringTranslator for an object.
493: * @throws Exception if an unexpected error occurs.
494: */
495: public void testGetStringTranslatorFor() throws Exception {
496: com.sun.jbi.StringTranslator st1 = mContext
497: .getStringTranslatorFor(this );
498: com.sun.jbi.StringTranslator st2 = mContext
499: .getStringTranslatorFor(this );
500: com.sun.jbi.StringTranslator st3 = mContext
501: .getStringTranslatorFor(mContext.getManagementService());
502:
503: assertNotNull("Failure on getStringTranslatorFor(this): "
504: + "expected a non-null value, got a null", st1);
505: assertNotNull(
506: "Failure on getStringTranslatorFor(ManagementService): "
507: + "expected a non-null value, got a null", st3);
508: assertSame("Received different instances of StringTranslator, "
509: + "should have been the same: ", st1, st2);
510: assertNotSame("Received same instances of StringTranslator, "
511: + "should have been different: ", st1, st3);
512: }
513:
514: /**
515: * Test the get method for the Tools Runtime Service.
516: * @throws Exception if an unexpected error occurs.
517: *
518: public void testGetToolsRuntimeService()
519: throws Exception
520: {
521: com.sun.jbi.ui.runtime.ToolsRuntimeService trs =
522: mContext.getToolsRuntimeService();
523: assertNotNull("Failure on getToolsRuntimeService(): " +
524: "expected a non-null value, got a null",
525: trs);
526: }*/
527:
528: /**
529: * Test the get method for the Transaction Manager with a missing
530: * PlatformContext.
531: * @throws Exception if an unexpected error occurs.
532: */
533: public void testGetTransactionManagerBad() throws Exception {
534: // Test with a missing PlatformContext. This simulates that no
535: // TransactionManager was available.
536:
537: ScaffoldPlatformContext pc = new ScaffoldPlatformContext();
538: pc
539: .setTransactionManager((javax.transaction.TransactionManager) null);
540: mContext.setPlatformContext(pc);
541:
542: assertNull("Failure on getTransactionManager(): "
543: + "expected a null value, got a non-null", mContext
544: .getTransactionManager());
545: }
546:
547: /**
548: * Test the get method for the Transaction Manager.
549: * @throws Exception if an unexpected error occurs.
550: */
551: public void testGetTransactionManagerGood() throws Exception {
552: // Test with a PlatformContext set up.
553:
554: PlatformContext pc = new ScaffoldPlatformContext();
555: mContext.setPlatformContext(pc);
556:
557: javax.transaction.TransactionManager tm = mContext
558: .getTransactionManager();
559: assertNotNull("Failure on getTransactionManager(): "
560: + "expected a non-null value, got a null", tm);
561: }
562:
563: /**
564: * Test the get method for the Version Info.
565: * @throws Exception if an unexpected error occurs.
566: */
567: public void testGetVersionInfo() throws Exception {
568: com.sun.jbi.VersionInfo vi = mContext.getVersionInfo();
569: assertNotNull("Failure on getVersionInfo(): "
570: + "expected a non-null value, got a null", vi);
571: }
572:
573: /**
574: * Test the get method for the WSDL factory.
575: * @throws Exception if an unexpected error occurs.
576: */
577: public void testGetWsdlFactory() throws Exception {
578: com.sun.jbi.wsdl2.WsdlFactory wf = mContext.getWsdlFactory();
579: assertNotNull("Failure on getWsdlFactory(): "
580: + "expected a non-null value, got a null", wf);
581: }
582:
583: /**
584: * Test getting the read only registry DOM
585: */
586: public void testGetReadOnlyRegistry() throws Exception {
587: mContext.setJbiInstanceRoot(mJbiInstanceRoot);
588: org.w3c.dom.Document regDoc = mContext.getReadOnlyRegistry();
589: assertNotNull(regDoc);
590: }
591: }
|