001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
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: // Created on Jun 16, 2006
018: package edu.iu.uis.eden.xml;
019:
020: import java.io.StringReader;
021:
022: import javax.xml.xpath.XPath;
023: import javax.xml.xpath.XPathConstants;
024: import javax.xml.xpath.XPathExpressionException;
025: import javax.xml.xpath.XPathFactory;
026:
027: import org.junit.Test;
028: import org.kuali.rice.test.LoggableTestCase;
029: import org.xml.sax.InputSource;
030: import org.xml.sax.SAXParseException;
031:
032: import edu.iu.uis.eden.util.XmlHelper;
033:
034: public class DocumentTypeXmlValidationTest extends LoggableTestCase {
035: private final XPath xpath = XPathFactory.newInstance().newXPath();
036:
037: /**
038: * Emulates the policy value parsing code
039: * @param name policy name
040: * @param expected expected value
041: * @param xml input xml
042: * @throws XPathExpressionException
043: */
044: private void testPolicyValue(String name, boolean expected,
045: String xml) throws XPathExpressionException {
046: Boolean b = (Boolean) xpath.evaluate(
047: "/doc/policies/policy[name='" + name + "']/value",
048: new InputSource(new StringReader(xml)),
049: XPathConstants.BOOLEAN);
050: assertNotNull(b);
051: if (b.booleanValue()) {
052: String value = (String) xpath.evaluate(
053: "/doc/policies/policy[name='" + name + "']/value",
054: new InputSource(new StringReader(xml)),
055: XPathConstants.STRING);
056: log.error(name + " value: " + value);
057: b = Boolean.valueOf(value);
058: } else {
059: b = Boolean.FALSE;
060: }
061: assertNotNull(b);
062: assertEquals(name + " policy value was not " + expected,
063: expected, b.booleanValue());
064: }
065:
066: /**
067: * Exercises the type of XPath expressions that are used to evaluate policy values
068: * @throws XPathExpressionException
069: */
070: @Test
071: public void testXPathBooleanEvaluation()
072: throws XPathExpressionException {
073: String xml = "<doc>" + "<policies>" + "<policy>"
074: + "<inherited>false</inherited>"
075: + "<name>PRE_APPROVE</name>" + "<value>true</value>"
076: + "</policy>" + "<policy>"
077: + "<inherited>false</inherited>"
078: + "<name>DEFAULT_APPROVE</name>"
079: + "<value>false</value>" + "</policy>" + "<policy>"
080: + "<inherited>false</inherited>" + "<name>TEST1</name>"
081: + "<value>blah</value>" + "</policy>" + "<policy>"
082: + "<inherited>false</inherited>" + "<name>TEST2</name>"
083: + "<value></value>" + "</policy>" + "<policy>"
084: + "<inherited>false</inherited>" + "<name>TEST3</name>"
085: + "</policy>" + "</policies>" + "</doc>";
086:
087: testPolicyValue("PRE_APPROVE", true, xml);
088: testPolicyValue("DEFAULT_APPROVE", false, xml);
089: testPolicyValue("TEST1", false, xml);
090: testPolicyValue("TEST2", false, xml);
091: testPolicyValue("TEST3", false, xml);
092: testPolicyValue("BOGUS", false, xml);
093: }
094:
095: @Test
096: public void testValidActivationTypes() throws Exception {
097: XmlHelper.validate(new InputSource(getClass()
098: .getResourceAsStream("ValidActivationTypes.xml")));
099: }
100:
101: @Test
102: public void testBadActivationType() throws Exception {
103: try {
104: XmlHelper.validate(new InputSource(getClass()
105: .getResourceAsStream("BadActivationType.xml")));
106: fail("Bad activation type passed validation");
107: } catch (SAXParseException spe) {
108: // expected
109: log.error("Bad activation type exception: " + spe);
110: }
111: }
112:
113: @Test
114: public void testValidPolicyNames() throws Exception {
115: XmlHelper.validate(new InputSource(getClass()
116: .getResourceAsStream("ValidPolicyNames.xml")));
117: }
118:
119: @Test
120: public void testBadPolicyName() throws Exception {
121: try {
122: XmlHelper.validate(new InputSource(getClass()
123: .getResourceAsStream("BadPolicyName.xml")));
124: fail("Bad policy name passed validation");
125: } catch (SAXParseException spe) {
126: // expected
127: log.error("Bad policy name exception: " + spe);
128: }
129: }
130:
131: /*
132: validation can't catch this;
133: we could possibly catch a *missing* nextNode though,
134: but that would complicate the schema somewhat so is not
135: implemented at this point
136: public void testBadNextNode() throws Exception {
137: try {
138: XmlHelper.validate(new InputSource(getClass().getResourceAsStream("BadNextNode.xml")));
139: fail("Bad nextNode passed validation");
140: } catch (SAXParseException spe) {
141: // expected
142: log.error("Bad nextNode exception: " + spe);
143: }
144: }
145: */
146:
147: @Test
148: public void testNoDocHandler() throws Exception {
149: try {
150: XmlHelper.validate(new InputSource(getClass()
151: .getResourceAsStream("NoDocHandler.xml")));
152: fail("Missing docHandler passed validation");
153: } catch (SAXParseException spe) {
154: // expected
155: log.error("Missing docHandler exception: " + spe);
156: }
157: }
158:
159: @Test
160: public void testInvalidParentDocType() throws Exception {
161: // although semantically invalid, this should actually pass XML validation as it is syntactically valid
162: XmlHelper.validate(new InputSource(getClass()
163: .getResourceAsStream("InvalidParent.xml")));
164: }
165: }
|