1 package org.apache.commons.digester3;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
168 }
169 }
170 }