1 24 25 package org.objectweb.cjdbc.scenario.tools.testlet.xml; 26 27 import java.lang.reflect.Constructor ; 28 import java.net.URL ; 29 import java.util.Enumeration ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import junit.framework.TestCase; 34 import junit.framework.TestFailure; 35 import junit.framework.TestResult; 36 37 import org.dom4j.Document; 38 import org.dom4j.Element; 39 import org.dom4j.Node; 40 import org.dom4j.io.SAXReader; 41 import org.objectweb.cjdbc.scenario.templates.Template; 42 import org.objectweb.cjdbc.scenario.tools.components.ComponentInterface; 43 import org.objectweb.cjdbc.scenario.tools.components.backend.DatabaseManager; 44 import org.objectweb.cjdbc.scenario.tools.components.controller.ControllerManager; 45 import org.objectweb.cjdbc.scenario.tools.testlet.AbstractConnectionTestLet; 46 47 53 public class XmlBasedRegressionTesting extends TestCase 54 { 55 56 private Document doc; 57 private DatabaseManager backendManager; 58 private ControllerManager controllerManager; 59 private String scenarioFileName; 60 61 67 public XmlBasedRegressionTesting() 68 { 69 URL url = Class .class.getResource("/xml/scenario.xml"); 70 if(url==null) 71 throw new RuntimeException ("Cannot find xml file"); 72 scenarioFileName = url.getFile(); 73 } 74 75 79 public void setUp() 80 { 81 try 82 { 83 SAXReader xmlReader = new SAXReader(); 84 this.doc = xmlReader.read(scenarioFileName); 85 86 backendManager = new DatabaseManager(); 87 controllerManager = new ControllerManager(); 88 List database = doc.selectNodes("//scenario/configuration/database"); 89 for (Iterator i = database.iterator(); i.hasNext();) 90 { 91 Element data = (Element) i.next(); 92 System.out.println("Starting Backend:" + data.valueOf("@port") + ":" 93 + data.valueOf("@data")); 94 ComponentInterface hsqldb = backendManager.instanciateProcess(data 95 .valueOf("@port")); 96 System.out.println("Echo port:" + hsqldb.getPort() + " is started:" 97 + backendManager.isStarted(data.valueOf("@port"))); 98 hsqldb.loadDatabase(data.valueOf("@data")); 99 } 100 101 List controllers = doc.selectNodes("//scenario/configuration/controller"); 102 for (Iterator i = controllers.iterator(); i.hasNext();) 103 { 104 Element data = (Element) i.next(); 106 System.out.println("Starting Controller:" + data.valueOf("@port")); 107 ComponentInterface controller = controllerManager 108 .instanciateProcess(data.valueOf("@port")); 109 110 Iterator iter = data.elementIterator(); 112 while (iter.hasNext()) 113 { 114 controller.loadDatabase(((Node) iter.next()).valueOf("@data")); 115 } 116 } 117 } 118 catch (Exception e) 119 { 120 fail(e.getMessage()); 121 } 122 123 } 124 125 131 public void testMe() throws Exception 132 { 133 List lets = doc.selectNodes("//scenario/testlet"); 134 for (Iterator i = lets.iterator(); i.hasNext();) 135 { 136 Element let = (Element) i.next(); 137 String test = "org.objectweb.cjdbc.scenario.tools.testlet." 138 + let.valueOf("@class"); 139 System.out.println("Starting test:" + test); 140 Constructor [] constructors = Class.forName(test).getConstructors(); 141 Constructor constructor = null; 142 System.out.println(constructors.length); 143 for (int j = 0; j < constructors.length; j++) 144 { 145 constructor = constructors[j]; 146 Class [] klasses = constructor.getParameterTypes(); 147 if (klasses.length == 0) 148 break; 149 } 150 System.out.println("Constructor is:" + constructor.getName()); 151 AbstractConnectionTestLet testLet = (AbstractConnectionTestLet) constructor 152 .newInstance(new Object []{Template.getCJDBCConnection()}); 153 testLet.execute(); 154 TestResult result = new TestResult(); 155 testLet.run(result); 156 System.out.println("Failures" + result.failureCount()); 157 Enumeration enumaration = result.errors(); 158 while (enumaration.hasMoreElements()) 159 { 160 System.out.println(enumaration.nextElement()); 161 } 162 enumaration = result.failures(); 163 while (enumaration.hasMoreElements()) 164 { 165 TestFailure elem = (TestFailure) enumaration.nextElement(); 166 elem.thrownException().printStackTrace(); 167 System.out.println(elem.trace()); 168 System.out.println(elem); 170 } 171 System.out.println("Test was successful:" + result.wasSuccessful()); 172 } 173 } 174 175 179 public void tearDown() 180 { 181 controllerManager.stopAll(); 182 backendManager.stopAll(); 183 } 184 } | Popular Tags |