1 /*
2 * ====================================================================
3 * License:
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The end-user documentation included with the redistribution, if any,
13 * must include the following acknowledgment: "This product includes software
14 * developed by Robert Half International (http://www.rhi.com/)." Alternately,
15 * this acknowledgment may appear in the software itself, if and wherever such
16 * third-party acknowledgments normally appear.
17 * 4. The names "Parc", "RHI", and "Robert Half International" must not be
18 * used to endorse or promote products derived from this software without prior
19 * written permission. For written permission, please contact
20 * pete.mckinstry@rhi.com.
21 * 5. Products derived from this software may not be called "PARC", nor may
22 * "PARC" appear in their name, without prior written permission of Robert Half
23 * International.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
27 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * ====================================================================
36 *
37 */
38 package com.rhi.architecture.parc;
39
40 import com.rhi.architecture.logging.DefaultLogger;
41 import com.rhi.architecture.logging.Logger;
42
43 import junit.framework.Test;
44 import junit.framework.TestCase;
45 import junit.framework.TestSuite;
46 import junit.textui.TestRunner;
47
48 /***
49 * ExceptionHandlerTest
50 *
51 * @author Pete McKinstry
52 * @version 1.0
53 */
54 public class ExceptionHandlerTest extends TestCase {
55
56 private Logger log = new DefaultLogger();
57
58 private ExceptionHandler handler;
59
60 /***
61 * constructor
62 * @param name
63 */
64 public ExceptionHandlerTest(String name) {
65 super(name);
66 }
67
68 /***
69 * Prepare resources for test.
70 * @throws Exception
71 */
72 protected void setUp() throws Exception {
73 super.setUp();
74 handler = new ExceptionHandler();
75 }
76
77 /***
78 * Cleanup test resources
79 * @throws Exception
80 */
81 protected void tearDown() throws Exception {
82 super.tearDown();
83 handler = null;
84 }
85
86 /***
87 * Method testReportException.
88 */
89 public void testReportException() {
90 handler.reportException(new ProcessingException("test"));
91 assertTrue(handler.hasError());
92 }
93
94 /***
95 * Method testRethrowException.
96 */
97 public void testRethrowException() {
98 testReportException();
99 try {
100 handler.rethrowException();
101 }
102 catch (ProcessingException e) {
103 return;
104 }
105 fail("rethrow of handled exception failed.");
106 }
107
108 /***
109 * Method testThreadSafety. Clearly you can't test for concurrency errors
110 * quite this simply, but any test is better than none...
111 */
112 public void testThreadSafety() {
113 try {
114 // start thread
115 new Thread(new HandlerTestRunner()).run();
116
117 // wait for it to finish
118 long start = System.currentTimeMillis();
119 long timeout = 1000;
120 while (handler.hasError() == false) {
121 Thread.sleep(50);
122 if ((System.currentTimeMillis() - start) > timeout) {
123 fail("timeout failure in thread test.");
124 }
125 }
126 // just to match standard usage pattern. just calling rethrow()
127 // would work also.
128 if (handler.hasError()) {
129 handler.rethrowException();
130 }
131 }
132 catch (InterruptedException e) {
133 // ignore & hit fail below.
134 }
135 catch (ProcessingException e) {
136 return;
137 }
138 fail("Thread test failure.");
139 }
140
141 /***
142 * run all tests
143 * @return
144 */
145 public static Test suite() {
146 return new TestSuite(ExceptionHandlerTest.class);
147 }
148
149 /***
150 * main - run all the tests.
151 * @param args
152 */
153 public static void main(String[] args) {
154 TestRunner.run(ExceptionHandlerTest.suite());
155 }
156
157 class HandlerTestRunner implements Runnable {
158
159 /***
160 * Comment for <code>handler</code>
161 */
162 public ExceptionHandler eHandler = ExceptionHandlerTest.this.handler;
163
164 /***
165 * Constructor
166 *
167 * @see java.lang.Object#Object()
168 */
169 public HandlerTestRunner() {
170 // default ctor
171 }
172
173 /***
174 * @see java.lang.Runnable#run()
175 */
176 public void run() {
177 eHandler.reportException(new ProcessingException("whoops."));
178 }
179 }
180
181 }
This page was automatically generated by Maven