KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > testcase > TemplateTestSuite


1 /*
2  * Copyright (c) 2005 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.testcase;
54
55 import junit.framework.*;
56 import freemarker.ext.dom.NodeModel;
57 import freemarker.testcase.servlets.TestJspTaglibs;
58 import javax.xml.parsers.*;
59 import java.util.*;
60 import java.io.*;
61 import java.lang.reflect.*;
62 import org.w3c.dom.*;
63 import org.xml.sax.SAXException JavaDoc;
64
65
66 /**
67  * Test suite for FreeMarker. The suite conforms to interface expected by
68  * <a HREF="http://junit.sourceforge.net/" target="_top">JUnit</a>.
69  *
70  * @version $Id: TemplateTestSuite.java,v 1.8 2005/06/11 15:18:26 revusky Exp $
71  */

72 public class TemplateTestSuite extends TestSuite {
73     
74     private String JavaDoc inputDir, refDir;
75     private Map configParams = new LinkedHashMap();
76     
77     public static TestSuite suite() throws Exception JavaDoc {
78         return new TemplateTestSuite();
79     }
80     
81     public TemplateTestSuite() throws Exception JavaDoc {
82         NodeModel.useJaxenXPathSupport();
83         readConfig();
84     }
85     
86     void readConfig() throws Exception JavaDoc {
87         java.net.URL JavaDoc url = TemplateTestSuite.class.getResource("testcases.xml");
88         File f = new File(url.getFile());
89         readConfig(f);
90     }
91     
92     /**
93      * Read the testcase configurations file and build up the test suite
94      */

95     public void readConfig(File f) throws Exception JavaDoc {
96         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
97         //dbf.setValidating(true);
98
DocumentBuilder db = dbf.newDocumentBuilder();
99         Document d = db.parse(f);
100         Element root = d.getDocumentElement();
101         NodeList children = root.getChildNodes();
102         for (int i=0; i<children.getLength(); i++) {
103             Node n = children.item(i);
104             if (n.getNodeType() == Node.ELEMENT_NODE) {
105                 if (n.getNodeName().equals("config")) {
106                     NamedNodeMap atts = n.getAttributes();
107                     for (int j=0; j<atts.getLength(); j++) {
108                         Attr att = (Attr) atts.item(j);
109                         configParams.put(att.getName(), att.getValue());
110                     }
111                 }
112                 if (n.getNodeName().equals("testcase")) {
113                     TestCase tc = createTestCaseFromNode((Element) n);
114                     addTest(tc);
115                 }
116             }
117         }
118     }
119     
120     String JavaDoc getTextInElement(Element e) {
121         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
122         NodeList children = e.getChildNodes();
123         for(int i=0; i<children.getLength(); i++) {
124             Node n = children.item(i);
125             short type = n.getNodeType();
126             if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
127                 buf.append(n.getNodeValue());
128             }
129         }
130         return buf.toString();
131     }
132     
133     /**
134      * Takes as in put the dom node that specifies the testcase
135      * and instantiates a testcase. If class is not specified,
136      * it uses the TemplateTestCase class. If the class is specified,
137      * it must be a TestCase class and have a constructor that
138      * takes two strings as parameters.
139      */

140     TestCase createTestCaseFromNode(Element e) throws Exception JavaDoc {
141         String JavaDoc filename = e.getAttribute("filename");
142         String JavaDoc name = e.getAttribute("name");
143         String JavaDoc classname = e.getAttribute("class");
144         if (classname != null && classname.length() >0) {
145             Class JavaDoc cl = Class.forName(classname);
146             Constructor cons = cl.getConstructor(new Class JavaDoc[] {String JavaDoc.class, String JavaDoc.class});
147             return (TestCase) cons.newInstance(new Object JavaDoc [] {name, filename});
148         }
149         TemplateTestCase result = new TemplateTestCase(name, filename);
150         for (Iterator it=configParams.entrySet().iterator(); it.hasNext();) {
151             Map.Entry entry = (Map.Entry) it.next();
152             String JavaDoc key = entry.getKey().toString();
153             String JavaDoc value = entry.getValue().toString();
154             System.out.println("Setting " + key + " to " + value);
155             result.setConfigParam(entry.getKey().toString(), entry.getValue().toString());
156         }
157         NodeList configs = e.getElementsByTagName("config");
158         for (int i=0; i<configs.getLength(); i++) {
159             NamedNodeMap atts = configs.item(i).getAttributes();
160             for (int j=0; j<atts.getLength(); j++) {
161                 Attr att = (Attr) atts.item(j);
162                 result.setConfigParam(att.getName(), att.getValue());
163             }
164         }
165         return result;
166     }
167     
168     
169     
170
171     public static void main (String JavaDoc[] args) throws Exception JavaDoc {
172         
173         junit.textui.TestRunner.run(new TemplateTestSuite());
174 // junit.swingui.TestRunner.run (TemplateTestSuite.class);
175
// junit.awtui.TestRunner.run (TemplateTestSuite.class);
176
}
177 }
178
Popular Tags