You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ed...@apache.org on 2002/02/15 04:00:59 UTC
cvs commit: xml-xerces/java/tests/jaxp/data personal-schema-badhint.xml personal-schema-err-nohint.xml personal-schema-err.xml personal-schema-nohint.xml personal-schema.xml personal.xsd
edwingo 02/02/14 19:00:59
Added: java/tests/jaxp JAXP12Tests.java JAXPConstants.java
java/tests/jaxp/data personal-schema-badhint.xml
personal-schema-err-nohint.xml
personal-schema-err.xml personal-schema-nohint.xml
personal-schema.xml personal.xsd
Log:
Added junit JAXP test program to test JAXP 1.2 features. A minmal set of
features is tested, currently.
Revision Changes Path
1.1 xml-xerces/java/tests/jaxp/JAXP12Tests.java
Index: JAXP12Tests.java
===================================================================
/*
* $Id: JAXP12Tests.java,v 1.1 2002/02/15 03:00:58 edwingo Exp $
*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
* http://www.sun.com. For more information on the Apache Software
* Foundation, please see <http://www.apache.org/>.
*/
package jaxp;
import junit.framework.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import java.net.*;
/**
* Test JAXP 1.2 specific features
*
* @author Edwin Goei
*/
public class JAXP12Tests extends TestCase implements JAXPConstants {
protected DocumentBuilderFactory dbf;
protected DocumentBuilder db;
protected DocumentBuilder dbn;
protected DocumentBuilder dbnv;
SAXParserFactory spf;
SAXParser spn;
SAXParser spnv;
public JAXP12Tests(String name) {
super(name);
}
private static class MyErrorHandler implements ErrorHandler {
public void fatalError(SAXParseException x) throws SAXException {
x.printStackTrace();
fail("ErrorHandler#fatalError() should not have been" +
" called: " +
x.getMessage() +
" [line = " + x.getLineNumber() + ", systemId = " +
x.getSystemId() + "]");
}
public void error(SAXParseException x) throws SAXException {
x.printStackTrace();
fail("ErrorHandler#error() should not have been called: " +
x.getMessage() +
" [line = " + x.getLineNumber() + ", systemId = " +
x.getSystemId() + "]");
}
public void warning(SAXParseException x) throws SAXException {
x.printStackTrace();
fail("ErrorHandler#warning() should not have been called: "
+ x.getMessage() +
" [line = " + x.getLineNumber() + ", systemId = " +
x.getSystemId() + "]");
}
}
/**
* Overrides method error() to see if it was ever called
*/
private static class ErrorHandlerCheck extends MyErrorHandler {
Boolean gotError = Boolean.FALSE;
public void error(SAXParseException x) throws SAXException {
gotError = Boolean.TRUE;
throw x;
}
public Object getStatus() {
return gotError;
}
};
protected void setUp() throws Exception {
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder(); // non-namespaceAware version
dbf.setNamespaceAware(true);
dbn = dbf.newDocumentBuilder(); // namespaceAware version
dbn.setErrorHandler(new MyErrorHandler());
dbf.setValidating(true);
dbnv = dbf.newDocumentBuilder(); // validating version
dbnv.setErrorHandler(new MyErrorHandler());
spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spn = spf.newSAXParser();
spf.setValidating(true);
spnv = spf.newSAXParser();
}
/**
* Should not cause a validation error. Problem is that you get same
* result if no validation is occurring at all. See other tests that
* checks that validation actually occurs.
*/
public void testSaxParseXSD() throws Exception {
spnv.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
XMLReader xr = spnv.getXMLReader();
xr.setErrorHandler(new MyErrorHandler());
xr.parse(new InputData("personal-schema.xml"));
}
/**
* Should cause a validation error. Checks that validation is indeed
* occurring. Warning: does not actually check for particular
* validation error, but assumes any exception thrown is a validation
* error of the type we expect.
*/
public void testSaxParseXSD2() throws Exception {
spnv.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
XMLReader xr = spnv.getXMLReader();
ErrorHandlerCheck meh = new ErrorHandlerCheck();
xr.setErrorHandler(meh);
try {
xr.parse(new InputData("personal-schema-err.xml"));
fail("ErrorHandler.error() should have thrown a SAXParseException");
} catch (SAXException x) {
assertEquals("Should have caused validation error.",
Boolean.TRUE, meh.getStatus());
}
}
/**
* Check that setting schemaSource overrides xsi: hint in instance doc
*/
public void testSaxParseSchemaSource() throws Exception {
spnv.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
spnv.setProperty(JAXP_SCHEMA_SOURCE, new InputData("personal.xsd"));
XMLReader xr = spnv.getXMLReader();
xr.setErrorHandler(new MyErrorHandler());
xr.parse(new InputData("personal-schema-badhint.xml"));
xr.parse(new InputData("personal-schema-nohint.xml"));
}
/**
* Turn on DTD validation and expect an error b/c instance doc has no
* doctypedecl
*/
public void testSaxParseNoXSD() throws Exception {
XMLReader xr = spnv.getXMLReader();
ErrorHandlerCheck meh = new ErrorHandlerCheck();
xr.setErrorHandler(meh);
try {
xr.parse(new InputData("personal-schema.xml"));
fail("ErrorHandler.error() should have thrown a SAXParseException");
} catch (SAXException x) {
assertEquals("Should have caused validation error.",
Boolean.TRUE, meh.getStatus());
}
}
/**
* Should not cause a validation error. Problem is that you get same
* result if no validation is occurring at all. See other tests that
* checks that validation actually occurs.
*/
public void testDomParseXSD() throws Exception {
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
DocumentBuilder mydb = dbf.newDocumentBuilder();
mydb.setErrorHandler(new MyErrorHandler());
mydb.parse(new InputData("personal-schema.xml"));
}
/**
* Should cause a validation error. Checks that validation is indeed
* occurring. Warning: does not actually check for particular
* validation error, but assumes any exception thrown is a validation
* error of the type we expect.
*/
public void testDomParseXSD2() throws Exception {
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
DocumentBuilder mydb = dbf.newDocumentBuilder();
ErrorHandlerCheck meh = new ErrorHandlerCheck();
mydb.setErrorHandler(meh);
try {
mydb.parse(new InputData("personal-schema-err.xml"));
fail("ErrorHandler.error() should have thrown a SAXParseException");
} catch (SAXException x) {
assertEquals("Should have caused validation error.",
Boolean.TRUE, meh.getStatus());
}
}
/**
* Check that setting schemaSource overrides xsi: hint in instance doc
*/
public void testDomParseSchemaSource() throws Exception {
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
dbf.setAttribute(JAXP_SCHEMA_SOURCE, new InputData("personal.xsd"));
DocumentBuilder mydb = dbf.newDocumentBuilder();
mydb.setErrorHandler(new MyErrorHandler());
mydb.parse(new InputData("personal-schema-badhint.xml"));
mydb.parse(new InputData("personal-schema-nohint.xml"));
}
/**
* Turn on DTD validation and expect an error b/c instance doc has no
* doctypedecl
*/
public void testDomParseNoXSD() throws Exception {
dbf.setNamespaceAware(true);
dbf.setValidating(true);
DocumentBuilder mydb = dbf.newDocumentBuilder();
ErrorHandlerCheck meh = new ErrorHandlerCheck();
mydb.setErrorHandler(meh);
try {
mydb.parse(new InputData("personal-schema.xml"));
fail("ErrorHandler.error() should have thrown a SAXParseException");
} catch (SAXException x) {
assertEquals("Should have caused validation error.",
Boolean.TRUE, meh.getStatus());
}
}
/**
* Used to run a single test for debuggin. Remove the "Debug" suffix.
*/
public static Test suiteDebug() {
TestSuite suite = new TestSuite();
suite.addTest(new JAXP12Tests("testDomParseNoXSD"));
return suite;
}
}
1.1 xml-xerces/java/tests/jaxp/JAXPConstants.java
Index: JAXPConstants.java
===================================================================
/*
* $Id: JAXPConstants.java,v 1.1 2002/02/15 03:00:58 edwingo Exp $
*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
* http://www.sun.com. For more information on the Apache Software
* Foundation, please see <http://www.apache.org/>.
*/
package jaxp;
/**
* This interface holds JAXP constant property/attribute names and values.
* Since JAXP 1.2 is a maintenance release of JAXP 1.1, no public
* signatures are allowed so these values cannot be exposed in the
* javax.xml.parsers package. Once equivalent constants have been defined
* in future JAXP spec versions, this interface can be removed.
*
* @author Edwin Goei
*/
public interface JAXPConstants {
static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";
static final String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";
static final String SPEC_XML_URI = "http://www.w3.org/XML/1998/namespace";
static final String SPEC_XMLNS_URI = "http://www.w3.org/2000/xmlns/";
}
1.1 xml-xerces/java/tests/jaxp/data/personal-schema-badhint.xml
Index: personal-schema-badhint.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='a-non-existent-file.xsd'>
<person id="Big.Boss" >
<name><family>Boss</family> <given>Big</given></name>
<email>chief@foo.com</email>
<link subordinates="one.worker two.worker three.worker four.worker five.worker"/>
</person>
<person id="one.worker">
<name><family>Worker</family> <given>One</given></name>
<email>one@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="two.worker">
<name><family>Worker</family> <given>Two</given></name>
<email>two@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="three.worker">
<name><family>Worker</family> <given>Three</given></name>
<email>three@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="four.worker">
<name><family>Worker</family> <given>Four</given></name>
<email>four@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="five.worker">
<name><family>Worker</family> <given>Five</given></name>
<email>five@foo.com</email>
<link manager="Big.Boss"/>
</person>
</personnel>
1.1 xml-xerces/java/tests/jaxp/data/personal-schema-err-nohint.xml
Index: personal-schema-err-nohint.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xperson id="Big.Boss" >
<name><family>Boss</family> <given>Big</given></name>
<email>chief@foo.com</email>
<link subordinates="one.worker two.worker three.worker four.worker five.worker"/>
</xperson>
<person id="one.worker">
<name><family>Worker</family> <given>One</given></name>
<email>one@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="two.worker">
<name><family>Worker</family> <given>Two</given></name>
<email>two@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="three.worker">
<name><family>Worker</family> <given>Three</given></name>
<email>three@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="four.worker">
<name><family>Worker</family> <given>Four</given></name>
<email>four@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="five.worker">
<name><family>Worker</family> <given>Five</given></name>
<email>five@foo.com</email>
<link manager="Big.Boss"/>
</person>
</personnel>
1.1 xml-xerces/java/tests/jaxp/data/personal-schema-err.xml
Index: personal-schema-err.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='personal.xsd'>
<person id="Big.Boss" >
<name><xfamily>Boss</xfamily> <given>Big</given></name>
<email>chief@foo.com</email>
<link subordinates="one.worker two.worker three.worker four.worker five.worker"/>
</person>
<person id="one.worker">
<name><family>Worker</family> <given>One</given></name>
<xemail>one@foo.com</xemail>
<link xmanager="Big.Boss"/>
</person>
<person id="two.worker">
<name><family>Worker</family> <given>Two</given></name>
<email>two@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="three.worker">
<name><family>Worker</family> <given>Three</given></name>
<email>three@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="four.worker">
<name><family>Worker</family> <given>Four</given></name>
<email>four@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="five.worker">
<name><family>Worker</family> <given>Five</given></name>
<email>five@foo.com</email>
<link manager="Big.Boss"/>
</person>
</personnel>
1.1 xml-xerces/java/tests/jaxp/data/personal-schema-nohint.xml
Index: personal-schema-nohint.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<person id="Big.Boss" >
<name><family>Boss</family> <given>Big</given></name>
<email>chief@foo.com</email>
<link subordinates="one.worker two.worker three.worker four.worker five.worker"/>
</person>
<person id="one.worker">
<name><family>Worker</family> <given>One</given></name>
<email>one@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="two.worker">
<name><family>Worker</family> <given>Two</given></name>
<email>two@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="three.worker">
<name><family>Worker</family> <given>Three</given></name>
<email>three@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="four.worker">
<name><family>Worker</family> <given>Four</given></name>
<email>four@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="five.worker">
<name><family>Worker</family> <given>Five</given></name>
<email>five@foo.com</email>
<link manager="Big.Boss"/>
</person>
</personnel>
1.1 xml-xerces/java/tests/jaxp/data/personal-schema.xml
Index: personal-schema.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='personal.xsd'>
<person id="Big.Boss" >
<name><family>Boss</family> <given>Big</given></name>
<email>chief@foo.com</email>
<link subordinates="one.worker two.worker three.worker four.worker five.worker"/>
</person>
<person id="one.worker">
<name><family>Worker</family> <given>One</given></name>
<email>one@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="two.worker">
<name><family>Worker</family> <given>Two</given></name>
<email>two@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="three.worker">
<name><family>Worker</family> <given>Three</given></name>
<email>three@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="four.worker">
<name><family>Worker</family> <given>Four</given></name>
<email>four@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="five.worker">
<name><family>Worker</family> <given>Five</given></name>
<email>five@foo.com</email>
<link manager="Big.Boss"/>
</person>
</personnel>
1.1 xml-xerces/java/tests/jaxp/data/personal.xsd
Index: personal.xsd
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name="personnel">
<xs:complexType>
<xs:sequence>
<xs:element ref="person" minOccurs='1' maxOccurs='unbounded'/>
</xs:sequence>
</xs:complexType>
<xs:unique name="unique1">
<xs:selector xpath="person"/>
<xs:field xpath="name/given"/>
<xs:field xpath="name/family"/>
</xs:unique>
<xs:key name='empid'>
<xs:selector xpath="person"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:keyref name="keyref1" refer='empid'>
<xs:selector xpath="person"/>
<xs:field xpath="link/@manager"/>
</xs:keyref>
</xs:element>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="email" minOccurs='0' maxOccurs='unbounded'/>
<xs:element ref="url" minOccurs='0' maxOccurs='unbounded'/>
<xs:element ref="link" minOccurs='0' maxOccurs='1'/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use='required'/>
<xs:attribute name="note" type="xs:string"/>
<xs:attribute name="contr" default="false">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:enumeration value="true"/>
<xs:enumeration value="false"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="salary" type="xs:integer"/>
</xs:complexType>
</xs:element>
<xs:element name="name">
<xs:complexType>
<xs:all>
<xs:element ref="family"/>
<xs:element ref="given"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="family" type='xs:string'/>
<xs:element name="given" type='xs:string'/>
<xs:element name="email" type='xs:string'/>
<xs:element name="url">
<xs:complexType>
<xs:attribute name="href" type="xs:string" default="http://"/>
</xs:complexType>
</xs:element>
<xs:element name="link">
<xs:complexType>
<xs:attribute name="manager" type="xs:IDREF"/>
<xs:attribute name="subordinates" type="xs:IDREFS"/>
</xs:complexType>
</xs:element>
<xs:notation name='gif' public='-//APP/Photoshop/4.0' system='photoshop.exe'/>
</xs:schema>
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org