From 0961ff2949cd10d4da1e6c7abea7c0fdea7e4d52 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Fri, 23 Mar 2018 11:17:38 +0000 Subject: [PATCH] Refuse empty strings in Wikidata schema --- .../wikidata/schema/WbStringConstant.java | 15 +++++++++++ .../wikidata/schema/WbStringConstantTest.java | 27 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbStringConstantTest.java diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java index 5fe0afa0d..7a1ddf9e3 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java @@ -37,6 +37,8 @@ public class WbStringConstant implements WbExpression { @JsonCreator public WbStringConstant(@JsonProperty("value") String value) { Validate.notNull(value); + Validate.isTrue(!value.isEmpty()); // for now we don't accept empty strings + // because in the variable counterpart of this expression, they are skipped this.value = value; } @@ -49,4 +51,17 @@ public class WbStringConstant implements WbExpression { public String getValue() { return value; } + + @Override + public boolean equals(Object other) { + if(other == null || !WbStringConstant.class.isInstance(other)) { + return false; + } + return value.equals(((WbStringConstant)other).getValue()); + } + + @Override + public int hashCode() { + return value.hashCode(); + } } diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbStringConstantTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbStringConstantTest.java new file mode 100644 index 000000000..9066b6c93 --- /dev/null +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbStringConstantTest.java @@ -0,0 +1,27 @@ +package org.openrefine.wikidata.schema; + +import org.openrefine.wikidata.testing.JacksonSerializationTest; +import org.testng.annotations.Test; +import org.wikidata.wdtk.datamodel.helpers.Datamodel; +import org.wikidata.wdtk.datamodel.interfaces.StringValue; + +public class WbStringConstantTest extends WbExpressionTest { + + private WbStringConstant constant = new WbStringConstant("hello world"); + + @Test + public void testSerialize() { + JacksonSerializationTest.canonicalSerialization(WbExpression.class, constant, + "{\"type\":\"wbstringconstant\",\"value\":\"hello world\"}"); + } + + @Test + public void testEvaluate() { + evaluatesTo(Datamodel.makeStringValue("hello world"), constant); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testEmpty() { + new WbStringConstant(""); + } +}