|
| 1 | +/* |
| 2 | + * Copyright (C) 2014, Evolved Binary Ltd |
| 3 | + * |
| 4 | + * This file was originally ported from FusionDB to eXist-db by |
| 5 | + * Evolved Binary, for the benefit of the eXist-db Open Source community. |
| 6 | + * Only the ported code as it appears in this file, at the time that |
| 7 | + * it was contributed to eXist-db, was re-licensed under The GNU |
| 8 | + * Lesser General Public License v2.1 only for use in eXist-db. |
| 9 | + * |
| 10 | + * This license grant applies only to a snapshot of the code as it |
| 11 | + * appeared when ported, it does not offer or infer any rights to either |
| 12 | + * updates of this source code or access to the original source code. |
| 13 | + * |
| 14 | + * The GNU Lesser General Public License v2.1 only license follows. |
| 15 | + * |
| 16 | + * --------------------------------------------------------------------- |
| 17 | + * |
| 18 | + * Copyright (C) 2014, Evolved Binary Ltd |
| 19 | + * |
| 20 | + * This library is free software; you can redistribute it and/or |
| 21 | + * modify it under the terms of the GNU Lesser General Public |
| 22 | + * License as published by the Free Software Foundation; version 2.1. |
| 23 | + * |
| 24 | + * This library is distributed in the hope that it will be useful, |
| 25 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 27 | + * Lesser General Public License for more details. |
| 28 | + * |
| 29 | + * You should have received a copy of the GNU Lesser General Public |
| 30 | + * License along with this library; if not, write to the Free Software |
| 31 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 | + */ |
| 33 | +package org.exist.test; |
| 34 | + |
| 35 | +import org.exist.xquery.value.Item; |
| 36 | +import org.exist.xquery.value.NodeValue; |
| 37 | +import org.exist.xquery.value.Sequence; |
| 38 | +import org.hamcrest.Description; |
| 39 | +import org.hamcrest.DiagnosingMatcher; |
| 40 | +import org.w3c.dom.Node; |
| 41 | +import org.xmlunit.builder.DiffBuilder; |
| 42 | +import org.xmlunit.builder.Input; |
| 43 | +import org.xmlunit.diff.Diff; |
| 44 | +import org.xmlunit.util.Convert; |
| 45 | + |
| 46 | +import javax.xml.transform.Source; |
| 47 | + |
| 48 | +/** |
| 49 | + * Implementation of a Hamcrest Matcher |
| 50 | + * which will compare XML nodes. |
| 51 | + * |
| 52 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
| 53 | + */ |
| 54 | +public class DiffMatcher extends DiagnosingMatcher<Sequence> { |
| 55 | + private final Source expectedSource; |
| 56 | + private final boolean identical; |
| 57 | + |
| 58 | + private DiffMatcher(final Source expectedSource) { |
| 59 | + this(expectedSource, false); |
| 60 | + } |
| 61 | + |
| 62 | + private DiffMatcher(final Source expectedSource, final boolean identical) { |
| 63 | + this.expectedSource = expectedSource; |
| 64 | + this.identical = identical; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Compares that the XML sources are similar. |
| 69 | + * |
| 70 | + * In this context "similar" is defined by {@link DiffBuilder#checkForSimilar()}. |
| 71 | + * |
| 72 | + * @param expectedSource the expected XML |
| 73 | + * |
| 74 | + * @return The Hamcrest Matcher |
| 75 | + */ |
| 76 | + public static DiffMatcher hasSimilarXml(final Source expectedSource) { |
| 77 | + return new DiffMatcher(expectedSource); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Compares that the XML sources are identical. |
| 82 | + * |
| 83 | + * In this context "similar" is defined by {@link DiffBuilder#checkForIdentical()} ()}. |
| 84 | + * |
| 85 | + * @param expectedSource the expected XML |
| 86 | + * |
| 87 | + * @return The Hamcrest Matcher |
| 88 | + */ |
| 89 | + public static DiffMatcher hasIdenticalXml(final Source expectedSource) { |
| 90 | + return new DiffMatcher(expectedSource, true); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean matches(final Object item, final Description mismatch) { |
| 95 | + if (item == null) { |
| 96 | + mismatch.appendText("null"); |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + final Item actualItem; |
| 101 | + if (item instanceof NodeValue) { |
| 102 | + actualItem = (NodeValue) item; |
| 103 | + |
| 104 | + } else if (item instanceof Sequence) { |
| 105 | + final Sequence actual = ((Sequence) item); |
| 106 | + |
| 107 | + if (actual.getItemCount() != 1) { |
| 108 | + mismatch.appendText("Sequence does not contain 1 item"); |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + actualItem = actual.itemAt(0); |
| 113 | + if (!(actualItem instanceof NodeValue)) { |
| 114 | + mismatch.appendText("Sequence does not contain a Node"); |
| 115 | + return false; |
| 116 | + } |
| 117 | + } else { |
| 118 | + mismatch.appendText("is not a Node"); |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + final Source actualSource = Input.fromNode((org.w3c.dom.Node) actualItem).build(); |
| 123 | + |
| 124 | + DiffBuilder diffBuilder = DiffBuilder.compare(expectedSource) |
| 125 | + .withTest(actualSource); |
| 126 | + if (identical) { |
| 127 | + diffBuilder = diffBuilder.checkForIdentical(); |
| 128 | + } else { |
| 129 | + diffBuilder = diffBuilder.checkForSimilar(); |
| 130 | + } |
| 131 | + |
| 132 | + final Diff diff = diffBuilder.build(); |
| 133 | + if (diff.hasDifferences()) { |
| 134 | + mismatch.appendText("differences: " + diff.toString()); |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void describeTo(final Description description) { |
| 143 | + description |
| 144 | + .appendText("nodes match ") |
| 145 | + .appendValue(expectedSource); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Creates an Document Source form an XML String. |
| 150 | + * |
| 151 | + * @param str a string representation of XML. |
| 152 | + * |
| 153 | + * @return a Document Source. |
| 154 | + */ |
| 155 | + public static Source docSource(final String str) { |
| 156 | + return Input.fromString(str).build(); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Creates an Element Source form an XML String. |
| 161 | + * |
| 162 | + * @param str a string representation of XML. |
| 163 | + * |
| 164 | + * @return an Element Source. |
| 165 | + */ |
| 166 | + public static Source elemSource(final String str) { |
| 167 | + final Node documentNode = Convert.toNode(docSource(str)); |
| 168 | + final Node firstElement = documentNode.getFirstChild(); |
| 169 | + return Input.fromNode(firstElement).build(); |
| 170 | + } |
| 171 | +} |
0 commit comments