001    /*
002     * Copyright 1999-2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.latka.junit;
018    
019    import org.apache.commons.latka.AbstractReporter;
020    import org.apache.commons.latka.ValidationException;
021    import org.apache.commons.latka.event.ReportMessageEvent;
022    import org.apache.commons.latka.event.RequestErrorEvent;
023    import org.apache.commons.latka.event.RequestEvent;
024    import org.apache.commons.latka.event.RequestFailedEvent;
025    import org.apache.commons.latka.event.SuiteEvent;
026    
027    import org.apache.log4j.Category;
028    
029    import junit.framework.AssertionFailedError;
030    import junit.framework.Test;
031    import junit.framework.TestResult;
032    
033    /**
034     * A Latka reporter that takes the various latka events and adapts them into
035     * JUnit Tests.
036     *
037     * In particular this class handles the request error, failed, skipped and
038     * succeeded events
039     *
040     * @author Chuck Burdick
041     * @author dIon Gillard
042     * @version $Id: JUnitEventReporter.java 155424 2005-02-26 13:09:29Z dirkv $
043     */
044    public class JUnitEventReporter extends AbstractReporter {
045        /** log4j category for log output */
046        private static final Category _log = Category.getInstance(
047            JUnitEventReporter.class);
048    
049        /** the result from running the tests */
050        private TestResult _testResult = null;
051    
052        /** 
053         * Create a JUnitEventReporter, storing the
054         * JUnit Test results in the provided object
055         * @param result the JUnit TestResult to add failures and errors to
056         */   
057        protected JUnitEventReporter(TestResult result) {
058            _testResult = result;
059        }
060    
061        /**
062         * A {@link Test JUnit Test} that knows how to be a Latka reporter
063         */
064        private class EventTestAdapter implements Test {
065            /** the JUnit assertion error */
066            private AssertionFailedError _failed = null;
067            /** the error that occurred */
068            private Throwable _error = null;
069    
070            /** 
071             * A JUnit Test class accumulates test results when run
072             * based on the constructor that was called
073             */      
074            public EventTestAdapter() {
075            }
076          
077            /** 
078             * Create an instance with a JUnit
079             * {@link junit.framework.AssertionFailedError AssertionFailedError} 
080             * that can be added to the results in the
081             * {@link #run(junit.framework.TestResult) run} method
082             *
083             * @param t The AssertionFailedError that will be stored for
084             * later addition to results
085             */      
086            public EventTestAdapter(AssertionFailedError t) {
087                _failed = t;
088            }
089          
090            /**
091             * Create an instance with a JUnit {@link java.lang.Throwable Throwable}
092             * that can be added to the results in the 
093             * {@link #run(junit.framework.TestResult) run} method
094             * 
095             * @param t The Throwable that will be stored for later addition to
096             *      results
097             */      
098            public EventTestAdapter(Throwable t) {
099                _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 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 requestUrl = event.getRequest().getURL().toString();
151            String requestLabel = event.getRequest().getLabel();
152            String 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    }