001/* 
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.digester3;
018
019import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
020
021import java.io.File;
022
023import org.apache.commons.digester3.binder.AbstractRulesModule;
024import org.junit.Test;
025import org.xml.sax.ErrorHandler;
026import org.xml.sax.SAXException;
027import org.xml.sax.SAXParseException;
028
029/**
030 * Tests for entity resolution and dtd validation
031 *
032 * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
033 */
034public class DTDValidationTestCase
035{
036
037    @Test( expected = SAXParseException.class )
038    public void testDigesterDTDError()
039        throws Exception
040    {
041        newLoader( new AbstractRulesModule() {
042
043            @Override
044            protected void configure()
045            {
046                // do nothing
047            }
048
049        } )
050        .setValidating( true )
051        .setErrorHandler( new ErrorHandler()
052        {
053
054            public void warning( SAXParseException e )
055                throws SAXException
056            {
057                throw e;
058            }
059
060            public void fatalError( SAXParseException e )
061                throws SAXException
062            {
063                throw e;
064            }
065
066            public void error( SAXParseException e )
067                throws SAXException
068            {
069                throw e;
070            }
071
072        } )
073        .newDigester()
074        .parse( new File( "src/test/resources/org/apache/commons/digester3/document-with-relative-dtd-error.xml" ) );
075    }
076
077    @Test
078    public void testDigesterNoDTDValidation()
079        throws Exception
080    {
081        newLoader( new AbstractRulesModule()
082        {
083
084            @Override
085            protected void configure()
086            {
087                // do nothing
088            }
089
090        } )
091        .setValidating( false )
092        .newDigester()
093        .parse( new File( "src/test/resources/org/apache/commons/digester3/document-with-relative-dtd-error.xml" ) );
094    }
095
096    @Test
097    public void testDigesterValidation()
098        throws Exception
099    {
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}