| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| JUnitEventReporter |
|
| 1.1666666666666667;1.167 | ||||
| JUnitEventReporter$EventTestAdapter |
|
| 1.1666666666666667;1.167 |
| 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 | 0 | private static final Category _log = Category.getInstance( |
| 47 | 0 | JUnitEventReporter.class); |
| 48 | ||
| 49 | /** the result from running the tests */ | |
| 50 | 0 | 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 | 0 | protected JUnitEventReporter(TestResult result) { |
| 58 | 0 | _testResult = result; |
| 59 | 0 | } |
| 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 | 0 | private AssertionFailedError _failed = null; |
| 67 | /** the error that occurred */ | |
| 68 | 0 | private Throwable _error = null; |
| 69 | ||
| 70 | /** | |
| 71 | * A JUnit Test class accumulates test results when run | |
| 72 | * based on the constructor that was called | |
| 73 | */ | |
| 74 | 0 | public EventTestAdapter() { |
| 75 | 0 | } |
| 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 | 0 | public EventTestAdapter(AssertionFailedError t) { |
| 87 | 0 | _failed = t; |
| 88 | 0 | } |
| 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 | 0 | public EventTestAdapter(Throwable t) { |
| 99 | 0 | _error = t; |
| 100 | 0 | } |
| 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 | 0 | 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 | 0 | result.startTest(this); |
| 121 | 0 | if (_error != null) { |
| 122 | 0 | result.addError(this, _error); |
| 123 | 0 | } else if (_failed != null) { |
| 124 | 0 | result.addFailure(this, _failed); |
| 125 | } | |
| 126 | 0 | result.endTest(this); |
| 127 | 0 | } |
| 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 | 0 | _log.debug("Received latka RequestErrorEvent"); |
| 136 | 0 | Throwable error = ((RequestErrorEvent) event).getError(); |
| 137 | 0 | Test test = new EventTestAdapter(error); |
| 138 | 0 | test.run(_testResult); |
| 139 | 0 | } |
| 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 | 0 | _log.debug("Received latka RequestFailedEvent"); |
| 147 | 0 | RequestFailedEvent fe = (RequestFailedEvent) event; |
| 148 | 0 | ValidationException ve = (ValidationException) |
| 149 | fe.getValidationException(); | |
| 150 | 0 | String requestUrl = event.getRequest().getURL().toString(); |
| 151 | 0 | String requestLabel = event.getRequest().getLabel(); |
| 152 | 0 | String message = requestUrl + " -- " + requestLabel + ": " |
| 153 | + ve.getReason(); | |
| 154 | 0 | AssertionFailedError failure = new AssertionFailedError(message); |
| 155 | 0 | Test test = new EventTestAdapter(failure); |
| 156 | 0 | test.run(_testResult); |
| 157 | 0 | } |
| 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 | 0 | _log.debug("Received latka RequestSkippedEvent"); |
| 165 | 0 | AssertionFailedError failure = new AssertionFailedError( |
| 166 | "Skipped due to earlier error"); | |
| 167 | 0 | Test test = new EventTestAdapter(failure); |
| 168 | 0 | test.run(_testResult); |
| 169 | 0 | } |
| 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 | 0 | _log.debug("Received latka RequestSucceededEvent"); |
| 177 | 0 | Test test = new EventTestAdapter(); |
| 178 | 0 | test.run(_testResult); |
| 179 | 0 | } |
| 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 | 0 | } |
| 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 | 0 | } |
| 197 | } |