Refuse empty strings in Wikidata schema

This commit is contained in:
Antonin Delpeuch 2018-03-23 11:17:38 +00:00
parent b1e368e7a8
commit 0961ff2949
2 changed files with 42 additions and 0 deletions

View File

@ -37,6 +37,8 @@ public class WbStringConstant implements WbExpression<StringValue> {
@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<StringValue> {
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();
}
}

View File

@ -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<StringValue> {
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("");
}
}