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: * @(#)TestXmlReader.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.system;
030:
031: import java.io.File;
032:
033: import com.sun.jbi.management.registry.Registry;
034: import com.sun.jbi.management.registry.RegistryBuilder;
035: import com.sun.jbi.management.registry.Updater;
036: import com.sun.jbi.management.repository.Repository;
037: import com.sun.jbi.management.repository.ArchiveType;
038: import com.sun.jbi.management.repository.Archive;
039: import com.sun.jbi.management.system.ManagementContext;
040: import com.sun.jbi.management.system.DeploymentService;
041: import com.sun.jbi.management.system.Util;
042: import com.sun.jbi.management.system.ScaffoldEnvironmentContext;
043:
044: /**
045: * Tests some methods in DeploymentService
046: * @author Sun Microsystems, Inc.
047: */
048:
049: public class TestDeploymentService extends junit.framework.TestCase {
050:
051: private DeploymentService mDeploymentSvc;
052: private Repository mRepository;
053: private ManagementContext mCtx;
054: private String mJbiRoot;
055: private File mRegFile;
056: private File mRegBkupFile;
057:
058: private static final String SERVICE_ASSEMBLY_ARCHIVE = "service-assembly.zip";
059:
060: private static final String SERVICE_ASSEMBLY_ARCHIVE_PATH = "/test-classes/testdata/"
061: + SERVICE_ASSEMBLY_ARCHIVE;
062:
063: private static final String JBI_ROOT_PATH = "/bld";
064:
065: private Registry mRegistry;
066:
067: /**
068: * The constructor for this testcase, forwards the test name to
069: * the jUnit TestCase base class.
070: * @param aTestName String with the name of this test.
071: */
072: public TestDeploymentService(String aTestName) throws Exception {
073: super (aTestName);
074: String srcroot = System.getProperty("junit.srcroot");
075: String manage = "/runtime/manage"; // open-esb build
076: String mConfigDir = srcroot + manage
077: + "/bld/test-classes/testdata/";
078: String mRegFilePath = mConfigDir + File.separator
079: + "jbi-registry.xml";
080: String mRegBkupFilePath = mConfigDir + File.separator
081: + "jbi-registry-backup.xml";
082: mRegFile = new File(mRegFilePath);
083: mRegBkupFile = new File(mRegBkupFilePath);
084: mJbiRoot = srcroot + manage + JBI_ROOT_PATH;
085: ScaffoldEnvironmentContext envCtx = new ScaffoldEnvironmentContext();
086:
087: mRegistry = Util.createRegistry();
088: mRepository = new Repository(mCtx);
089:
090: envCtx.setJbiInstanceRoot(mJbiRoot);
091: envCtx.setAppServerInstanceRoot(mJbiRoot);
092: envCtx.setProperty("com.sun.jbi.home", System
093: .getProperty("junit.as8base")
094: + "/jbi");
095: envCtx.setJbiInstallRoot(System.getProperty("junit.as8base")
096: + "/jbi");
097: envCtx.setRegistry(mRegistry);
098: com.sun.jbi.util.EnvironmentAccess.setContext(envCtx);
099:
100: mCtx = new ManagementContext(envCtx);
101: mDeploymentSvc = new DeploymentService(mCtx);
102:
103: }
104:
105: /**
106: * Test entity resolver.
107: * @throws Exception if an unexpected error occurs
108: */
109: public void testGetServiceAssemblyDescriptor() throws Exception {
110:
111: try {
112:
113: Updater updater = mRegistry.getUpdater();
114: updater.addServiceAssembly("CompositeApplication",
115: "service-assembly.zip", java.util.Calendar
116: .getInstance());
117:
118: String path = mJbiRoot + SERVICE_ASSEMBLY_ARCHIVE_PATH;
119: Archive archive = mRepository.addArchive(
120: ArchiveType.SERVICE_ASSEMBLY, path);
121:
122: String descriptor = mDeploymentSvc
123: .getServiceAssemblyDescriptor("CompositeApplication");
124:
125: assertTrue(descriptor
126: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?"));
127:
128: } catch (Exception ex) {
129: ex.printStackTrace();
130: }
131:
132: }
133:
134: /**
135: * Test the code that creates a component-task-result from a plain-text exception message
136: */
137: public void testGetComponentResultXmlFragment() throws Exception {
138: Class clazz = mDeploymentSvc.getClass();
139:
140: java.lang.reflect.Method mtd = clazz.getDeclaredMethod(
141: "getComponentResultXmlFragment", new Class[] {
142: java.lang.String.class, java.lang.String.class,
143: java.lang.Exception.class });
144: mtd.setAccessible(true);
145: Object elem = mtd.invoke(mDeploymentSvc, new Object[] {
146: "myTask", "myComponent",
147: new Exception("My exception message.") });
148: assertNotNull(elem);
149: org.w3c.dom.Element element = (org.w3c.dom.Element) elem;
150: System.out.println(element.getTagName());
151: assertTrue("component-task-result".equals(element.getTagName()));
152:
153: }
154:
155: /**
156: * Test fix for Issue 52 : https://open-esb.dev.java.net/issues/show_bug.cgi?id=52
157: * An exception with a message string less than 9 chars should not result in a NPE.
158: */
159: public void testBuildComponentExMsg() throws Exception {
160: Class clazz = mDeploymentSvc.getClass();
161:
162: java.lang.reflect.Method mtd = clazz.getDeclaredMethod(
163: "buildComponentExMsg", new Class[] {
164: java.lang.String.class, java.lang.String.class,
165: java.lang.Exception.class });
166: mtd.setAccessible(true);
167: Object elem = mtd.invoke(mDeploymentSvc, new Object[] {
168: "myTask", "myComponent", new Exception("null") });
169: assertNotNull(elem);
170: org.w3c.dom.Element element = (org.w3c.dom.Element) elem;
171: System.out.println(element.getTagName());
172: assertTrue("component-task-result".equals(element.getTagName()));
173: }
174:
175: /**
176: * Test fix for Issue 52 : https://open-esb.dev.java.net/issues/show_bug.cgi?id=52
177: * A message string less than 9 chars should not result in a exception.
178: */
179: public void testBuildFrmwkInfoMsg() throws Exception {
180: Class clazz = mDeploymentSvc.getClass();
181:
182: java.lang.reflect.Method mtd = clazz.getDeclaredMethod(
183: "buildFrmwkInfoMsg",
184: new Class[] { java.lang.String.class });
185: mtd.setAccessible(true);
186: Object elem = mtd
187: .invoke(mDeploymentSvc, new Object[] { "abc" });
188: assertNotNull(elem);
189: org.w3c.dom.Element element = (org.w3c.dom.Element) elem;
190: System.out.println(element.getTagName());
191: org.w3c.dom.NodeList list = element.getChildNodes();
192: assertTrue(list.getLength() > 0);
193: assertEquals("msg-loc-info", list.item(0).getNodeName());
194: }
195:
196: public void setUp() throws Exception {
197: super .setUp();
198: if (mRegFile.exists()) {
199: mRegFile.delete();
200: }
201: if (mRegBkupFile.exists()) {
202: mRegBkupFile.delete();
203: }
204: }
205:
206: public void tearDown() throws Exception {
207: // -- restore registry.xml
208: RegistryBuilder.destroyRegistry();
209: }
210:
211: }
|