001: /*
002: * Copyright (c) 2005 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.testcase;
054:
055: import junit.framework.*;
056: import freemarker.ext.dom.NodeModel;
057: import freemarker.testcase.servlets.TestJspTaglibs;
058: import javax.xml.parsers.*;
059: import java.util.*;
060: import java.io.*;
061: import java.lang.reflect.*;
062: import org.w3c.dom.*;
063: import org.xml.sax.SAXException;
064:
065: /**
066: * Test suite for FreeMarker. The suite conforms to interface expected by
067: * <a href="http://junit.sourceforge.net/" target="_top">JUnit</a>.
068: *
069: * @version $Id: TemplateTestSuite.java,v 1.8 2005/06/11 15:18:26 revusky Exp $
070: */
071: public class TemplateTestSuite extends TestSuite {
072:
073: private String inputDir, refDir;
074: private Map configParams = new LinkedHashMap();
075:
076: public static TestSuite suite() throws Exception {
077: return new TemplateTestSuite();
078: }
079:
080: public TemplateTestSuite() throws Exception {
081: NodeModel.useJaxenXPathSupport();
082: readConfig();
083: }
084:
085: void readConfig() throws Exception {
086: java.net.URL url = TemplateTestSuite.class
087: .getResource("testcases.xml");
088: File f = new File(url.getFile());
089: readConfig(f);
090: }
091:
092: /**
093: * Read the testcase configurations file and build up the test suite
094: */
095: public void readConfig(File f) throws Exception {
096: DocumentBuilderFactory dbf = DocumentBuilderFactory
097: .newInstance();
098: //dbf.setValidating(true);
099: DocumentBuilder db = dbf.newDocumentBuilder();
100: Document d = db.parse(f);
101: Element root = d.getDocumentElement();
102: NodeList children = root.getChildNodes();
103: for (int i = 0; i < children.getLength(); i++) {
104: Node n = children.item(i);
105: if (n.getNodeType() == Node.ELEMENT_NODE) {
106: if (n.getNodeName().equals("config")) {
107: NamedNodeMap atts = n.getAttributes();
108: for (int j = 0; j < atts.getLength(); j++) {
109: Attr att = (Attr) atts.item(j);
110: configParams.put(att.getName(), att.getValue());
111: }
112: }
113: if (n.getNodeName().equals("testcase")) {
114: TestCase tc = createTestCaseFromNode((Element) n);
115: addTest(tc);
116: }
117: }
118: }
119: }
120:
121: String getTextInElement(Element e) {
122: StringBuffer buf = new StringBuffer();
123: NodeList children = e.getChildNodes();
124: for (int i = 0; i < children.getLength(); i++) {
125: Node n = children.item(i);
126: short type = n.getNodeType();
127: if (type == Node.TEXT_NODE
128: || type == Node.CDATA_SECTION_NODE) {
129: buf.append(n.getNodeValue());
130: }
131: }
132: return buf.toString();
133: }
134:
135: /**
136: * Takes as in put the dom node that specifies the testcase
137: * and instantiates a testcase. If class is not specified,
138: * it uses the TemplateTestCase class. If the class is specified,
139: * it must be a TestCase class and have a constructor that
140: * takes two strings as parameters.
141: */
142: TestCase createTestCaseFromNode(Element e) throws Exception {
143: String filename = e.getAttribute("filename");
144: String name = e.getAttribute("name");
145: String classname = e.getAttribute("class");
146: if (classname != null && classname.length() > 0) {
147: Class cl = Class.forName(classname);
148: Constructor cons = cl.getConstructor(new Class[] {
149: String.class, String.class });
150: return (TestCase) cons.newInstance(new Object[] { name,
151: filename });
152: }
153: TemplateTestCase result = new TemplateTestCase(name, filename);
154: for (Iterator it = configParams.entrySet().iterator(); it
155: .hasNext();) {
156: Map.Entry entry = (Map.Entry) it.next();
157: String key = entry.getKey().toString();
158: String value = entry.getValue().toString();
159: System.out.println("Setting " + key + " to " + value);
160: result.setConfigParam(entry.getKey().toString(), entry
161: .getValue().toString());
162: }
163: NodeList configs = e.getElementsByTagName("config");
164: for (int i = 0; i < configs.getLength(); i++) {
165: NamedNodeMap atts = configs.item(i).getAttributes();
166: for (int j = 0; j < atts.getLength(); j++) {
167: Attr att = (Attr) atts.item(j);
168: result.setConfigParam(att.getName(), att.getValue());
169: }
170: }
171: return result;
172: }
173:
174: public static void main(String[] args) throws Exception {
175:
176: junit.textui.TestRunner.run(new TemplateTestSuite());
177: // junit.swingui.TestRunner.run (TemplateTestSuite.class);
178: // junit.awtui.TestRunner.run (TemplateTestSuite.class);
179: }
180: }
|