1 18 package sync4j.test.tools; 19 20 import java.net.*; 21 import java.io.*; 22 import java.util.Properties ; 23 import java.util.StringTokenizer ; 24 import java.util.List ; 25 import java.util.Iterator ; 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 29 import org.apache.tools.ant.*; 30 import org.apache.tools.ant.taskdefs.*; 31 32 import org.jdom.*; 33 import org.jdom.input.SAXBuilder; 34 import org.jdom.output.XMLOutputter; 35 36 import org.vmguys.vmtools.ota.OtaUpdate; 37 import org.vmguys.vmtools.ota.UniqueId; 38 import org.vmguys.vmtools.utils.DomFactory; 39 40 import sync4j.framework.core.*; 41 import sync4j.framework.protocol.*; 42 import sync4j.framework.tools.IOTools; 43 44 import sync4j.test.tools.HttpClientConnection; 45 46 import sync4j.test.TestFailedException; 47 48 import org.jibx.runtime.*; 49 import org.jibx.runtime.impl.*; 50 51 94 public class PostSyncML { 95 96 98 public static String LOG_NAME = "sync4j.test.tools.PostSyncML"; 99 100 public static String FILE_ERROR = "error" ; 101 public static String FILE_RESPONSE = "response" ; 102 public static String FILE_REFERENCE = "reference"; 103 104 106 private CommandIdGenerator idGenerator = null; 107 private String nextURL = null; 108 private String [] msgs = null; 109 private String [] msgFiles = null; 110 private String [] ignoreXPaths = null; 111 112 private Ant antTask = null; 113 114 private File baseDir = null; 115 private File responseDir = null, referenceDir = null, errorDir = null; 116 117 private static final Logger log = Logger.getLogger(LOG_NAME); 118 119 121 public PostSyncML(String initialURL , 122 File baseDir , 123 String [] msgFiles , 124 String [] ignoreXPaths, 125 Ant antTask) 126 throws IOException { 127 idGenerator = new CommandIdGenerator(); 128 129 if ((msgFiles == null) || (msgFiles.length == 0)) { 130 msgs = new String [0]; 131 } 132 133 msgs = new String [msgFiles.length]; 134 135 this.baseDir = baseDir; 136 this.responseDir = new File(baseDir, FILE_RESPONSE ); 137 this.referenceDir = new File(baseDir, FILE_REFERENCE); 138 this.errorDir = new File(baseDir, FILE_ERROR ); 139 140 this.msgFiles = msgFiles; 141 for (int i=0; i<msgFiles.length; ++i) { 142 msgs[i] = IOTools.readFileString(new File(baseDir, msgFiles[i])); 143 } 144 145 this.ignoreXPaths = ignoreXPaths; 146 this.antTask = antTask; 147 148 nextURL = initialURL; 149 } 150 151 153 public void syncAndTest() throws IOException, TestFailedException { 154 clean(); 158 159 SyncML response = null; 160 String respURI = null; 161 String referenceXML = null; 162 163 File responseFile = null; 164 for (int i=0; i<msgs.length; ++i) { 165 166 if (antTask != null) { 167 log.info("Trying to execute task: " + msgFiles[i]); 168 antTask.setTarget(msgFiles[i]); 169 try { 170 antTask.execute(); 171 } catch (BuildException ex) { 172 } 174 } 175 176 Properties prop = new Properties (); 180 181 File f = new File(baseDir, msgFiles[i] + ".properties"); 182 if (f.exists()) { 183 prop.load(new FileInputStream(f)); 184 } 185 186 String propertiesValues = prop.getProperty("replace"); 187 188 if (propertiesValues != null) { 192 StringTokenizer values = new StringTokenizer (propertiesValues, ","); 193 int y = 1; 194 while (values.hasMoreTokens()) { 195 String v = values.nextToken(); 196 msgs[i] = msgs[i].replaceAll("VALUE_" + y, v); 197 y++; 198 } 199 } 200 201 log.info("Sending " + msgFiles[i]); 202 203 try { 204 response = postRequest(msgs[i]); 205 } catch (RepresentationException e) { 206 IOTools.writeFile(e.getMessage(), new File(errorDir, msgFiles[i])); 207 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e); 208 } catch (Sync4jException e) { 209 IOTools.writeFile(e.getMessage(), new File(errorDir, msgFiles[i])); 210 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e); 211 } 212 213 responseFile = new File(responseDir, msgFiles[i]); 219 log.info("Writing the response into " + responseFile); 220 221 try { 222 223 String xmlMsg = marshallSyncML(response); 224 IOTools.writeFile(xmlMsg, responseFile); 225 226 } catch(Exception e) { 227 e.printStackTrace(); 228 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e); 229 } 230 231 referenceXML = IOTools.readFileString(new File(referenceDir, msgFiles[i])); 232 233 compare(msgFiles[i]); 234 235 respURI = response.getSyncHdr().getRespURI(); 236 237 if (respURI != null) { 238 nextURL = respURI; 239 } 240 } 241 } 242 243 245 private SyncML postRequest(String request) 246 throws IOException, Sync4jException, RepresentationException { 247 HttpClientConnection syncMLConnection = new HttpClientConnection(nextURL); 248 return syncMLConnection.sendMessage(request); 249 } 250 251 private void compare(String msgFile) 252 throws IOException, TestFailedException { 253 File responseFile = new File(responseDir , msgFile); 254 File referenceFile = new File(referenceDir, msgFile); 255 256 SAXBuilder sb = new SAXBuilder(); 257 sb.setFactory(new DomFactory()); 258 259 try { 260 Document response = sb.build(responseFile ); 261 Document reference = sb.build(referenceFile); 262 263 OtaUpdate update = new OtaUpdate(false); 264 265 UniqueId id = new UniqueId("SyncMLTest", msgFile); 266 Element diffs = update.generateDiffs(response.getRootElement() , 267 reference.getRootElement(), 268 id ); 269 270 if (log.isLoggable(Level.FINEST)) { 271 saveDiffs(diffs, new File(errorDir, msgFile + ".dbg")); 272 } 273 274 if (checkDiffs(diffs)) { 275 saveDiffs(diffs, new File(errorDir, msgFile)); 276 277 throw new TestFailedException( "Test failed on " 278 + msgFile 279 + ". Diff file saved in " 280 + new File(errorDir, msgFile) 281 ); 282 } 283 } catch (JDOMException e) { 284 IOTools.writeFile(e.getMessage(), new File(errorDir, msgFile)); 285 throw new TestFailedException("Test failed on " 286 + msgFile 287 + ": " 288 + e.getMessage() 289 + ". Error message saved in " 290 + new File(errorDir, msgFile) 291 ); 292 } 293 } 294 295 305 private boolean checkDiffs(Element diffs) { 306 List positions = diffs.getChildren("Position", diffs.getNamespace()); 307 308 Element position = null; 309 310 Iterator i = positions.iterator(); 311 while(i.hasNext()) { 312 position = (Element)i.next(); 313 314 if (!ignore(position.getAttributeValue("XPath"))) { 315 return true; 319 } 320 } 321 322 return false; 323 } 324 325 328 private void clean() { 329 FilenameFilter filter = IOTools.getFileTypeFilter("xml"); 330 331 String [] files = responseDir.list(filter); 332 333 for (int i=0; ((files != null) && (i<files.length)); ++i) { 334 new File(files[i]).delete(); 335 } 336 337 files = errorDir.list(filter); 338 339 for (int i=0; ((files != null) && (i<files.length)); ++i) { 340 new File(files[i]).delete(); 341 } 342 } 343 344 352 private boolean ignore(String xPath) { 353 354 for (int i=0; 355 ((xPath != null) && (ignoreXPaths != null) && (i<ignoreXPaths.length)); 356 ++i) { 357 if (xPath.equals(ignoreXPaths[i])) { 358 return true; 359 } 360 } 361 362 return false; 363 } 364 365 371 private void saveDiffs(Element diffs, File file) throws IOException { 372 XMLOutputter xmlo = new XMLOutputter(" ", true); 373 xmlo.setTextNormalize(true); 374 375 FileOutputStream fos = new FileOutputStream(file); 376 xmlo.output(diffs, fos); 377 fos.close(); 378 } 379 380 private static void syntax() { 381 System.out.println("Syntax: " + (PostSyncML.class) + "<initial URL> <msg1> ... <msgN>"); 382 } 383 384 private String marshallSyncML(SyncML syncML) throws Sync4jException { 385 String msg = null; 386 try { 387 388 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 389 390 IBindingFactory f = BindingDirectory.getFactory(SyncML.class); 391 IMarshallingContext c = f.createMarshallingContext(); 392 c.setIndent(0); 393 c.marshalDocument(syncML, "UTF-8", null, bout); 394 msg = new String (bout.toByteArray()); 395 396 } catch(Exception e) { 397 e.printStackTrace(); 398 throw new Sync4jException(e); 399 } 400 return msg; 401 } 402 404 public static void main(String args[]) 405 throws Exception { 406 if(args.length < 2) { 407 syntax(); 408 } 409 410 String [] msgFiles = new String [args.length-1]; 411 412 System.arraycopy(args, 1, msgFiles, 0, msgFiles.length); 413 414 PostSyncML postsyncml = new PostSyncML(args[0], new File("."), msgFiles, new String [0], null); 415 postsyncml.syncAndTest(); 416 } 417 } 418 | Popular Tags |