Skip to content

Commit f106ce2

Browse files
wnobstoty
authored andcommitted
[CALCITE-5536] Clean up some of the magic numbers in AvaticaResultSetConversionsTest and AbstractCursor
1 parent 0f9d2de commit f106ce2

File tree

5 files changed

+88
-42
lines changed

5 files changed

+88
-42
lines changed

core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ protected Accessor createAccessor(ColumnMetaData columnMetaData,
173173
default:
174174
throw new AssertionError("bad " + columnMetaData.type.rep);
175175
}
176-
case 2013: // TIME_WITH_TIMEZONE
176+
case Types.TIME_WITH_TIMEZONE:
177177
switch (columnMetaData.type.rep) {
178178
case STRING:
179179
return new StringAccessor(getter);
180180
default:
181181
throw new AssertionError("bad " + columnMetaData.type.rep);
182182
}
183-
case 2014: // TIMESTAMP_WITH_TIMEZONE
183+
case Types.TIMESTAMP_WITH_TIMEZONE:
184184
switch (columnMetaData.type.rep) {
185185
case STRING:
186186
return new StringAccessor(getter);

core/src/test/java/org/apache/calcite/avatica/AvaticaResultSetConversionsTest.java

+49-26
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@
7171
*/
7272
@RunWith(Parameterized.class)
7373
public class AvaticaResultSetConversionsTest {
74+
75+
// UTC: 2016-10-10 20:18:38.123
76+
// October 10 is considered DST in all time zones that observe DST (both hemispheres), so tests
77+
// using this value will cover daylight time zone conversion when run in a location that observes
78+
// DST. This is just a matter of coverage; all tests must succeed no matter where the host is.
79+
private static final long DST_INSTANT = 1476130718123L;
80+
private static final String DST_DATE_STRING = "2016-10-10";
81+
private static final String DST_TIME_STRING = "20:18:38";
82+
private static final String DST_TIMESTAMP_STRING = "2016-10-10 20:18:38";
83+
84+
// UTC: 2016-11-14 11:32:03.242
85+
// There is no date where all time zones (both hemispheres) are on standard time, but all northern
86+
// time zones observe standard time by mid-November. Tests using this value may or may not
87+
// exercise standard time zone conversion, but this is just a matter of coverage; all tests must
88+
// succeed no matter where the host is.
89+
private static final long STANDARD_INSTANT = 1479123123242L;
90+
91+
// UTC: 00:24:36.123
92+
private static final long VALID_TIME = 1476123L;
93+
94+
// UTC: 41:05:12.242
95+
private static final long OVERFLOW_TIME = 147912242L;
96+
7497
/**
7598
* A fake test driver for test.
7699
*/
@@ -215,15 +238,15 @@ public TestMetaImpl(AvaticaConnection connection) {
215238
List<Object> row = Collections.<Object>singletonList(
216239
new Object[] {
217240
true, (byte) 1, (short) 2, 3, 4L, 5.0f, 6.0d, "testvalue",
218-
new Date(1476130718123L), new Time(1476130718123L),
219-
new Timestamp(1476130718123L),
241+
new Date(DST_INSTANT), new Time(DST_INSTANT),
242+
new Timestamp(DST_INSTANT),
220243
Arrays.asList(1, 2, 3),
221244
new StructImpl(Arrays.asList(42, false)),
222245
true,
223246
null,
224247
Arrays.asList(123, 18234),
225-
Arrays.asList(1476130718123L, 1479123123242L),
226-
Arrays.asList(1476123L, 147912242L),
248+
Arrays.asList(DST_INSTANT, STANDARD_INSTANT),
249+
Arrays.asList(VALID_TIME, OVERFLOW_TIME),
227250
Arrays.asList(1, 1.1)
228251
});
229252

@@ -653,7 +676,7 @@ private TimeArrayAccessorTestHelper(Getter g) {
653676
ColumnMetaData.scalar(Types.TIME, "TIME", ColumnMetaData.Rep.NUMBER);
654677
Array expectedArray =
655678
new ArrayFactoryImpl(TimeZone.getTimeZone("UTC")).createArray(
656-
intType, Arrays.asList(1476123L, 147912242L));
679+
intType, Arrays.asList(VALID_TIME, OVERFLOW_TIME));
657680
assertTrue(ArrayImpl.equalContents(expectedArray, g.getArray(resultSet)));
658681
}
659682
}
@@ -671,7 +694,7 @@ private TimestampArrayAccessorTestHelper(Getter g) {
671694
ColumnMetaData.scalar(Types.TIMESTAMP, "TIMESTAMP", ColumnMetaData.Rep.PRIMITIVE_LONG);
672695
Array expectedArray =
673696
new ArrayFactoryImpl(TimeZone.getTimeZone("UTC")).createArray(
674-
intType, Arrays.asList(1476130718123L, 1479123123242L));
697+
intType, Arrays.asList(DST_INSTANT, STANDARD_INSTANT));
675698
assertTrue(ArrayImpl.equalContents(expectedArray, g.getArray(resultSet)));
676699
}
677700
}
@@ -986,7 +1009,7 @@ private DateAccessorTestHelper(Getter g) {
9861009
}
9871010

9881011
@Override public void testGetString(ResultSet resultSet) throws SQLException {
989-
assertEquals("2016-10-10", g.getString(resultSet));
1012+
assertEquals(DST_DATE_STRING, g.getString(resultSet));
9901013
}
9911014

9921015
@Override public void testGetBoolean(ResultSet resultSet) throws SQLException {
@@ -1010,7 +1033,7 @@ private DateAccessorTestHelper(Getter g) {
10101033
}
10111034

10121035
@Override public void testGetDate(ResultSet resultSet, Calendar calendar) throws SQLException {
1013-
assertEquals(new Date(1476130718123L), g.getDate(resultSet, calendar));
1036+
assertEquals(new Date(DST_INSTANT), g.getDate(resultSet, calendar));
10141037
}
10151038
}
10161039

@@ -1023,31 +1046,31 @@ private TimeAccessorTestHelper(Getter g) {
10231046
}
10241047

10251048
@Override public void testGetString(ResultSet resultSet) throws SQLException {
1026-
assertEquals("20:18:38", g.getString(resultSet));
1049+
assertEquals(DST_TIME_STRING, g.getString(resultSet));
10271050
}
10281051

10291052
@Override public void testGetBoolean(ResultSet resultSet) throws SQLException {
10301053
assertEquals(true, g.getBoolean(resultSet));
10311054
}
10321055

10331056
@Override public void testGetByte(ResultSet resultSet) throws SQLException {
1034-
assertEquals((byte) -85, g.getByte(resultSet));
1057+
assertEquals((byte) DST_INSTANT, g.getByte(resultSet));
10351058
}
10361059

10371060
@Override public void testGetShort(ResultSet resultSet) throws SQLException {
1038-
assertEquals((short) -20053, g.getShort(resultSet));
1061+
assertEquals((short) (DST_INSTANT % DateTimeUtils.MILLIS_PER_DAY), g.getShort(resultSet));
10391062
}
10401063

10411064
@Override public void testGetInt(ResultSet resultSet) throws SQLException {
1042-
assertEquals(73118123, g.getInt(resultSet));
1065+
assertEquals((int) (DST_INSTANT % DateTimeUtils.MILLIS_PER_DAY), g.getInt(resultSet));
10431066
}
10441067

10451068
@Override public void testGetLong(ResultSet resultSet) throws SQLException {
1046-
assertEquals(73118123, g.getLong(resultSet));
1069+
assertEquals(DST_INSTANT % DateTimeUtils.MILLIS_PER_DAY, g.getLong(resultSet));
10471070
}
10481071

10491072
@Override public void testGetTime(ResultSet resultSet, Calendar calendar) throws SQLException {
1050-
assertEquals(new Time(1476130718123L), g.getTime(resultSet, calendar));
1073+
assertEquals(new Time(DST_INSTANT), g.getTime(resultSet, calendar));
10511074
}
10521075
}
10531076

@@ -1060,40 +1083,40 @@ private TimestampAccessorTestHelper(Getter g) {
10601083
}
10611084

10621085
@Override public void testGetString(ResultSet resultSet) throws SQLException {
1063-
assertEquals("2016-10-10 20:18:38", g.getString(resultSet));
1086+
assertEquals(DST_TIMESTAMP_STRING, g.getString(resultSet));
10641087
}
10651088

10661089
@Override public void testGetBoolean(ResultSet resultSet) throws SQLException {
10671090
assertEquals(true, g.getBoolean(resultSet));
10681091
}
10691092

10701093
@Override public void testGetByte(ResultSet resultSet) throws SQLException {
1071-
assertEquals((byte) -85, g.getByte(resultSet));
1094+
assertEquals((byte) DST_INSTANT, g.getByte(resultSet));
10721095
}
10731096

10741097
@Override public void testGetShort(ResultSet resultSet) throws SQLException {
1075-
assertEquals((short) 16811, g.getShort(resultSet));
1098+
assertEquals((short) DST_INSTANT, g.getShort(resultSet));
10761099
}
10771100

10781101
@Override public void testGetInt(ResultSet resultSet) throws SQLException {
1079-
assertEquals(-1338031701, g.getInt(resultSet));
1102+
assertEquals((int) DST_INSTANT, g.getInt(resultSet));
10801103
}
10811104

10821105
@Override public void testGetLong(ResultSet resultSet) throws SQLException {
1083-
assertEquals(1476130718123L, g.getLong(resultSet));
1106+
assertEquals(DST_INSTANT, g.getLong(resultSet));
10841107
}
10851108

10861109
@Override public void testGetDate(ResultSet resultSet, Calendar calendar) throws SQLException {
1087-
assertEquals(new Date(1476130718123L), g.getDate(resultSet, calendar));
1110+
assertEquals(new Date(DST_INSTANT), g.getDate(resultSet, calendar));
10881111
}
10891112

10901113
@Override public void testGetTime(ResultSet resultSet, Calendar calendar) throws SQLException {
1091-
assertEquals(new Time(1476130718123L), g.getTime(resultSet, calendar));
1114+
assertEquals(new Time(DST_INSTANT), g.getTime(resultSet, calendar));
10921115
}
10931116

10941117
@Override public void testGetTimestamp(ResultSet resultSet, Calendar calendar)
10951118
throws SQLException {
1096-
assertEquals(new Timestamp(1476130718123L), g.getTimestamp(resultSet, calendar));
1119+
assertEquals(new Timestamp(DST_INSTANT), g.getTimestamp(resultSet, calendar));
10971120
}
10981121
}
10991122

@@ -1110,7 +1133,7 @@ private StringAccessorTestHelper(Getter g) {
11101133
}
11111134
}
11121135

1113-
private static final Calendar DEFAULT_CALENDAR = DateTimeUtils.calendar();
1136+
private static final Calendar UTC_CALENDAR = DateTimeUtils.calendar();
11141137

11151138
private static Connection connection = null;
11161139
private static ResultSet resultSet = null;
@@ -1282,17 +1305,17 @@ public void testGetArray() throws SQLException {
12821305

12831306
@Test
12841307
public void testGetDate() throws SQLException {
1285-
testHelper.testGetDate(resultSet, DEFAULT_CALENDAR);
1308+
testHelper.testGetDate(resultSet, UTC_CALENDAR);
12861309
}
12871310

12881311
@Test
12891312
public void testGetTime() throws SQLException {
1290-
testHelper.testGetTime(resultSet, DEFAULT_CALENDAR);
1313+
testHelper.testGetTime(resultSet, UTC_CALENDAR);
12911314
}
12921315

12931316
@Test
12941317
public void testGetTimestamp() throws SQLException {
1295-
testHelper.testGetTimestamp(resultSet, DEFAULT_CALENDAR);
1318+
testHelper.testGetTimestamp(resultSet, UTC_CALENDAR);
12961319
}
12971320

12981321
@Test

core/src/test/java/org/apache/calcite/avatica/util/TimestampAccessorTest.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public class TimestampAccessorTest {
4141
private static final Calendar UTC =
4242
Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT);
4343

44+
// UTC: 2014-09-30 15:28:27.356
45+
private static final long DST_INSTANT = 1412090907356L;
46+
private static final String DST_STRING = "2014-09-30 15:28:27";
47+
48+
// UTC: 1500-04-30 12:00:00.123 (PROLEPTIC GREGORIAN CALENDAR)
49+
private static final long PRE_GREG_INSTANT = -14820580799877L;
50+
private static final String PRE_GREG_STRING = "1500-04-30 12:00:00";
51+
4452
private Cursor.Accessor instance;
4553
private Calendar localCalendar;
4654
private Timestamp value;
@@ -191,11 +199,11 @@ public class TimestampAccessorTest {
191199
value = new Timestamp(0L);
192200
assertThat(instance.getString(), is("1970-01-01 00:00:00"));
193201

194-
value = new Timestamp(1412090907356L /* 2014-09-30 15:28:27.356 UTC */);
195-
assertThat(instance.getString(), is("2014-09-30 15:28:27"));
202+
value = new Timestamp(DST_INSTANT);
203+
assertThat(instance.getString(), is(DST_STRING));
196204

197-
value = new Timestamp(-14820580799877L /* 1500-04-30 12:00:00.123 */);
198-
assertThat(instance.getString(), is("1500-04-30 12:00:00"));
205+
value = new Timestamp(PRE_GREG_INSTANT);
206+
assertThat(instance.getString(), is(PRE_GREG_STRING));
199207
}
200208

201209
/**

core/src/test/java/org/apache/calcite/avatica/util/TimestampFromNumberAccessorTest.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
*/
3838
public class TimestampFromNumberAccessorTest {
3939

40+
// UTC: 2014-09-30 15:28:27.356
41+
private static final long DST_INSTANT = 1412090907356L;
42+
private static final String DST_STRING = "2014-09-30 15:28:27";
43+
44+
// UTC: 1500-04-30 12:00:00.123 (JULIAN CALENDAR)
45+
private static final long PRE_GREG_INSTANT = -14821444799877L;
46+
private static final String PRE_GREG_STRING = "1500-04-30 12:00:00";
47+
4048
private Cursor.Accessor instance;
4149
private Calendar localCalendar;
4250
private Object value;
@@ -48,8 +56,7 @@ public class TimestampFromNumberAccessorTest {
4856
@Before public void before() {
4957
final AbstractCursor.Getter getter = new LocalGetter();
5058
localCalendar = Calendar.getInstance(TimeZone.getDefault(), Locale.ROOT);
51-
instance = new AbstractCursor.TimestampFromNumberAccessor(getter,
52-
localCalendar);
59+
instance = new AbstractCursor.TimestampFromNumberAccessor(getter, localCalendar);
5360
}
5461

5562
/**
@@ -132,11 +139,11 @@ public class TimestampFromNumberAccessorTest {
132139
value = 0L;
133140
assertThat(instance.getString(), is("1970-01-01 00:00:00"));
134141

135-
value = 1412090907356L; // 2014-09-30 15:28:27.356 UTC
136-
assertThat(instance.getString(), is("2014-09-30 15:28:27"));
142+
value = DST_INSTANT;
143+
assertThat(instance.getString(), is(DST_STRING));
137144

138-
value = -14821444799877L; // 1500-04-30 12:00:00.123
139-
assertThat(instance.getString(), is("1500-04-30 12:00:00"));
145+
value = PRE_GREG_INSTANT;
146+
assertThat(instance.getString(), is(PRE_GREG_STRING));
140147
}
141148

142149
/**

core/src/test/java/org/apache/calcite/avatica/util/TimestampFromUtilDateAccessorTest.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ public class TimestampFromUtilDateAccessorTest {
4242
private static final Calendar UTC =
4343
Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT);
4444

45+
// UTC: 2014-09-30 15:28:27.356
46+
private static final long DST_INSTANT = 1412090907356L;
47+
private static final String DST_STRING = "2014-09-30 15:28:27";
48+
49+
// UTC: 1500-04-30 12:00:00.123 (PROLEPTIC GREGORIAN CALENDAR)
50+
private static final long PRE_GREG_INSTANT = -14820580799877L;
51+
private static final String PRE_GREG_STRING = "1500-04-30 12:00:00";
52+
4553
private Cursor.Accessor instance;
4654
private Calendar localCalendar;
4755
private Date value;
@@ -194,11 +202,11 @@ public class TimestampFromUtilDateAccessorTest {
194202
value = new Timestamp(0L);
195203
assertThat(instance.getString(), is("1970-01-01 00:00:00"));
196204

197-
value = new Timestamp(1412090907356L /* 2014-09-30 15:28:27.356 UTC */);
198-
assertThat(instance.getString(), is("2014-09-30 15:28:27"));
205+
value = new Timestamp(DST_INSTANT);
206+
assertThat(instance.getString(), is(DST_STRING));
199207

200-
value = new Timestamp(-14820580799877L /* 1500-04-30 12:00:00.123 UTC */);
201-
assertThat(instance.getString(), is("1500-04-30 12:00:00"));
208+
value = new Timestamp(PRE_GREG_INSTANT);
209+
assertThat(instance.getString(), is(PRE_GREG_STRING));
202210
}
203211

204212
/**

0 commit comments

Comments
 (0)