KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > TestErrorHandler


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import hivemind.test.FrameworkTestCase;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.ErrorHandler;
22 import org.apache.hivemind.Location;
23 import org.apache.hivemind.Resource;
24 import org.apache.hivemind.util.ClasspathResource;
25
26 /**
27  * Tests for {@link org.apache.hivemind.impl.DefaultErrorHandler}.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class TestErrorHandler extends FrameworkTestCase
32 {
33     public void testDefaultErrorHandlerWithLocation()
34     {
35         Log log = (Log) newMock(Log.class);
36
37         Resource r = new ClasspathResource(getClassResolver(), "/foo/bar/Baz.module");
38         Location l = new LocationImpl(r, 13);
39
40         Throwable JavaDoc ex = new IllegalArgumentException JavaDoc();
41
42         log.error("Error at classpath:/foo/bar/Baz.module, line 13: Bad frob value.", ex);
43
44         replayControls();
45
46         ErrorHandler eh = new DefaultErrorHandler();
47
48         eh.error(log, "Bad frob value.", l, ex);
49
50         verifyControls();
51     }
52
53     public void testDefaultErrorHandlerWithNoLocation()
54     {
55         Log log = (Log) newMock(Log.class);
56
57         Throwable JavaDoc ex = new IllegalArgumentException JavaDoc();
58
59         log.error("Error: Bad frob value.", ex);
60
61         replayControls();
62
63         ErrorHandler eh = new DefaultErrorHandler();
64
65         eh.error(log, "Bad frob value.", null, ex);
66
67         verifyControls();
68     }
69
70     public void testStrictErrorHandler()
71     {
72         ErrorHandler eh = new StrictErrorHandler();
73         Throwable JavaDoc cause = new NullPointerException JavaDoc();
74
75         try
76         {
77             eh.error(null, "An error message.", null, cause);
78             unreachable();
79         }
80         catch (ApplicationRuntimeException ex)
81         {
82             assertEquals("Error: An error message.", ex.getMessage());
83             assertSame(cause, ex.getRootCause());
84         }
85     }
86
87     public void testStrictErrorHandlerWithLocation()
88     {
89         ErrorHandler eh = new StrictErrorHandler();
90         Throwable JavaDoc cause = new NullPointerException JavaDoc();
91         Location l = newLocation();
92
93         try
94         {
95             eh.error(null, "An error message.", l, cause);
96             unreachable();
97         }
98         catch (ApplicationRuntimeException ex)
99         {
100             assertEquals("Error at " + l + ": An error message.", ex.getMessage());
101             assertSame(l, ex.getLocation());
102             assertSame(cause, ex.getRootCause());
103         }
104     }
105 }
106
Popular Tags