001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.xml.cookies;
043:
044: import java.net.URL;
045: import junit.framework.TestCase;
046: import org.netbeans.api.xml.cookies.CookieMessage;
047: import org.netbeans.api.xml.cookies.CookieObserver;
048: import org.xml.sax.InputSource;
049:
050: /**
051: * Trivial golden type support tests.
052: * <p>
053: * It tests class that is exposed by CheckXMLSupport
054: * or ValidateXMLSupport.
055: *
056: * @author Petr Kuzel
057: */
058: public class SharedXMLSupportTest extends TestCase {
059:
060: public SharedXMLSupportTest(String testName) {
061: super (testName);
062: }
063:
064: /** Test of checkXML method, of class org.netbeans.spi.xml.cookies.SharedXMLSupport. */
065: public void testCheckXML() {
066: System.out.println("testCheckXML");
067:
068: URL dtd = getClass().getResource("data/DTD.dtd");
069: URL entity = getClass().getResource("data/Entity.ent");
070: URL invalidDTD = getClass().getResource("data/InvalidDTD.dtd");
071: URL invalidDocument = getClass().getResource(
072: "data/InvalidDocument.xml");
073: URL invalidEntity = getClass().getResource(
074: "data/InvalidEntity.ent");
075: URL validDocument = getClass().getResource(
076: "data/ValidDocument.xml");
077: URL wellformedDocument = getClass().getResource(
078: "data/WellformedDocument.xml");
079: URL namespacesDocument = getClass().getResource(
080: "data/NamespacesDocument.xml");
081:
082: CheckXMLSupport support;
083: support = new CheckXMLSupport(new InputSource(dtd
084: .toExternalForm()),
085: CheckXMLSupport.CHECK_PARAMETER_ENTITY_MODE);
086: assertTrue("DTD check failed!", support.checkXML(null));
087:
088: support = new CheckXMLSupport(new InputSource(entity
089: .toExternalForm()), CheckXMLSupport.CHECK_ENTITY_MODE);
090: assertTrue("Entity check failed!", support.checkXML(null));
091:
092: support = new CheckXMLSupport(new InputSource(invalidDTD
093: .toExternalForm()),
094: CheckXMLSupport.CHECK_PARAMETER_ENTITY_MODE);
095: assertTrue("Invalid DTD must not pass!",
096: support.checkXML(null) == false);
097:
098: support = new CheckXMLSupport(new InputSource(invalidDocument
099: .toExternalForm()));
100: assertTrue("Invalid document must not pass", support
101: .checkXML(null) == false);
102:
103: support = new CheckXMLSupport(new InputSource(invalidEntity
104: .toExternalForm()), CheckXMLSupport.CHECK_ENTITY_MODE);
105: assertTrue("Invalid rntity must not pass!", support
106: .checkXML(null) == false);
107:
108: support = new CheckXMLSupport(new InputSource(validDocument
109: .toExternalForm()));
110: assertTrue("Valid document must pass!", support.checkXML(null));
111:
112: support = new CheckXMLSupport(new InputSource(
113: wellformedDocument.toExternalForm()));
114: assertTrue("Wellformed document must pass", support
115: .checkXML(null));
116:
117: Observer observer = new Observer();
118: support = new CheckXMLSupport(new InputSource(
119: namespacesDocument.toExternalForm()));
120: assertTrue("Wellformed document with namespaces must pass",
121: support.checkXML(observer));
122: assertTrue("Unexpected warnings!", observer.getWarnings() == 0);
123:
124: }
125:
126: /** Test of validateXML method, of class org.netbeans.spi.xml.cookies.SharedXMLSupport. */
127: public void testValidateXML() {
128: System.out.println("testValidateXML");
129:
130: URL dtd = getClass().getResource("data/DTD.dtd");
131: URL entity = getClass().getResource("data/Entity.ent");
132: URL invalidDTD = getClass().getResource("data/InvalidDTD.dtd");
133: URL invalidDocument = getClass().getResource(
134: "data/InvalidDocument.xml");
135: URL invalidEntity = getClass().getResource(
136: "data/InvalidEntity.ent");
137: URL validDocument = getClass().getResource(
138: "data/ValidDocument.xml");
139: URL wellformedDocument = getClass().getResource(
140: "data/WellformedDocument.xml");
141: URL validNamespacesDocument = getClass().getResource(
142: "data/ValidNamespacesDocument.xml");
143: URL conformingNamespacesDocument = getClass().getResource(
144: "data/ConformingNamespacesDocument.xml");
145:
146: SharedXMLSupport support;
147: support = new ValidateXMLSupport(new InputSource(dtd
148: .toExternalForm()));
149: assertTrue("DTD validation must fail!", support
150: .validateXML(null) == false);
151:
152: support = new ValidateXMLSupport(new InputSource(entity
153: .toExternalForm()));
154: assertTrue("Entity validation must fail!", support
155: .validateXML(null) == false);
156:
157: support = new ValidateXMLSupport(new InputSource(invalidDTD
158: .toExternalForm()));
159: assertTrue("Invalid DTD must not pass!", support
160: .validateXML(null) == false);
161:
162: support = new ValidateXMLSupport(new InputSource(
163: invalidDocument.toExternalForm()));
164: assertTrue("Invalid document must not pass", support
165: .validateXML(null) == false);
166:
167: support = new ValidateXMLSupport(new InputSource(invalidEntity
168: .toExternalForm()));
169: assertTrue("Invalid rntity must not pass!", support
170: .validateXML(null) == false);
171:
172: support = new ValidateXMLSupport(new InputSource(validDocument
173: .toExternalForm()));
174: assertTrue("Valid document must pass!", support
175: .validateXML(null));
176:
177: support = new ValidateXMLSupport(new InputSource(
178: wellformedDocument.toExternalForm()));
179: assertTrue("Wellformed document must not pass", support
180: .validateXML(null) == false);
181:
182: Observer observer = new Observer();
183: support = new ValidateXMLSupport(new InputSource(
184: validNamespacesDocument.toExternalForm()));
185: assertTrue("Valid document with namespaces must pass", support
186: .validateXML(observer));
187: assertTrue("Unexpected warnings!", observer.getWarnings() == 0);
188:
189: observer = new Observer();
190: support = new ValidateXMLSupport(new InputSource(
191: conformingNamespacesDocument.toExternalForm()));
192: assertTrue("Conforming document must pass", support
193: .validateXML(observer));
194: assertTrue("Unexpected warnings!", observer.getWarnings() == 0);
195:
196: }
197:
198: private static class Observer implements CookieObserver {
199: private int warnings;
200:
201: public void receive(CookieMessage msg) {
202: if (msg.getLevel() >= msg.WARNING_LEVEL) {
203: warnings++;
204: }
205: }
206:
207: public int getWarnings() {
208: return warnings;
209: }
210: };
211:
212: }
|