KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2002,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 java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestResult;
25 import junit.framework.TestSuite;
26
27 import org.apache.commons.latka.ValidationException;
28 import org.apache.commons.latka.event.RequestErrorEvent;
29 import org.apache.commons.latka.event.RequestEvent;
30 import org.apache.commons.latka.event.RequestFailedEvent;
31 import org.apache.commons.latka.event.RequestSkippedEvent;
32 import org.apache.commons.latka.event.RequestSucceededEvent;
33 import org.apache.commons.latka.http.Request;
34 import org.apache.commons.latka.http.SessionImpl;
35
36 /**
37  * @author Chuck Burdick
38  * @version $Id: TestJUnitEventReporter.java 155424 2005-02-26 13:09:29Z dirkv $
39  */

40 public class TestJUnitEventReporter extends TestCase {
41    private JUnitEventReporter _reporter = null;
42    private TestResult _result = null;
43    private Request _request = null;
44
45    public TestJUnitEventReporter(String JavaDoc testName) {
46       super(testName);
47    }
48
49    public static Test suite() {
50       return new TestSuite(TestJUnitEventReporter.class);
51    }
52
53    public static void main(String JavaDoc args[]) {
54       String JavaDoc[] testCaseName = { TestJUnitEventReporter.class.getName() };
55       junit.textui.TestRunner.main(testCaseName);
56    }
57
58    public void setUp() throws MalformedURLException JavaDoc {
59       _result = new TestResult();
60       _reporter = new JUnitEventReporter(_result);
61
62       URL JavaDoc url = new URL JavaDoc("http://www.example.com/");
63       SessionImpl session = new SessionImpl();
64       _request = session.createRequest(url, Request.HTTP_METHOD_GET, "1.1");
65    }
66
67    public void testSuccess() {
68       RequestEvent e = new RequestSucceededEvent(_request, null);
69       _reporter.requestSucceeded(e);
70       assertEquals("Should have run 1 test", 1, _result.runCount());
71       assertEquals("Should have 0 errors", 0, _result.errorCount());
72       assertEquals("Should have 0 failures", 0, _result.failureCount());
73       assertTrue("Should represent a successful run", _result.wasSuccessful());
74    }
75
76    public void testError() {
77       RequestEvent e = new RequestErrorEvent(
78          _request, null, new Exception JavaDoc("test exception"));
79       _reporter.requestError(e);
80       assertEquals("Should have run 1 test", 1, _result.runCount());
81       assertEquals("Should have 1 error", 1, _result.errorCount());
82       assertEquals("Should have 0 failures", 0, _result.failureCount());
83       assertTrue("Should not represent a successful run",
84                  !_result.wasSuccessful());
85    }
86
87
88    public void testFailed() {
89       RequestEvent e = new RequestFailedEvent(
90          _request, null, new ValidationException());
91       _reporter.requestFailed(e);
92       assertEquals("Should have run 1 test", 1, _result.runCount());
93       assertEquals("Should have 0 errors", 0, _result.errorCount());
94       assertEquals("Should have 1 failure", 1, _result.failureCount());
95       assertTrue("Should not represent a successful run",
96                  !_result.wasSuccessful());
97    }
98
99    public void testSkipped() {
100       RequestEvent e = new RequestSkippedEvent(_request, null);
101       _reporter.requestSkipped(e);
102       assertEquals("Should have run 1 test", 1, _result.runCount());
103       assertEquals("Should have 0 errors", 0, _result.errorCount());
104       assertEquals("Should have 1 failure", 1, _result.failureCount());
105       assertTrue("Should not represent a successful run",
106                  !_result.wasSuccessful());
107    }
108 }
109
Popular Tags