001package org.apache.commons.digester3;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
023import static org.junit.Assert.fail;
024
025import java.io.IOException;
026
027import org.apache.commons.digester3.binder.AbstractRulesModule;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.junit.Test;
031import org.xml.sax.ErrorHandler;
032import org.xml.sax.SAXException;
033import org.xml.sax.SAXParseException;
034
035public class ErrorHandlerTest
036{
037
038    @Test
039    public void noCustomErrorHandler()
040    {
041
042        try
043        {
044            newLoader( new AbstractRulesModule()
045            {
046                @Override
047                protected void configure()
048                {
049                    forPattern( "employee" ).createObject().ofType( Employee.class );
050                    forPattern( "employee/firstName" ).setBeanProperty().extractPropertyNameFromAttribute( "name" );
051                }
052            } ).newDigester().parse( getClass().getResource( "Test-digester-172-wrong.xml" ) );
053            fail( "Expected SAXException" );
054        }
055        catch ( IOException e )
056        {
057            fail( "Expected SAXException" );
058        }
059        catch ( SAXException e )
060        {
061            // expected
062        }
063
064    }
065
066    @Test
067    public void customErrorHandlerWithStack()
068    {
069
070        ErrorHandler customErrorHandler = new ErrorHandler()
071        {
072            Log log = LogFactory.getLog( this.getClass() );
073
074            public void warning( SAXParseException arg0 )
075                throws SAXException
076            {
077                log.warn( "Custom Warn Handler" );
078            }
079
080            public void fatalError( SAXParseException e )
081                throws SAXException
082            {
083                log.fatal( "Custom Fatal Error Handler", e );
084            }
085
086            public void error( SAXParseException e )
087                throws SAXException
088            {
089                log.error( "Custom Error Handler", e );
090            }
091        };
092
093        try
094        {
095            Digester digester = newLoader( new AbstractRulesModule()
096            {
097                @Override
098                protected void configure()
099                {
100                    forPattern( "employee" ).createObject().ofType( Employee.class );
101                    forPattern( "employee/firstName" ).setBeanProperty().extractPropertyNameFromAttribute( "name" );
102                }
103            } ).newDigester();
104            digester.setErrorHandler( customErrorHandler );
105            digester.parse( getClass().getResource( "Test-digester-172-wrong.xml" ) );
106            fail( "Expected SAXException" );
107        }
108        catch ( IOException e )
109        {
110            fail( "Expected SAXException" );
111        }
112        catch ( SAXException e )
113        {
114            // expected
115        }
116    }
117
118    @Test
119    public void customErrorHandler()
120    {
121
122        ErrorHandler customErrorHandler = new ErrorHandler()
123        {
124            Log log = LogFactory.getLog( this.getClass() );
125
126            public void warning( SAXParseException arg0 )
127                throws SAXException
128            {
129                log.warn( "Custom Warn Handler" );
130            }
131
132            public void fatalError( SAXParseException e )
133                throws SAXException
134            {
135                log.fatal( "Custom Fatal Error Handler" );
136            }
137
138            public void error( SAXParseException e )
139                throws SAXException
140            {
141                log.error( "Custom Error Handler" );
142            }
143        };
144
145        try
146        {
147            Digester digester = newLoader( new AbstractRulesModule()
148            {
149                @Override
150                protected void configure()
151                {
152                    forPattern( "employee" ).createObject().ofType( Employee.class );
153                    forPattern( "employee/firstName" ).setBeanProperty().extractPropertyNameFromAttribute( "name" );
154                }
155            } ).newDigester();
156            digester.setErrorHandler( customErrorHandler );
157            digester.parse( getClass().getResource( "Test-digester-172-wrong.xml" ) );
158            fail( "No Expected SAXException" );
159
160        }
161        catch ( IOException e )
162        {
163            fail( "Expected SAXException" );
164        }
165        catch ( SAXException e )
166        {
167            // expected
168        }
169    }
170}