View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.digester3;
18  
19  import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
20  
21  import java.io.File;
22  
23  import org.apache.commons.digester3.binder.AbstractRulesModule;
24  import org.junit.Test;
25  import org.xml.sax.ErrorHandler;
26  import org.xml.sax.SAXException;
27  import org.xml.sax.SAXParseException;
28  
29  /**
30   * Tests for entity resolution and dtd validation
31   *
32   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
33   */
34  public class DTDValidationTestCase
35  {
36  
37      @Test( expected = SAXParseException.class )
38      public void testDigesterDTDError()
39          throws Exception
40      {
41          newLoader( new AbstractRulesModule() {
42  
43              @Override
44              protected void configure()
45              {
46                  // do nothing
47              }
48  
49          } )
50          .setValidating( true )
51          .setErrorHandler( new ErrorHandler()
52          {
53  
54              public void warning( SAXParseException e )
55                  throws SAXException
56              {
57                  throw e;
58              }
59  
60              public void fatalError( SAXParseException e )
61                  throws SAXException
62              {
63                  throw e;
64              }
65  
66              public void error( SAXParseException e )
67                  throws SAXException
68              {
69                  throw e;
70              }
71  
72          } )
73          .newDigester()
74          .parse( new File( "src/test/resources/org/apache/commons/digester3/document-with-relative-dtd-error.xml" ) );
75      }
76  
77      @Test
78      public void testDigesterNoDTDValidation()
79          throws Exception
80      {
81          newLoader( new AbstractRulesModule()
82          {
83  
84              @Override
85              protected void configure()
86              {
87                  // do nothing
88              }
89  
90          } )
91          .setValidating( false )
92          .newDigester()
93          .parse( new File( "src/test/resources/org/apache/commons/digester3/document-with-relative-dtd-error.xml" ) );
94      }
95  
96      @Test
97      public void testDigesterValidation()
98          throws Exception
99      {
100         newLoader( new AbstractRulesModule()
101         {
102             @Override
103             protected void configure()
104             {
105                 // do nothing
106             }
107         } )
108         .setValidating( true )
109         .newDigester()
110         .parse( new File( "src/test/resources/org/apache/commons/digester3/document-with-relative-dtd.xml" ) );
111     }
112 
113 }