Skip to content

Commit c94bbe7

Browse files
committed
fixing test for warnings in headers
1 parent 8ca0fee commit c94bbe7

25 files changed

+314
-87
lines changed

server/src/main/java/org/elasticsearch/common/joda/JodaDeprecationPatterns.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class JodaDeprecationPatterns {
3737
"'z' time zone text. Will print 'Z' for Zulu given UTC timezone.");
3838
}
3939

40-
4140
public static final String USE_PREFIX_8_WARNING = "Prefix your date format with '8' to use the new specifier.";
4241

4342
public static boolean isDeprecatedFormat(String format) {

server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.test.ESTestCase;
2626
import org.joda.time.DateTime;
2727
import org.joda.time.DateTimeZone;
28+
import org.joda.time.format.DateTimeFormat;
2829

2930
import java.time.ZoneOffset;
3031
import java.time.ZonedDateTime;
@@ -33,9 +34,85 @@
3334
import java.util.Locale;
3435

3536
import static org.hamcrest.Matchers.containsString;
37+
import static org.hamcrest.Matchers.equalTo;
3638
import static org.hamcrest.Matchers.is;
3739

3840
public class JavaJodaTimeDuellingTests extends ESTestCase {
41+
@Override
42+
protected boolean enableWarningsCheck() {
43+
// disable warning checks as deprecated patterns are used to compare Joda vs Java results (y - year and Z zone offset)
44+
return false;
45+
}
46+
47+
public void testIncompatiblePatterns() {
48+
// in joda 'y' means year, this is changed to 'u' in java.time. difference is in before era yeaers
49+
assertSameMillis("-0001-01-01", "yyyy-MM-dd", "8uuuu-MM-dd");
50+
assertSameMillis("-1", "y", "8u");
51+
52+
// year-of-era in joda becomes 'y' in java.time
53+
assertSameMillis("2019-01-01", "YYYY-MM-dd", "8yyyy-MM-dd");
54+
55+
56+
//in joda 'Z' was able to parse 'Z' zulu but in java it fails. You have to use 'X' to do that.
57+
//Caused by: java.time.format.DateTimeParseException: Text '2019-01-01T01:01:01.001Z' could not be parsed at index 23
58+
// assertSameMillis("2019-01-01T01:01:01.001Z","YYYY-MM-dd'T'HH:mm:ss.SSSZ","8yyyy-MM-dd'T'HH:mm:ss.SSSZ");
59+
assertSameMillis("2019-01-01T01:01:01.001Z", "YYYY-MM-dd'T'HH:mm:ss.SSSZ", "8yyyy-MM-dd'T'HH:mm:ss.SSSX");
60+
assertSameMillis("2019-01-01T01:01:01.001+0000", "YYYY-MM-dd'T'HH:mm:ss.SSSZ", "8yyyy-MM-dd'T'HH:mm:ss.SSSZ");
61+
62+
63+
// 'z' zoneId in joda prints UTC whereas joda prints 'Z' for zulu
64+
TemporalAccessor parse = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz",Locale.getDefault())
65+
.parse("2019-01-01T01:01:01.001+00:00");
66+
String javaZoneId = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz",Locale.getDefault())
67+
.format(parse);
68+
69+
DateTime dateTime = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ").withOffsetParsed()
70+
.parseDateTime("2019-01-01T01:01:01.001+0000");
71+
String jodaZoneId = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSz").print(dateTime);
72+
assertThat(javaZoneId, equalTo("2019-01-01T01:01:01.001Z"));
73+
assertThat(jodaZoneId, equalTo("2019-01-01T01:01:01.001UTC"));
74+
75+
// for some zones it will fail, but mostly passes. for instance
76+
//Expected :2018-12-31T18:01:01.001∅∅∅
77+
//Actual :2018-12-31T18:01:01.001GMT-07:00
78+
// ZoneId zoneId = randomZone();
79+
// if (zoneId != ZoneOffset.UTC) {
80+
// parse = ZonedDateTime.from(parse)
81+
// .withZoneSameInstant(zoneId);
82+
// javaZoneId = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz")
83+
// .format(parse);
84+
//
85+
// dateTime = dateTime.withZone(DateTimeZone.forID(zoneId.getId()));
86+
// jodaZoneId = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSz").print(dateTime);
87+
// assertThat(javaZoneId, equalTo(jodaZoneId));
88+
// }
89+
90+
//week year 'x' in joda becomes 'Y' in java.time. Parsing fails at the moment
91+
// assertSameMillis("2019-01", "xxxx-ww", "8YYYY-ww");
92+
//java.lang.IllegalArgumentException: temporal accessor [
93+
// {WeekOfWeekBasedYear[WeekFields[SUNDAY,1]]=1, WeekBasedYear[WeekFields[SUNDAY,1]]=2019},ISO]
94+
// cannot be converted to zoned date time
95+
ZonedDateTime now = ZonedDateTime.now();
96+
DateFormatter jodaFormatter = Joda.forPattern("xxxx-ww");
97+
DateFormatter javaFormatter = DateFormatter.forPattern("8YYYY-ww");
98+
assertThat(jodaFormatter.format(now), equalTo(javaFormatter.format(now)));
99+
//TODO in progress..
100+
}
101+
102+
private void assertSameMillis(String input, String jodaFormat, String javaFormat) {
103+
DateFormatter jodaFormatter = Joda.forPattern(jodaFormat);
104+
DateFormatter javaFormatter = DateFormatter.forPattern(javaFormat);
105+
106+
DateTime jodaDateTime = jodaFormatter.parseJoda(input);
107+
108+
TemporalAccessor javaTimeAccessor = javaFormatter.parse(input);
109+
ZonedDateTime zonedDateTime = DateFormatters.from(javaTimeAccessor);
110+
111+
String msg = String.format(Locale.ROOT, "Input [%s] JodaFormat [%s] JavaFormat [%s] Joda [%s], Java [%s]",
112+
input, jodaFormat, javaFormat, jodaDateTime, DateTimeFormatter.ISO_INSTANT.format(zonedDateTime.toInstant()));
113+
114+
assertThat(msg, jodaDateTime.getMillis(), is(zonedDateTime.toInstant().toEpochMilli()));
115+
}
39116

40117
public void testTimeZoneFormatting() {
41118
assertSameDate("2001-01-01T00:00:00Z", "date_time_no_millis");
@@ -360,14 +437,14 @@ public void testDuellingFormatsValidParsing() {
360437
assertSameDate("2012-W1-1", "weekyear_week_day");
361438
}
362439

363-
public void testCompositeParsing(){
440+
public void testCompositeParsing() {
364441
//in all these examples the second pattern will be used
365442
assertSameDate("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS");
366443
assertSameDate("2014-06-06T12:01:02.123", "strictDateTimeNoMillis||yyyy-MM-dd'T'HH:mm:ss.SSS");
367444
assertSameDate("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss+HH:MM||yyyy-MM-dd'T'HH:mm:ss.SSS");
368445
}
369446

370-
public void testExceptionWhenCompositeParsingFails(){
447+
public void testExceptionWhenCompositeParsingFails() {
371448
assertParseException("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SS");
372449
}
373450

@@ -691,7 +768,7 @@ private void assertSamePrinterOutput(String format, ZonedDateTime javaDate, Date
691768
jodaTimeOut += ".0";
692769
}
693770
String message = String.format(Locale.ROOT, "expected string representation to be equal for format [%s]: joda [%s], java [%s]",
694-
format, jodaTimeOut, javaTimeOut);
771+
format, jodaTimeOut, javaTimeOut);
695772
assertThat(message, javaTimeOut, is(jodaTimeOut));
696773
}
697774

@@ -725,7 +802,7 @@ private void assertJodaParseException(String input, String format, String expect
725802
}
726803

727804
private void assertJavaTimeParseException(String input, String format) {
728-
DateFormatter javaTimeFormatter = DateFormatter.forPattern("8"+format);
805+
DateFormatter javaTimeFormatter = DateFormatter.forPattern("8" + format);
729806
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> javaTimeFormatter.parse(input));
730807
assertThat(e.getMessage(), containsString(input));
731808
assertThat(e.getMessage(), containsString(format));

server/src/test/java/org/elasticsearch/common/joda/JodaDateMathParserTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
import static org.hamcrest.Matchers.equalTo;
3535

3636
public class JodaDateMathParserTests extends ESTestCase {
37+
@Override
38+
protected boolean enableWarningsCheck() {
39+
// disable warning checks as deprecated patterns are used to compare Joda vs Java results (y - year and Z zone offset)
40+
return false;
41+
}
3742

3843
DateFormatter formatter = DateFormatter.forPattern("dateOptionalTime||epoch_millis");
3944
DateMathParser parser = formatter.toDateMathParser();
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.joda;
21+
22+
import org.elasticsearch.common.time.DateFormatter;
23+
import org.elasticsearch.test.ESTestCase;
24+
import org.joda.time.DateTime;
25+
26+
import java.util.Locale;
27+
28+
import static org.hamcrest.Matchers.is;
29+
30+
public class JodaWarningTests extends ESTestCase {
31+
32+
public void testDeprecatedFormatSpecifiers() {
33+
Joda.forPattern("CC");
34+
assertWarnings("'C' century of era is no longer supported. Prefix your date format with '8' to use the new specifier.");
35+
Joda.forPattern("YYYY");
36+
assertWarnings("'Y' year-of-era becomes 'y'. Use 'Y' for week-based-year." +
37+
" Prefix your date format with '8' to use the new specifier.");
38+
Joda.forPattern("xxxx");
39+
assertWarnings("'x' weak-year becomes 'Y'. Use 'x' for zone-offset. Prefix your date format with '8' to use the new specifier.");
40+
// multiple deprecations - one field combines warnings
41+
Joda.forPattern("CC-YYYY");
42+
assertWarnings("'Y' year-of-era becomes 'y'. Use 'Y' for week-based-year.; " +
43+
"'C' century of era is no longer supported. " +
44+
"Prefix your date format with '8' to use the new specifier.");
45+
}
46+
47+
public void testDeprecatedEpochScientificNotation() {
48+
assertValidDateFormatParsing("epoch_second", "1.234e5", "123400");
49+
assertWarnings("Use of scientific notation" +
50+
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
51+
assertValidDateFormatParsing("epoch_millis", "1.234e5", "123400");
52+
assertWarnings("Use of scientific notation" +
53+
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
54+
}
55+
56+
public void testThatNegativeEpochsCanBeParsed() {
57+
// problem: negative epochs can be arbitrary in size...
58+
boolean parseMilliSeconds = randomBoolean();
59+
DateFormatter formatter = DateFormatter.forPattern(parseMilliSeconds ? "epoch_millis" : "epoch_second");
60+
DateTime dateTime = formatter.parseJoda("-10000");
61+
62+
assertThat(dateTime.getYear(), is(1969));
63+
assertThat(dateTime.getMonthOfYear(), is(12));
64+
assertThat(dateTime.getDayOfMonth(), is(31));
65+
if (parseMilliSeconds) {
66+
assertThat(dateTime.getHourOfDay(), is(23)); // utc timezone, +2 offset due to CEST
67+
assertThat(dateTime.getMinuteOfHour(), is(59));
68+
assertThat(dateTime.getSecondOfMinute(), is(50));
69+
} else {
70+
assertThat(dateTime.getHourOfDay(), is(21)); // utc timezone, +2 offset due to CEST
71+
assertThat(dateTime.getMinuteOfHour(), is(13));
72+
assertThat(dateTime.getSecondOfMinute(), is(20));
73+
}
74+
75+
// test floats get truncated
76+
String epochFloatValue = String.format(Locale.US, "%d.%d", dateTime.getMillis() / (parseMilliSeconds ? 1L : 1000L),
77+
randomNonNegativeLong());
78+
assertThat(formatter.parseJoda(epochFloatValue).getMillis(), is(dateTime.getMillis()));
79+
80+
// every negative epoch must be parsed, no matter if exact the size or bigger
81+
if (parseMilliSeconds) {
82+
formatter.parseJoda("-100000000");
83+
formatter.parseJoda("-999999999999");
84+
formatter.parseJoda("-1234567890123");
85+
formatter.parseJoda("-1234567890123456789");
86+
87+
formatter.parseJoda("-1234567890123.9999");
88+
formatter.parseJoda("-1234567890123456789.9999");
89+
} else {
90+
formatter.parseJoda("-100000000");
91+
formatter.parseJoda("-1234567890");
92+
formatter.parseJoda("-1234567890123456");
93+
94+
formatter.parseJoda("-1234567890.9999");
95+
formatter.parseJoda("-1234567890123456.9999");
96+
}
97+
98+
assertWarnings("Use of negative values" +
99+
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
100+
}
101+
102+
public void testDeprecatedEpochNegative() {
103+
assertValidDateFormatParsing("epoch_second", "-12345", "-12345");
104+
assertWarnings("Use of negative values" +
105+
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
106+
assertValidDateFormatParsing("epoch_millis", "-12345", "-12345");
107+
assertWarnings("Use of negative values" +
108+
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
109+
}
110+
111+
private void assertValidDateFormatParsing(String pattern, String dateToParse, String expectedDate) {
112+
DateFormatter formatter = DateFormatter.forPattern(pattern);
113+
assertThat(formatter.formatMillis(formatter.parseMillis(dateToParse)), is(expectedDate));
114+
}
115+
}

server/src/test/java/org/elasticsearch/common/joda/SimpleJodaTests.java

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
import static org.hamcrest.Matchers.is;
4343

4444
public class SimpleJodaTests extends ESTestCase {
45+
@Override
46+
protected boolean enableWarningsCheck() {
47+
// disable warning checks as deprecated patterns are used to compare Joda vs Java results (y - year and Z zone offset)
48+
return false;
49+
}
50+
4551
public void testMultiParsers() {
4652
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
4753
DateTimeParser[] parsers = new DateTimeParser[3];
@@ -262,51 +268,7 @@ public void testThatEpochsCanBeParsed() {
262268
assertThat(formatter.parseJoda(epochFloatValue).getMillis(), is(dateTime.getMillis()));
263269
}
264270

265-
public void testThatNegativeEpochsCanBeParsed() {
266-
// problem: negative epochs can be arbitrary in size...
267-
boolean parseMilliSeconds = randomBoolean();
268-
DateFormatter formatter = DateFormatter.forPattern(parseMilliSeconds ? "epoch_millis" : "epoch_second");
269-
DateTime dateTime = formatter.parseJoda("-10000");
270-
271-
assertThat(dateTime.getYear(), is(1969));
272-
assertThat(dateTime.getMonthOfYear(), is(12));
273-
assertThat(dateTime.getDayOfMonth(), is(31));
274-
if (parseMilliSeconds) {
275-
assertThat(dateTime.getHourOfDay(), is(23)); // utc timezone, +2 offset due to CEST
276-
assertThat(dateTime.getMinuteOfHour(), is(59));
277-
assertThat(dateTime.getSecondOfMinute(), is(50));
278-
} else {
279-
assertThat(dateTime.getHourOfDay(), is(21)); // utc timezone, +2 offset due to CEST
280-
assertThat(dateTime.getMinuteOfHour(), is(13));
281-
assertThat(dateTime.getSecondOfMinute(), is(20));
282-
}
283-
284-
// test floats get truncated
285-
String epochFloatValue = String.format(Locale.US, "%d.%d", dateTime.getMillis() / (parseMilliSeconds ? 1L : 1000L),
286-
randomNonNegativeLong());
287-
assertThat(formatter.parseJoda(epochFloatValue).getMillis(), is(dateTime.getMillis()));
288-
289-
// every negative epoch must be parsed, no matter if exact the size or bigger
290-
if (parseMilliSeconds) {
291-
formatter.parseJoda("-100000000");
292-
formatter.parseJoda("-999999999999");
293-
formatter.parseJoda("-1234567890123");
294-
formatter.parseJoda("-1234567890123456789");
295-
296-
formatter.parseJoda("-1234567890123.9999");
297-
formatter.parseJoda("-1234567890123456789.9999");
298-
} else {
299-
formatter.parseJoda("-100000000");
300-
formatter.parseJoda("-1234567890");
301-
formatter.parseJoda("-1234567890123456");
302271

303-
formatter.parseJoda("-1234567890.9999");
304-
formatter.parseJoda("-1234567890123456.9999");
305-
}
306-
307-
assertWarnings("Use of negative values" +
308-
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
309-
}
310272

311273
public void testForInvalidDatesInEpochSecond() {
312274
DateFormatter formatter = DateFormatter.forPattern("epoch_second");
@@ -739,41 +701,6 @@ public void testThatRootObjectParsingIsStrict() throws Exception {
739701
}
740702
}
741703

742-
public void testDeprecatedFormatSpecifiers() {
743-
Joda.forPattern("CC");
744-
assertWarnings("Use of 'C' (century-of-era) is deprecated and will not be supported in the" +
745-
" next major version of Elasticsearch.");
746-
Joda.forPattern("YYYY");
747-
assertWarnings("Use of 'Y' (year-of-era) will change to 'y' in the" +
748-
" next major version of Elasticsearch. Prefix your date format with '8' to use the new specifier.");
749-
Joda.forPattern("xxxx");
750-
assertWarnings("Use of 'x' (week-based-year) will change" +
751-
" to 'Y' in the next major version of Elasticsearch. Prefix your date format with '8' to use the new specifier.");
752-
// multiple deprecations
753-
Joda.forPattern("CC-YYYY");
754-
assertWarnings("Use of 'C' (century-of-era) is deprecated and will not be supported in the" +
755-
" next major version of Elasticsearch.", "Use of 'Y' (year-of-era) will change to 'y' in the" +
756-
" next major version of Elasticsearch. Prefix your date format with '8' to use the new specifier.");
757-
}
758-
759-
public void testDeprecatedEpochScientificNotation() {
760-
assertValidDateFormatParsing("epoch_second", "1.234e5", "123400");
761-
assertWarnings("Use of scientific notation" +
762-
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
763-
assertValidDateFormatParsing("epoch_millis", "1.234e5", "123400");
764-
assertWarnings("Use of scientific notation" +
765-
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
766-
}
767-
768-
public void testDeprecatedEpochNegative() {
769-
assertValidDateFormatParsing("epoch_second", "-12345", "-12345");
770-
assertWarnings("Use of negative values" +
771-
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
772-
assertValidDateFormatParsing("epoch_millis", "-12345", "-12345");
773-
assertWarnings("Use of negative values" +
774-
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
775-
}
776-
777704
private void assertValidDateFormatParsing(String pattern, String dateToParse) {
778705
assertValidDateFormatParsing(pattern, dateToParse, dateToParse);
779706
}

server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public void setup() {
5252
parser = indexService.mapperService().documentMapperParser();
5353
}
5454

55+
@Override
56+
protected boolean enableWarningsCheck() {
57+
// disable warning checks as deprecated patterns are used to compare Joda vs Java results (y - year and Z zone offset)
58+
return false;
59+
}
60+
5561
@Override
5662
protected Collection<Class<? extends Plugin>> getPlugins() {
5763
return pluginList(InternalSettingsPlugin.class);

server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public void testValueFormat() {
151151
ft.docValueFormat(null, DateTimeZone.UTC).parseLong("2015-10-12T14:10:55", true, null));
152152
assertEquals(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseJoda("2015-10-13").getMillis() - 1,
153153
ft.docValueFormat(null, DateTimeZone.UTC).parseLong("2015-10-12||/d", true, null));
154+
assertWarnings("'y' year becomes 'u'. Use 'y' for year-of-era. Prefix your date format with '8' to use the new specifier.");
154155
}
155156

156157
public void testValueForSearch() {

0 commit comments

Comments
 (0)