@Override public String toString() { return this.diff.toString(); }
public boolean hasDifferences() { return this.diff.hasDifferences(); }
XmlUnitDiff(String expected, String actual) { this.diff = DiffBuilder.compare(expected).withTest(actual) .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)) .ignoreWhitespace().ignoreComments() .checkForSimilar() .build(); }
private static boolean belongsToProlog(Comparison comparison, boolean ignoreDoctypeDeclarationAsWell) { if (comparison.getType().isDoctypeComparison()) { return ignoreDoctypeDeclarationAsWell; } return belongsToProlog(comparison.getControlDetails().getTarget(), ignoreDoctypeDeclarationAsWell) || belongsToProlog(comparison.getTestDetails().getTarget(), ignoreDoctypeDeclarationAsWell); }
public boolean matches(final HttpRequest context, NottableString matched) { boolean result = false; if (diffBuilder != null) { try { Diff diff = diffBuilder.withTest(Input.fromString(normaliseXmlString(matched.getValue()))).build(); result = !diff.hasDifferences(); if (!result) { mockServerLogger.trace("Failed to match [{}] with schema [{}] because [{}]", matched, this.matcher, diff.toString()); } } catch (Exception e) { mockServerLogger.trace(context, "Failed to match [{}] with schema [{}] because [{}]", matched, this.matcher, e.getMessage()); } } return matcher.isNot() != (not != result); }
@Test public void lazyInit() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setContextPath(CONTEXT_PATH); marshaller.setLazyInit(true); marshaller.afterPropertiesSet(); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); marshaller.marshal(flights, result); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING).withDifferenceEvaluator(ev)); }
@Test public void testManagerAddedToConfig() throws URISyntaxException { XmlMultiConfiguration source = XmlMultiConfiguration.fromNothing().withManager("foo", emptyConfiguration()).build(); XmlMultiConfiguration xmlMultiConfiguration = XmlMultiConfiguration.from(source).withManager("baz", emptyConfiguration()).build(); assertThat(xmlMultiConfiguration.configuration("foo"), notNullValue()); assertThat(xmlMultiConfiguration.configuration("foo", "prod"), notNullValue()); assertThat(xmlMultiConfiguration.configuration("baz"), notNullValue()); assertThat(xmlMultiConfiguration.configuration("baz", "prod"), notNullValue()); assertThat(xmlMultiConfiguration.identities(), containsInAnyOrder("foo", "baz")); assertThat(xmlMultiConfiguration.variants("foo"), empty()); assertThat(xmlMultiConfiguration.variants("baz"), empty()); assertThat(xmlMultiConfiguration.toString(), isSimilarTo( "<configurations xmlns='http://www.ehcache.org/v3/multi'>" + "<configuration identity='foo'><config xmlns='http://www.ehcache.org/v3'/></configuration>" + "<configuration identity='baz'><config xmlns='http://www.ehcache.org/v3'/></configuration>" + "</configurations>").ignoreWhitespace().ignoreComments().withNodeMatcher( new DefaultNodeMatcher(and(byNameAndText, byNameAndAllAttributes)))); }
@Override public ElementSelectors.ConditionalSelectorBuilderThen whenElementIsNamed(QName expectedName) { return when(ElementSelectors.elementNamePredicate(expectedName)); } @Override
public static void assertElement(String inputString, Node rootElement) { assertThat(Input.from(rootElement), isSimilarTo(Input.from(inputString)).ignoreComments() .ignoreWhitespace() .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))); } }
@Test public void toMessage() throws Exception { MyBean payload = new MyBean(); payload.setName("Foo"); Message<?> message = this.converter.toMessage(payload, null); assertNotNull(message); String actual = new String((byte[]) message.getPayload(), StandardCharsets.UTF_8); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat(actual, isSimilarTo("<myBean><name>Foo</name></myBean>").withDifferenceEvaluator(ev)); }
@Test public void testCompleteXmlToString() { URL resource = XmlConfigurationTest.class.getResource("/configs/ehcache-complete.xml"); Configuration config = new XmlConfiguration(resource); XmlConfiguration xmlConfig = new XmlConfiguration(config); assertThat(xmlConfig.toString(), isSimilarTo(Input.from(resource)).ignoreComments().ignoreWhitespace().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))); }
@Test // SPR-10714 public void marshalAWrappedObjectHoldingAnXmlElementDeclElement() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan("org.springframework.oxm.jaxb"); marshaller.afterPropertiesSet(); Airplane airplane = new Airplane(); airplane.setName("test"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); marshaller.marshal(airplane, result); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Marshalling should use root Element", writer.toString(), isSimilarTo("<airplane><name>test</name></airplane>").withDifferenceEvaluator(ev)); }
@Test public void testTransactionalCacheXmlTranslationToString() { URL resource = XmlConfigurationTest.class.getResource("/configs/transactional-cache.xml"); Configuration config = new XmlConfiguration(resource); XmlConfiguration xmlConfig = new XmlConfiguration(config); assertThat(xmlConfig.toString(), isSimilarTo(Input.from(resource)).ignoreComments() .ignoreWhitespace() .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))); } }
@Test public void writeXmlRootElement() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(rootElement, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType()); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev)); }
@Test public void testClusteredCacheXmlTranslationToString() { URL resource = XmlConfigurationTest.class.getResource("/configs/clustered-cache.xml"); Configuration config = new XmlConfiguration(resource); XmlConfiguration xmlConfig = new XmlConfiguration(config); assertThat(xmlConfig.toString(), isSimilarTo(Input.from(resource)).ignoreComments() .ignoreWhitespace() .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes))); } }
@Test public void writeXmlRootElementSubclass() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(rootElementCglib, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType()); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev)); }
@Test public void testManagementRegistryXmlTranslationToString() { URL resource = XmlConfigurationTest.class.getResource("/ehcache-management.xml"); Configuration config = new XmlConfiguration(resource); XmlConfiguration xmlConfig = new XmlConfiguration(config); assertThat(xmlConfig.toString(), isSimilarTo(Input.from(resource)).ignoreComments() .ignoreWhitespace() .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))); } }
@Test public void customizeMarshaller() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter(); myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<myRootElement><element>a|||b</element></myRootElement>").withDifferenceEvaluator(ev)); }
Input.from(expected.getFile()), CompareMatcher.isSimilarTo(Input.from(resource.getFile())) .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)));
@Test public void write() throws IOException, SAXException { Feed feed = new Feed("atom_1.0"); feed.setTitle("title"); Entry entry1 = new Entry(); entry1.setId("id1"); entry1.setTitle("title1"); Entry entry2 = new Entry(); entry2.setId("id2"); entry2.setTitle("title2"); List<Entry> entries = new ArrayList<>(2); entries.add(entry1); entries.add(entry2); feed.setEntries(entries); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(feed, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "atom+xml", StandardCharsets.UTF_8), outputMessage.getHeaders().getContentType()); String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>title</title>" + "<entry><id>id1</id><title>title1</title></entry>" + "<entry><id>id2</id><title>title2</title></entry></feed>"; NodeMatcher nm = new DefaultNodeMatcher(ElementSelectors.byName); assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo(expected).ignoreWhitespace().withNodeMatcher(nm)); }