private void assertXMLFilesMatch(String label, IFile testFile, String expectedFileLocation)
     throws Exception {
   Reader testReader = null;
   Reader expectedReader = null;
   try {
     testReader = new InputStreamReader(testFile.getContents());
     expectedReader =
         new InputStreamReader(
             CreateSwitchYardProjectTest.class
                 .getClassLoader()
                 .getResourceAsStream(expectedFileLocation));
     Diff diff = XMLUnit.compareXML(testReader, expectedReader);
     assertTrue(label + ": " + diff.toString(), diff.identical());
   } finally {
     if (testReader != null) {
       try {
         testReader.close();
       } catch (Exception e) {
         // for codestyle check
         e.fillInStackTrace();
       }
       testReader = null;
     }
     if (expectedReader != null) {
       try {
         expectedReader.close();
       } catch (Exception e) {
         // for codestyle check
         e.fillInStackTrace();
       }
       expectedReader = null;
     }
   }
 }
  private boolean updateExistingJob(AbstractProject<?, ?> project, String config) {
    boolean created;

    // Leverage XMLUnit to perform diffs
    Diff diff;
    try {
      String oldJob = project.getConfigFile().asString();
      diff = XMLUnit.compareXML(oldJob, config);
      if (diff.similar()) {
        LOGGER.log(Level.FINE, String.format("Project %s is identical", project.getName()));
        return false;
      }
    } catch (Exception e) {
      // It's not a big deal if we can't diff, we'll just move on
      LOGGER.warning(e.getMessage());
    }

    // TODO Perform comparison between old and new, and print to console
    // TODO Print out, for posterity, what the user might have changed, in the format of the DSL

    LOGGER.log(Level.FINE, String.format("Updating project %s as %s", project.getName(), config));
    StreamSource streamSource =
        new StreamSource(new StringReader(config)); // TODO use real xmlReader
    try {
      project.updateByXml(streamSource);
      created = true;
    } catch (IOException ioex) {
      LOGGER.log(Level.WARNING, String.format("Error writing updated project to file."), ioex);
      created = false;
    }
    return created;
  }
  private boolean updateExistingItem(AbstractItem item, String config) {
    boolean created;

    // Leverage XMLUnit to perform diffs
    Diff diff;
    try {
      String oldJob = item.getConfigFile().asString();
      diff = XMLUnit.compareXML(oldJob, config);
      if (diff.similar()) {
        LOGGER.log(Level.FINE, format("Item %s is identical", item.getName()));
        return false;
      }
    } catch (Exception e) {
      // It's not a big deal if we can't diff, we'll just move on
      LOGGER.warning(e.getMessage());
    }

    LOGGER.log(Level.FINE, format("Updating item %s as %s", item.getName(), config));
    Source streamSource = new StreamSource(new StringReader(config));
    try {
      item.updateByXml(streamSource);
      created = true;
    } catch (IOException e) {
      LOGGER.log(Level.WARNING, format("Error writing updated item to file."), e);
      created = false;
    }
    return created;
  }
 @Test
 public void testWriteComplete() throws Exception {
   String old_xml = new StringPuller().pull(COMPLETE_XML, getClass());
   SwitchYardModel switchyard = _puller.pull(new StringReader(old_xml));
   String new_xml = switchyard.toString();
   XMLUnit.setIgnoreWhitespace(true);
   Diff diff = XMLUnit.compareXML(old_xml, new_xml);
   Assert.assertTrue(diff.toString(), diff.identical());
 }
 protected boolean checkDocs(Document doc1, Document doc2) {
   XMLUnit.setIgnoreAttributeOrder(true);
   XMLUnit.setIgnoreWhitespace(true);
   DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(doc1, doc2));
   boolean result = diff.similar();
   if (!result) {
     for (Difference d : (List<Difference>) diff.getAllDifferences()) {
       System.out.println(d);
     }
   }
   return result;
 }
 public static void assertSimilarXml(String expectedXml, String xml) {
   XMLUnit.setIgnoreWhitespace(true);
   Diff diff;
   try {
     diff = XMLUnit.compareXML(xml, expectedXml);
   } catch (SAXException e) {
     throw new IllegalArgumentException("Could not run XML comparison", e);
   } catch (IOException e) {
     throw new IllegalArgumentException("Could not run XML comparison", e);
   }
   String message = "Diff: " + diff.toString() + CharUtils.LF + "XML: " + xml;
   assertTrue(message, diff.similar());
 }
 /**
  * Gets the diff.
  *
  * @param objectXML the object XML
  * @param exampleXML the example XML
  * @return the detailed diff
  * @throws SAXException the sAX exception
  * @throws IOException Signals that an I/O exception has occurred.
  */
 @SuppressWarnings("unchecked")
 private DetailedDiff getDiff(StringWriter objectXML, StringBuffer exampleXML)
     throws SAXException, IOException {
   DetailedDiff myDiff =
       new DetailedDiff(XMLUnit.compareXML(exampleXML.toString(), objectXML.toString()));
   List<Difference> allDifferences = myDiff.getAllDifferences();
   if (allDifferences.size() > 0) {
     for (Difference d : allDifferences) {
       System.err.println(d);
     }
   }
   return myDiff;
 }
  public void test_indent() throws IOException, SAXException {
    Smooks smooks = new Smooks(getClass().getResourceAsStream("indent-config.xml"));
    StringResult result = new StringResult();

    smooks.filterSource(
        new StreamSource(getClass().getResourceAsStream("input-message.jsn")), result);

    assertTrue(
        XMLUnit.compareXML(
                StreamUtils.readStreamAsString(
                    getClass().getResourceAsStream("indent-expected.xml")),
                result.toString())
            .identical());
  }
Exemple #9
0
  @Test
  public void test() throws IOException, SAXException {
    XMLBinding xmlBinding =
        new XMLBinding().add(getClass().getResourceAsStream("POType-binding.xml")).intiailize();

    String poXML = StreamUtils.readStreamAsString(getClass().getResourceAsStream("po.xml"));
    POType po = xmlBinding.fromXML(new StringSource(poXML), POType.class);

    StringWriter writer = new StringWriter();
    xmlBinding.toXML(po, writer);

    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.compareXML(poXML, writer.toString());
  }
Exemple #10
0
  protected List<Difference> getDifferences(final String xmlA, final String xmlB) {
    DetailedDiff myDiff;
    try {
      myDiff = new DetailedDiff(XMLUnit.compareXML(xmlA, xmlB));
    } catch (final Exception e) {
      throw new RuntimeException(e);
    }
    final List<Difference> retDifferences = new ArrayList<Difference>();
    @SuppressWarnings("unchecked")
    final List<Difference> allDifferences = myDiff.getAllDifferences();
    if (allDifferences.size() > 0) {
      DIFFERENCES:
      for (final Difference d : allDifferences) {
        final NodeDetail controlNodeDetail = d.getControlNodeDetail();
        final String control = controlNodeDetail.getValue();
        final NodeDetail testNodeDetail = d.getTestNodeDetail();
        final String test = testNodeDetail.getValue();

        if (d.getDescription().equals("namespace URI")) {
          if (control != null && !"null".equals(control)) {
            if (ignoreNamespace(control.toLowerCase())) {
              LOG.trace("Ignoring {}: {}", d.getDescription(), d);
              continue DIFFERENCES;
            }
          }
          if (test != null && !"null".equals(test)) {
            if (ignoreNamespace(test.toLowerCase())) {
              LOG.trace("Ignoring {}: {}", d.getDescription(), d);
              continue DIFFERENCES;
            }
          }
        } else if (d.getDescription().equals("namespace prefix")) {
          if (control != null && !"null".equals(control)) {
            if (ignorePrefix(control.toLowerCase())) {
              LOG.trace("Ignoring {}: {}", d.getDescription(), d);
              continue DIFFERENCES;
            }
          }
          if (test != null && !"null".equals(test)) {
            if (ignorePrefix(test.toLowerCase())) {
              LOG.trace("Ignoring {}: {}", d.getDescription(), d);
              continue DIFFERENCES;
            }
          }
        } else if (d.getDescription().equals("xsi:schemaLocation attribute")) {
          LOG.debug(
              "Schema location '{}' does not match.  Ignoring.",
              controlNodeDetail.getValue() == null
                  ? testNodeDetail.getValue()
                  : controlNodeDetail.getValue());
          continue DIFFERENCES;
        }

        if (ignoreDifference(d)) {
          LOG.debug(
              "ignoreDifference matched.  Ignoring difference: {}: {}", d.getDescription(), d);
          continue DIFFERENCES;
        } else {
          LOG.warn("Found difference: {}: {}", d.getDescription(), d);
          retDifferences.add(d);
        }
      }
    }
    return retDifferences;
  }
  private void runProjectTest(String projectName, boolean isWeb) throws Exception {
    ResolverConfiguration configuration = new ResolverConfiguration();
    IProject project =
        importProject("test-data/projects/" + projectName + "/pom.xml", configuration);
    waitForJobs();

    project.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
    waitForJobs();

    assertNoErrors(project);

    // make sure we get through an incremental build (SWITCHYARD-1108)
    project.touch(new NullProgressMonitor());
    project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
    waitForJobs();

    assertNoErrors(project);

    assertTrue(
        project
            .getFile("target/classes/META-INF/switchyard.xml")
            .isSynchronized(IResource.DEPTH_ZERO));
    assertTrue(project.getFile("target/classes/META-INF/switchyard.xml").isAccessible());

    assertTrue(!project.getFile("src/main/java/META-INF/MANIFEST.MF").exists());

    Reader sourceReader = null;
    Reader testReader = null;
    try {
      sourceReader =
          new InputStreamReader(
              project.getFile("target/classes/META-INF/switchyard.xml").getContents());
      testReader =
          new InputStreamReader(
              SwitchYardConfigurationTest.class
                  .getClassLoader()
                  .getResourceAsStream(
                      "test-data/validation/"
                          + projectName
                          + (isWeb ? "/WEB-INF/switchyard.xml" : "/META-INF/switchyard.xml")));
      XMLUnit.setIgnoreComments(true);
      XMLUnit.setIgnoreWhitespace(true);
      Diff diff = XMLUnit.compareXML(sourceReader, testReader);
      diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
      assertTrue(diff.toString(), diff.similar());
    } finally {
      if (sourceReader != null) {
        try {
          sourceReader.close();
        } catch (Exception e) {
          // for codestyle check
          e.fillInStackTrace();
        }
      }
      if (testReader != null) {
        try {
          testReader.close();
        } catch (Exception e) {
          // for codestyle check
          e.fillInStackTrace();
        }
      }
    }
  }
Exemple #12
0
 static Diff isSimilarXml(String expectedXml, String xml) throws Exception {
   XMLUnit.setIgnoreWhitespace(true);
   return XMLUnit.compareXML(xml, expectedXml);
 }