1 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 ; 64 65 66 72 public class TemplateTestSuite extends TestSuite { 73 74 private String inputDir, refDir; 75 private Map configParams = new LinkedHashMap(); 76 77 public static TestSuite suite() throws Exception { 78 return new TemplateTestSuite(); 79 } 80 81 public TemplateTestSuite() throws Exception { 82 NodeModel.useJaxenXPathSupport(); 83 readConfig(); 84 } 85 86 void readConfig() throws Exception { 87 java.net.URL url = TemplateTestSuite.class.getResource("testcases.xml"); 88 File f = new File(url.getFile()); 89 readConfig(f); 90 } 91 92 95 public void readConfig(File f) throws Exception { 96 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 97 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 getTextInElement(Element e) { 121 StringBuffer buf = new StringBuffer (); 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 140 TestCase createTestCaseFromNode(Element e) throws Exception { 141 String filename = e.getAttribute("filename"); 142 String name = e.getAttribute("name"); 143 String classname = e.getAttribute("class"); 144 if (classname != null && classname.length() >0) { 145 Class cl = Class.forName(classname); 146 Constructor cons = cl.getConstructor(new Class [] {String .class, String .class}); 147 return (TestCase) cons.newInstance(new Object [] {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 key = entry.getKey().toString(); 153 String 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 [] args) throws Exception { 172 173 junit.textui.TestRunner.run(new TemplateTestSuite()); 174 } 177 } 178 | Popular Tags |