1 18 package sync4j.test.tools; 19 20 import java.net.*; 21 import java.io.*; 22 import java.util.List ; 23 import java.util.Iterator ; 24 import java.util.logging.Logger ; 25 import java.util.logging.Level ; 26 27 import org.jdom.*; 28 import org.jdom.input.SAXBuilder; 29 import org.jdom.output.XMLOutputter; 30 31 import org.vmguys.vmtools.ota.OtaUpdate; 32 import org.vmguys.vmtools.ota.UniqueId; 33 import org.vmguys.vmtools.utils.DomFactory; 34 35 import org.apache.tools.ant.*; 36 import org.apache.tools.ant.taskdefs.*; 37 38 import sync4j.framework.core.*; 39 import sync4j.framework.protocol.*; 40 import sync4j.framework.tools.IOTools; 41 import sync4j.framework.tools.WBXMLTools; 42 43 import sync4j.test.tools.HttpClientConnection; 44 import sync4j.test.TestFailedException; 45 46 47 90 public class PostWBXMLSyncML { 91 92 94 public static String LOG_NAME = "sync4j.test.tools.PostSyncML"; 95 96 public static String FILE_ERROR = "error" ; 97 public static String FILE_RESPONSE = "response" ; 98 public static String FILE_REFERENCE = "reference"; 99 100 102 private CommandIdGenerator idGenerator = null; 103 private String nextURL = null; 104 private byte[][] msgs = null; 105 private String [] msgFiles = null; 106 private String [] ignoreXPaths = null; 107 108 private Ant antTask = null; 109 110 private File baseDir = null; 111 private File responseDir = null, referenceDir = null, errorDir = null; 112 113 private static final Logger log = Logger.getLogger(LOG_NAME); 114 115 117 public PostWBXMLSyncML(String initialURL , 118 File baseDir , 119 String [] msgFiles , 120 String [] ignoreXPaths, 121 Ant antTask) 122 throws IOException { 123 idGenerator = new CommandIdGenerator(); 124 125 if ((msgFiles == null) || (msgFiles.length == 0)) { 126 msgs = new byte[0][]; 127 } 128 129 msgs = new byte[msgFiles.length][]; 130 131 this.baseDir = baseDir; 132 this.responseDir = new File(baseDir, FILE_RESPONSE ); 133 this.referenceDir = new File(baseDir, FILE_REFERENCE); 134 this.errorDir = new File(baseDir, FILE_ERROR ); 135 136 this.msgFiles = msgFiles; 137 for (int i=0; i<msgFiles.length; ++i) { 138 msgs[i] = IOTools.readFileBytes(new File(baseDir, msgFiles[i])); 139 } 140 141 this.ignoreXPaths = ignoreXPaths; 142 this.antTask = antTask; 143 144 nextURL = initialURL; 145 } 146 147 149 public void syncAndTest() throws IOException, TestFailedException { 150 clean(); 154 155 SyncML response = null; 156 String respURI = null; 157 158 String referenceXML = null; 159 160 File responseFile = null; 161 for (int i=0; i<msgs.length; ++i) { 162 163 if (antTask != null) { 164 log.info("Trying to execute task: " + msgFiles[i]); 165 antTask.setTarget(msgFiles[i]); 166 try { 167 antTask.execute(); 168 } catch (BuildException ex) { 169 } 171 } 172 173 log.info("Sending " + msgFiles[i]); 174 175 try { 176 response = postRequest(msgs[i]); 177 } catch (RepresentationException e) { 178 IOTools.writeFile(e.getMessage(), new File(errorDir, msgFiles[i])); 179 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e); 180 } catch (Sync4jException e) { 181 IOTools.writeFile(e.getMessage(), new File(errorDir, msgFiles[i])); 182 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e); 183 } 184 185 responseFile = new File(responseDir, msgFiles[i]); 191 log.info("Writing the response into " + responseFile); 192 193 try { 194 IOTools.writeFile(WBXMLTools.toWBXML(response), responseFile); 195 referenceXML = WBXMLTools.wbxmlToXml(IOTools.readFileBytes(new File(referenceDir, msgFiles[i]))); 196 compare(msgFiles[i]); 197 } catch (Sync4jException e) { 198 IOTools.writeFile(e.getMessage(), new File(errorDir, msgFiles[i])); 199 throw new TestFailedException ("WBXML error: " + e.getMessage(), e); 200 } 201 202 respURI = response.getSyncHdr().getRespURI(); 203 204 if (respURI != null) { 205 nextURL = respURI; 206 } 207 } 208 } 209 210 212 private SyncML postRequest(byte[] request) 213 throws IOException, Sync4jException, RepresentationException { 214 HttpClientConnection syncMLConnection = new HttpClientConnection(nextURL); 215 return syncMLConnection.sendWBXMLMessage(request); 216 } 217 218 private void compare(String msgFile) 219 throws IOException, Sync4jException, TestFailedException { 220 File responseFile = new File(responseDir , msgFile); 221 File referenceFile = new File(referenceDir, msgFile); 222 223 String responseXml = WBXMLConverter.readWbxmlAsXml(responseFile); 225 String referenceXml = WBXMLConverter.readWbxmlAsXml(referenceFile); 226 227 StringReader responseReader = new StringReader(responseXml); 228 StringReader referenceReader = new StringReader(referenceXml); 229 230 SAXBuilder sb = new SAXBuilder(); 231 sb.setFactory(new DomFactory()); 232 233 try { 234 Document response = sb.build(responseReader); 235 Document reference = sb.build(referenceReader); 236 237 OtaUpdate update = new OtaUpdate(false); 238 239 UniqueId id = new UniqueId("SyncMLTest", msgFile); 240 Element diffs = update.generateDiffs(response.getRootElement() , 241 reference.getRootElement(), 242 id ); 243 244 if (log.isLoggable(Level.FINEST)) { 245 saveDiffs(diffs, new File(errorDir, msgFile + ".dbg")); 246 } 247 248 if (checkDiffs(diffs)) { 249 saveDiffs(diffs, new File(errorDir, msgFile)); 250 251 throw new TestFailedException( "Test failed on " 252 + msgFile 253 + ". Diff file saved in " 254 + new File(errorDir, msgFile) 255 ); 256 } 257 } catch (JDOMException e) { 258 IOTools.writeFile(e.getMessage(), new File(errorDir, msgFile)); 259 throw new TestFailedException("Test failed on " 260 + msgFile 261 + ": " 262 + e.getMessage() 263 + ". Error message saved in " 264 + new File(errorDir, msgFile) 265 ); 266 } 267 } 268 269 279 private boolean checkDiffs(Element diffs) { 280 List positions = diffs.getChildren("Position", diffs.getNamespace()); 281 282 Element position = null; 283 284 Iterator i = positions.iterator(); 285 while(i.hasNext()) { 286 position = (Element)i.next(); 287 288 if (!ignore(position.getAttributeValue("XPath"))) { 289 return true; 293 } 294 } 295 296 return false; 297 } 298 299 302 private void clean() { 303 FilenameFilter filter = IOTools.getFileTypeFilter("wbxml"); 304 305 String [] files = responseDir.list(filter); 306 307 for (int i=0; ((files != null) && (i<files.length)); ++i) { 308 new File(files[i]).delete(); 309 } 310 311 files = errorDir.list(filter); 312 313 for (int i=0; ((files != null) && (i<files.length)); ++i) { 314 new File(files[i]).delete(); 315 } 316 } 317 318 326 private boolean ignore(String xPath) { 327 328 for (int i=0; 329 ((xPath != null) && (ignoreXPaths != null) && (i<ignoreXPaths.length)); 330 ++i) { 331 if (xPath.equals(ignoreXPaths[i])) { 332 return true; 333 } 334 } 335 336 return false; 337 } 338 339 345 private void saveDiffs(Element diffs, File file) throws IOException { 346 XMLOutputter xmlo = new XMLOutputter(" ", true); 347 xmlo.setTextNormalize(true); 348 349 FileOutputStream fos = new FileOutputStream(file); 350 xmlo.output(diffs, fos); 351 fos.close(); 352 } 353 354 private static void syntax() { 355 System.out.println("Syntax: " + (PostSyncML.class) + "<initial URL> <msg1> ... <msgN>"); 356 } 357 358 360 public static void main(String args[]) 361 throws Exception { 362 if(args.length < 2) { 363 syntax(); 364 } 365 366 String [] msgFiles = new String [args.length-1]; 367 368 System.arraycopy(args, 1, msgFiles, 0, msgFiles.length); 369 370 PostWBXMLSyncML postsyncml = new PostWBXMLSyncML(args[0], new File("."), msgFiles, new String [0], null); 371 postsyncml.syncAndTest(); 372 } 373 } 374 | Popular Tags |