1 25 26 package org.objectweb.jonas_lib.deployment.tests; 27 28 import java.io.Reader ; 30 import java.io.StringReader ; 31 import java.io.File ; 32 import java.io.FileReader ; 33 import java.lang.reflect.Method ; 34 import java.util.Hashtable ; 35 import java.util.StringTokenizer ; 36 import javax.xml.namespace.QName ; 37 38 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 48 public abstract class AbsDeploymentTest { 49 50 53 private Hashtable elementsCounter = null; 54 55 58 protected AbsDeploymentTest() { 59 elementsCounter = new Hashtable (); 60 } 61 62 67 public void stress() throws Exception { 68 69 startTest(false); 70 71 for (int i = 0; i < 100; i++) { 72 startTest(true); 73 } 74 } 75 76 78 public abstract AbsElement getTopLevelElement() throws Exception ; 79 80 public abstract String parse(Reader reader, String name, boolean validation) throws Exception ; 81 82 87 public void startTest(boolean random) throws Exception { 88 AbsElement ae = getTopLevelElement(); 89 fill(ae, random); 90 String xmlOriginal = ae.toXML(); 91 String xmlParsed = parse(new StringReader (xmlOriginal), "test", false); 92 checkDiff(xmlOriginal, xmlParsed); 93 94 } 95 96 100 public void parseElement() throws Exception { 101 AbsElement ae = getTopLevelElement(); 102 fill(ae, false); 103 System.out.println("Parsing xml :"); 104 System.out.println(ae); 105 String xmlOriginal = ae.toXML(); 106 String xmlParsed = parse(new StringReader (xmlOriginal), "test", false); 107 System.out.println("Result = "); 108 System.out.println(xmlParsed); 109 } 110 111 115 public void parseXmlfromFile(String fileName) throws Exception { 116 FileReader fileReader = null; 117 118 if (!new File (fileName).exists()) { 120 throw new DeploymentDescException("The file '" + fileName + "' was not found."); 121 } 122 fileReader = new FileReader (fileName); 123 System.out.println("Parsing xml : " + fileName); 124 String xmlParsed = parse(fileReader, fileName, true); 125 System.out.println("Parsing OK Result = "); 126 System.out.println(xmlParsed); 127 } 128 129 136 protected void checkDiff(String xmlOriginal, String xmlParsed) throws Exception { 137 StringTokenizer stOri = new StringTokenizer (xmlOriginal, "\n"); 138 StringTokenizer stPardsed = new StringTokenizer (xmlParsed, "\n"); 139 140 while (stOri.hasMoreTokens() && stPardsed.hasMoreTokens()) { 142 String lineOri = stOri.nextToken(); 143 String lineparsed = stPardsed.nextToken(); 144 if (!lineOri.equals(lineparsed)) { 145 System.err.println("Failure in xml :"); 146 System.err.println(xmlOriginal); 147 throw new Exception ("Line : " + lineOri + " is different than parsed value: " + lineparsed); 148 } 149 } 150 } 151 152 159 public void fill(Element element, boolean random) throws Exception { 160 161 Method [] methods = element.getClass().getMethods(); 163 164 for (int i = 0; i < methods.length; i++) { 166 Method method = methods[i]; 168 String name = method.getName(); 169 Class [] argsMethod = null; 170 argsMethod = method.getParameterTypes(); 171 172 if ((argsMethod.length == 1) && (name.startsWith("set") || name.startsWith("add"))) { 174 Class cl = argsMethod[0]; 175 if (cl.getName().equals("java.lang.String")) { 176 fillString(element, method, random); 178 } else if (cl.getName().equals("javax.xml.namespace.QName")) { 179 fillQName(element, method, random); 180 } else { 181 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 198 protected boolean aleatOK() { 199 return (Math.random() > 0.5); 200 } 201 202 206 protected int nbAleat() { 207 return (int) (Math.random() * 5); 208 } 209 210 219 protected void addElement(Element element, Method method, Class [] argsMethod, boolean random) throws Exception { 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 238 protected void setElement(Element element, Method method, Class [] argsMethod, boolean random) throws Exception { 239 if (random && !aleatOK()) { 240 return; 241 } 242 243 String name = method.getName(); 244 245 Class cl = argsMethod[0]; 246 String className = cl.getName(); 247 248 if (Element.class.isAssignableFrom(cl)) { 250 Object subElement = cl.newInstance(); 252 253 method.invoke(element, new Object [] {subElement}); 255 256 fill((Element) subElement, random); 258 259 if (cl.isAssignableFrom(Qname.class)) { 261 String qNameElement = convertUpperCaseToXml(name.substring(3, name.length())); 263 ((Qname) subElement).setName(qNameElement); 264 } 265 266 } 267 } 268 269 276 protected void fillString(Element element, Method method, boolean random) throws Exception { 277 if (random && !aleatOK()) { 278 return; 279 } 280 method.invoke(element, (Object []) new String [] {getNameCounterForElement(element, method)}); 281 } 282 283 290 protected void fillQName(Element element, Method method, boolean random) throws Exception { 291 if (random && !aleatOK()) { 292 return; 293 } 294 QName qName = new QName ("prefix:Test", getNameCounterForElement(element, method)); 295 method.invoke(element, (Object []) new QName [] {qName}); 296 } 297 298 306 protected String getNameCounterForElement(Element element, Method method) { 307 StringBuffer sb = new StringBuffer (); 308 309 String name = method.getName().substring(3, method.getName().length()); 311 String packageClassname = element.getClass().getPackage().getName(); 312 String fullClass = packageClassname + "." + name; 313 314 Integer counter = (Integer ) elementsCounter.get(fullClass); 316 if (counter == null) { 317 counter = new Integer (0); 319 } else { 320 counter = new Integer (counter.intValue() + 1); 322 } 323 elementsCounter.put(fullClass, counter); 324 325 return name + counter.intValue(); 327 } 328 329 335 protected String convertUpperCaseToXml(String name) { 336 StringBuffer sb = new StringBuffer (); 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 |