View Javadoc

1   package org.apache.commons.digester3;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
23  import static org.junit.Assert.fail;
24  
25  import java.io.IOException;
26  
27  import org.apache.commons.digester3.binder.AbstractRulesModule;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.junit.Test;
31  import org.xml.sax.ErrorHandler;
32  import org.xml.sax.SAXException;
33  import org.xml.sax.SAXParseException;
34  
35  public class ErrorHandlerTest
36  {
37  
38      @Test
39      public void noCustomErrorHandler()
40      {
41  
42          try
43          {
44              newLoader( new AbstractRulesModule()
45              {
46                  @Override
47                  protected void configure()
48                  {
49                      forPattern( "employee" ).createObject().ofType( Employee.class );
50                      forPattern( "employee/firstName" ).setBeanProperty().extractPropertyNameFromAttribute( "name" );
51                  }
52              } ).newDigester().parse( getClass().getResource( "Test-digester-172-wrong.xml" ) );
53              fail( "Expected SAXException" );
54          }
55          catch ( IOException e )
56          {
57              fail( "Expected SAXException" );
58          }
59          catch ( SAXException e )
60          {
61              // expected
62          }
63  
64      }
65  
66      @Test
67      public void customErrorHandlerWithStack()
68      {
69  
70          ErrorHandler customErrorHandler = new ErrorHandler()
71          {
72              Log log = LogFactory.getLog( this.getClass() );
73  
74              public void warning( SAXParseException arg0 )
75                  throws SAXException
76              {
77                  log.warn( "Custom Warn Handler" );
78              }
79  
80              public void fatalError( SAXParseException e )
81                  throws SAXException
82              {
83                  log.fatal( "Custom Fatal Error Handler", e );
84              }
85  
86              public void error( SAXParseException e )
87                  throws SAXException
88              {
89                  log.error( "Custom Error Handler", e );
90              }
91          };
92  
93          try
94          {
95              Digester digester = newLoader( new AbstractRulesModule()
96              {
97                  @Override
98                  protected void configure()
99                  {
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 }