001/* $Id: XMLSchemaTestCase.java 1102402 2011-05-12 18:03:26Z simonetripodi $
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.commons.digester3;
020
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertFalse;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertTrue;
025
026import java.io.IOException;
027import java.io.InputStream;
028
029import javax.xml.XMLConstants;
030import javax.xml.validation.Schema;
031import javax.xml.validation.SchemaFactory;
032
033import org.apache.commons.digester3.Digester;
034import org.junit.After;
035import org.junit.Before;
036import org.junit.Test;
037import org.xml.sax.ErrorHandler;
038import org.xml.sax.SAXException;
039import org.xml.sax.SAXParseException;
040
041/**
042 * <p>
043 * Tests for XInclude aware parsing.
044 * </p>
045 */
046public class XMLSchemaTestCase
047{
048
049    // ----------------------------------------------------- Instance Variables
050
051    /**
052     * The digester instance we will be processing.
053     */
054    protected Digester digester = null;
055
056    // --------------------------------------------------- Overall Test Methods
057
058    /**
059     * Set up instance variables required by this test case.
060     */
061    @Before
062    public void setUp()
063        throws SAXException
064    {
065
066        digester = new Digester();
067
068        // Use the test schema
069        digester.setNamespaceAware( true );
070        Schema test13schema =
071            SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ).newSchema( this.getClass().getClassLoader().getResource( "org/apache/commons/digester3/Test13.xsd" ) );
072        digester.setXMLSchema( test13schema );
073
074        // Configure the digester as required
075        digester.addObjectCreate( "employee", Employee.class );
076        digester.addCallMethod( "employee/firstName", "setFirstName", 0 );
077        digester.addCallMethod( "employee/lastName", "setLastName", 0 );
078
079        digester.addObjectCreate( "employee/address", Address.class );
080        digester.addCallMethod( "employee/address/type", "setType", 0 );
081        digester.addCallMethod( "employee/address/city", "setCity", 0 );
082        digester.addCallMethod( "employee/address/state", "setState", 0 );
083        digester.addSetNext( "employee/address", "addAddress" );
084
085    }
086
087    /**
088     * Tear down instance variables required by this test case.
089     */
090    @After
091    public void tearDown()
092    {
093
094        digester = null;
095
096    }
097
098    // ------------------------------------------------ Individual Test Methods
099
100    /**
101     * Test XML Schema validation.
102     */
103    @Test
104    public void testGoodDocument()
105        throws SAXException, IOException
106    {
107
108        // Listen to validation errors
109        TestErrorHandler teh = new TestErrorHandler();
110        digester.setErrorHandler( teh );
111
112        // Parse our test input
113        Employee employee = digester.parse( getInputStream( "Test13-01.xml" ) );
114        assertNotNull( "failed to parsed an employee", employee );
115        assertTrue( "Test13-01 should not generate errors in Schema validation", teh.clean );
116
117        // Test document has been processed
118        Address ha = employee.getAddress( "home" );
119        assertNotNull( ha );
120        assertEquals( "Home City", ha.getCity() );
121        assertEquals( "HS", ha.getState() );
122
123    }
124
125    @Test
126    public void testBadDocument()
127        throws SAXException, IOException
128    {
129
130        // Listen to validation errors
131        TestErrorHandler teh = new TestErrorHandler();
132        digester.setErrorHandler( teh );
133
134        // Parse our test input
135        digester.parse( getInputStream( "Test13-02.xml" ) );
136        assertFalse( "Test13-02 should generate errors in Schema validation", teh.clean );
137
138    }
139
140    // ------------------------------------ Utility Support Methods and Classes
141
142    /**
143     * Return an appropriate InputStream for the specified test file (which must be inside our current package.
144     * 
145     * @param name Name of the test file we want
146     * @exception IOException if an input/output error occurs
147     */
148    protected InputStream getInputStream( String name )
149        throws IOException
150    {
151
152        return ( this.getClass().getResourceAsStream( "/org/apache/commons/digester3/" + name ) );
153
154    }
155
156    static final class TestErrorHandler
157        implements ErrorHandler
158    {
159        public boolean clean = true;
160
161        public TestErrorHandler()
162        {
163        }
164
165        public void error( SAXParseException exception )
166        {
167            clean = false;
168        }
169
170        public void fatalError( SAXParseException exception )
171        {
172            clean = false;
173        }
174
175        public void warning( SAXParseException exception )
176        {
177            clean = false;
178        }
179    }
180
181}