|
25 | 25 | import org.elasticsearch.cluster.metadata.IndexMetaData; |
26 | 26 | import org.elasticsearch.common.compress.CompressedXContent; |
27 | 27 | import org.elasticsearch.common.settings.Settings; |
28 | | -import org.elasticsearch.common.xcontent.ToXContent; |
29 | | -import org.elasticsearch.common.xcontent.XContentBuilder; |
30 | 28 | import org.elasticsearch.common.xcontent.XContentFactory; |
31 | 29 | import org.elasticsearch.index.IndexService; |
32 | | -import org.elasticsearch.index.mapper.DocumentMapper; |
33 | | -import org.elasticsearch.index.mapper.DocumentMapperParser; |
34 | | -import org.elasticsearch.index.mapper.FieldMapper; |
35 | | -import org.elasticsearch.index.mapper.MapperParsingException; |
36 | | -import org.elasticsearch.index.mapper.ParsedDocument; |
37 | 30 | import org.elasticsearch.plugins.Plugin; |
38 | 31 | import org.elasticsearch.test.ESSingleNodeTestCase; |
39 | 32 | import org.elasticsearch.test.InternalSettingsPlugin; |
40 | 33 | import org.elasticsearch.test.VersionUtils; |
| 34 | +import org.joda.time.DateTime; |
| 35 | +import org.joda.time.DateTimeZone; |
| 36 | +import org.joda.time.format.DateTimeFormat; |
41 | 37 | import org.junit.Before; |
42 | 38 |
|
43 | 39 | import java.io.IOException; |
@@ -354,4 +350,39 @@ public void testEmptyName() throws IOException { |
354 | 350 | DocumentMapper defaultMapper = parser.parse("type", new CompressedXContent(mapping)); |
355 | 351 | assertEquals(mapping, defaultMapper.mappingSource().toString()); |
356 | 352 | } |
| 353 | + |
| 354 | + /** |
| 355 | + * Test that time zones are correctly parsed by the {@link DateFieldMapper}. |
| 356 | + * There is a known bug with Joda 2.9.4 reported in https://github.com/JodaOrg/joda-time/issues/373. |
| 357 | + */ |
| 358 | + public void testTimeZoneParsing() throws Exception { |
| 359 | + final String timeZonePattern = "yyyy-MM-dd" + randomFrom("ZZZ", "[ZZZ]", "'['ZZZ']'"); |
| 360 | + |
| 361 | + String mapping = XContentFactory.jsonBuilder().startObject() |
| 362 | + .startObject("type") |
| 363 | + .startObject("properties") |
| 364 | + .startObject("field") |
| 365 | + .field("type", "date") |
| 366 | + .field("format", timeZonePattern) |
| 367 | + .endObject() |
| 368 | + .endObject() |
| 369 | + .endObject().endObject().string(); |
| 370 | + |
| 371 | + DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); |
| 372 | + assertEquals(mapping, mapper.mappingSource().toString()); |
| 373 | + |
| 374 | + final DateTimeZone randomTimeZone = randomBoolean() ? DateTimeZone.forID(randomFrom("UTC", "CET")) : randomDateTimeZone(); |
| 375 | + final DateTime randomDate = new DateTime(2016, 03, 11, 0, 0, 0, randomTimeZone); |
| 376 | + |
| 377 | + ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder() |
| 378 | + .startObject() |
| 379 | + .field("field", DateTimeFormat.forPattern(timeZonePattern).print(randomDate)) |
| 380 | + .endObject() |
| 381 | + .bytes()); |
| 382 | + |
| 383 | + IndexableField[] fields = doc.rootDoc().getFields("field"); |
| 384 | + assertEquals(2, fields.length); |
| 385 | + |
| 386 | + assertEquals(randomDate.withZone(DateTimeZone.UTC).getMillis(), fields[0].numericValue().longValue()); |
| 387 | + } |
357 | 388 | } |
0 commit comments