Skip to content

Commit 3721cd5

Browse files
committed
Add a helper method to get a random java.util.TimeZone (#29487)
* 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.
1 parent 0935250 commit 3721cd5

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
import java.util.Objects;
136136
import java.util.Random;
137137
import java.util.Set;
138+
import java.util.TimeZone;
138139
import java.util.concurrent.ExecutorService;
139140
import java.util.concurrent.TimeUnit;
140141
import java.util.concurrent.atomic.AtomicInteger;
@@ -173,6 +174,9 @@
173174
@LuceneTestCase.SuppressReproduceLine
174175
public abstract class ESTestCase extends LuceneTestCase {
175176

177+
private static final List<String> JODA_TIMEZONE_IDS;
178+
private static final List<String> JAVA_TIMEZONE_IDS;
179+
176180
private static final AtomicInteger portGenerator = new AtomicInteger();
177181

178182
@AfterClass
@@ -191,6 +195,14 @@ public static void resetPortCounter() {
191195
}));
192196

193197
BootstrapForTesting.ensureInitialized();
198+
199+
List<String> jodaTZIds = new ArrayList<>(DateTimeZone.getAvailableIDs());
200+
Collections.sort(jodaTZIds);
201+
JODA_TIMEZONE_IDS = Collections.unmodifiableList(jodaTZIds);
202+
203+
List<String> javaTZIds = Arrays.asList(TimeZone.getAvailableIDs());
204+
Collections.sort(javaTZIds);
205+
JAVA_TIMEZONE_IDS = Collections.unmodifiableList(javaTZIds);
194206
}
195207

196208
protected final Logger logger = Loggers.getLogger(getClass());
@@ -669,9 +681,14 @@ public static String randomPositiveTimeValue() {
669681
* generate a random DateTimeZone from the ones available in joda library
670682
*/
671683
public static DateTimeZone randomDateTimeZone() {
672-
List<String> ids = new ArrayList<>(DateTimeZone.getAvailableIDs());
673-
Collections.sort(ids);
674-
return DateTimeZone.forID(randomFrom(ids));
684+
return DateTimeZone.forID(randomFrom(JODA_TIMEZONE_IDS));
685+
}
686+
687+
/**
688+
* generate a random TimeZone from the ones available in java.time
689+
*/
690+
public static TimeZone randomTimeZone() {
691+
return TimeZone.getTimeZone(randomFrom(JAVA_TIMEZONE_IDS));
675692
}
676693

677694
/**

0 commit comments

Comments
 (0)