Skip to content

Commit b3ffd4a

Browse files
committed
Tests: Remove use of joda time in some tests (elastic#31922)
This also extends the dateformatters test to ensure that the printers are acting the same in java time and joda time.
1 parent d81577f commit b3ffd4a

File tree

16 files changed

+274
-124
lines changed

16 files changed

+274
-124
lines changed

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
import org.elasticsearch.ingest.TestTemplateService;
2525
import org.elasticsearch.script.TemplateScript;
2626
import org.elasticsearch.test.ESTestCase;
27-
import org.joda.time.DateTime;
28-
import org.joda.time.DateTimeZone;
2927

28+
import java.time.ZoneId;
29+
import java.time.ZoneOffset;
30+
import java.time.ZonedDateTime;
3031
import java.util.ArrayList;
3132
import java.util.Collections;
3233
import java.util.HashMap;
@@ -36,19 +37,21 @@
3637

3738
import static org.hamcrest.CoreMatchers.containsString;
3839
import static org.hamcrest.CoreMatchers.equalTo;
39-
import static org.joda.time.DateTimeZone.UTC;
4040

4141
public class DateProcessorTests extends ESTestCase {
42+
4243
private TemplateScript.Factory templatize(Locale locale) {
4344
return new TestTemplateService.MockTemplateScript.Factory(locale.getLanguage());
4445
}
4546

46-
private TemplateScript.Factory templatize(DateTimeZone timezone) {
47-
return new TestTemplateService.MockTemplateScript.Factory(timezone.getID());
47+
private TemplateScript.Factory templatize(ZoneId timezone) {
48+
// prevent writing "UTC" as string, as joda time does not parse it
49+
String id = timezone.equals(ZoneOffset.UTC) ? "UTC" : timezone.getId();
50+
return new TestTemplateService.MockTemplateScript.Factory(id);
4851
}
4952
public void testJodaPattern() {
5053
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10),
51-
templatize(DateTimeZone.forID("Europe/Amsterdam")), templatize(Locale.ENGLISH),
54+
templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH),
5255
"date_as_string", Collections.singletonList("yyyy dd MM hh:mm:ss"), "date_as_date");
5356
Map<String, Object> document = new HashMap<>();
5457
document.put("date_as_string", "2010 12 06 11:05:15");
@@ -63,7 +66,7 @@ public void testJodaPatternMultipleFormats() {
6366
matchFormats.add("dd/MM/yyyy");
6467
matchFormats.add("dd-MM-yyyy");
6568
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10),
66-
templatize(DateTimeZone.forID("Europe/Amsterdam")), templatize(Locale.ENGLISH),
69+
templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH),
6770
"date_as_string", matchFormats, "date_as_date");
6871

6972
Map<String, Object> document = new HashMap<>();
@@ -98,7 +101,7 @@ public void testJodaPatternMultipleFormats() {
98101
public void testInvalidJodaPattern() {
99102
try {
100103
DateProcessor processor = new DateProcessor(randomAlphaOfLength(10),
101-
templatize(UTC), templatize(randomLocale(random())),
104+
templatize(ZoneOffset.UTC), templatize(randomLocale(random())),
102105
"date_as_string", Collections.singletonList("invalid pattern"), "date_as_date");
103106
Map<String, Object> document = new HashMap<>();
104107
document.put("date_as_string", "2010");
@@ -112,7 +115,7 @@ public void testInvalidJodaPattern() {
112115

113116
public void testJodaPatternLocale() {
114117
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10),
115-
templatize(DateTimeZone.forID("Europe/Amsterdam")), templatize(Locale.ITALIAN),
118+
templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ITALIAN),
116119
"date_as_string", Collections.singletonList("yyyy dd MMM"), "date_as_date");
117120
Map<String, Object> document = new HashMap<>();
118121
document.put("date_as_string", "2010 12 giugno");
@@ -123,18 +126,18 @@ public void testJodaPatternLocale() {
123126

124127
public void testJodaPatternDefaultYear() {
125128
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10),
126-
templatize(DateTimeZone.forID("Europe/Amsterdam")), templatize(Locale.ENGLISH),
129+
templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH),
127130
"date_as_string", Collections.singletonList("dd/MM"), "date_as_date");
128131
Map<String, Object> document = new HashMap<>();
129132
document.put("date_as_string", "12/06");
130133
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
131134
dateProcessor.execute(ingestDocument);
132135
assertThat(ingestDocument.getFieldValue("date_as_date", String.class),
133-
equalTo(DateTime.now().getYear() + "-06-12T00:00:00.000+02:00"));
136+
equalTo(ZonedDateTime.now().getYear() + "-06-12T00:00:00.000+02:00"));
134137
}
135138

136139
public void testTAI64N() {
137-
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), templatize(DateTimeZone.forOffsetHours(2)),
140+
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), templatize(ZoneOffset.ofHours(2)),
138141
templatize(randomLocale(random())),
139142
"date_as_string", Collections.singletonList("TAI64N"), "date_as_date");
140143
Map<String, Object> document = new HashMap<>();
@@ -146,8 +149,8 @@ public void testTAI64N() {
146149
}
147150

148151
public void testUnixMs() {
149-
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), templatize(UTC), templatize(randomLocale(random())),
150-
"date_as_string", Collections.singletonList("UNIX_MS"), "date_as_date");
152+
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), templatize(ZoneOffset.UTC),
153+
templatize(randomLocale(random())), "date_as_string", Collections.singletonList("UNIX_MS"), "date_as_date");
151154
Map<String, Object> document = new HashMap<>();
152155
document.put("date_as_string", "1000500");
153156
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
@@ -162,7 +165,7 @@ public void testUnixMs() {
162165
}
163166

164167
public void testUnix() {
165-
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), templatize(UTC),
168+
DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), templatize(ZoneOffset.UTC),
166169
templatize(randomLocale(random())),
167170
"date_as_string", Collections.singletonList("UNIX"), "date_as_date");
168171
Map<String, Object> document = new HashMap<>();
@@ -186,7 +189,7 @@ public void testInvalidTimezone() {
186189

187190
public void testInvalidLocale() {
188191
DateProcessor processor = new DateProcessor(randomAlphaOfLength(10),
189-
templatize(UTC), new TestTemplateService.MockTemplateScript.Factory("invalid_locale"),
192+
templatize(ZoneOffset.UTC), new TestTemplateService.MockTemplateScript.Factory("invalid_locale"),
190193
"date_as_string", Collections.singletonList("yyyy"), "date_as_date");
191194
Map<String, Object> document = new HashMap<>();
192195
document.put("date_as_string", "2010");

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.carrotsearch.hppc.cursors.IntObjectCursor;
2424
import com.carrotsearch.hppc.cursors.ObjectCursor;
2525
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
26-
2726
import org.elasticsearch.Version;
2827
import org.elasticsearch.action.admin.indices.rollover.RolloverInfo;
2928
import org.elasticsearch.action.support.ActiveShardCount;
@@ -56,10 +55,11 @@
5655
import org.elasticsearch.index.mapper.MapperService;
5756
import org.elasticsearch.index.shard.ShardId;
5857
import org.elasticsearch.rest.RestStatus;
59-
import org.joda.time.DateTime;
60-
import org.joda.time.DateTimeZone;
6158

6259
import java.io.IOException;
60+
import java.time.Instant;
61+
import java.time.ZoneOffset;
62+
import java.time.ZonedDateTime;
6363
import java.util.Arrays;
6464
import java.util.Collections;
6565
import java.util.EnumSet;
@@ -1346,7 +1346,7 @@ public static Settings addHumanReadableSettings(Settings settings) {
13461346
}
13471347
Long creationDate = settings.getAsLong(SETTING_CREATION_DATE, null);
13481348
if (creationDate != null) {
1349-
DateTime creationDateTime = new DateTime(creationDate, DateTimeZone.UTC);
1349+
ZonedDateTime creationDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(creationDate), ZoneOffset.UTC);
13501350
builder.put(SETTING_CREATION_DATE_STRING, creationDateTime.toString());
13511351
}
13521352
return builder.build();

server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@
7575
import org.elasticsearch.indices.InvalidIndexNameException;
7676
import org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason;
7777
import org.elasticsearch.threadpool.ThreadPool;
78-
import org.joda.time.DateTime;
79-
import org.joda.time.DateTimeZone;
8078

8179
import java.io.UnsupportedEncodingException;
8280
import java.nio.file.Path;
81+
import java.time.Instant;
8382
import java.util.ArrayList;
8483
import java.util.Collections;
8584
import java.util.HashMap;
@@ -390,7 +389,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
390389
}
391390

392391
if (indexSettingsBuilder.get(SETTING_CREATION_DATE) == null) {
393-
indexSettingsBuilder.put(SETTING_CREATION_DATE, new DateTime(DateTimeZone.UTC).getMillis());
392+
indexSettingsBuilder.put(SETTING_CREATION_DATE, Instant.now().toEpochMilli());
394393
}
395394
indexSettingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getProvidedName());
396395
indexSettingsBuilder.put(SETTING_INDEX_UUID, UUIDs.randomBase64UUID());

server/src/main/java/org/elasticsearch/common/time/DateFormatters.java

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@
5252

5353
public class DateFormatters {
5454

55-
private static final DateTimeFormatter TIME_ZONE_FORMATTER = new DateTimeFormatterBuilder()
56-
.optionalStart().appendZoneId().optionalEnd()
57-
.optionalStart().appendOffset("+HHmm", "Z").optionalEnd()
58-
.optionalStart().appendOffset("+HH:mm", "Z").optionalEnd()
59-
.toFormatter(Locale.ROOT);
60-
6155
private static final DateTimeFormatter TIME_ZONE_FORMATTER_ZONE_ID = new DateTimeFormatterBuilder()
6256
.appendZoneId()
6357
.toFormatter(Locale.ROOT);
@@ -70,12 +64,80 @@ public class DateFormatters {
7064
.appendOffset("+HH:mm", "Z")
7165
.toFormatter(Locale.ROOT);
7266

67+
private static final DateTimeFormatter TIME_ZONE_FORMATTER = new DateTimeFormatterBuilder()
68+
.optionalStart().appendZoneId().optionalEnd()
69+
.optionalStart().appendOffset("+HHmm", "Z").optionalEnd()
70+
.optionalStart().appendOffset("+HH:mm", "Z").optionalEnd()
71+
.toFormatter(Locale.ROOT);
72+
7373
private static final DateTimeFormatter OPTIONAL_TIME_ZONE_FORMATTER = new DateTimeFormatterBuilder()
7474
.optionalStart()
7575
.append(TIME_ZONE_FORMATTER)
7676
.optionalEnd()
7777
.toFormatter(Locale.ROOT);
7878

79+
private static final DateTimeFormatter STRICT_YEAR_MONTH_DAY_FORMATTER = new DateTimeFormatterBuilder()
80+
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
81+
.appendLiteral("-")
82+
.appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE)
83+
.appendLiteral('-')
84+
.appendValue(DAY_OF_MONTH, 2, 2, SignStyle.NOT_NEGATIVE)
85+
.toFormatter(Locale.ROOT);
86+
87+
private static final DateTimeFormatter STRICT_HOUR_MINUTE_SECOND_FORMATTER = new DateTimeFormatterBuilder()
88+
.appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE)
89+
.appendLiteral(':')
90+
.appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE)
91+
.appendLiteral(':')
92+
.appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE)
93+
.toFormatter(Locale.ROOT);
94+
95+
private static final DateTimeFormatter STRICT_DATE_OPTIONAL_TIME_FORMATTER_1 = new DateTimeFormatterBuilder()
96+
.append(STRICT_YEAR_MONTH_DAY_FORMATTER)
97+
.optionalStart()
98+
.appendLiteral('T')
99+
.append(STRICT_HOUR_MINUTE_SECOND_FORMATTER)
100+
.optionalStart()
101+
.appendFraction(MILLI_OF_SECOND, 3, 3, true)
102+
.optionalEnd()
103+
.optionalStart()
104+
.append(TIME_ZONE_FORMATTER_WITHOUT_COLON)
105+
.optionalEnd()
106+
.optionalEnd()
107+
.toFormatter(Locale.ROOT);
108+
109+
private static final DateTimeFormatter STRICT_DATE_OPTIONAL_TIME_FORMATTER_2 = new DateTimeFormatterBuilder()
110+
.append(STRICT_YEAR_MONTH_DAY_FORMATTER)
111+
.optionalStart()
112+
.appendLiteral('T')
113+
.append(STRICT_HOUR_MINUTE_SECOND_FORMATTER)
114+
.optionalStart()
115+
.appendFraction(MILLI_OF_SECOND, 3, 3, true)
116+
.optionalEnd()
117+
.optionalStart()
118+
.append(TIME_ZONE_FORMATTER_WITH_COLON)
119+
.optionalEnd()
120+
.optionalEnd()
121+
.toFormatter(Locale.ROOT);
122+
123+
private static final DateTimeFormatter STRICT_DATE_OPTIONAL_TIME_FORMATTER_3 = new DateTimeFormatterBuilder()
124+
.append(STRICT_YEAR_MONTH_DAY_FORMATTER)
125+
.optionalStart()
126+
.appendLiteral('T')
127+
.append(STRICT_HOUR_MINUTE_SECOND_FORMATTER)
128+
.optionalStart()
129+
.appendFraction(MILLI_OF_SECOND, 3, 3, true)
130+
.optionalEnd()
131+
.optionalStart()
132+
.append(TIME_ZONE_FORMATTER_ZONE_ID)
133+
.optionalEnd()
134+
.optionalEnd()
135+
.toFormatter(Locale.ROOT);
136+
137+
private static final CompoundDateTimeFormatter STRICT_DATE_OPTIONAL_TIME =
138+
new CompoundDateTimeFormatter(STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, STRICT_DATE_OPTIONAL_TIME_FORMATTER_2,
139+
STRICT_DATE_OPTIONAL_TIME_FORMATTER_3);
140+
79141
private static final DateTimeFormatter BASIC_TIME_NO_MILLIS_FORMATTER = new DateTimeFormatterBuilder()
80142
.appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE)
81143
.appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE)
@@ -258,7 +320,8 @@ public class DateFormatters {
258320
.append(OPTIONAL_TIME_ZONE_FORMATTER)
259321
.toFormatter(Locale.ROOT));
260322

261-
private static final CompoundDateTimeFormatter DATE_OPTIONAL_TIME = new CompoundDateTimeFormatter(new DateTimeFormatterBuilder()
323+
private static final CompoundDateTimeFormatter DATE_OPTIONAL_TIME = new CompoundDateTimeFormatter(STRICT_DATE_OPTIONAL_TIME.printer,
324+
new DateTimeFormatterBuilder()
262325
.append(DATE_FORMATTER)
263326
.parseLenient()
264327
.optionalStart()
@@ -560,14 +623,6 @@ public class DateFormatters {
560623
private static final CompoundDateTimeFormatter STRICT_DATE_HOUR_MINUTE = new CompoundDateTimeFormatter(
561624
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", Locale.ROOT));
562625

563-
private static final DateTimeFormatter STRICT_YEAR_MONTH_DAY_FORMATTER = new DateTimeFormatterBuilder()
564-
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
565-
.appendLiteral("-")
566-
.appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE)
567-
.appendLiteral('-')
568-
.appendValue(DAY_OF_MONTH, 2, 2, SignStyle.NOT_NEGATIVE)
569-
.toFormatter(Locale.ROOT);
570-
571626
private static final CompoundDateTimeFormatter STRICT_YEAR_MONTH_DAY = new CompoundDateTimeFormatter(STRICT_YEAR_MONTH_DAY_FORMATTER);
572627

573628
private static final CompoundDateTimeFormatter STRICT_YEAR_MONTH = new CompoundDateTimeFormatter(new DateTimeFormatterBuilder()
@@ -580,14 +635,6 @@ public class DateFormatters {
580635
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
581636
.toFormatter(Locale.ROOT));
582637

583-
private static final DateTimeFormatter STRICT_HOUR_MINUTE_SECOND_FORMATTER = new DateTimeFormatterBuilder()
584-
.appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE)
585-
.appendLiteral(':')
586-
.appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE)
587-
.appendLiteral(':')
588-
.appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE)
589-
.toFormatter(Locale.ROOT);
590-
591638
private static final CompoundDateTimeFormatter STRICT_HOUR_MINUTE_SECOND =
592639
new CompoundDateTimeFormatter(STRICT_HOUR_MINUTE_SECOND_FORMATTER);
593640

@@ -601,18 +648,6 @@ public class DateFormatters {
601648
.append(OPTIONAL_TIME_ZONE_FORMATTER)
602649
.toFormatter(Locale.ROOT));
603650

604-
private static final CompoundDateTimeFormatter STRICT_DATE_OPTIONAL_TIME = new CompoundDateTimeFormatter(new DateTimeFormatterBuilder()
605-
.append(STRICT_YEAR_MONTH_DAY_FORMATTER)
606-
.optionalStart()
607-
.appendLiteral('T')
608-
.append(STRICT_HOUR_MINUTE_SECOND_FORMATTER)
609-
.optionalStart()
610-
.appendFraction(MILLI_OF_SECOND, 3, 3, true)
611-
.optionalEnd()
612-
.append(OPTIONAL_TIME_ZONE_FORMATTER)
613-
.optionalEnd()
614-
.toFormatter(Locale.ROOT));
615-
616651
private static final CompoundDateTimeFormatter STRICT_ORDINAL_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter(
617652
new DateTimeFormatterBuilder()
618653
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
@@ -918,8 +953,8 @@ public static CompoundDateTimeFormatter forPattern(String input, Locale locale)
918953
return forPattern(formats[0], locale);
919954
} else {
920955
Collection<DateTimeFormatter> parsers = new LinkedHashSet<>(formats.length);
921-
for (int i = 0; i < formats.length; i++) {
922-
CompoundDateTimeFormatter dateTimeFormatter = forPattern(formats[i], locale);
956+
for (String format : formats) {
957+
CompoundDateTimeFormatter dateTimeFormatter = forPattern(format, locale);
923958
try {
924959
parsers.addAll(Arrays.asList(dateTimeFormatter.parsers));
925960
} catch (IllegalArgumentException e) {

server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545
import org.elasticsearch.rest.RestResponse;
4646
import org.elasticsearch.rest.action.RestActionListener;
4747
import org.elasticsearch.rest.action.RestResponseListener;
48-
import org.joda.time.DateTime;
49-
import org.joda.time.DateTimeZone;
5048

49+
import java.time.Instant;
50+
import java.time.ZoneOffset;
51+
import java.time.ZonedDateTime;
5152
import java.util.Arrays;
5253
import java.util.Collections;
5354
import java.util.HashSet;
@@ -379,7 +380,7 @@ Table buildTable(RestRequest request, Index[] indices, ClusterHealthResponse res
379380
table.addCell(primaryStats.getDocs() == null ? null : primaryStats.getDocs().getDeleted());
380381

381382
table.addCell(indexMetaData.getCreationDate());
382-
table.addCell(new DateTime(indexMetaData.getCreationDate(), DateTimeZone.UTC));
383+
table.addCell(ZonedDateTime.ofInstant(Instant.ofEpochMilli(indexMetaData.getCreationDate()), ZoneOffset.UTC));
383384

384385
table.addCell(totalStats.getStore() == null ? null : totalStats.getStore().size());
385386
table.addCell(primaryStats.getStore() == null ? null : primaryStats.getStore().size());

0 commit comments

Comments
 (0)