Skip to content

Apache Lucene open-source search software

LUCENE-5339: also catch invalid components in *FacetField

Michael McCandlessf8b91da0d40b
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/FacetField.java b/lucene/facet/src/java/org/apache/lucene/facet/FacetField.java
index c1a519e58c5..7c41e1e5c3a 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/FacetField.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/FacetField.java
@@ -42,6 +42,10 @@ public class FacetField extends Field {
* {@code path}. */
public FacetField(String dim, String... path) {
super("dummy", TYPE);
+ verifyLabel(dim);
+ for(String label : path) {
+ verifyLabel(label);
+ }
this.dim = dim;
if (path.length == 0) {
throw new IllegalArgumentException("path must have at least one element");
@@ -53,4 +57,13 @@ public class FacetField extends Field {
public String toString() {
return "FacetField(dim=" + dim + " path=" + Arrays.toString(path) + ")";
}
+
+ /** Verifies the label is not null or empty string.
+ *
+ * @lucene.internal */
+ public static void verifyLabel(String label) {
+ if (label == null || label.isEmpty()) {
+ throw new IllegalArgumentException("empty or null components not allowed; got: " + label);
+ }
+ }
}
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetField.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetField.java
index 33e5381e028..48ec6ff46bf 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetField.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetField.java
@@ -19,6 +19,7 @@ package org.apache.lucene.facet.sortedset;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
+import org.apache.lucene.facet.FacetField;
/** Add an instance of this to your Document for every facet
* label to be indexed via SortedSetDocValues. */
@@ -40,6 +41,8 @@ public class SortedSetDocValuesFacetField extends Field {
/** Sole constructor. */
public SortedSetDocValuesFacetField(String dim, String label) {
super("dummy", TYPE);
+ FacetField.verifyLabel(label);
+ FacetField.verifyLabel(dim);
this.dim = dim;
this.label = label;
}
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/AssociationFacetField.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/AssociationFacetField.java
index b03591d87f8..88a4d0cb514 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/AssociationFacetField.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/AssociationFacetField.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
import org.apache.lucene.document.Document; // javadocs
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
+import org.apache.lucene.facet.FacetField;
import org.apache.lucene.facet.Facets;
import org.apache.lucene.util.BytesRef;
@@ -56,6 +57,10 @@ public class AssociationFacetField extends Field {
* association */
public AssociationFacetField(BytesRef assoc, String dim, String... path) {
super("dummy", TYPE);
+ FacetField.verifyLabel(dim);
+ for(String label : path) {
+ FacetField.verifyLabel(label);
+ }
this.dim = dim;
this.assoc = assoc;
if (path.length == 0) {
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestFacetLabel.java b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestFacetLabel.java
index bb025311589..e0200086e03 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestFacetLabel.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestFacetLabel.java
@@ -2,7 +2,10 @@ package org.apache.lucene.facet.taxonomy;
import java.util.Arrays;
+import org.apache.lucene.facet.FacetField;
import org.apache.lucene.facet.FacetTestCase;
+import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
+import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util._TestUtil;
import org.junit.Test;
@@ -158,8 +161,104 @@ public class TestFacetLabel extends FacetTestCase {
assertNotNull(new FacetLabel(components));
fail("empty or null components should not be allowed: " + Arrays.toString(components));
} catch (IllegalArgumentException e) {
- // ok
+ // expected
}
+ try {
+ new FacetField("dim", components);
+ fail("empty or null components should not be allowed: " + Arrays.toString(components));
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new AssociationFacetField(new BytesRef(), "dim", components);
+ fail("empty or null components should not be allowed: " + Arrays.toString(components));
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new IntAssociationFacetField(17, "dim", components);
+ fail("empty or null components should not be allowed: " + Arrays.toString(components));
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new FloatAssociationFacetField(17.0f, "dim", components);
+ fail("empty or null components should not be allowed: " + Arrays.toString(components));
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+ try {
+ new FacetField(null, new String[] {"abc"});
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new FacetField("", new String[] {"abc"});
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new IntAssociationFacetField(17, null, new String[] {"abc"});
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new IntAssociationFacetField(17, "", new String[] {"abc"});
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new FloatAssociationFacetField(17.0f, null, new String[] {"abc"});
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new FloatAssociationFacetField(17.0f, "", new String[] {"abc"});
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new AssociationFacetField(new BytesRef(), null, new String[] {"abc"});
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new AssociationFacetField(new BytesRef(), "", new String[] {"abc"});
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new SortedSetDocValuesFacetField(null, "abc");
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new SortedSetDocValuesFacetField("", "abc");
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new SortedSetDocValuesFacetField("dim", null);
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ new SortedSetDocValuesFacetField("dim", "");
+ fail("empty or null components should not be allowed");
+ } catch (IllegalArgumentException e) {
+ // expected
}
}