[TEST] Beef up test to sometimes pass a plain number as a string

Original commit: elastic/x-pack-elasticsearch@f42662c719
This commit is contained in:
Simon Willnauer 2015-06-22 12:06:42 +02:00
parent 5ea2d0528c
commit 797945b586
1 changed files with 9 additions and 3 deletions

View File

@ -8,6 +8,7 @@ package org.elasticsearch.watcher.support;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test; import org.junit.Test;
@ -18,7 +19,6 @@ import static java.util.concurrent.TimeUnit.*;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentParser; import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentParser;
import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
@ -30,8 +30,14 @@ public class WatcherDateTimeUtilsTests extends ElasticsearchTestCase {
@Test(expected = WatcherDateTimeUtils.ParseException.class) @Test(expected = WatcherDateTimeUtils.ParseException.class)
public void testParseTimeValue_Numeric() throws Exception { public void testParseTimeValue_Numeric() throws Exception {
TimeValue value = new TimeValue(randomInt(100), randomFrom(TimeUnit.values())); TimeValue value = new TimeValue(randomInt(100), randomFrom(TimeUnit.values()));
long millis = value.getMillis();
XContentParser parser = xContentParser(jsonBuilder().startObject().field("value", value.getMillis()).endObject()); XContentBuilder xContentBuilder = jsonBuilder().startObject();
if (randomBoolean() || millis == 0) { // 0 is special - no unit required
xContentBuilder.field("value", millis);
} else {
xContentBuilder.field("value", Long.toString(millis));
}
XContentParser parser = xContentParser(xContentBuilder.endObject());
parser.nextToken(); // start object parser.nextToken(); // start object
parser.nextToken(); // field name parser.nextToken(); // field name
parser.nextToken(); // value parser.nextToken(); // value