1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/test/com/sun/ebxml/registry/conformance/TestSupport.java,v 1.11 2002/12/27 13:36:50 ritzmann Exp $ 3 */ 4 package com.sun.ebxml.registry.conformance; 5 6 import java.util.*; 7 import java.io.*; 8 import java.util.zip.*; 9 10 import javax.xml.soap.*; 11 12 import org.xml.sax.*; 13 import org.apache.xerces.parsers.*; 14 15 import junit.framework.*; 16 import com.sun.ebxml.registry.interfaces.soap.*; 17 import com.sun.ebxml.registry.security.authentication.*; 18 19 /*** 20 * 21 * @author Michal Zaremba 22 */ 23 public class TestSupport { 24 25 private String conforDir, resultsDir, sysSeparator; 26 27 public TestSupport() { 28 sysSeparator = System.getProperty("file.separator"); 29 conforDir = "misc" + sysSeparator + "samples" + 30 sysSeparator + "conformance"; 31 32 resultsDir = "misc" + sysSeparator + "samples" + 33 sysSeparator + "conformance" + sysSeparator + "results"; 34 } 35 36 public TestSupport(String sourceDir, String destinationDir) { 37 sysSeparator = System.getProperty("file.separator"); 38 conforDir = sourceDir; 39 resultsDir = destinationDir; 40 } 41 42 public SOAPSender connectRegistry(String url) throws Exception{ 43 SOAPSender sender; 44 AuthenticationServiceImpl authc; 45 try { 46 sender = new SOAPSender(); 47 authc = AuthenticationServiceImpl.getInstance(); 48 sender.setAlias(authc.ALIAS_REGISTRY_OPERATOR); 49 sender.setLocalCall(true); 50 sender.setRegistryURL(url); 51 sender.setKeyPassword(null); 52 } catch (Exception e) { 53 throw new Exception("Failed to connect registry", e); 54 } 55 return sender; 56 } 57 58 /*** 59 * Submits file to registry and test if response either Success other Failure 60 */ 61 public SOAPMessage submitFileToRegistry(SOAPSender sender, String fileName) throws Exception { 62 SOAPMessage reply; 63 sender.setRequestFileName(conforDir + sysSeparator + fileName); 64 reply = sender.send(); 65 Assert.assertTrue(reply != null); 66 writeResponseToFile(reply, resultsDir + sysSeparator + "Response_" + fileName); 67 String registryResponseValue = getAttributeValue(reply, "RegistryResponse", "status"); 68 Assert.assertTrue(registryResponseValue.equalsIgnoreCase("Success")); 69 return reply; 70 } 71 72 /*** 73 * The method writes the SOAP Message down to file 74 * 75 */ 76 public void writeResponseToFile(SOAPMessage reply, String filePath) throws Exception{ 77 try { 78 if (reply != null) { 79 FileOutputStream responseFileStream = new 80 FileOutputStream(filePath); 81 reply.writeTo(responseFileStream); 82 } 83 } catch (IOException ioe) { 84 throw new Exception(ioe); 85 } catch (SOAPException soape) { 86 throw new Exception(soape); 87 } 88 } 89 90 /*** 91 * Return value of attribute from SOAP Message 92 * Provide name of element and name of attribute 93 */ 94 public String getAttributeValue(SOAPMessage reply, String elementName, String attributeName) throws Exception{ 95 String registryResponse = ""; 96 Object object; 97 try { 98 SOAPPart soapPart = reply.getSOAPPart(); 99 SOAPEnvelope envelope = soapPart.getEnvelope(); 100 SOAPBody body = envelope.getBody(); 101 Name attr = envelope.createName(attributeName); 102 for(Iterator iterator = body.getChildElements(); iterator.hasNext();){ 103 object = iterator.next(); 104 if (object instanceof SOAPElement) { 105 SOAPElement soapElement = (SOAPElement)object; 106 if (soapElement.getElementName().getLocalName().equalsIgnoreCase(elementName)) { 107 registryResponse = soapElement.getAttributeValue(attr); 108 } 109 else 110 registryResponse = traverseSOAPTree(soapElement, elementName, attributeName, attr); 111 } 112 } 113 } 114 catch(SOAPException soape) { 115 throw new Exception("SOAP Exception generated during traversing SOAP message", soape); 116 } 117 return registryResponse; 118 } 119 120 /*** 121 * Help recursive method for getAttributeValue() method 122 */ 123 public String traverseSOAPTree(SOAPElement soapElement, String elementName, String attributeName, Name attr) throws SOAPException{ 124 String registryResponse = ""; 125 Object object; 126 for(Iterator iterator = soapElement.getChildElements(); iterator.hasNext();){ 127 object = iterator.next(); 128 if (object instanceof SOAPElement) { 129 SOAPElement newSOAPElement = (SOAPElement)object; 130 if (newSOAPElement.getElementName().getLocalName().equalsIgnoreCase(elementName)) { 131 registryResponse = newSOAPElement.getAttributeValue(attr); 132 } 133 else 134 return traverseSOAPTree(newSOAPElement, elementName, attributeName, attr); 135 } 136 } 137 return registryResponse; 138 } 139 140 /* 141 * Set UUID in new file that is created during parsing old file 142 */ 143 public void setUUIDInFile(String UUIDString, String argValue, String fileName) throws Exception{ 144 ContentHandler contentHandler = new SAXContentHandler(conforDir + sysSeparator + fileName, argValue, UUIDString); 145 try { 146 XMLReader parser = new SAXParser(); 147 parser.setContentHandler(contentHandler); 148 parser.parse(conforDir + sysSeparator + fileName); 149 } catch (IOException ioe) { 150 throw new Exception("IO Exception generated during parsing XML document", ioe); 151 } catch (SAXException e) { 152 throw new Exception("SAX Exception generated by SAXParser", e); 153 } 154 } 155 156 157 /*** 158 * Print message 159 */ 160 161 public void printMessage(String message) { 162 int stringLength = 5 + message.length() + 5; 163 System.out.println(); 164 for (int i=0; i < stringLength; i++) 165 System.out.print('#'); 166 System.out.println(); 167 System.out.println("#### " + message + " ####"); 168 for (int i=0; i < stringLength; i++) 169 System.out.print('#'); 170 System.out.println(); 171 } 172 173 /* 174 * Delete old raport 175 */ 176 public void deleteRaport (String file) { 177 File delFile = new File(resultsDir + sysSeparator + file); 178 delFile.delete(); 179 } 180 181 /* 182 * Process tests results 183 */ 184 185 public void processResults(String testName, TestResult results) { 186 int runs = results.runCount(); 187 int errors = results.errorCount(); 188 int failures = results.failureCount(); 189 boolean success = results.wasSuccessful(); 190 try { 191 File file = new File(resultsDir + sysSeparator + "raport.txt"); 192 RandomAccessFile raport = new RandomAccessFile(file, "rw"); 193 raport.seek(raport.length()); 194 raport.writeBytes(testName + "\n"); 195 raport.writeBytes(runs + "\n" + errors + "\n" + failures + "\n"); 196 raport.close(); 197 } catch (FileNotFoundException fnfe) { 198 fnfe.printStackTrace(); 199 } catch (IOException ioe) { 200 ioe.printStackTrace(); 201 } 202 } 203 204 public void printRaport(String title, boolean detailedRaport) { 205 String breaks = "*****************************************************"; 206 String[] namesOfTests; 207 int[] runs, errors, failures; 208 int numberOfRuns = 0, numberOfErrors = 0, numberOfFailures = 0; 209 int numberOfLines = 0; 210 try { 211 BufferedReader in = new BufferedReader(new FileReader(resultsDir + 212 sysSeparator + "raport.txt")); 213 while(in.readLine() !=null) { 214 numberOfLines++; 215 } 216 in.close(); 217 numberOfLines/=4; 218 namesOfTests = new String[numberOfLines]; 219 runs = new int[numberOfLines]; 220 errors = new int[numberOfLines]; 221 failures = new int[numberOfLines]; 222 223 in = new BufferedReader(new FileReader(resultsDir + 224 sysSeparator + "raport.txt")); 225 for (int i=0; i < numberOfLines; i++) { 226 namesOfTests[i] = in.readLine(); 227 runs[i] = Integer.parseInt(in.readLine()); 228 errors[i] = Integer.parseInt(in.readLine()); 229 failures[i] = Integer.parseInt(in.readLine()); 230 } 231 for (int i=0; i < numberOfLines; i++) { 232 numberOfRuns += runs[i]; 233 numberOfErrors += errors[i]; 234 numberOfFailures += failures [i]; 235 } 236 in.close(); 237 System.out.println("\n\n" + title + " - FINAL REPORT\n" + breaks); 238 System.out.println ("Total number of all Tests: " + numberOfRuns + "\n" + breaks); 239 System.out.println("Number of Errors: " + numberOfErrors + " !"); 240 System.out.println("Number of Failures: " + numberOfFailures + " !" + "\n" + breaks); 241 if (detailedRaport) { 242 System.out.println("\n\nFINAL REPORT - DETAILS\n" + breaks); 243 for(int i=0; i < numberOfLines; i++) { 244 System.out.println("Test group: " + namesOfTests[i]); 245 System.out.println("Number of runs in the test group: " + runs[i]); 246 System.out.println("Number of errors in the test group: " + errors[i]); 247 System.out.println("Number of failures in the test group: " + failures[i] + "\n" + breaks + "\n"); 248 } 249 } 250 } catch (FileNotFoundException fnfe) { 251 fnfe.printStackTrace(); 252 } catch (IOException ioe) { 253 ioe.printStackTrace(); 254 } 255 } 256 257 /* 258 * Unzip list of files from given directory 259 */ 260 261 public String[] unZipAllFiles() { 262 BufferedOutputStream dest = null; 263 BufferedInputStream is = null; 264 ZipEntry entry; 265 ZipFile zipfile; 266 Enumeration e; 267 Vector v = new Vector(); 268 final int BUFFER = 2048; 269 File dir = new File(conforDir); 270 String[] files = dir.list(); 271 for (int i=0; i < files.length; i++) { 272 if (new File(conforDir + sysSeparator + files[i]).isFile()) 273 if (files[i].substring(files[i].indexOf('.') + 1, files[i].length()).equalsIgnoreCase("zip")) { 274 try { 275 zipfile = new ZipFile(conforDir + sysSeparator + files[i]); 276 e = zipfile.entries(); 277 while(e.hasMoreElements()) { 278 entry = (ZipEntry) e.nextElement(); 279 System.out.println("Extracting: " +entry); 280 v.add(entry.getName()); 281 is = new BufferedInputStream 282 (zipfile.getInputStream(entry)); 283 int count; 284 byte data[] = new byte[BUFFER]; 285 FileOutputStream fos = new 286 FileOutputStream(conforDir + sysSeparator + entry.getName()); 287 dest = new 288 BufferedOutputStream(fos, BUFFER); 289 while ((count = is.read(data, 0, BUFFER)) 290 != -1) { 291 dest.write(data, 0, count); 292 } 293 dest.flush(); 294 dest.close(); 295 is.close(); 296 } 297 } catch(Exception ex) { 298 ex.printStackTrace(); 299 } 300 } 301 } 302 String[] array = (String[])v.toArray(new String[0]); 303 304 return array; 305 } 306 307 /* 308 * Remove files that names are provided with array 309 */ 310 public void removeFiles(String[] files) { 311 File delFile; 312 if (files == null) { 313 return; 314 } 315 for (int i=0; i < files.length; i++) { 316 delFile = new File(conforDir + sysSeparator + files[i]); 317 delFile.delete(); 318 } 319 } 320 321 /*** 322 * Extract URL from the list of command line arguments. 323 * 324 * @param args The array of command line arguments 325 * 326 * @return The text after the first "url=" command line argument. 327 * null if there is no "url=" argument. 328 */ 329 public static String getURL(String[] args) { 330 String url = null; 331 for (int i=0; i < args.length; i++) { 332 if (args[i].startsWith("url=")) { 333 url = args[i].substring(4, args[i].length()); 334 return url; 335 } 336 } 337 return url; 338 } 339 }

This page was automatically generated by Maven