Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JUnitTestAdapter |
|
| 1.75;1.75 | ||||
JUnitTestAdapter$TestCounter |
|
| 1.75;1.75 |
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 | // java imports | |
20 | import java.io.File; | |
21 | import java.io.IOException; | |
22 | import java.net.MalformedURLException; | |
23 | import java.net.URL; | |
24 | // jaxp imports | |
25 | import javax.xml.parsers.FactoryConfigurationError; | |
26 | import javax.xml.parsers.ParserConfigurationException; | |
27 | import javax.xml.parsers.SAXParser; | |
28 | import javax.xml.parsers.SAXParserFactory; | |
29 | // latka imports | |
30 | import org.apache.commons.latka.DefaultLatkaEventInfo; | |
31 | import org.apache.commons.latka.Latka; | |
32 | import org.apache.commons.latka.LatkaException; | |
33 | import org.apache.commons.latka.Suite; | |
34 | // log4j imports | |
35 | import org.apache.log4j.Category; | |
36 | // sax imports | |
37 | import org.xml.sax.Attributes; | |
38 | import org.xml.sax.InputSource; | |
39 | import org.xml.sax.SAXException; | |
40 | import org.xml.sax.XMLReader; | |
41 | import org.xml.sax.helpers.DefaultHandler; | |
42 | // junit imports | |
43 | import junit.framework.Test; | |
44 | import junit.framework.TestResult; | |
45 | ||
46 | /** | |
47 | * A JUnit {@link junit.framework.Test Test} which is created by | |
48 | * wrapping a Latka {@link org.apache.commons.latka.Suite Suite} | |
49 | * | |
50 | * @author Chuck Burdick | |
51 | * @author dIon Gillard | |
52 | * @version $Id: JUnitTestAdapter.java 155424 2005-02-26 13:09:29Z dirkv $ | |
53 | */ | |
54 | public class JUnitTestAdapter implements Test { | |
55 | /** log4j category that output is logged to */ | |
56 | 0 | private static final Category _log = Category.getInstance( |
57 | 0 | JUnitTestAdapter.class); |
58 | ||
59 | /** The latka {@link org.apache.commons.latka.Suite Suite} to be run*/ | |
60 | 0 | private Suite _latkaSuite = null; |
61 | /** the number of tests in the suite */ | |
62 | 0 | private int _testCount = 0; |
63 | ||
64 | /** | |
65 | * Create a Test from a Latka suite and a number of tests | |
66 | * | |
67 | * @param suite The Latka {@link org.apache.commons.latka.Suite} | |
68 | * to be run as a JUnit Test | |
69 | * @param testCount The number of 'request's in the Latka suite | |
70 | */ | |
71 | 0 | protected JUnitTestAdapter(Suite suite, int testCount) { |
72 | 0 | _latkaSuite = suite; |
73 | 0 | _testCount = testCount; |
74 | 0 | } |
75 | ||
76 | /** | |
77 | * Create a Test from a Latka file | |
78 | * @param fileName The name of a readable file in Latka's XML format | |
79 | * @return a JUnit Test, ready to run, or null if the file can't be resolved | |
80 | */ | |
81 | public static Test createTestFromFile(String fileName) { | |
82 | 0 | Test result = null; |
83 | 0 | File file = new File(fileName); |
84 | 0 | if (file.exists()) { |
85 | 0 | result = createTestFromFile(file); |
86 | } else { | |
87 | 0 | _log.debug("Input file " + file.getAbsolutePath() |
88 | + " does not exist"); | |
89 | } | |
90 | 0 | return result; |
91 | } | |
92 | ||
93 | /** | |
94 | * Create a Test from a {@link java.io.File Java file} | |
95 | * @param file A readable java file containing Latka's XML format | |
96 | * @return a JUnit Test, ready to run, or null if the file can't be resolved | |
97 | */ | |
98 | public static Test createTestFromFile(File file) { | |
99 | 0 | Test result = null; |
100 | try { | |
101 | 0 | result = createTestFromURL(file.toURL()); |
102 | 0 | } catch (MalformedURLException e) { |
103 | 0 | _log.debug("Could not access input file", e); |
104 | 0 | } |
105 | 0 | return result; |
106 | } | |
107 | ||
108 | /** | |
109 | * Create a Test from a resource accessible via | |
110 | * the {@link java.lang.ClassLoader#getResource(String) class loader} | |
111 | * @param resourceName A resource accessible by the class loader in Latka's | |
112 | * XML format | |
113 | * @return a JUnit Test, ready to run, or null if the resource can't be | |
114 | * resolved | |
115 | */ | |
116 | public static Test createTestFromResource(String resourceName) { | |
117 | 0 | Test result = null; |
118 | 0 | ClassLoader loader = JUnitTestAdapter.class.getClassLoader(); |
119 | 0 | URL resource = loader.getResource(resourceName); |
120 | 0 | if (resource != null) { |
121 | 0 | result = createTestFromURL(resource); |
122 | } | |
123 | 0 | return result; |
124 | } | |
125 | ||
126 | /** | |
127 | * Create a Test from a String containing a URL whose | |
128 | * contents are in Latka's XML format | |
129 | * @param url the {@link java.net.URL URL} to fetch | |
130 | * @return a JUnit Test, ready to run, or null if the url can't be resolved | |
131 | */ | |
132 | public static Test createTestFromURL(String url) { | |
133 | 0 | Test result = null; |
134 | try { | |
135 | 0 | result = createTestFromURL(new URL(url)); |
136 | 0 | } catch (MalformedURLException e) { |
137 | 0 | _log.debug("Unable to create URL " + url, e); |
138 | 0 | } |
139 | 0 | return result; |
140 | } | |
141 | ||
142 | /** | |
143 | * Create a Test from a URL whose contents are in Latka's XML format | |
144 | * @param url the {@link java.net.URL URL} to fetch | |
145 | * @return a JUnit Test, ready to run, or null if the url can't be resolved | |
146 | */ | |
147 | public static Test createTestFromURL(URL url) { | |
148 | 0 | Test result = null; |
149 | try { | |
150 | 0 | InputSource source = new InputSource(url.toString()); |
151 | 0 | Suite suite = new Suite(url); |
152 | 0 | result = new JUnitTestAdapter(suite, parse(source)); |
153 | 0 | } catch (IOException ioe) { |
154 | 0 | _log.debug("IOException obtaining xml from URL " + url, ioe); |
155 | 0 | } catch (SAXException se) { |
156 | 0 | _log.debug("Problem parsing URL " + url, se); |
157 | 0 | } catch (ParserConfigurationException pce) { |
158 | 0 | _log.debug("Problem determining parser", pce); |
159 | 0 | } |
160 | 0 | return result; |
161 | } | |
162 | ||
163 | /** | |
164 | * Parse the Latka XML document to count the requests | |
165 | * @param xml The inputsource to parse | |
166 | * @throws IOException When an IO occurs reading the document | |
167 | * @throws SAXException When the document is invalid XML | |
168 | * @throws FactoryConfigurationError When the SAX Parser factory can't be | |
169 | * configured correctly | |
170 | * @throws ParserConfigurationException When the SAX Parser can't be | |
171 | * configured correctly | |
172 | * @return the number of tests in the Latka suite | |
173 | */ | |
174 | protected static int parse(InputSource xml) throws IOException, SAXException | |
175 | , FactoryConfigurationError, ParserConfigurationException { | |
176 | 0 | int result = 0; |
177 | 0 | XMLReader reader = null; |
178 | 0 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
179 | 0 | factory.setValidating(false); |
180 | 0 | SAXParser parser = factory.newSAXParser(); |
181 | 0 | reader = parser.getXMLReader(); |
182 | 0 | TestCounter handler = new TestCounter(); |
183 | 0 | reader.setContentHandler(handler); |
184 | 0 | reader.parse(xml); |
185 | 0 | result = handler.getCount(); |
186 | 0 | return result; |
187 | } | |
188 | ||
189 | /** | |
190 | * A SAX Handler to count the number of request tags in the document | |
191 | * | |
192 | * @author Chuck Burdick | |
193 | * @author dIon Gillard | |
194 | * @version | |
195 | * $Id: JUnitTestAdapter.java 155424 2005-02-26 13:09:29Z dirkv $ | |
196 | */ | |
197 | private static class TestCounter extends DefaultHandler { | |
198 | /** number of requests (ie junit tests) */ | |
199 | 0 | private int _count = 0; |
200 | ||
201 | /** | |
202 | * Create a DefaultHandler to count request elements | |
203 | */ | |
204 | 0 | public TestCounter() { |
205 | 0 | _count = 0; |
206 | 0 | } |
207 | ||
208 | /** | |
209 | * process the start of an xml element | |
210 | * @param uri uri | |
211 | * @param localName localName | |
212 | * @param qName qName | |
213 | * @param atts atts | |
214 | */ | |
215 | public void startElement(String uri, String localName, String qName, | |
216 | Attributes atts) { | |
217 | 0 | if (qName.equals("request")) { |
218 | 0 | _count++; |
219 | } | |
220 | 0 | } |
221 | ||
222 | /** | |
223 | * Provides the number of <request> tags | |
224 | * @return the count of request elements found | |
225 | */ | |
226 | public int getCount() { | |
227 | 0 | return _count; |
228 | } | |
229 | } | |
230 | ||
231 | /** | |
232 | * Provides access, post-parsing, of the number of | |
233 | * request elements in the Latka Suite | |
234 | * @return the number of test cases in the Latka suite | |
235 | */ | |
236 | public int countTestCases() { | |
237 | 0 | return _testCount; |
238 | } | |
239 | ||
240 | /** | |
241 | * Run the test, adding results to the provided | |
242 | * {@link junit.framework.TestResult TestResult} | |
243 | * | |
244 | * @param r TestResult to accumulate | |
245 | */ | |
246 | public void run(TestResult r) { | |
247 | 0 | _log.debug("Attempting to perform latka tests"); |
248 | 0 | Latka latka = new Latka(); |
249 | try { | |
250 | 0 | latka.runTests(_latkaSuite, new DefaultLatkaEventInfo(new JUnitEventReporter(r))); |
251 | 0 | } catch (LatkaException e) { |
252 | 0 | _log.error("Unable to execute latka tests", e); |
253 | 0 | } |
254 | 0 | } |
255 | } |