KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > deployment > tests > AbsDeploymentTest


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2003-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AbsDeploymentTest.java,v 1.5 2004/10/08 07:53:18 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.deployment.tests;
27
28 // java
29
import java.io.Reader JavaDoc;
30 import java.io.StringReader JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileReader JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36 import javax.xml.namespace.QName JavaDoc;
37
38 // jonas
39
import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
40 import org.objectweb.jonas_lib.deployment.xml.AbsElement;
41 import org.objectweb.jonas_lib.deployment.xml.Element;
42 import org.objectweb.jonas_lib.deployment.xml.Qname;
43
44 /**
45  * Defines an abstract class for testing the classes built with Digester
46  * @author Florent Benoit
47  */

48 public abstract class AbsDeploymentTest {
49
50     /**
51      * Counter for each kind of element
52      */

53     private Hashtable JavaDoc elementsCounter = null;
54
55     /**
56      * Build a new Test
57      */

58     protected AbsDeploymentTest() {
59         elementsCounter = new Hashtable JavaDoc();
60     }
61
62     /**
63      * Create an xml structure and then parse the resulting xml and check the
64      * result No validation is done if random is set to true
65      * @throws Exception if the stress test is not successfull
66      */

67     public void stress() throws Exception JavaDoc {
68
69         startTest(false);
70
71         for (int i = 0; i < 100; i++) {
72             startTest(true);
73         }
74     }
75
76     // Abstract Methods used by startTest implemented in subclasses
77

78     public abstract AbsElement getTopLevelElement() throws Exception JavaDoc;
79
80     public abstract String JavaDoc parse(Reader JavaDoc reader, String JavaDoc name, boolean validation) throws Exception JavaDoc;
81
82     /**
83      * Defines the function for the specific test
84      * @param random use random or not to fill elements
85      * @throws Exception if the test failed
86      */

87     public void startTest(boolean random) throws Exception JavaDoc {
88         AbsElement ae = getTopLevelElement();
89         fill(ae, random);
90         String JavaDoc xmlOriginal = ae.toXML();
91         String JavaDoc xmlParsed = parse(new StringReader JavaDoc(xmlOriginal), "test", false);
92         checkDiff(xmlOriginal, xmlParsed);
93
94     }
95
96     /**
97      * Gets the xml after digester parsing
98      * @throws Exception if the parsing fail
99      */

100     public void parseElement() throws Exception JavaDoc {
101         AbsElement ae = getTopLevelElement();
102         fill(ae, false);
103         System.out.println("Parsing xml :");
104         System.out.println(ae);
105         String JavaDoc xmlOriginal = ae.toXML();
106         String JavaDoc xmlParsed = parse(new StringReader JavaDoc(xmlOriginal), "test", false);
107         System.out.println("Result = ");
108         System.out.println(xmlParsed);
109     }
110
111     /**
112      * parse with validation from an xml file
113      * @throws Exception if the parsing fail
114      */

115     public void parseXmlfromFile(String JavaDoc fileName) throws Exception JavaDoc {
116         FileReader JavaDoc fileReader = null;
117
118         // Check if the file exists.
119
if (!new File JavaDoc(fileName).exists()) {
120             throw new DeploymentDescException("The file '" + fileName + "' was not found.");
121         }
122         fileReader = new FileReader JavaDoc(fileName);
123         System.out.println("Parsing xml : " + fileName);
124         String JavaDoc xmlParsed = parse(fileReader, fileName, true);
125         System.out.println("Parsing OK Result = ");
126         System.out.println(xmlParsed);
127     }
128
129     /**
130      * Check the difference between original xml and parsed xml
131      * @param xmlOriginal original XML
132      * @param xmlParsed parsed XML
133      * @throws Exception if there is a difference between original and parsed
134      * XML
135      */

136     protected void checkDiff(String JavaDoc xmlOriginal, String JavaDoc xmlParsed) throws Exception JavaDoc {
137         StringTokenizer JavaDoc stOri = new StringTokenizer JavaDoc(xmlOriginal, "\n");
138         StringTokenizer JavaDoc stPardsed = new StringTokenizer JavaDoc(xmlParsed, "\n");
139
140         // Compare line by line
141
while (stOri.hasMoreTokens() && stPardsed.hasMoreTokens()) {
142             String JavaDoc lineOri = stOri.nextToken();
143             String JavaDoc lineparsed = stPardsed.nextToken();
144             if (!lineOri.equals(lineparsed)) {
145                 System.err.println("Failure in xml :");
146                 System.err.println(xmlOriginal);
147                 throw new Exception JavaDoc("Line : " + lineOri + " is different than parsed value: " + lineparsed);
148             }
149         }
150     }
151
152     /**
153      * Fill the structure of the given element. Fill randomly if random is set
154      * to true
155      * @param element element to fill
156      * @param random determines if the element must be filled randomly or not
157      * @throws Exception if the element can not be filled
158      */

159     public void fill(Element element, boolean random) throws Exception JavaDoc {
160
161         // Get setters of the element
162
Method JavaDoc[] methods = element.getClass().getMethods();
163
164         // Analyze if it is Set or Add Method
165
for (int i = 0; i < methods.length; i++) {
166             // Get name
167
Method JavaDoc method = methods[i];
168             String JavaDoc name = method.getName();
169             Class JavaDoc[] argsMethod = null;
170             argsMethod = method.getParameterTypes();
171
172             // One argument method
173
if ((argsMethod.length == 1) && (name.startsWith("set") || name.startsWith("add"))) {
174                 Class JavaDoc cl = argsMethod[0];
175                 if (cl.getName().equals("java.lang.String")) {
176                     // Test if it's one argument with String type :
177
fillString(element, method, random);
178                 } else if (cl.getName().equals("javax.xml.namespace.QName")) {
179                     fillQName(element, method, random);
180                 } else {
181                     // It's a subelement
182
if (name.startsWith("set")) {
183                         setElement(element, method, argsMethod, random);
184                     } else {
185                         addElement(element, method, argsMethod, random);
186                     }
187                 }
188             }
189
190         }
191
192     }
193
194     /**
195      * Random for returning true or false
196      * @return true or false with random
197      */

198     protected boolean aleatOK() {
199         return (Math.random() > 0.5);
200     }
201
202     /**
203      * Gives a number between 0 and 5
204      * @return a random intger number between 0 and 5
205      */

206     protected int nbAleat() {
207         return (int) (Math.random() * 5);
208     }
209
210     /**
211      * Add to an element its sub element Add many times a sub-element if random
212      * is not set to true
213      * @param element element on which we have to add sub elements
214      * @param method method of the element (determine type of the sub element
215      * @param argsMethod arguments of the method
216      * @param random use random or not
217      * @throws Exception if the subelement can not be added
218      */

219     protected void addElement(Element element, Method JavaDoc method, Class JavaDoc[] argsMethod, boolean random) throws Exception JavaDoc {
220         int nb = 1;
221         if (random) {
222             nb = nbAleat();
223         }
224         for (int i = 0; i < nb; i++) {
225             setElement(element, method, argsMethod, random);
226         }
227     }
228
229     /**
230      * Set the subelement of an element The subelement may not be set if random
231      * is used
232      * @param element element on which we have to add sub elements
233      * @param method method of the element (determine type of the sub element
234      * @param argsMethod arguments of the method
235      * @param random use random or not
236      * @throws Exception if the subelement can not be set
237      */

238     protected void setElement(Element element, Method JavaDoc method, Class JavaDoc[] argsMethod, boolean random) throws Exception JavaDoc {
239         if (random && !aleatOK()) {
240             return;
241         }
242
243         String JavaDoc name = method.getName();
244
245         Class JavaDoc cl = argsMethod[0];
246         String JavaDoc className = cl.getName();
247
248         // Element object ?
249
if (Element.class.isAssignableFrom(cl)) {
250             // Create required object
251
Object JavaDoc subElement = cl.newInstance();
252
253             // Add object on top level object
254
method.invoke(element, new Object JavaDoc[] {subElement});
255
256             // Fill values of this element
257
fill((Element) subElement, random);
258
259             // Qname is a particular type, need to set the name
260
if (cl.isAssignableFrom(Qname.class)) {
261                 // Transform name into Qname
262
String JavaDoc qNameElement = convertUpperCaseToXml(name.substring(3, name.length()));
263                 ((Qname) subElement).setName(qNameElement);
264             }
265
266         }
267     }
268
269     /**
270      * Set the string attribute of the given element
271      * @param element element on which we have to set the string
272      * @param method method of the element (determine type of the sub element
273      * @param random use random or not
274      * @throws Exception if the String attribute can not be added
275      */

276     protected void fillString(Element element, Method JavaDoc method, boolean random) throws Exception JavaDoc {
277         if (random && !aleatOK()) {
278             return;
279         }
280         method.invoke(element, (Object JavaDoc[]) new String JavaDoc[] {getNameCounterForElement(element, method)});
281     }
282
283     /**
284      * Set the QName attribute of the given element
285      * @param element element on which we have to set the string
286      * @param method method of the element (determine type of the sub element
287      * @param random use random or not
288      * @throws Exception if the QName can not be set
289      */

290     protected void fillQName(Element element, Method JavaDoc method, boolean random) throws Exception JavaDoc {
291         if (random && !aleatOK()) {
292             return;
293         }
294         QName JavaDoc qName = new QName JavaDoc("prefix:Test", getNameCounterForElement(element, method));
295         method.invoke(element, (Object JavaDoc[]) new QName JavaDoc[] {qName});
296     }
297
298     /**
299      * Gives a Name + counter for a type of an element This is used to add
300      * counter when adding xml attributes Only use in order to make easier the
301      * read of the parsed XML file
302      * @param element the given element for which we want a counter
303      * @param method the name of the string to add
304      * @return the Name + counter for the specified element type
305      */

306     protected String JavaDoc getNameCounterForElement(Element element, Method JavaDoc method) {
307         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
308
309         // Extract package.classname
310
String JavaDoc name = method.getName().substring(3, method.getName().length());
311         String JavaDoc packageClassname = element.getClass().getPackage().getName();
312         String JavaDoc fullClass = packageClassname + "." + name;
313
314         // Existing counter ?
315
Integer JavaDoc counter = (Integer JavaDoc) elementsCounter.get(fullClass);
316         if (counter == null) {
317             // init counter as it don't already exist
318
counter = new Integer JavaDoc(0);
319         } else {
320             // Increment
321
counter = new Integer JavaDoc(counter.intValue() + 1);
322         }
323         elementsCounter.put(fullClass, counter);
324
325         // return value
326
return name + counter.intValue();
327     }
328
329     /**
330      * Convert the name of an element into its xml string representation example :
331      * WebApp --> web-app
332      * @param name the name of the element to convert
333      * @return the xml string representation of an element
334      */

335     protected String JavaDoc convertUpperCaseToXml(String JavaDoc name) {
336         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
337         sb.append(Character.toLowerCase(name.charAt(0)));
338
339         for (int i = 1; i < name.length(); i++) {
340             if (Character.isUpperCase(name.charAt(i))) {
341                 sb.append("-");
342             }
343             sb.append(Character.toLowerCase(name.charAt(i)));
344
345         }
346         return sb.toString();
347
348     }
349
350 }
Popular Tags