Fix #443 - XhtmlNode.equalsDeep() did not work

This commit is contained in:
James Agnew 2016-09-29 15:00:01 -04:00
parent 5da1d22d02
commit 8b80d932da
4 changed files with 100 additions and 12 deletions

View File

@ -28,6 +28,8 @@ POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.hl7.fhir.utilities.xhtml; package org.hl7.fhir.utilities.xhtml;
import static org.apache.commons.lang3.StringUtils.isBlank;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -39,10 +41,34 @@ import ca.uhn.fhir.model.primitive.XhtmlDt;
@ca.uhn.fhir.model.api.annotation.DatatypeDef(name="xhtml") @ca.uhn.fhir.model.api.annotation.DatatypeDef(name="xhtml")
public class XhtmlNode implements IBaseXhtml { public class XhtmlNode implements IBaseXhtml {
private static final long serialVersionUID = -4362547161441436492L;
public static class Location {
private int line;
private int column;
public Location(int line, int column) {
super();
this.line = line;
this.column = column;
}
public int getLine() {
return line;
}
public int getColumn() {
return column;
}
@Override
public String toString() {
return "Line "+Integer.toString(line)+", column "+Integer.toString(column);
}
}
public static final String NBSP = Character.toString((char)0xa0); public static final String NBSP = Character.toString((char)0xa0);
private static final String DECL_XMLNS = " xmlns=\"http://www.w3.org/1999/xhtml\""; private static final String DECL_XMLNS = " xmlns=\"http://www.w3.org/1999/xhtml\"";
private Location location;
private NodeType nodeType; private NodeType nodeType;
private String name; private String name;
private Map<String, String> attributes = new HashMap<String, String>(); private Map<String, String> attributes = new HashMap<String, String>();
@ -252,25 +278,27 @@ public class XhtmlNode implements IBaseXhtml {
return dst; return dst;
} }
@Override
public boolean isEmpty() { public boolean isEmpty() {
return (childNodes == null || childNodes.isEmpty()) && content == null; return (childNodes == null || childNodes.isEmpty()) && content == null;
} }
public boolean equalsDeep(XhtmlNode other) { public boolean equalsDeep(XhtmlNode other) {
if (other instanceof XhtmlNode) if (other == null) {
return false; return false;
XhtmlNode o = (XhtmlNode) other; }
if (!(nodeType == o.nodeType) || !compare(name, o.name) || !compare(content, o.content))
if (!(nodeType == other.nodeType) || !compare(name, other.name) || !compare(content, other.content))
return false; return false;
if (attributes.size() != o.attributes.size()) if (attributes.size() != other.attributes.size())
return false; return false;
for (String an : attributes.keySet()) for (String an : attributes.keySet())
if (!attributes.get(an).equals(o.attributes.get(an))) if (!attributes.get(an).equals(other.attributes.get(an)))
return false; return false;
if (childNodes.size() != o.childNodes.size()) if (childNodes.size() != other.childNodes.size())
return false; return false;
for (int i = 0; i < childNodes.size(); i++) { for (int i = 0; i < childNodes.size(); i++) {
if (!compareDeep(childNodes.get(i), o.childNodes.get(i))) if (!compareDeep(childNodes.get(i), other.childNodes.get(i)))
return false; return false;
} }
return true; return true;
@ -302,6 +330,7 @@ public class XhtmlNode implements IBaseXhtml {
} }
@Override
public String getValueAsString() { public String getValueAsString() {
if (isEmpty()) { if (isEmpty()) {
return null; return null;
@ -316,20 +345,18 @@ public class XhtmlNode implements IBaseXhtml {
} }
} }
@Override
public void setValueAsString(String theValue) throws IllegalArgumentException { public void setValueAsString(String theValue) throws IllegalArgumentException {
this.attributes = null; this.attributes = null;
this.childNodes = null; this.childNodes = null;
this.content = null; this.content = null;
this.name = null; this.name = null;
this.nodeType= null; this.nodeType= null;
if (theValue == null) { if (isBlank(theValue)) {
return; return;
} }
String val = theValue.trim(); String val = theValue.trim();
if (theValue == null || theValue.isEmpty()) {
return;
}
if (!val.startsWith("<")) { if (!val.startsWith("<")) {
val = "<div" + DECL_XMLNS +">" + val + "</div>"; val = "<div" + DECL_XMLNS +">" + val + "</div>";
@ -399,4 +426,14 @@ public List<String> getFormatCommentsPost() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
} }

View File

@ -0,0 +1,44 @@
package ca.uhn.fhir.model;
import static org.junit.Assert.*;
import org.hl7.fhir.dstu3.model.ExplanationOfBenefit;
import org.junit.AfterClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.util.TestUtil;
public class XhtmlNodeTest {
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
private static FhirContext ourCtx = FhirContext.forDstu3();
/**
* See #443
*/
@Test
public void testDeepEquals() {
String input =
"<ExplanationOfBenefit xmlns=\"http://hl7.org/fhir\">" +
"<text>" +
"<status value=\"generated\"/>" +
"<div xmlns=\"http://www.w3.org/1999/xhtml\">A human-readable rendering of the ExplanationOfBenefit</div>" +
"</text>" +
"</ExplanationOfBenefit>";
ExplanationOfBenefit copy1 = ourCtx.newXmlParser().parseResource(ExplanationOfBenefit.class, input);
ExplanationOfBenefit copy2 = ourCtx.newXmlParser().parseResource(ExplanationOfBenefit.class, input);
assertTrue(copy1.equalsDeep(copy2));
assertTrue(copy1.equalsShallow(copy2));
}
}

View File

@ -114,6 +114,13 @@
STU3 validator has been upgrated to include fixes made since the STU3 validator has been upgrated to include fixes made since the
1.6.0 ballot 1.6.0 ballot
</action> </action>
<action type="fix" issue="443">
XhtmlNode.equalsDeep() contained a bug which caused resources
containing a narrative to always return
<![CDATA[<code>false</code>]]> for STU3
<![CDATA[<code>Resource#equalsDeep()</code>]]>. Thanks to
GitHub user @XcrigX for reporting!
</action>
</release> </release>
<release version="2.0" date="2016-08-30"> <release version="2.0" date="2016-08-30">
<action type="fix"> <action type="fix">

View File

@ -64,7 +64,7 @@
</tr> </tr>
<tr> <tr>
<td>2.0</td> <td>2.0</td>
<td><a href="http://hl7-fhir.github.io/">STU3 1.5.0 Snapshot</a> (SVN 9395)</td> <td><a href="http://hl7-fhir.github.io/">STU3 1.6.0</a> (SVN 9663)</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>