Fix NPE in empty InExpression.toString

This commit is contained in:
Ville Skyttä 2016-10-13 00:20:59 +03:00
parent 586abba94e
commit 6fbafc4441
2 changed files with 16 additions and 8 deletions

View File

@ -61,15 +61,12 @@ public abstract class UnaryExpression implements Expression {
final boolean not) { final boolean not) {
// Use a HashSet if there are many elements. // Use a HashSet if there are many elements.
Collection<Object> t; final Collection<Object> inList;
if (elements.size() == 0) { if (elements.size() < 5) {
t = null; inList = elements;
} else if (elements.size() < 5) {
t = elements;
} else { } else {
t = new HashSet<>(elements); inList = new HashSet<>(elements);
} }
final Collection<Object> inList = t;
return new BooleanUnaryExpression(right) { return new BooleanUnaryExpression(right) {
@Override @Override
@ -83,7 +80,7 @@ public abstract class UnaryExpression implements Expression {
return null; return null;
} }
return (inList != null && inList.contains(rvalue)) ^ not; return inList.contains(rvalue) ^ not;
} }
@Override @Override

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.activemq.artemis.selector.filter; package org.apache.activemq.artemis.selector.filter;
import java.util.Collections;
import org.apache.activemq.artemis.selector.impl.SelectorParser; import org.apache.activemq.artemis.selector.impl.SelectorParser;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -30,4 +32,13 @@ public class UnaryExpressionTest {
Assert.assertTrue("Created unary expression 2", expr2 instanceof UnaryExpression); Assert.assertTrue("Created unary expression 2", expr2 instanceof UnaryExpression);
Assert.assertEquals("Unary expressions are equal", expr1, expr2); Assert.assertEquals("Unary expressions are equal", expr1, expr2);
} }
@Test
public void testInExpressionToString() throws Exception {
BooleanExpression expr;
expr = UnaryExpression.createInExpression(new PropertyExpression("foo"), Collections.<Object>singletonList("bar"), false);
Assert.assertTrue(expr.toString().matches("foo\\s+IN\\s+.*bar.*"));
expr = UnaryExpression.createInExpression(new PropertyExpression("foo"), Collections.emptyList(), false);
Assert.assertTrue(expr.toString().matches("foo\\s+IN\\s+.*"));
}
} }