1 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 44 public class JUnitEventReporter extends AbstractReporter { 45 46 private static final Category _log = Category.getInstance( 47 JUnitEventReporter.class); 48 49 50 private TestResult _testResult = null; 51 52 57 protected JUnitEventReporter(TestResult result) { 58 _testResult = result; 59 } 60 61 64 private class EventTestAdapter implements Test { 65 66 private AssertionFailedError _failed = null; 67 68 private Throwable _error = null; 69 70 74 public EventTestAdapter() { 75 } 76 77 86 public EventTestAdapter(AssertionFailedError t) { 87 _failed = t; 88 } 89 90 98 public EventTestAdapter(Throwable t) { 99 _error = t; 100 } 101 102 107 public int countTestCases() { 108 return 1; 109 } 110 111 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 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 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 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 175 public void requestSucceeded(RequestEvent event) { 176 _log.debug("Received latka RequestSucceededEvent"); 177 Test test = new EventTestAdapter(); 178 test.run(_testResult); 179 } 180 181 187 public void reportMessage(ReportMessageEvent event) { 188 189 } 190 191 195 public void suiteCompleted(SuiteEvent event) { 196 } 197 } 198 | Popular Tags |