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 org.xml.sax.EntityResolver;
032:
033: import com.sun.jbi.management.internal.support.XmlReader;
034:
035: /**
036: * Tests Xml Parser/validation.
037: * @author Sun Microsystems, Inc.
038: */
039:
040: public class TestXmlReader extends junit.framework.TestCase {
041:
042: /**
043: * one sample jbi.xml filename
044: */
045: private String mConfig1 = null;
046:
047: /**
048: * another sample jbi.xml filename
049: */
050: private String mConfig2 = null;
051:
052: /**
053: * The constructor for this testcase, forwards the test name to
054: * the jUnit TestCase base class.
055: * @param aTestName String with the name of this test.
056: */
057: public TestXmlReader(String aTestName) {
058: super (aTestName);
059: String testPath = System.getProperty("junit.srcroot")
060: + "/runtime/manage/";
061: mConfig1 = testPath + "/regress/support/binding1.xml";
062: mConfig2 = testPath + "/regress/support/flawed1.xml";
063:
064: // Instantiate an Environment Context instance and set the context in EnvironmentAccess
065: ScaffoldEnvironmentContext envCtx = new ScaffoldEnvironmentContext();
066:
067: envCtx.setJbiInstanceRoot(testPath + "/bld/");
068: envCtx.setAppServerInstanceRoot(testPath + "/bld/");
069: envCtx.setJbiInstallRoot(System.getProperty("junit.as8base")
070: + "/jbi");
071: com.sun.jbi.util.EnvironmentAccess.setContext(envCtx);
072: }
073:
074: /**
075: * Test entity resolver.
076: * @throws Exception if an unexpected error occurs
077: */
078: public void testEntityLoader() throws Exception {
079: String testname = "testEntityLoader";
080: try {
081: // -- Test the resolveEntity method
082: EntityResolver er = new XmlReader();
083: System.out.println("er.resolver.resolveEntity: "
084: + er.resolveEntity(null, "./regress/jbi.xsd"));
085: System.out.println("er.resolver.resolveEntity: "
086: + er.resolveEntity(null, "./jbi.xsd"));
087: } catch (Exception anEx) {
088: anEx.printStackTrace();
089: fail(testname + ": failed due to -" + anEx.getMessage());
090: throw anEx;
091: }
092: }
093:
094: /**
095: * Test the load without any validation.
096: * @throws Exception if an unexpected error occurs
097: */
098: public void testLoadAndParse() throws Exception {
099: String testname = "testLoadAndParse";
100: try {
101: // -- Test the loadAndParse method
102: XmlReader xmlReader = new XmlReader();
103: String doc = xmlReader.loadAndParse(mConfig1, true);
104: assertNotNull("The Document was not loaded correctly.", doc);
105: } catch (Exception anEx) {
106: anEx.printStackTrace();
107: fail(testname + ": failed due to -" + anEx.getMessage());
108: throw anEx;
109: }
110: }
111:
112: /**
113: * Test the load with validation.
114: * @throws Exception if an unexpected error occurs
115: */
116: public void testLoadWithValidate() throws Exception {
117: String testname = "testLoadWithValidate";
118: try {
119: // -- Test the validate method
120: XmlReader xmlReader = new XmlReader();
121: boolean valid = xmlReader.validate(mConfig1);
122: assertTrue("The Document did not get validated.", valid);
123: String doc = xmlReader.loadAndParse(mConfig1, true);
124: assertNotNull("The Document was not loaded correctly.", doc);
125: } catch (Exception anEx) {
126: anEx.printStackTrace();
127: fail(testname + ": failed due to -" + anEx.getMessage());
128: throw anEx;
129: }
130: }
131:
132: /**
133: * Test the load (for a flawed jbi.xml) without any validation.
134: * @throws Exception if an unexpected error occurs
135: */
136: public void testBadLoadAndParse() throws Exception {
137: String testname = "testBadLoadAndParse";
138: try {
139: // -- Test the loadAndParse method
140: XmlReader xmlReader = new XmlReader();
141: String doc = xmlReader.loadAndParse(mConfig2, true);
142: assertNull("Document was not flagged as invalid.", doc);
143: } catch (Exception anEx) {
144: anEx.printStackTrace();
145: fail(testname + ": failed due to -" + anEx.getMessage());
146: throw anEx;
147: }
148: }
149:
150: /**
151: * Test the load (for a flawed jbi.xml) with validation.
152: * @throws Exception if an unexpected error occurs
153: */
154: public void testBadValidate() throws Exception {
155: String testname = "testBadValidate";
156: try {
157: // -- Test the validate method
158: XmlReader xmlReader = new XmlReader();
159: boolean valid = xmlReader.validate(mConfig2);
160: fail("The Document got incorrectly validated.");
161: } catch (Exception anEx) {
162: // expect this exception -- ignore it.
163: assertTrue("caught expected exception", true);
164: }
165: }
166: }
|