From a5e564f390f0fa37651c2dc333b7eef215a49e50 Mon Sep 17 00:00:00 2001 From: Owen O'Malley Date: Mon, 26 Mar 2018 11:32:48 -0700 Subject: [PATCH] ORC-327. Fix java unit test cases to pass when -Duser.timezone is set. Fixes #246 Signed-off-by: Owen O'Malley --- .../org/apache/orc/TestColumnStatistics.java | 16 ++++++++-------- .../orc/impl/TestColumnStatisticsImpl.java | 4 ++++ .../test/org/apache/orc/tools/TestFileDump.java | 13 +++++++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/java/core/src/test/org/apache/orc/TestColumnStatistics.java b/java/core/src/test/org/apache/orc/TestColumnStatistics.java index fd87b4ca34..bbc85ea64f 100644 --- a/java/core/src/test/org/apache/orc/TestColumnStatistics.java +++ b/java/core/src/test/org/apache/orc/TestColumnStatistics.java @@ -169,9 +169,8 @@ public void testTimestampMergeUTC() throws Exception { } private static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; - private final SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT); - private Timestamp parseTime(String value) { + private static Timestamp parseTime(SimpleDateFormat format, String value) { try { return new Timestamp(format.parse(value).getTime()); } catch (ParseException e) { @@ -185,13 +184,14 @@ public void testTimestampMergeLA() throws Exception { TimeZone original = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")); + SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT); ColumnStatisticsImpl stats1 = ColumnStatisticsImpl.create(schema); ColumnStatisticsImpl stats2 = ColumnStatisticsImpl.create(schema); - stats1.updateTimestamp(parseTime("2000-04-02 03:30:00")); - stats1.updateTimestamp(parseTime("2000-04-02 01:30:00")); + stats1.updateTimestamp(parseTime(format, "2000-04-02 03:30:00")); + stats1.updateTimestamp(parseTime(format, "2000-04-02 01:30:00")); stats1.increment(2); - stats2.updateTimestamp(parseTime("2000-10-29 01:30:00")); - stats2.updateTimestamp(parseTime("2000-10-29 03:30:00")); + stats2.updateTimestamp(parseTime(format, "2000-10-29 01:30:00")); + stats2.updateTimestamp(parseTime(format, "2000-10-29 03:30:00")); stats2.increment(2); TimestampColumnStatistics typed = (TimestampColumnStatistics) stats1; assertEquals("2000-04-02 01:30:00.0", typed.getMinimum().toString()); @@ -200,8 +200,8 @@ public void testTimestampMergeLA() throws Exception { assertEquals("2000-04-02 01:30:00.0", typed.getMinimum().toString()); assertEquals("2000-10-29 03:30:00.0", typed.getMaximum().toString()); stats1.reset(); - stats1.updateTimestamp(parseTime("1999-04-04 00:00:00")); - stats1.updateTimestamp(parseTime("2009-03-08 12:00:00")); + stats1.updateTimestamp(parseTime(format, "1999-04-04 00:00:00")); + stats1.updateTimestamp(parseTime(format, "2009-03-08 12:00:00")); stats1.merge(stats2); assertEquals("1999-04-04 00:00:00.0", typed.getMinimum().toString()); assertEquals("2009-03-08 12:00:00.0", typed.getMaximum().toString()); diff --git a/java/core/src/test/org/apache/orc/impl/TestColumnStatisticsImpl.java b/java/core/src/test/org/apache/orc/impl/TestColumnStatisticsImpl.java index 6528e75a35..e53cdda471 100644 --- a/java/core/src/test/org/apache/orc/impl/TestColumnStatisticsImpl.java +++ b/java/core/src/test/org/apache/orc/impl/TestColumnStatisticsImpl.java @@ -30,6 +30,7 @@ import org.junit.Test; import java.io.IOException; +import java.util.TimeZone; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -72,6 +73,8 @@ private void assertDateStatistics(ColumnStatisticsImpl stat, int count, int mini @Test public void testOldTimestamps() throws IOException { + TimeZone original = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")); Path exampleDir = new Path(System.getProperty("example.dir")); Path file = new Path(exampleDir, "TestOrcFile.testTimestamp.orc"); Configuration conf = new Configuration(); @@ -80,5 +83,6 @@ public void testOldTimestamps() throws IOException { (TimestampColumnStatistics) reader.getStatistics()[0]; assertEquals("1995-01-01 00:00:00.688", stats.getMinimum().toString()); assertEquals("2037-01-01 00:00:00.0", stats.getMaximum().toString()); + TimeZone.setDefault(original); } } diff --git a/java/tools/src/test/org/apache/orc/tools/TestFileDump.java b/java/tools/src/test/org/apache/orc/tools/TestFileDump.java index d69d080124..9e21fad5f0 100644 --- a/java/tools/src/test/org/apache/orc/tools/TestFileDump.java +++ b/java/tools/src/test/org/apache/orc/tools/TestFileDump.java @@ -31,11 +31,13 @@ import java.nio.charset.StandardCharsets; import java.sql.Date; import java.sql.Timestamp; +import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.TimeZone; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; @@ -262,6 +264,7 @@ public void testDump() throws Exception { @Test public void testDataDump() throws Exception { TypeDescription schema = getAllTypesType(); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Writer writer = OrcFile.createWriter(testFilePath, OrcFile.writerOptions(conf) .fileSystem(fs) @@ -282,8 +285,9 @@ public void testDataDump() throws Exception { 4.0f, 20.0, new HiveDecimalWritable("4.2222"), - new Timestamp(1416967764000L), - new DateWritable(new Date(1416967764000L)), + new Timestamp(format.parse("2014-11-25 18:09:24").getTime()), + new DateWritable(DateWritable.millisToDays( + format.parse("2014-11-25 00:00:00").getTime())), "string", "hello", "hello", @@ -302,8 +306,9 @@ public void testDataDump() throws Exception { 8.0f, 40.0, new HiveDecimalWritable("2.2222"), - new Timestamp(1416967364000L), - new DateWritable(new Date(1411967764000L)), + new Timestamp(format.parse("2014-11-25 18:02:44").getTime()), + new DateWritable(DateWritable.millisToDays( + format.parse("2014-09-28 00:00:00").getTime())), "abcd", "world", "world",