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: package org.netbeans.api.xml.parsers;
042:
043: import java.io.*;
044: import java.net.URL;
045: import junit.framework.*;
046: import org.netbeans.junit.*;
047: import org.xml.sax.*;
048: import org.xml.sax.helpers.*;
049: import org.xml.sax.ext.*;
050:
051: /**
052: * Tests SAXEntityParser as DTDParser.
053: * Tests wrapping logic for relative references.
054: *
055: * @author Petr Kuzel
056: */
057: public class SAXEntityParserTest extends NbTestCase {
058:
059: public SAXEntityParserTest(java.lang.String testName) {
060: super (testName);
061: }
062:
063: public static void main(java.lang.String[] args) {
064: junit.textui.TestRunner.run(suite());
065: }
066:
067: public static Test suite() {
068: TestSuite suite = new NbTestSuite(SAXEntityParserTest.class);
069:
070: return suite;
071: }
072:
073: /** Test of parse method, of class org.netbeans.api.xml.parsers.SAXEntityParser. */
074: public void testParse() throws Exception {
075: System.out.println("testParse");
076:
077: // DTD parser test
078:
079: InputSource input = new InputSource(new StringReader(
080: "<!ELEMENT x ANY>"));
081: input.setSystemId("StringReader");
082:
083: XMLReader peer = XMLReaderFactory
084: .createXMLReader("org.apache.crimson.parser.XMLReaderImpl");
085: TestDeclHandler dtdHandler = new TestDeclHandler();
086: peer.setProperty(
087: "http://xml.org/sax/properties/declaration-handler",
088: dtdHandler);
089: SAXEntityParser parser = new SAXEntityParser(peer, false);
090: parser.parse(input);
091:
092: // Add your test code below by replacing the default call to fail.
093: assertTrue("DTD entity parser did not detected 'x' decl!",
094: dtdHandler.pass);
095:
096: // Reentrance test
097:
098: boolean exceptionThrown = false;
099: try {
100: parser.parse(new InputSource(new StringReader("")));
101: } catch (IllegalStateException ex) {
102: exceptionThrown = true;
103: } finally {
104: assertTrue("Parser may not be reused!", exceptionThrown);
105: }
106:
107: relativeReferenceTest();
108: }
109:
110: /**
111: * Wrapping used to broke relative references.
112: */
113: private void relativeReferenceTest() throws Exception {
114:
115: final boolean pass[] = { false };
116:
117: try {
118: URL url = getClass().getResource("data/RelativeTest.dtd");
119: System.out.println("URL:" + url);
120: InputSource input = new InputSource(url.toExternalForm());
121: XMLReader peer = XMLReaderFactory
122: .createXMLReader("org.apache.crimson.parser.XMLReaderImpl");
123: peer.setDTDHandler(new DefaultHandler() {
124: public void notationDecl(String name, String publicId,
125: String systemId) {
126: if ("notation".equals(name))
127: pass[0] = true;
128: }
129: });
130: SAXEntityParser parser = new SAXEntityParser(peer, false);
131: parser.parse(input);
132: } finally {
133: assertTrue("External entity not reached!", pass[0]);
134: }
135: }
136:
137: class TestDeclHandler implements DeclHandler {
138:
139: boolean pass;
140:
141: public void attributeDecl(String str, String str1, String str2,
142: String str3, String str4)
143: throws org.xml.sax.SAXException {
144: }
145:
146: public void elementDecl(String str, String str1)
147: throws org.xml.sax.SAXException {
148: if ("x".equals(str))
149: pass = true;
150: }
151:
152: public void externalEntityDecl(String str, String str1,
153: String str2) throws org.xml.sax.SAXException {
154: }
155:
156: public void internalEntityDecl(String str, String str1)
157: throws org.xml.sax.SAXException {
158: }
159:
160: }
161:
162: }
|