001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional;
019:
020: import org.apache.tools.ant.BuildException;
021: import org.apache.tools.ant.BuildFileTest;
022:
023: /**
024: * Tests the XMLValidate optional task, by running targets in the test script
025: * <code>src/etc/testcases/taskdefs/optional/xmlvalidate.xml</code>
026: * <p>
027: *
028: * @see XmlValidateCatalogTest
029: * @since Ant 1.5
030: */
031: public class XmlValidateTest extends BuildFileTest {
032:
033: /**
034: * where tasks run
035: */
036: private final static String TASKDEFS_DIR = "src/etc/testcases/taskdefs/optional/";
037:
038: /**
039: * Constructor
040: *
041: * @param name testname
042: */
043: public XmlValidateTest(String name) {
044: super (name);
045: }
046:
047: /**
048: * The JUnit setup method
049: */
050: public void setUp() {
051: configureProject(TASKDEFS_DIR + "xmlvalidate.xml");
052: }
053:
054: /**
055: * The teardown method for JUnit
056: */
057: public void tearDown() {
058: }
059:
060: /**
061: * Basic inline 'dtd' element test.
062: */
063: public void testValidate() throws Exception {
064: executeTarget("testValidate");
065: }
066:
067: /**
068: * Test indirect validation.
069: */
070: public void testDeepValidate() throws Exception {
071: executeTarget("testDeepValidate");
072: }
073:
074: /**
075: *
076: */
077: public void testXmlCatalog() {
078: executeTarget("xmlcatalog");
079: }
080:
081: /**
082: *
083: */
084: public void testXmlCatalogViaRefid() {
085: executeTarget("xmlcatalogViaRefid");
086: }
087:
088: /**
089: * Test that the nested dtd element is used when resolver.jar is not
090: * present. This test should pass either way.
091: */
092: public void testXmlCatalogFiles() {
093: executeTarget("xmlcatalogfiles-override");
094: }
095:
096: /**
097: * Test nested catalogpath.
098: * Test that the nested dtd element is used when resolver.jar is not
099: * present. This test should pass either way.
100: */
101: public void testXmlCatalogPath() {
102: executeTarget("xmlcatalogpath-override");
103: }
104:
105: /**
106: * Test nested xmlcatalog definitions
107: */
108: public void testXmlCatalogNested() {
109: executeTarget("xmlcatalognested");
110: }
111:
112: /**
113: * Test xml schema validation
114: */
115: public void testXmlSchemaGood() throws BuildException {
116: try {
117: executeTarget("testSchemaGood");
118: } catch (BuildException e) {
119: if (e
120: .getMessage()
121: .endsWith(
122: " doesn't recognize feature http://apache.org/xml/features/validation/schema")
123: || e
124: .getMessage()
125: .endsWith(
126: " doesn't support feature http://apache.org/xml/features/validation/schema")) {
127: System.err
128: .println(" skipped, parser doesn't support schema");
129: } else {
130: throw e;
131: }
132: }
133: }
134:
135: /**
136: * Test xml schema validation
137: */
138: public void testXmlSchemaBad() {
139: try {
140: executeTarget("testSchemaBad");
141: fail("Should throw BuildException because 'Bad Schema Validation'");
142:
143: expectBuildExceptionContaining("testSchemaBad",
144: "Bad Schema Validation", "not a valid XML document");
145: } catch (BuildException e) {
146: if (e
147: .getMessage()
148: .endsWith(
149: " doesn't recognize feature http://apache.org/xml/features/validation/schema")
150: || e
151: .getMessage()
152: .endsWith(
153: " doesn't support feature http://apache.org/xml/features/validation/schema")) {
154: System.err
155: .println(" skipped, parser doesn't support schema");
156: } else {
157: assertTrue(e.getMessage().indexOf(
158: "not a valid XML document") > -1);
159: }
160: }
161: }
162:
163: /**
164: * iso-2022-jp.xml is valid but wouldn't get recognized on systems
165: * with a different native encoding.
166: *
167: * Bug 11279
168: */
169: public void testIso2022Jp() {
170: executeTarget("testIso2022Jp");
171: }
172:
173: /**
174: * utf-8.xml is invalid as it contains non-UTF-8 characters, but
175: * would pass on systems with a native iso-8859-1 (or similar)
176: * encoding.
177: *
178: * Bug 11279
179: */
180: public void testUtf8() {
181: expectBuildException("testUtf8", "invalid characters in file");
182: }
183:
184: // Tests property element, using XML schema properties as an example.
185:
186: public void testPropertySchemaForValidXML() {
187: executeTarget("testProperty.validXML");
188: }
189:
190: public void testPropertySchemaForInvalidXML() {
191: expectBuildException("testProperty.invalidXML",
192: "XML file does not satisfy schema.");
193: }
194:
195: }
|