View Javadoc

1   /* $Id: XMLSchemaTestCase.java 1102402 2011-05-12 18:03:26Z simonetripodi $
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.commons.digester3;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.IOException;
27  import java.io.InputStream;
28  
29  import javax.xml.XMLConstants;
30  import javax.xml.validation.Schema;
31  import javax.xml.validation.SchemaFactory;
32  
33  import org.apache.commons.digester3.Digester;
34  import org.junit.After;
35  import org.junit.Before;
36  import org.junit.Test;
37  import org.xml.sax.ErrorHandler;
38  import org.xml.sax.SAXException;
39  import org.xml.sax.SAXParseException;
40  
41  /**
42   * <p>
43   * Tests for XInclude aware parsing.
44   * </p>
45   */
46  public class XMLSchemaTestCase
47  {
48  
49      // ----------------------------------------------------- Instance Variables
50  
51      /**
52       * The digester instance we will be processing.
53       */
54      protected Digester digester = null;
55  
56      // --------------------------------------------------- Overall Test Methods
57  
58      /**
59       * Set up instance variables required by this test case.
60       */
61      @Before
62      public void setUp()
63          throws SAXException
64      {
65  
66          digester = new Digester();
67  
68          // Use the test schema
69          digester.setNamespaceAware( true );
70          Schema test13schema =
71              SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ).newSchema( this.getClass().getClassLoader().getResource( "org/apache/commons/digester3/Test13.xsd" ) );
72          digester.setXMLSchema( test13schema );
73  
74          // Configure the digester as required
75          digester.addObjectCreate( "employee", Employee.class );
76          digester.addCallMethod( "employee/firstName", "setFirstName", 0 );
77          digester.addCallMethod( "employee/lastName", "setLastName", 0 );
78  
79          digester.addObjectCreate( "employee/address", Address.class );
80          digester.addCallMethod( "employee/address/type", "setType", 0 );
81          digester.addCallMethod( "employee/address/city", "setCity", 0 );
82          digester.addCallMethod( "employee/address/state", "setState", 0 );
83          digester.addSetNext( "employee/address", "addAddress" );
84  
85      }
86  
87      /**
88       * Tear down instance variables required by this test case.
89       */
90      @After
91      public void tearDown()
92      {
93  
94          digester = null;
95  
96      }
97  
98      // ------------------------------------------------ Individual Test Methods
99  
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 }