From 0306886ea9374a38f02019da786ddf7448485ba6 Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Wed, 11 Apr 2018 16:29:35 -0600 Subject: [PATCH 1/2] Add a helper method to get a random java.util.TimeZone This adds a helper method to ESTestCase that returns a randomized `java.util.TimeZone`. This can be used when transitioning code from Joda to the JDK's time classes. --- .../main/java/org/elasticsearch/test/ESTestCase.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index a65b8b430e681..2491b42ec8b94 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -135,6 +135,7 @@ import java.util.Objects; import java.util.Random; import java.util.Set; +import java.util.TimeZone; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -674,6 +675,15 @@ public static DateTimeZone randomDateTimeZone() { return DateTimeZone.forID(randomFrom(ids)); } + /** + * generate a random TimeZone from the ones available in java.time + */ + public static TimeZone randomTimeZone() { + List ids = Arrays.asList(TimeZone.getAvailableIDs()); + Collections.sort(ids); + return TimeZone.getTimeZone(randomFrom(ids)); + } + /** * helper to randomly perform on consumer with value */ From 00a418b24234d58bf1784294feca45a5aad83aee Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Thu, 12 Apr 2018 10:13:58 -0600 Subject: [PATCH 2/2] Switch to a statically initialized and sorted set of TZ ids --- .../org/elasticsearch/test/ESTestCase.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 2491b42ec8b94..32c660cd5d24b 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -174,6 +174,9 @@ @LuceneTestCase.SuppressReproduceLine public abstract class ESTestCase extends LuceneTestCase { + private static final List JODA_TIMEZONE_IDS; + private static final List JAVA_TIMEZONE_IDS; + private static final AtomicInteger portGenerator = new AtomicInteger(); @AfterClass @@ -192,6 +195,14 @@ public static void resetPortCounter() { })); BootstrapForTesting.ensureInitialized(); + + List jodaTZIds = new ArrayList<>(DateTimeZone.getAvailableIDs()); + Collections.sort(jodaTZIds); + JODA_TIMEZONE_IDS = Collections.unmodifiableList(jodaTZIds); + + List javaTZIds = Arrays.asList(TimeZone.getAvailableIDs()); + Collections.sort(javaTZIds); + JAVA_TIMEZONE_IDS = Collections.unmodifiableList(javaTZIds); } protected final Logger logger = Loggers.getLogger(getClass()); @@ -670,18 +681,14 @@ public static String randomPositiveTimeValue() { * generate a random DateTimeZone from the ones available in joda library */ public static DateTimeZone randomDateTimeZone() { - List ids = new ArrayList<>(DateTimeZone.getAvailableIDs()); - Collections.sort(ids); - return DateTimeZone.forID(randomFrom(ids)); + return DateTimeZone.forID(randomFrom(JODA_TIMEZONE_IDS)); } /** * generate a random TimeZone from the ones available in java.time */ public static TimeZone randomTimeZone() { - List ids = Arrays.asList(TimeZone.getAvailableIDs()); - Collections.sort(ids); - return TimeZone.getTimeZone(randomFrom(ids)); + return TimeZone.getTimeZone(randomFrom(JAVA_TIMEZONE_IDS)); } /**