Java源码示例:org.xmlunit.diff.DifferenceEvaluator

示例1
@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));
}
 
示例2
@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));
}
 
示例3
@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));
}
 
示例4
@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));
}
 
示例5
@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));
}
 
示例6
@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));
}
 
示例7
@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));
}
 
示例8
@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));
}
 
示例9
@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));
}
 
示例10
@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));
}
 
示例11
@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));
}
 
示例12
@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));
}
 
示例13
/**
 * {@inheritDoc}
 *
 * @see DiffBuilder#withDifferenceEvaluator(DifferenceEvaluator)
 */
@Override
public CompareAssert withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator) {
    diffBuilder.withDifferenceEvaluator(differenceEvaluator);
    return this;
}
 
示例14
/**
 * @see DiffBuilder#withDifferenceEvaluator(DifferenceEvaluator)
 */
@Override
public CompareMatcher withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator) {
    diffBuilder.withDifferenceEvaluator(differenceEvaluator);
    return this;
}
 
示例15
/**
 * Adds placeholder support to a {@link DifferenceEngineConfigurer} considering an additional {@link DifferenceEvaluator}.
 *
 * @param configurer the configurer to add support to
 * @param placeholderOpeningDelimiterRegex regular expression for
 * the opening delimiter of placeholder, defaults to {@link
 * PlaceholderDifferenceEvaluator#PLACEHOLDER_DEFAULT_OPENING_DELIMITER_REGEX}
 * if the parameter is null or blank
 * @param placeholderClosingDelimiterRegex regular expression for
 * the closing delimiter of placeholder, defaults to {@link
 * PlaceholderDifferenceEvaluator#PLACEHOLDER_DEFAULT_CLOSING_DELIMITER_REGEX}
 * if the parameter is null or blank
 * @param evaluator the additional evaluator - placeholder support is
 * {@link DifferenceEvaluators#chain chain}ed after the given
 * evaluator
 */
public static <D extends DifferenceEngineConfigurer<D>>
    D withPlaceholderSupportUsingDelimitersChainedAfter(D configurer, String placeholderOpeningDelimiterRegex,
        String placeholderClosingDelimiterRegex, DifferenceEvaluator evaluator) {
    return withPlaceholderSupportUsingDelimitersChainedAfter(configurer, placeholderOpeningDelimiterRegex,
        placeholderClosingDelimiterRegex, null, null, null, evaluator);
}
 
示例16
/**
 * Adds placeholder support to a {@link DifferenceEngineConfigurer} considering an additional {@link DifferenceEvaluator}.
 *
 * @param configurer the configurer to add support to
 * @param placeholderOpeningDelimiterRegex regular expression for
 * the opening delimiter of placeholder, defaults to {@link
 * PlaceholderDifferenceEvaluator#PLACEHOLDER_DEFAULT_OPENING_DELIMITER_REGEX}
 * if the parameter is null or blank
 * @param placeholderClosingDelimiterRegex regular expression for
 * the closing delimiter of placeholder, defaults to {@link
 * PlaceholderDifferenceEvaluator#PLACEHOLDER_DEFAULT_CLOSING_DELIMITER_REGEX}
 * if the parameter is null or blank
 * @param evaluator the additional evaluator - placeholder support is
 * {@link DifferenceEvaluators#chain chain}ed after the given
 * evaluator
 * @param placeholderArgsOpeningDelimiterRegex regular expression for
 * the opening delimiter of the placeholder's argument list, defaults to {@link
 * PlaceholderDifferenceEvaluator#PLACEHOLDER_DEFAULT_ARGS_OPENING_DELIMITER_REGEX}
 * if the parameter is null or blank
 * @param placeholderArgsClosingDelimiterRegex regular expression for
 * the closing delimiter of the placeholder's argument list, defaults to {@link
 * PlaceholderDifferenceEvaluator#PLACEHOLDER_DEFAULT_ARGS_CLOSING_DELIMITER_REGEX}
 * if the parameter is null or blank
 * @param placeholderArgsSeparatorRegex regular expression for the
 * delimiter between arguments inside of the placeholder's
 * argument list, defaults to {@link
 * PlaceholderDifferenceEvaluator#PLACEHOLDER_DEFAULT_ARGS_SEPARATOR_REGEX}
 * if the parameter is null or blank
 */
public static <D extends DifferenceEngineConfigurer<D>>
    D withPlaceholderSupportUsingDelimitersChainedAfter(D configurer,
        final String placeholderOpeningDelimiterRegex,
        final String placeholderClosingDelimiterRegex,
        final String placeholderArgsOpeningDelimiterRegex,
        final String placeholderArgsClosingDelimiterRegex,
        final String placeholderArgsSeparatorRegex,
        final DifferenceEvaluator evaluator) {
    return configurer.withDifferenceEvaluator(DifferenceEvaluators.chain(
      evaluator, new PlaceholderDifferenceEvaluator(placeholderOpeningDelimiterRegex,
          placeholderClosingDelimiterRegex, placeholderArgsOpeningDelimiterRegex,
          placeholderArgsClosingDelimiterRegex, placeholderArgsSeparatorRegex)));
}
 
示例17
/**
 * Provide your own custom {@link DifferenceEvaluator} implementation.
 * This overwrites the Default DifferenceEvaluator.
 * If you want use your custom DifferenceEvaluator in combination with the default or another DifferenceEvaluator
 * you should use {@link DifferenceEvaluators#chain(DifferenceEvaluator...)}
 * or {@link DifferenceEvaluators#first(DifferenceEvaluator...)} to combine them:
 * <pre>
 * Diff myDiff = DiffBuilder.compare(control).withTest(test)
 *         .withDifferenceEvaluator(
 *             DifferenceEvaluators.chain(
 *                 DifferenceEvaluators.Default,
 *                 new MyCustomDifferenceEvaluator()))
 *         ....
 *         .build();
 * </pre>
 */
@Override
public DiffBuilder withDifferenceEvaluator(final DifferenceEvaluator differenceEvaluator) {
    this.differenceEvaluator = differenceEvaluator;
    return this;
}
 
示例18
/**
 * Provide your own custom {@link DifferenceEvaluator} implementation.
 * <p>This overwrites the Default DifferenceEvaluator.</p>
 *
 * <p>If you want use your custom DifferenceEvaluator in
 * combination with the default or another DifferenceEvaluator you
 * should use {@link
 * DifferenceEvaluators#chain(DifferenceEvaluator...)}  or {@link
 * DifferenceEvaluators#first(DifferenceEvaluator...)} to combine
 * them:</p>
 *
 * <pre>
 *         .withDifferenceEvaluator(
 *             DifferenceEvaluators.chain(
 *                 DifferenceEvaluators.Default,
 *                 new MyCustomDifferenceEvaluator()))
 *         ....
 * </pre>
 */
D withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator);
 
示例19
/**
 * Adds placeholder support to a {@link DifferenceEngineConfigurer} considering an additional {@link DifferenceEvaluator}.
 *
 * @param configurer the configurer to add support to
 * @param evaluator the additional evaluator - placeholder support is
 * {@link DifferenceEvaluators#chain chain}ed after the given
 * evaluator
 */
public static <D extends DifferenceEngineConfigurer<D>>
    D withPlaceholderSupportChainedAfter(D configurer, DifferenceEvaluator evaluator) {
    return withPlaceholderSupportUsingDelimitersChainedAfter(configurer, null, null, evaluator);
}