KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > testlet > xml > XmlBasedRegressionTesting


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.testlet.xml;
26
27 import java.lang.reflect.Constructor JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
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 /**
48  * This class defines a XmlBasedRegressionTesting
49  *
50  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
51  * @version 1.0
52  */

53 public class XmlBasedRegressionTesting extends TestCase
54 {
55
56   private Document doc;
57   private DatabaseManager backendManager;
58   private ControllerManager controllerManager;
59   private String JavaDoc scenarioFileName;
60
61   /**
62    *
63    * Creates a new <code>XmlBasedRegressionTesting</code> object
64    *
65    *
66    */

67   public XmlBasedRegressionTesting()
68   {
69     URL JavaDoc url = Class JavaDoc.class.getResource("/xml/scenario.xml");
70     if(url==null)
71       throw new RuntimeException JavaDoc("Cannot find xml file");
72     scenarioFileName = url.getFile();
73   }
74
75   /**
76    *
77    * @see junit.framework.TestCase#setUp()
78    */

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 JavaDoc database = doc.selectNodes("//scenario/configuration/database");
89       for (Iterator JavaDoc 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 JavaDoc controllers = doc.selectNodes("//scenario/configuration/controller");
102       for (Iterator JavaDoc i = controllers.iterator(); i.hasNext();)
103       {
104         // Starting controller
105
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         // Loading virtual databases
111
Iterator JavaDoc iter = data.elementIterator();
112         while (iter.hasNext())
113         {
114           controller.loadDatabase(((Node) iter.next()).valueOf("@data"));
115         }
116       }
117     }
118     catch (Exception JavaDoc e)
119     {
120       fail(e.getMessage());
121     }
122
123   }
124
125   /**
126    *
127    * Run main test
128    *
129    * @throws Exception if fails
130    */

131   public void testMe() throws Exception JavaDoc
132   {
133     List JavaDoc lets = doc.selectNodes("//scenario/testlet");
134     for (Iterator JavaDoc i = lets.iterator(); i.hasNext();)
135     {
136       Element let = (Element) i.next();
137       String JavaDoc test = "org.objectweb.cjdbc.scenario.tools.testlet."
138           + let.valueOf("@class");
139       System.out.println("Starting test:" + test);
140       Constructor JavaDoc[] constructors = Class.forName(test).getConstructors();
141       Constructor JavaDoc constructor = null;
142       System.out.println(constructors.length);
143       for (int j = 0; j < constructors.length; j++)
144       {
145         constructor = constructors[j];
146         Class JavaDoc[] 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 JavaDoc[]{Template.getCJDBCConnection()});
153       testLet.execute();
154       TestResult result = new TestResult();
155       testLet.run(result);
156       System.out.println("Failures" + result.failureCount());
157       Enumeration JavaDoc 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();
169
System.out.println(elem);
170       }
171       System.out.println("Test was successful:" + result.wasSuccessful());
172     }
173   }
174
175   /**
176    *
177    * @see junit.framework.TestCase#tearDown()
178    */

179   public void tearDown()
180   {
181     controllerManager.stopAll();
182     backendManager.stopAll();
183   }
184 }
Popular Tags