KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > junit > JUnitEventReporter


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

16
17 package org.apache.commons.latka.junit;
18
19 import org.apache.commons.latka.AbstractReporter;
20 import org.apache.commons.latka.ValidationException;
21 import org.apache.commons.latka.event.ReportMessageEvent;
22 import org.apache.commons.latka.event.RequestErrorEvent;
23 import org.apache.commons.latka.event.RequestEvent;
24 import org.apache.commons.latka.event.RequestFailedEvent;
25 import org.apache.commons.latka.event.SuiteEvent;
26
27 import org.apache.log4j.Category;
28
29 import junit.framework.AssertionFailedError;
30 import junit.framework.Test;
31 import junit.framework.TestResult;
32
33 /**
34  * A Latka reporter that takes the various latka events and adapts them into
35  * JUnit Tests.
36  *
37  * In particular this class handles the request error, failed, skipped and
38  * succeeded events
39  *
40  * @author Chuck Burdick
41  * @author dIon Gillard
42  * @version $Id: JUnitEventReporter.java 155424 2005-02-26 13:09:29Z dirkv $
43  */

44 public class JUnitEventReporter extends AbstractReporter {
45     /** log4j category for log output */
46     private static final Category _log = Category.getInstance(
47         JUnitEventReporter.class);
48
49     /** the result from running the tests */
50     private TestResult _testResult = null;
51
52     /**
53      * Create a JUnitEventReporter, storing the
54      * JUnit Test results in the provided object
55      * @param result the JUnit TestResult to add failures and errors to
56      */

57     protected JUnitEventReporter(TestResult result) {
58         _testResult = result;
59     }
60
61     /**
62      * A {@link Test JUnit Test} that knows how to be a Latka reporter
63      */

64     private class EventTestAdapter implements Test {
65         /** the JUnit assertion error */
66         private AssertionFailedError _failed = null;
67         /** the error that occurred */
68         private Throwable JavaDoc _error = null;
69
70         /**
71          * A JUnit Test class accumulates test results when run
72          * based on the constructor that was called
73          */

74         public EventTestAdapter() {
75         }
76       
77         /**
78          * Create an instance with a JUnit
79          * {@link junit.framework.AssertionFailedError AssertionFailedError}
80          * that can be added to the results in the
81          * {@link #run(junit.framework.TestResult) run} method
82          *
83          * @param t The AssertionFailedError that will be stored for
84          * later addition to results
85          */

86         public EventTestAdapter(AssertionFailedError t) {
87             _failed = t;
88         }
89       
90         /**
91          * Create an instance with a JUnit {@link java.lang.Throwable Throwable}
92          * that can be added to the results in the
93          * {@link #run(junit.framework.TestResult) run} method
94          *
95          * @param t The Throwable that will be stored for later addition to
96          * results
97          */

98         public EventTestAdapter(Throwable JavaDoc t) {
99             _error = t;
100         }
101       
102         /**
103          * The number of test cases this test contains
104          *
105          * @return Currently hard coded to one test cases
106          */

107         public int countTestCases() {
108             return 1;
109         }
110       
111         /**
112          * Run this test.
113          * Since Latka has already executed the request we simply check if there
114          * has been a failure or error stored and add it to the test results
115          *
116          * @param result The {@link junit.framework.TestResult TestResult} to
117          * store failures or errors in
118          */

119         public void run(TestResult result) {
120             result.startTest(this);
121             if (_error != null) {
122                 result.addError(this, _error);
123             } else if (_failed != null) {
124                 result.addFailure(this, _failed);
125             }
126             result.endTest(this);
127         }
128     }
129
130     /**
131      * Process a Latka request error
132      * @param event the latka request event that is in error
133      */

134     public void requestError(RequestEvent event) {
135         _log.debug("Received latka RequestErrorEvent");
136         Throwable JavaDoc error = ((RequestErrorEvent) event).getError();
137         Test test = new EventTestAdapter(error);
138         test.run(_testResult);
139     }
140
141     /**
142      * Process a Latka request failure
143      * @param event The event describing the request that has failed
144      */

145     public void requestFailed(RequestEvent event) {
146         _log.debug("Received latka RequestFailedEvent");
147         RequestFailedEvent fe = (RequestFailedEvent) event;
148         ValidationException ve = (ValidationException)
149             fe.getValidationException();
150         String JavaDoc requestUrl = event.getRequest().getURL().toString();
151         String JavaDoc requestLabel = event.getRequest().getLabel();
152         String JavaDoc message = requestUrl + " -- " + requestLabel + ": "
153             + ve.getReason();
154         AssertionFailedError failure = new AssertionFailedError(message);
155         Test test = new EventTestAdapter(failure);
156         test.run(_testResult);
157     }
158
159     /**
160      * Process a Latka event being skipped
161      * @param event The event describing the request that has been skipped
162      */

163     public void requestSkipped(RequestEvent event) {
164         _log.debug("Received latka RequestSkippedEvent");
165         AssertionFailedError failure = new AssertionFailedError(
166                                         "Skipped due to earlier error");
167         Test test = new EventTestAdapter(failure);
168         test.run(_testResult);
169     }
170
171     /**
172      * Process a Latka request success event
173      * @param event The event describing the request that has succeeded
174      */

175     public void requestSucceeded(RequestEvent event) {
176         _log.debug("Received latka RequestSucceededEvent");
177         Test test = new EventTestAdapter();
178         test.run(_testResult);
179     }
180
181     /**
182      * This method is currently ignored by the JUnitEventReporter.
183      * It may be implemented later.
184      *
185      * @param event reportMessage (ignored)
186      */

187     public void reportMessage(ReportMessageEvent event) {
188
189     }
190
191     /**
192      * Process a Latka suite completion event
193      * @param event The event describing the suite that has completed
194      */

195     public void suiteCompleted(SuiteEvent event) {
196     }
197 }
198
Popular Tags