adopt @krystiankaluzny's approach for #225 · xmlunit/xmlunit@223ccf8 (original) (raw)

Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
22 22 import org.assertj.core.api.Assert;
23 23 import org.assertj.core.api.AssertFactory;
24 24 import org.w3c.dom.Node;
25 -import org.xmlunit.xpath.JAXPXPathEngine;
25 +import org.xmlunit.xpath.XPathEngine;
26 26
27 27 import java.lang.reflect.ParameterizedType;
28 28 import java.lang.reflect.Type;
@@ -71,7 +71,7 @@ class AssertFactoryProvider {
71 71 @SuppressWarnings("rawtypes")
72 72 private static Class<? extends AssertFactory> assertFactoryClass;
73 73
74 -AssertFactory<Node, SingleNodeAssert> create(JAXPXPathEngine engine) {
74 +AssertFactory<Node, SingleNodeAssert> create(XPathEngine engine) {
75 75
76 76 if (hasAssertFactoryUpperBoundOnAssertType()) {
77 77 return createProxyInstance(engine);
@@ -98,7 +98,7 @@ private boolean hasAssertFactoryUpperBoundOnAssertType() {
98 98 return false;
99 99 }
100 100
101 -private AssertFactory<Node, SingleNodeAssert> createProxyInstance(JAXPXPathEngine engine) {
101 +private AssertFactory<Node, SingleNodeAssert> createProxyInstance(XPathEngine engine) {
102 102
103 103 try {
104 104 synchronized (AssertFactoryProvider.class) {
@@ -125,7 +125,7 @@ private AssertFactory<Node, SingleNodeAssert> createProxyInstance(JAXPXPathEngin
125 125 return createDefaultInstance(engine);
126 126 }
127 127
128 -private NodeAssertFactory createDefaultInstance(JAXPXPathEngine engine) {
128 +private NodeAssertFactory createDefaultInstance(XPathEngine engine) {
129 129 return new NodeAssertFactory(engine);
130 130 }
131 131
Original file line number Diff line number Diff line change
@@ -13,18 +13,21 @@
13 13 */
14 14 package org.xmlunit.assertj;
15 15
16 +import org.assertj.core.api.AbstractAssert;
16 17 import org.assertj.core.api.AbstractBooleanAssert;
17 18 import org.assertj.core.api.AbstractCharSequenceAssert;
18 19 import org.assertj.core.api.AbstractDoubleAssert;
19 20 import org.assertj.core.api.AbstractIntegerAssert;
20 21 import org.assertj.core.api.AbstractObjectArrayAssert;
21 22 import org.assertj.core.api.AbstractObjectAssert;
23 +import org.assertj.core.api.AssertionInfo;
22 24 import org.assertj.core.api.BooleanAssert;
23 25 import org.assertj.core.api.DoubleAssert;
24 26 import org.assertj.core.api.IntegerAssert;
25 27 import org.assertj.core.api.ObjectArrayAssert;
26 28 import org.assertj.core.api.ObjectAssert;
27 29 import org.assertj.core.api.StringAssert;
30 +import org.assertj.core.api.WritableAssertionInfo;
28 31
29 32 /**
30 33 * Class that is proxy for AssertJ assertions used by org.xmlunit.assertj.*Assert classes.
@@ -37,7 +40,7 @@
37 40 * @see GitHub issude #135
38 41 * @since XMLUnit 2.6.2
39 42 */
40 -class AssertionsAdapter {
43 +final class AssertionsAdapter {
41 44
42 45 private AssertionsAdapter() {
43 46 }
@@ -46,23 +49,56 @@ static AbstractObjectAssert<?, T> assertThat(T actual) {
46 49 return new ObjectAssert<>(actual);
47 50 }
48 51
52 +static <T> AbstractObjectAssert<?, T> assertThat(T actual, AssertionInfo info) {
53 +return withAssertInfo(assertThat(actual), info);
54 + }
55 +
49 56 static <T> AbstractObjectArrayAssert<?, T> assertThat(T[] actual) {
50 57 return new ObjectArrayAssert<>(actual);
51 58 }
52 59
60 +static <T> AbstractObjectArrayAssert<?, T> assertThat(T[] actual, AssertionInfo info) {
61 +return withAssertInfo(assertThat(actual), info);
62 + }
63 +
53 64 static AbstractCharSequenceAssert<?, String> assertThat(String actual) {
54 65 return new StringAssert(actual);
55 66 }
56 67
68 +static AbstractCharSequenceAssert<?, String> assertThat(String actual, AssertionInfo info) {
69 +return withAssertInfo(assertThat(actual), info);
70 + }
71 +
57 72 static AbstractIntegerAssert<?> assertThat(int actual) {
58 73 return new IntegerAssert(actual);
59 74 }
60 75
76 +static AbstractIntegerAssert<?> assertThat(int actual, AssertionInfo info) {
77 +return withAssertInfo(assertThat(actual), info);
78 + }
79 +
61 80 static AbstractDoubleAssert<?> assertThat(double actual) {
62 81 return new DoubleAssert(actual);
63 82 }
64 83
84 +static AbstractDoubleAssert<?> assertThat(double actual, AssertionInfo info) {
85 +return withAssertInfo(assertThat(actual), info);
86 + }
87 +
65 88 static AbstractBooleanAssert<?> assertThat(boolean actual) {
66 89 return new BooleanAssert(actual);
67 90 }
91 +
92 +static AbstractBooleanAssert<?> assertThat(boolean actual, AssertionInfo info) {
93 +return withAssertInfo(assertThat(actual), info);
94 + }
95 +
96 +static <T extends AbstractAssert> T withAssertInfo(T assertion, AssertionInfo info) {
97 +WritableAssertionInfo destInfo = assertion.getWritableAssertionInfo();
98 +destInfo.overridingErrorMessage(info.overridingErrorMessage());
99 +destInfo.description(info.description());
100 +destInfo.useRepresentation(info.representation());
101 +
102 +return assertion;
103 + }
68 104 }
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@
36 36
37 37 import javax.xml.parsers.DocumentBuilderFactory;
38 38
39 +import static org.xmlunit.assertj.AssertionsAdapter.withAssertInfo;
39 40 import static org.xmlunit.assertj.error.ShouldBeNotSimilar.shouldBeNotIdentical;
40 41 import static org.xmlunit.assertj.error.ShouldBeNotSimilar.shouldBeNotSimilar;
41 42 import static org.xmlunit.assertj.error.ShouldBeSimilar.shouldBeIdentical;
@@ -68,33 +69,31 @@ private enum ComparisonContext {
68 69 }
69 70
70 71 private static final String EXPECTING_NOT_NULL = "Expecting control not to be null";
71 -private static DifferenceEvaluator IgnoreNodeListSequence =
72 +private static final DifferenceEvaluator IgnoreNodeListSequence =
72 73 DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.CHILD_NODELIST_SEQUENCE);
73 74
74 75 private final DiffBuilder diffBuilder;
75 76 private ComparisonController customComparisonController;
76 77 private boolean formatXml;
77 78 private ComparisonFormatter formatter = new DefaultComparisonFormatter();
78 79
79 -private CompareAssert(Object actual, DiffBuilder diffBuilder, XmlAssert xmlAssert) {
80 +private CompareAssert(Object actual, DiffBuilder diffBuilder) {
80 81 super(actual, CompareAssert.class);
81 82 this.diffBuilder = diffBuilder;
82 -xmlAssert.fillInState(this);
83 83 }
84 84
85 -static CompareAssert create(Object actual, Object control, Map<String, String> prefix2Uri, DocumentBuilderFactory dbf,
86 -XmlAssert xmlAssert) {
85 +static CompareAssert create(Object actual, Object control, XmlAssertConfig config) {
87 86
88 -AssertionsAdapter.assertThat(control)
87 +AssertionsAdapter.assertThat(control, config.info)
89 88 .as(EXPECTING_NOT_NULL)
90 89 .isNotNull();
91 90
92 91 DiffBuilder diffBuilder = DiffBuilder.compare(control)
93 92 .withTest(actual)
94 - .withNamespaceContext(prefix2Uri)
95 - .withDocumentBuilderFactory(dbf);
93 + .withNamespaceContext(config.prefix2Uri)
94 + .withDocumentBuilderFactory(config.dbf);
96 95
97 -return new CompareAssert(actual, diffBuilder, xmlAssert);
96 +return withAssertInfo(new CompareAssert(actual, diffBuilder), config.info);
98 97 }
99 98
100 99 /**
@@ -348,7 +347,6 @@ private void compare(ComparisonContext context) {
348 347 Diff diff;
349 348
350 349 try {
351 -
352 350 diff = diffBuilder.build();
353 351 } catch (Exception e) {
354 352 throwAssertionError(shouldNotHaveThrown(e));
@@ -366,7 +364,6 @@ private void compare(ComparisonContext context) {
366 364 throwAssertionError(shouldBeSimilar(controlSystemId, testSystemId, firstDifferenceComparison, formatter, formatXml));
367 365 }
368 366 } else {
369 -
370 367 if (ComparisonContext.NOT_IDENTICAL == context) {
371 368 throwAssertionError(shouldBeNotIdentical(controlSystemId, testSystemId));
372 369 } else if (ComparisonContext.NOT_SIMILAR == context) {
Original file line number Diff line number Diff line change
@@ -18,18 +18,12 @@
18 18 import org.assertj.core.api.ObjectAssert;
19 19 import org.assertj.core.description.Description;
20 20 import org.w3c.dom.Node;
21 -import org.xmlunit.builder.Input;
22 -import org.xmlunit.util.Convert;
23 -import org.xmlunit.xpath.JAXPXPathEngine;
21 +import org.xmlunit.xpath.XPathEngine;
24 22
25 23 import java.util.ArrayList;
26 24 import java.util.List;
27 -import java.util.Map;
28 -
29 -import javax.xml.parsers.DocumentBuilderFactory;
30 -import javax.xml.transform.Source;
31 -import javax.xml.xpath.XPathFactory;
32 25
26 +import static org.xmlunit.assertj.AssertionsAdapter.withAssertInfo;
33 27 import static org.xmlunit.assertj.error.ElementsShouldSatisfy.UnsatisfiedRequirement;
34 28 import static org.xmlunit.assertj.error.ElementsShouldSatisfy.elementsShouldSatisfy;
35 29 import static org.xmlunit.assertj.error.ShouldAnyNodeHaveXPath.shouldAnyNodeHaveXPath;
@@ -57,27 +51,25 @@ interface SingleNodeAssertConsumer {
57 51
58 52 private static final AssertFactoryProvider ASSERT_FACTORY_PROVIDER = new AssertFactoryProvider();
59 53
60 -private MultipleNodeAssert(Iterable<Node> nodes, JAXPXPathEngine engine, XmlAssert xmlAssert) {
54 +private MultipleNodeAssert(Iterable<Node> nodes, XPathEngine engine) {
61 55 super(nodes, MultipleNodeAssert.class, ASSERT_FACTORY_PROVIDER.create(engine));
62 -xmlAssert.fillInState(this);
63 56 }
64 57
65 -static MultipleNodeAssert create(Object xmlSource, Map<String, String> prefix2Uri, DocumentBuilderFactory dbf,
66 -XPathFactory xpf, String xPath, XmlAssert xmlAssert) {
67 -
68 -AssertionsAdapter.assertThat(xPath).isNotBlank();
58 +static MultipleNodeAssert create(Object xmlSource, String xPath, XmlAssertConfig config) {
69 59
70 -final JAXPXPathEngine engine = xpf == null ? new JAXPXPathEngine() : new JAXPXPathEngine(xpf);
71 -if (prefix2Uri != null) {
72 -engine.setNamespaceContext(prefix2Uri);
73 - }
60 +AssertionsAdapter.assertThat(xPath, config.info).isNotBlank();
74 61
75 -Source s = Input.from(xmlSource).build();
76 -Node root = dbf != null ? Convert.toNode(s, dbf) : Convert.toNode(s);
62 +final XPathEngine engine = XPathEngineFactory.create(config);
63 +Node root = NodeUtils.parseSource(xmlSource, config);
77 64 Iterable<Node> nodes = engine.selectNodes(xPath, root);
78 65
79 -return new MultipleNodeAssert(nodes, engine, xmlAssert)
80 - .describedAs("XPath \"%s\" evaluated to node set", xPath);
66 +MultipleNodeAssert multipleNodeAssert =
67 +withAssertInfo(new MultipleNodeAssert(nodes, engine), config.info);
68 +if (!multipleNodeAssert.info.hasDescription()) {
69 +multipleNodeAssert.info.description("XPath \"%s\" evaluated to node set", xPath);
70 + }
71 +
72 +return multipleNodeAssert;
81 73 }
82 74
83 75 /**
@@ -292,9 +284,6 @@ private void allSatisfy(SingleNodeAssertConsumer consumer) {
292 284 asListAssert(final List<String> values) {
293 285 AbstractListAssert<?, List<? extends String>, String, ObjectAssert<String>> a =
294 286 newListAssertInstance(values);
295 -a.info.useRepresentation(info.representation());
296 -a.info.description(info.description());
297 -a.info.overridingErrorMessage(info.overridingErrorMessage());
298 -return a;
287 +return withAssertInfo(a, info);
299 288 }
300 289 }
Original file line number Diff line number Diff line change
@@ -15,16 +15,16 @@
15 15
16 16 import org.assertj.core.api.AssertFactory;
17 17 import org.w3c.dom.Node;
18 -import org.xmlunit.xpath.JAXPXPathEngine;
18 +import org.xmlunit.xpath.XPathEngine;
19 19
20 20 /**
21 21 * @since XMLUnit 2.6.1
22 22 */
23 23 class NodeAssertFactory implements AssertFactory<Node, SingleNodeAssert> {
24 24
25 -private JAXPXPathEngine engine;
25 +private XPathEngine engine;
26 26
27 -public NodeAssertFactory(JAXPXPathEngine engine) {
27 +public NodeAssertFactory(XPathEngine engine) {
28 28 this.engine = engine;
29 29 }
30 30
Original file line number Diff line number Diff line change
@@ -14,11 +14,14 @@
14 14 package org.xmlunit.assertj;
15 15
16 16 import org.w3c.dom.Node;
17 +import org.xmlunit.builder.Input;
18 +import org.xmlunit.util.Convert;
17 19 import org.xmlunit.util.Nodes;
18 20
19 21 import java.util.Map;
20 22
21 23 import javax.xml.namespace.QName;
24 +import javax.xml.transform.Source;
22 25
23 26 class NodeUtils {
24 27
@@ -39,6 +42,11 @@ static String attributeValue(Node node, String attributeName) {
39 42 return null;
40 43 }
41 44
45 +static Node parseSource(Object xmlSource, XmlAssertConfig config) {
46 +Source s = Input.from(xmlSource).build();
47 +return config.dbf != null ? Convert.toNode(s, config.dbf) : Convert.toNode(s);
48 + }
49 +
42 50 private static boolean matchQName(QName qName, String name) {
43 51
44 52 return qName.toString().equals(name)
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
15 15
16 16 import org.assertj.core.api.AbstractAssert;
17 17 import org.w3c.dom.Node;
18 -import org.xmlunit.xpath.JAXPXPathEngine;
18 +import org.xmlunit.xpath.XPathEngine;
19 19
20 20 import static org.xmlunit.assertj.error.ShouldHaveAttribute.shouldHaveAttribute;
21 21 import static org.xmlunit.assertj.error.ShouldHaveAttribute.shouldHaveAttributeWithValue;
@@ -40,9 +40,9 @@
40 40 */
41 41 public class SingleNodeAssert extends AbstractAssert<SingleNodeAssert, Node> {
42 42
43 -private final JAXPXPathEngine engine;
43 +private final XPathEngine engine;
44 44
45 -SingleNodeAssert(Node node, JAXPXPathEngine engine) {
45 +SingleNodeAssert(Node node, XPathEngine engine) {
46 46 super(node, SingleNodeAssert.class);
47 47 this.engine = engine;
48 48 }
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
22 22 import javax.xml.transform.Source;
23 23 import javax.xml.validation.Schema;
24 24
25 +import static org.xmlunit.assertj.AssertionsAdapter.withAssertInfo;
25 26 import static org.xmlunit.assertj.error.ShouldBeInvalid.shouldBeInvalid;
26 27 import static org.xmlunit.assertj.error.ShouldBeValid.shouldBeValid;
27 28
@@ -45,18 +46,17 @@ public class ValidationAssert extends AbstractAssert<ValidationAssert, Source> {
45 46 private final Source[] schemaSources;
46 47 private final Schema schema;
47 48
48 -private ValidationAssert(Source actual, Source[] schemaSources, Schema schema, XmlAssert xmlAssert) {
49 +private ValidationAssert(Source actual, Source[] schemaSources, Schema schema) {
49 50 super(actual, ValidationAssert.class);
50 51 this.schemaSources = schemaSources;
51 52 this.schema = schema;
52 -xmlAssert.fillInState(this);
53 53 }
54 54
55 -static ValidationAssert create(Object xmlSource, XmlAssert xmlAssert, Object... schemaSources) {
55 +static ValidationAssert create(Object xmlSource, XmlAssertConfig config, Object... schemaSources) {
56 56
57 -AssertionsAdapter.assertThat(xmlSource).isNotNull();
57 +AssertionsAdapter.assertThat(xmlSource, config.info).isNotNull();
58 58
59 -AssertionsAdapter.assertThat(schemaSources)
59 +AssertionsAdapter.assertThat(schemaSources, config.info)
60 60 .isNotNull()
61 61 .doesNotContainNull();
62 62
@@ -68,24 +68,24 @@ static ValidationAssert create(Object xmlSource, XmlAssert xmlAssert, Object...
68 68 sources[i] = Input.from(schemaSources[i]).build();
69 69 }
70 70
71 -return new ValidationAssert(source, sources, null, xmlAssert);
71 +return withAssertInfo(new ValidationAssert(source, sources, null), config.info);
72 72 }
73 73
74 -static ValidationAssert create(Object xmlSource, Schema schema, XmlAssert xmlAssert) {
74 +static ValidationAssert create(Object xmlSource, Schema schema, XmlAssertConfig config) {
75 75
76 -AssertionsAdapter.assertThat(xmlSource).isNotNull();
77 -AssertionsAdapter.assertThat(schema).isNotNull();
76 +AssertionsAdapter.assertThat(xmlSource, config.info).isNotNull();
77 +AssertionsAdapter.assertThat(schema, config.info).isNotNull();
78 78
79 79 Source source = Input.from(xmlSource).build();
80 80
81 -return new ValidationAssert(source, null, schema, xmlAssert);
81 +return withAssertInfo(new ValidationAssert(source, null, schema), config.info);
82 82 }
83 83
84 -static ValidationAssert create(Object xmlSource, XmlAssert xmlAssert) {
84 +static ValidationAssert create(Object xmlSource, XmlAssertConfig config) {
85 85
86 86 Source source = Input.from(xmlSource).build();
87 87
88 -return new ValidationAssert(source, null, null, xmlAssert);
88 +return withAssertInfo(new ValidationAssert(source, null, null), config.info);
89 89 }
90 90
91 91 private ValidationResult validate() {