Merge pull request #12444 from cbuescher/feature/query-refactoring-nullCheckCleanup

Query Refactoring: Move null-checks to validate
This commit is contained in:
Christoph Büscher 2015-07-24 18:17:36 +02:00
commit db5d73a617
22 changed files with 123 additions and 178 deletions

View File

@ -233,7 +233,7 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder> exte
return queries; return queries;
} }
protected static QueryValidationException validateInnerQueries(List<QueryBuilder> queryBuilders, QueryValidationException initialValidationException) { protected QueryValidationException validateInnerQueries(List<QueryBuilder> queryBuilders, QueryValidationException initialValidationException) {
QueryValidationException validationException = initialValidationException; QueryValidationException validationException = initialValidationException;
for (QueryBuilder queryBuilder : queryBuilders) { for (QueryBuilder queryBuilder : queryBuilders) {
validationException = validateInnerQuery(queryBuilder, validationException); validationException = validateInnerQuery(queryBuilder, validationException);
@ -241,13 +241,15 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder> exte
return validationException; return validationException;
} }
protected static QueryValidationException validateInnerQuery(QueryBuilder queryBuilder, QueryValidationException initialValidationException) { protected QueryValidationException validateInnerQuery(QueryBuilder queryBuilder, QueryValidationException initialValidationException) {
QueryValidationException validationException = initialValidationException; QueryValidationException validationException = initialValidationException;
if (queryBuilder != null) { if (queryBuilder != null) {
QueryValidationException queryValidationException = queryBuilder.validate(); QueryValidationException queryValidationException = queryBuilder.validate();
if (queryValidationException != null) { if (queryValidationException != null) {
validationException = QueryValidationException.addValidationErrors(queryValidationException.validationErrors(), validationException); validationException = QueryValidationException.addValidationErrors(queryValidationException.validationErrors(), validationException);
} }
} else {
validationException = addValidationError("inner query cannot be null", validationException);
} }
return validationException; return validationException;
} }

View File

@ -51,7 +51,7 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
*/ */
public AndQueryBuilder(QueryBuilder... filters) { public AndQueryBuilder(QueryBuilder... filters) {
for (QueryBuilder filter : filters) { for (QueryBuilder filter : filters) {
this.filters.add(Objects.requireNonNull(filter)); this.filters.add(filter);
} }
} }
@ -60,7 +60,7 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
* @param filterBuilder nested filter, no <tt>null</tt> value allowed * @param filterBuilder nested filter, no <tt>null</tt> value allowed
*/ */
public AndQueryBuilder add(QueryBuilder filterBuilder) { public AndQueryBuilder add(QueryBuilder filterBuilder) {
filters.add(Objects.requireNonNull(filterBuilder)); filters.add(filterBuilder);
return this; return this;
} }

View File

@ -68,7 +68,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> {
* contribute to scoring. No <tt>null</tt> value allowed. * contribute to scoring. No <tt>null</tt> value allowed.
*/ */
public BoolQueryBuilder must(QueryBuilder queryBuilder) { public BoolQueryBuilder must(QueryBuilder queryBuilder) {
mustClauses.add(Objects.requireNonNull(queryBuilder)); mustClauses.add(queryBuilder);
return this; return this;
} }
@ -84,7 +84,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> {
* not contribute to scoring. No <tt>null</tt> value allowed. * not contribute to scoring. No <tt>null</tt> value allowed.
*/ */
public BoolQueryBuilder filter(QueryBuilder queryBuilder) { public BoolQueryBuilder filter(QueryBuilder queryBuilder) {
filterClauses.add(Objects.requireNonNull(queryBuilder)); filterClauses.add(queryBuilder);
return this; return this;
} }
@ -100,7 +100,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> {
* No <tt>null</tt> value allowed. * No <tt>null</tt> value allowed.
*/ */
public BoolQueryBuilder mustNot(QueryBuilder queryBuilder) { public BoolQueryBuilder mustNot(QueryBuilder queryBuilder) {
mustNotClauses.add(Objects.requireNonNull(queryBuilder)); mustNotClauses.add(queryBuilder);
return this; return this;
} }
@ -119,7 +119,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> {
* @see #minimumNumberShouldMatch(int) * @see #minimumNumberShouldMatch(int)
*/ */
public BoolQueryBuilder should(QueryBuilder queryBuilder) { public BoolQueryBuilder should(QueryBuilder queryBuilder) {
shouldClauses.add(Objects.requireNonNull(queryBuilder)); shouldClauses.add(queryBuilder);
return this; return this;
} }

View File

@ -50,15 +50,7 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
private float negativeBoost = -1; private float negativeBoost = -1;
static final BoostingQueryBuilder PROTOTYPE = new BoostingQueryBuilder(); static final BoostingQueryBuilder PROTOTYPE = new BoostingQueryBuilder(null, null);
/**
* this constructor only used for prototype
*/
private BoostingQueryBuilder() {
this.positiveQuery = null;
this.negativeQuery = null;
}
/** /**
* Create a new {@link BoostingQueryBuilder} * Create a new {@link BoostingQueryBuilder}
@ -67,8 +59,8 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
* @param negativeQuery the negative query for this boosting query. * @param negativeQuery the negative query for this boosting query.
*/ */
public BoostingQueryBuilder(QueryBuilder positiveQuery, QueryBuilder negativeQuery) { public BoostingQueryBuilder(QueryBuilder positiveQuery, QueryBuilder negativeQuery) {
this.positiveQuery = Objects.requireNonNull(positiveQuery); this.positiveQuery = positiveQuery;
this.negativeQuery = Objects.requireNonNull(negativeQuery); this.negativeQuery = negativeQuery;
} }
/** /**
@ -118,8 +110,16 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
if (negativeBoost < 0) { if (negativeBoost < 0) {
validationException = addValidationError("query requires negativeBoost to be set to positive value", validationException); validationException = addValidationError("query requires negativeBoost to be set to positive value", validationException);
} }
validationException = validateInnerQuery(negativeQuery, validationException); if (negativeQuery == null) {
validationException = validateInnerQuery(positiveQuery, validationException); validationException = addValidationError("inner clause [negative] cannot be null.", validationException);
} else {
validationException = validateInnerQuery(negativeQuery, validationException);
}
if (positiveQuery == null) {
validationException = addValidationError("inner clause [positive] cannot be null.", validationException);
} else {
validationException = validateInnerQuery(positiveQuery, validationException);
}
return validationException; return validationException;
} }

View File

@ -38,12 +38,7 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScor
private final QueryBuilder filterBuilder; private final QueryBuilder filterBuilder;
static final ConstantScoreQueryBuilder PROTOTYPE = new ConstantScoreQueryBuilder(); static final ConstantScoreQueryBuilder PROTOTYPE = new ConstantScoreQueryBuilder(null);
// only used for prototype
private ConstantScoreQueryBuilder() {
this.filterBuilder = null;
}
/** /**
* A query that wraps another query and simply returns a constant score equal to the * A query that wraps another query and simply returns a constant score equal to the
@ -52,7 +47,7 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScor
* @param filterBuilder The query to wrap in a constant score query * @param filterBuilder The query to wrap in a constant score query
*/ */
public ConstantScoreQueryBuilder(QueryBuilder filterBuilder) { public ConstantScoreQueryBuilder(QueryBuilder filterBuilder) {
this.filterBuilder = Objects.requireNonNull(filterBuilder); this.filterBuilder = filterBuilder;
} }
/** /**
@ -83,7 +78,13 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScor
@Override @Override
public QueryValidationException validate() { public QueryValidationException validate() {
return validateInnerQuery(filterBuilder, null); QueryValidationException validationException = null;
if (filterBuilder == null) {
validationException = addValidationError("inner clause [filter] cannot be null.", validationException);
} else {
validateInnerQuery(filterBuilder, validationException);
}
return validationException;
} }
@Override @Override

View File

@ -52,7 +52,7 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder<DisMaxQueryBuilder>
* Add a sub-query to this disjunction. * Add a sub-query to this disjunction.
*/ */
public DisMaxQueryBuilder add(QueryBuilder queryBuilder) { public DisMaxQueryBuilder add(QueryBuilder queryBuilder) {
queries.add(Objects.requireNonNull(queryBuilder)); queries.add(queryBuilder);
return this; return this;
} }

View File

@ -39,7 +39,7 @@ public class FQueryFilterBuilder extends AbstractQueryBuilder<FQueryFilterBuilde
public static final String NAME = "fquery"; public static final String NAME = "fquery";
static final FQueryFilterBuilder PROTOTYPE = new FQueryFilterBuilder(); static final FQueryFilterBuilder PROTOTYPE = new FQueryFilterBuilder(null);
private final QueryBuilder queryBuilder; private final QueryBuilder queryBuilder;
@ -49,11 +49,7 @@ public class FQueryFilterBuilder extends AbstractQueryBuilder<FQueryFilterBuilde
* @param queryBuilder The query to wrap as a filter * @param queryBuilder The query to wrap as a filter
*/ */
public FQueryFilterBuilder(QueryBuilder queryBuilder) { public FQueryFilterBuilder(QueryBuilder queryBuilder) {
this.queryBuilder = Objects.requireNonNull(queryBuilder); this.queryBuilder = queryBuilder;
}
private FQueryFilterBuilder() {
this.queryBuilder = null;
} }
/** /**

View File

@ -38,13 +38,7 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
private final String fieldName; private final String fieldName;
static final FieldMaskingSpanQueryBuilder PROTOTYPE = new FieldMaskingSpanQueryBuilder(); static final FieldMaskingSpanQueryBuilder PROTOTYPE = new FieldMaskingSpanQueryBuilder(null, null);
// only used for prototype
private FieldMaskingSpanQueryBuilder() {
this.queryBuilder = null;
this.fieldName = null;
}
/** /**
* Constructs a new {@link FieldMaskingSpanQueryBuilder} given an inner {@link SpanQueryBuilder} for * Constructs a new {@link FieldMaskingSpanQueryBuilder} given an inner {@link SpanQueryBuilder} for
@ -53,8 +47,8 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
* @param fieldName the field name * @param fieldName the field name
*/ */
public FieldMaskingSpanQueryBuilder(SpanQueryBuilder queryBuilder, String fieldName) { public FieldMaskingSpanQueryBuilder(SpanQueryBuilder queryBuilder, String fieldName) {
this.queryBuilder = Objects.requireNonNull(queryBuilder); this.queryBuilder = queryBuilder;
this.fieldName = Objects.requireNonNull(fieldName); this.fieldName = fieldName;
} }
/** /**
@ -95,11 +89,16 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
@Override @Override
public QueryValidationException validate() { public QueryValidationException validate() {
QueryValidationException validationExceptions = validateInnerQuery(queryBuilder, null); QueryValidationException validationException = null;
if (fieldName == null || fieldName.isEmpty()) { if (queryBuilder == null) {
validationExceptions = addValidationError("field name is null or empty", validationExceptions); validationException = addValidationError("inner clause [query] cannot be null.", validationException);
} else {
validationException = validateInnerQuery(queryBuilder, validationException);
} }
return validationExceptions; if (fieldName == null || fieldName.isEmpty()) {
validationException = addValidationError("field name is null or empty", validationException);
}
return validationException;
} }
@Override @Override

View File

@ -37,14 +37,10 @@ public class NotQueryBuilder extends AbstractQueryBuilder<NotQueryBuilder> {
private final QueryBuilder filter; private final QueryBuilder filter;
static final NotQueryBuilder PROTOTYPE = new NotQueryBuilder(); static final NotQueryBuilder PROTOTYPE = new NotQueryBuilder(null);
public NotQueryBuilder(QueryBuilder filter) { public NotQueryBuilder(QueryBuilder filter) {
this.filter = Objects.requireNonNull(filter); this.filter = filter;
}
private NotQueryBuilder() {
this.filter = null;
} }
/** /**

View File

@ -48,7 +48,7 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
public OrQueryBuilder(QueryBuilder... filters) { public OrQueryBuilder(QueryBuilder... filters) {
for (QueryBuilder filter : filters) { for (QueryBuilder filter : filters) {
this.filters.add(Objects.requireNonNull(filter)); this.filters.add(filter);
} }
} }
@ -57,7 +57,7 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
* No <tt>null</tt> value allowed. * No <tt>null</tt> value allowed.
*/ */
public OrQueryBuilder add(QueryBuilder filterBuilder) { public OrQueryBuilder add(QueryBuilder filterBuilder) {
filters.add(Objects.requireNonNull(filterBuilder)); filters.add(filterBuilder);
return this; return this;
} }

View File

@ -40,7 +40,7 @@ public class QueryFilterBuilder extends AbstractQueryBuilder<QueryFilterBuilder>
private final QueryBuilder queryBuilder; private final QueryBuilder queryBuilder;
static final QueryFilterBuilder PROTOTYPE = new QueryFilterBuilder(); static final QueryFilterBuilder PROTOTYPE = new QueryFilterBuilder(null);
/** /**
* A filter that simply wraps a query. * A filter that simply wraps a query.
@ -48,11 +48,7 @@ public class QueryFilterBuilder extends AbstractQueryBuilder<QueryFilterBuilder>
* @param queryBuilder The query to wrap as a filter * @param queryBuilder The query to wrap as a filter
*/ */
public QueryFilterBuilder(QueryBuilder queryBuilder) { public QueryFilterBuilder(QueryBuilder queryBuilder) {
this.queryBuilder = Objects.requireNonNull(queryBuilder); this.queryBuilder = queryBuilder;
}
private QueryFilterBuilder() {
this.queryBuilder = null;
} }
/** /**

View File

@ -54,7 +54,7 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
private boolean collectPayloads = DEFAULT_COLLECT_PAYLOADS; private boolean collectPayloads = DEFAULT_COLLECT_PAYLOADS;
static final SpanNearQueryBuilder PROTOTYPE = new SpanNearQueryBuilder(); static final SpanNearQueryBuilder PROTOTYPE = new SpanNearQueryBuilder(0);
/** /**
* @param slop controls the maximum number of intervening unmatched positions permitted * @param slop controls the maximum number of intervening unmatched positions permitted
@ -63,13 +63,6 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
this.slop = slop; this.slop = slop;
} }
/**
* only used for prototype
*/
private SpanNearQueryBuilder() {
this.slop = 0;
}
/** /**
* @return the maximum number of intervening unmatched positions permitted * @return the maximum number of intervening unmatched positions permitted
*/ */

View File

@ -81,16 +81,6 @@ public class AndQueryBuilderTest extends BaseQueryTestCase<AndQueryBuilder> {
context.indexQueryParserService().queryParser(AndQueryBuilder.PROTOTYPE.getName()).fromXContent(context); context.indexQueryParserService().queryParser(AndQueryBuilder.PROTOTYPE.getName()).fromXContent(context);
} }
@Test(expected=NullPointerException.class)
public void testNullConstructor() {
new AndQueryBuilder(EmptyQueryBuilder.PROTOTYPE, null);
}
@Test(expected=NullPointerException.class)
public void testAddNull() {
new AndQueryBuilder(EmptyQueryBuilder.PROTOTYPE).add(null);
}
@Test @Test
public void testValidate() { public void testValidate() {
AndQueryBuilder andQuery = new AndQueryBuilder(); AndQueryBuilder andQuery = new AndQueryBuilder();
@ -98,7 +88,11 @@ public class AndQueryBuilderTest extends BaseQueryTestCase<AndQueryBuilder> {
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
for (int i = 0; i < iters; i++) { for (int i = 0; i < iters; i++) {
if (randomBoolean()) { if (randomBoolean()) {
andQuery.add(RandomQueryBuilder.createInvalidQuery(random())); if (randomBoolean()) {
andQuery.add(RandomQueryBuilder.createInvalidQuery(random()));
} else {
andQuery.add(null);
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
andQuery.add(RandomQueryBuilder.createQuery(random())); andQuery.add(RandomQueryBuilder.createQuery(random()));

View File

@ -101,7 +101,11 @@ public class BoolQueryBuilderTest extends BaseQueryTestCase<BoolQueryBuilder> {
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
for (int i = 0; i < iters; i++) { for (int i = 0; i < iters; i++) {
if (randomBoolean()) { if (randomBoolean()) {
booleanQuery.must(RandomQueryBuilder.createInvalidQuery(random())); if (randomBoolean()) {
booleanQuery.must(RandomQueryBuilder.createInvalidQuery(random()));
} else {
booleanQuery.must(null);
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
booleanQuery.must(RandomQueryBuilder.createQuery(random())); booleanQuery.must(RandomQueryBuilder.createQuery(random()));
@ -110,23 +114,37 @@ public class BoolQueryBuilderTest extends BaseQueryTestCase<BoolQueryBuilder> {
iters = randomIntBetween(0, 3); iters = randomIntBetween(0, 3);
for (int i = 0; i < iters; i++) { for (int i = 0; i < iters; i++) {
if (randomBoolean()) { if (randomBoolean()) {
booleanQuery.should(RandomQueryBuilder.createInvalidQuery(random())); if (randomBoolean()) {
booleanQuery.should(RandomQueryBuilder.createInvalidQuery(random()));
} else {
booleanQuery.should(null);
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
booleanQuery.should(RandomQueryBuilder.createQuery(random())); booleanQuery.should(RandomQueryBuilder.createQuery(random()));
} }
} }
iters = randomIntBetween(0, 3);
for (int i = 0; i < iters; i++) { for (int i = 0; i < iters; i++) {
if (randomBoolean()) { if (randomBoolean()) {
booleanQuery.mustNot(RandomQueryBuilder.createInvalidQuery(random())); if (randomBoolean()) {
booleanQuery.mustNot(RandomQueryBuilder.createInvalidQuery(random()));
} else {
booleanQuery.mustNot(null);
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
booleanQuery.mustNot(RandomQueryBuilder.createQuery(random())); booleanQuery.mustNot(RandomQueryBuilder.createQuery(random()));
} }
} }
iters = randomIntBetween(0, 3);
for (int i = 0; i < iters; i++) { for (int i = 0; i < iters; i++) {
if (randomBoolean()) { if (randomBoolean()) {
booleanQuery.filter(RandomQueryBuilder.createInvalidQuery(random())); if (randomBoolean()) {
booleanQuery.filter(RandomQueryBuilder.createInvalidQuery(random()));
} else {
booleanQuery.filter(null);
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
booleanQuery.filter(RandomQueryBuilder.createQuery(random())); booleanQuery.filter(RandomQueryBuilder.createQuery(random()));
@ -134,24 +152,4 @@ public class BoolQueryBuilderTest extends BaseQueryTestCase<BoolQueryBuilder> {
} }
assertValidate(booleanQuery, totalExpectedErrors); assertValidate(booleanQuery, totalExpectedErrors);
} }
@Test(expected=NullPointerException.class)
public void testAddNullMust() {
new BoolQueryBuilder().must(null);
}
@Test(expected=NullPointerException.class)
public void testAddNullMustNot() {
new BoolQueryBuilder().mustNot(null);
}
@Test(expected=NullPointerException.class)
public void testAddNullShould() {
new BoolQueryBuilder().should(null);
}
@Test(expected=NullPointerException.class)
public void testAddNullFilter() {
new BoolQueryBuilder().filter(null);
}
} }

View File

@ -47,16 +47,20 @@ public class BoostingQueryBuilderTest extends BaseQueryTestCase<BoostingQueryBui
@Test @Test
public void testValidate() { public void testValidate() {
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
QueryBuilder positive; QueryBuilder positive = null;
QueryBuilder negative; QueryBuilder negative = null;
if (frequently()) { if (frequently()) {
negative = RandomQueryBuilder.createInvalidQuery(random()); if (randomBoolean()) {
negative = RandomQueryBuilder.createInvalidQuery(random());
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
negative = RandomQueryBuilder.createQuery(random()); negative = RandomQueryBuilder.createQuery(random());
} }
if (frequently()) { if (frequently()) {
positive = RandomQueryBuilder.createInvalidQuery(random()); if (randomBoolean()) {
positive = RandomQueryBuilder.createInvalidQuery(random());
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
positive = RandomQueryBuilder.createQuery(random()); positive = RandomQueryBuilder.createQuery(random());
@ -70,13 +74,4 @@ public class BoostingQueryBuilderTest extends BaseQueryTestCase<BoostingQueryBui
} }
assertValidate(boostingQuery, totalExpectedErrors); assertValidate(boostingQuery, totalExpectedErrors);
} }
@Test(expected=NullPointerException.class)
public void testNullConstructorArgument() {
if (randomBoolean()) {
new BoostingQueryBuilder(null, RandomQueryBuilder.createQuery(random()));
} else {
new BoostingQueryBuilder(RandomQueryBuilder.createQuery(random()), null);
}
}
} }

View File

@ -62,10 +62,12 @@ public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase<ConstantSco
@Test @Test
public void testValidate() { public void testValidate() {
QueryBuilder innerQuery; QueryBuilder innerQuery = null;
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
if (randomBoolean()) { if (randomBoolean()) {
innerQuery = RandomQueryBuilder.createInvalidQuery(random()); if (randomBoolean()) {
innerQuery = RandomQueryBuilder.createInvalidQuery(random());
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
innerQuery = RandomQueryBuilder.createQuery(random()); innerQuery = RandomQueryBuilder.createQuery(random());
@ -73,9 +75,4 @@ public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase<ConstantSco
ConstantScoreQueryBuilder constantScoreQuery = new ConstantScoreQueryBuilder(innerQuery); ConstantScoreQueryBuilder constantScoreQuery = new ConstantScoreQueryBuilder(innerQuery);
assertValidate(constantScoreQuery, totalExpectedErrors); assertValidate(constantScoreQuery, totalExpectedErrors);
} }
@Test(expected=NullPointerException.class)
public void testNullConstructor() {
new ConstantScoreQueryBuilder(null);
}
} }

View File

@ -87,11 +87,6 @@ public class DisMaxQueryBuilderTest extends BaseQueryTestCase<DisMaxQueryBuilder
assertNull(disMaxBuilder.toQuery(context)); assertNull(disMaxBuilder.toQuery(context));
} }
@Test(expected=NullPointerException.class)
public void testAddNull() {
new DisMaxQueryBuilder().add(null);
}
@Test @Test
public void testValidate() { public void testValidate() {
DisMaxQueryBuilder disMaxQuery = new DisMaxQueryBuilder(); DisMaxQueryBuilder disMaxQuery = new DisMaxQueryBuilder();
@ -99,7 +94,11 @@ public class DisMaxQueryBuilderTest extends BaseQueryTestCase<DisMaxQueryBuilder
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
for (int i = 0; i < iters; i++) { for (int i = 0; i < iters; i++) {
if (randomBoolean()) { if (randomBoolean()) {
disMaxQuery.add(RandomQueryBuilder.createInvalidQuery(random())); if (randomBoolean()) {
disMaxQuery.add(RandomQueryBuilder.createInvalidQuery(random()));
} else {
disMaxQuery.add(null);
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
disMaxQuery.add(RandomQueryBuilder.createQuery(random())); disMaxQuery.add(RandomQueryBuilder.createQuery(random()));

View File

@ -78,10 +78,12 @@ public class FQueryFilterBuilderTest extends BaseQueryTestCase<FQueryFilterBuild
@Test @Test
public void testValidate() { public void testValidate() {
QueryBuilder innerQuery; QueryBuilder innerQuery = null;
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
if (randomBoolean()) { if (randomBoolean()) {
innerQuery = RandomQueryBuilder.createInvalidQuery(random()); if (randomBoolean()) {
innerQuery = RandomQueryBuilder.createInvalidQuery(random());
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
innerQuery = RandomQueryBuilder.createQuery(random()); innerQuery = RandomQueryBuilder.createQuery(random());
@ -89,9 +91,4 @@ public class FQueryFilterBuilderTest extends BaseQueryTestCase<FQueryFilterBuild
FQueryFilterBuilder fQueryFilter = new FQueryFilterBuilder(innerQuery); FQueryFilterBuilder fQueryFilter = new FQueryFilterBuilder(innerQuery);
assertValidate(fQueryFilter, totalExpectedErrors); assertValidate(fQueryFilter, totalExpectedErrors);
} }
@Test(expected=NullPointerException.class)
public void testNullConstructo() {
new FQueryFilterBuilder(null);
}
} }

View File

@ -55,17 +55,21 @@ public class FieldMaskingSpanQueryBuilderTest extends BaseQueryTestCase<FieldMas
@Test @Test
public void testValidate() { public void testValidate() {
String fieldName; String fieldName = null;
SpanQueryBuilder spanQueryBuilder; SpanQueryBuilder spanQueryBuilder = null;
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
if (randomBoolean()) { if (randomBoolean()) {
fieldName = "fieldName"; fieldName = "fieldName";
} else { } else {
fieldName = ""; if (randomBoolean()) {
fieldName = "";
}
totalExpectedErrors++; totalExpectedErrors++;
} }
if (randomBoolean()) { if (randomBoolean()) {
spanQueryBuilder = new SpanTermQueryBuilder("", "test"); if (randomBoolean()) {
spanQueryBuilder = new SpanTermQueryBuilder("", "test");
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
spanQueryBuilder = new SpanTermQueryBuilder("name", "value"); spanQueryBuilder = new SpanTermQueryBuilder("name", "value");
@ -73,14 +77,4 @@ public class FieldMaskingSpanQueryBuilderTest extends BaseQueryTestCase<FieldMas
FieldMaskingSpanQueryBuilder queryBuilder = new FieldMaskingSpanQueryBuilder(spanQueryBuilder, fieldName); FieldMaskingSpanQueryBuilder queryBuilder = new FieldMaskingSpanQueryBuilder(spanQueryBuilder, fieldName);
assertValidate(queryBuilder, totalExpectedErrors); assertValidate(queryBuilder, totalExpectedErrors);
} }
@Test(expected=NullPointerException.class)
public void testNullFieldName() {
new FieldMaskingSpanQueryBuilder(new SpanTermQueryBuilder("name", "value"), null);
}
@Test(expected=NullPointerException.class)
public void testNullInnerQuery() {
new FieldMaskingSpanQueryBuilder(null, "");
}
} }

View File

@ -45,11 +45,6 @@ public class NotQueryBuilderTest extends BaseQueryTestCase<NotQueryBuilder> {
return new NotQueryBuilder(RandomQueryBuilder.createQuery(random())); return new NotQueryBuilder(RandomQueryBuilder.createQuery(random()));
} }
@Test(expected=NullPointerException.class)
public void testNotQueryBuilderNull() {
new NotQueryBuilder(null);
}
/** /**
* @throws IOException * @throws IOException
*/ */
@ -65,10 +60,12 @@ public class NotQueryBuilderTest extends BaseQueryTestCase<NotQueryBuilder> {
@Test @Test
public void testValidate() { public void testValidate() {
QueryBuilder innerQuery; QueryBuilder innerQuery = null;
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
if (randomBoolean()) { if (randomBoolean()) {
innerQuery = RandomQueryBuilder.createInvalidQuery(random()); if (randomBoolean()) {
innerQuery = RandomQueryBuilder.createInvalidQuery(random());
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
innerQuery = RandomQueryBuilder.createQuery(random()); innerQuery = RandomQueryBuilder.createQuery(random());

View File

@ -89,7 +89,11 @@ public class OrQueryBuilderTest extends BaseQueryTestCase<OrQueryBuilder> {
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
for (int i = 0; i < iters; i++) { for (int i = 0; i < iters; i++) {
if (randomBoolean()) { if (randomBoolean()) {
orQuery.add(RandomQueryBuilder.createInvalidQuery(random())); if (randomBoolean()) {
orQuery.add(RandomQueryBuilder.createInvalidQuery(random()));
} else {
orQuery.add(null);
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
orQuery.add(RandomQueryBuilder.createQuery(random())); orQuery.add(RandomQueryBuilder.createQuery(random()));
@ -97,14 +101,4 @@ public class OrQueryBuilderTest extends BaseQueryTestCase<OrQueryBuilder> {
} }
assertValidate(orQuery, totalExpectedErrors); assertValidate(orQuery, totalExpectedErrors);
} }
@Test(expected=NullPointerException.class)
public void testNullConstructor() {
new OrQueryBuilder(EmptyQueryBuilder.PROTOTYPE, null);
}
@Test(expected=NullPointerException.class)
public void testAddNull() {
new OrQueryBuilder().add(null);
}
} }

View File

@ -43,17 +43,14 @@ public class QueryFilterBuilderTest extends BaseQueryTestCase<QueryFilterBuilder
return new QueryFilterBuilder(innerQuery); return new QueryFilterBuilder(innerQuery);
} }
@Test(expected=NullPointerException.class)
public void testQueryFilterBuilderNull() {
new QueryFilterBuilder(null);
}
@Test @Test
public void testValidate() { public void testValidate() {
QueryBuilder innerQuery; QueryBuilder innerQuery = null;
int totalExpectedErrors = 0; int totalExpectedErrors = 0;
if (randomBoolean()) { if (randomBoolean()) {
innerQuery = RandomQueryBuilder.createInvalidQuery(random()); if (randomBoolean()) {
innerQuery = RandomQueryBuilder.createInvalidQuery(random());
}
totalExpectedErrors++; totalExpectedErrors++;
} else { } else {
innerQuery = RandomQueryBuilder.createQuery(random()); innerQuery = RandomQueryBuilder.createQuery(random());