From 68b943dc53c50cb1c6dbc9c12dff37804057c4c5 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Thu, 25 Aug 2016 20:54:06 +0200 Subject: [PATCH] Fix MoreLikeThisQueryBuilderTests.testUnknownObjectException() Objects hierarchy must be tracked when entering/leaving an object so that it better knows if the "newField" has been inserted into an arbitrary holding object. Can be reproduced with gradle :core:test -Dtests.seed=760F8BD0F7E46D45 -Dtests.class=org.elasticsearch.index.query.MoreLikeThisQueryBuilderTests -Dtests.method="testUnknownObjectException" -Dtests.security.manager=true -Dtests.locale=ko -Dtests.timezone=Etc/Zulu --- .../test/AbstractQueryTestCase.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java index b9854c3a79f..cdf81428c37 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java @@ -403,25 +403,22 @@ public abstract class AbstractQueryTestCase> int mutation = 0; while (true) { - // Track the objects hierarchy - Deque hierarchy = new LinkedList<>(); + boolean expectException = true; BytesStreamOutput out = new BytesStreamOutput(); try ( XContentGenerator generator = XContentType.JSON.xContent().createGenerator(out); XContentParser parser = XContentHelper.createParser(new BytesArray(query)); ) { - // Parse the valid query and inserts a new object level called "newField" int objectIndex = -1; + Deque levels = new LinkedList<>(); + // Parse the valid query and inserts a new object level called "newField" XContentParser.Token token; while ((token = parser.nextToken()) != null) { if (token == XContentParser.Token.START_OBJECT) { objectIndex++; - - if (objectIndex <= mutation) { - hierarchy.push(parser.currentName()); - } + levels.addLast(parser.currentName()); if (objectIndex == mutation) { // We reached the place in the object tree where we want to insert a new object level @@ -430,9 +427,23 @@ public abstract class AbstractQueryTestCase> XContentHelper.copyCurrentStructure(generator, parser); generator.writeEndObject(); + if (hasArbitraryContent) { + // The query has one or more fields that hold arbitrary content. If the current + // field is one (or a child) of those, no exception is expected when parsing the mutated query. + String h = Arrays.toString(levels.toArray()); + for (String marker : arbitraryMarkers) { + if (levels.contains(marker)) { + expectException = false; + break; + } + } + } + // Jump to next token continue; } + } else if (token == XContentParser.Token.END_OBJECT) { + levels.removeLast(); } // We are walking through the object tree, so we can safely copy the current node @@ -451,18 +462,6 @@ public abstract class AbstractQueryTestCase> } } - boolean expectException = true; - if (hasArbitraryContent) { - // The query has one or more fields that hold arbitrary content. If the current - // field is one (or a child) of those, no exception is expected when parsing the mutated query. - for (String marker : arbitraryMarkers) { - if (hierarchy.contains(marker)) { - expectException = false; - break; - } - } - } - results.add(new Tuple<>(out.bytes().utf8ToString(), expectException)); } }