KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 // jaxp imports
25
import javax.xml.parsers.FactoryConfigurationError JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import javax.xml.parsers.SAXParser JavaDoc;
28 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.XMLReader JavaDoc;
41 import org.xml.sax.helpers.DefaultHandler JavaDoc;
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     private static final Category _log = Category.getInstance(
57         JUnitTestAdapter.class);
58
59     /** The latka {@link org.apache.commons.latka.Suite Suite} to be run*/
60     private Suite _latkaSuite = null;
61     /** the number of tests in the suite */
62     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     protected JUnitTestAdapter(Suite suite, int testCount) {
72         _latkaSuite = suite;
73         _testCount = testCount;
74     }
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 JavaDoc fileName) {
82         Test result = null;
83         File JavaDoc file = new File JavaDoc(fileName);
84         if (file.exists()) {
85             result = createTestFromFile(file);
86         } else {
87             _log.debug("Input file " + file.getAbsolutePath()
88                 + " does not exist");
89         }
90         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 JavaDoc file) {
99         Test result = null;
100         try {
101             result = createTestFromURL(file.toURL());
102         } catch (MalformedURLException JavaDoc e) {
103             _log.debug("Could not access input file", e);
104         }
105         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 JavaDoc resourceName) {
117         Test result = null;
118         ClassLoader JavaDoc loader = JUnitTestAdapter.class.getClassLoader();
119         URL JavaDoc resource = loader.getResource(resourceName);
120         if (resource != null) {
121             result = createTestFromURL(resource);
122         }
123         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 JavaDoc url) {
133         Test result = null;
134         try {
135             result = createTestFromURL(new URL JavaDoc(url));
136         } catch (MalformedURLException JavaDoc e) {
137             _log.debug("Unable to create URL " + url, e);
138         }
139         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 JavaDoc url) {
148         Test result = null;
149         try {
150             InputSource JavaDoc source = new InputSource JavaDoc(url.toString());
151             Suite suite = new Suite(url);
152             result = new JUnitTestAdapter(suite, parse(source));
153         } catch (IOException JavaDoc ioe) {
154             _log.debug("IOException obtaining xml from URL " + url, ioe);
155         } catch (SAXException JavaDoc se) {
156             _log.debug("Problem parsing URL " + url, se);
157         } catch (ParserConfigurationException JavaDoc pce) {
158             _log.debug("Problem determining parser", pce);
159         }
160         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 JavaDoc xml) throws IOException JavaDoc, SAXException JavaDoc
175         , FactoryConfigurationError JavaDoc, ParserConfigurationException JavaDoc {
176         int result = 0;
177         XMLReader JavaDoc reader = null;
178         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
179         factory.setValidating(false);
180         SAXParser JavaDoc parser = factory.newSAXParser();
181         reader = parser.getXMLReader();
182         TestCounter handler = new TestCounter();
183         reader.setContentHandler(handler);
184         reader.parse(xml);
185         result = handler.getCount();
186         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 JavaDoc {
198         /** number of requests (ie junit tests) */
199         private int _count = 0;
200       
201         /**
202          * Create a DefaultHandler to count request elements
203          */

204         public TestCounter() {
205             _count = 0;
206         }
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 JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
216                                  Attributes JavaDoc atts) {
217             if (qName.equals("request")) {
218                 _count++;
219             }
220         }
221         
222         /**
223          * Provides the number of <request> tags
224          * @return the count of request elements found
225          */

226         public int getCount() {
227             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         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         _log.debug("Attempting to perform latka tests");
248         Latka latka = new Latka();
249         try {
250             latka.runTests(_latkaSuite, new DefaultLatkaEventInfo(new JUnitEventReporter(r)));
251         } catch (LatkaException e) {
252             _log.error("Unable to execute latka tests", e);
253         }
254     }
255 }
Popular Tags