From 796fd9a5a38d77ff08c830d67604efaf17e63a45 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Mon, 9 Aug 2010 01:25:43 +0000 Subject: [PATCH] SOLR-2002: run tests under different timezones and locales git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@983495 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/common-build.xml | 6 +++ .../apache/lucene/util/LuceneTestCase.java | 29 ++++++++++- .../apache/lucene/util/LuceneTestCaseJ4.java | 49 ++++++++++++++++++- solr/build.xml | 2 + solr/common-build.xml | 2 + 5 files changed, 85 insertions(+), 3 deletions(-) diff --git a/lucene/common-build.xml b/lucene/common-build.xml index 752c867d9e5..fe9bd0d730d 100644 --- a/lucene/common-build.xml +++ b/lucene/common-build.xml @@ -57,6 +57,8 @@ + + @@ -437,6 +439,10 @@ + + + + diff --git a/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java index 57573fe8c8d..1c8e2ece3f1 100644 --- a/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java +++ b/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java @@ -22,10 +22,12 @@ import java.io.PrintStream; import java.io.IOException; import java.util.Arrays; import java.util.Iterator; +import java.util.Locale; import java.util.Random; import java.util.ArrayList; import java.util.List; import java.util.Collections; +import java.util.TimeZone; import junit.framework.TestCase; @@ -73,7 +75,11 @@ public abstract class LuceneTestCase extends TestCase { /** Gets the codec to run tests with. */ public static final String TEST_CODEC = LuceneTestCaseJ4.TEST_CODEC; - + /** Gets the locale to run tests with */ + static final String TEST_LOCALE = LuceneTestCaseJ4.TEST_LOCALE; + /** Gets the timezone to run tests with */ + static final String TEST_TIMEZONE = LuceneTestCaseJ4.TEST_TIMEZONE; + /** * A random multiplier which you should use when writing random tests: * multiply it by the number of iterations @@ -86,6 +92,11 @@ public abstract class LuceneTestCase extends TestCase { private Codec codec; + private Locale locale; + private Locale savedLocale; + private TimeZone timeZone; + private TimeZone savedTimeZone; + /** Used to track if setUp and tearDown are called correctly from subclasses */ private boolean setup; @@ -125,6 +136,16 @@ public abstract class LuceneTestCase extends TestCase { ConcurrentMergeScheduler.setTestMode(); savedBoolMaxClauseCount = BooleanQuery.getMaxClauseCount(); codec = LuceneTestCaseJ4.installTestCodecs(); + savedLocale = Locale.getDefault(); + locale = TEST_LOCALE.equals("random") + ? LuceneTestCaseJ4.randomLocale(seedRnd) + : LuceneTestCaseJ4.localeForName(TEST_LOCALE); + Locale.setDefault(locale); + savedTimeZone = TimeZone.getDefault(); + timeZone = TEST_TIMEZONE.equals("random") + ? LuceneTestCaseJ4.randomTimeZone(seedRnd) + : TimeZone.getTimeZone(TEST_TIMEZONE); + TimeZone.setDefault(timeZone); } /** @@ -151,6 +172,8 @@ public abstract class LuceneTestCase extends TestCase { setup = false; BooleanQuery.setMaxClauseCount(savedBoolMaxClauseCount); LuceneTestCaseJ4.removeTestCodecs(codec); + Locale.setDefault(savedLocale); + TimeZone.setDefault(savedTimeZone); try { Thread.setDefaultUncaughtExceptionHandler(savedUncaughtExceptionHandler); @@ -309,6 +332,10 @@ public abstract class LuceneTestCase extends TestCase { super.runBare(); } catch (Throwable e) { System.out.println("NOTE: random codec of testcase '" + getName() + "' was: " + codec); + if (TEST_LOCALE.equals("random")) + System.out.println("NOTE: random locale of testcase '" + getName() + "' was: " + locale); + if (TEST_TIMEZONE.equals("random")) + System.out.println("NOTE: random timezone of testcase '" + getName() + "' was: " + timeZone.getID()); if (seed != null) { System.out.println("NOTE: random seed of testcase '" + getName() + "' was: " + seed); } diff --git a/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java b/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java index 1c76be7ad13..51206fd7fe1 100644 --- a/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java +++ b/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java @@ -51,10 +51,12 @@ import java.io.PrintStream; import java.io.IOException; import java.util.Arrays; import java.util.Iterator; +import java.util.Locale; import java.util.Random; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.TimeZone; import java.util.WeakHashMap; import java.util.Collections; import java.lang.reflect.Method; @@ -124,7 +126,11 @@ public class LuceneTestCaseJ4 { // tests) /** Gets the codec to run tests with. */ static final String TEST_CODEC = System.getProperty("tests.codec", "random"); - + /** Gets the locale to run tests with */ + static final String TEST_LOCALE = System.getProperty("tests.locale", "random"); + /** Gets the timezone to run tests with */ + static final String TEST_TIMEZONE = System.getProperty("tests.timezone", "random"); + /** * A random multiplier which you should use when writing random tests: * multiply it by the number of iterations @@ -158,6 +164,11 @@ public class LuceneTestCaseJ4 { private static String savedDefaultCodec; private static Codec codec; + private static Locale locale; + private static Locale savedLocale; + private static TimeZone timeZone; + private static TimeZone savedTimeZone; + private static final String[] TEST_CODECS = new String[] {"MockSep", "MockFixedIntBlock", "MockVariableIntBlock"}; private static void swapCodec(Codec c) { @@ -231,11 +242,19 @@ public class LuceneTestCaseJ4 { @BeforeClass public static void beforeClassLuceneTestCaseJ4() { codec = installTestCodecs(); + savedLocale = Locale.getDefault(); + locale = TEST_LOCALE.equals("random") ? randomLocale(seedRnd) : localeForName(TEST_LOCALE); + Locale.setDefault(locale); + savedTimeZone = TimeZone.getDefault(); + timeZone = TEST_TIMEZONE.equals("random") ? randomTimeZone(seedRnd) : TimeZone.getTimeZone(TEST_TIMEZONE); + TimeZone.setDefault(timeZone); } @AfterClass public static void afterClassLuceneTestCaseJ4() { removeTestCodecs(codec); + Locale.setDefault(savedLocale); + TimeZone.setDefault(savedTimeZone); } // This is how we get control when errors occur. @@ -514,6 +533,29 @@ public class LuceneTestCaseJ4 { return c; } + /** return a random Locale from the available locales on the system */ + public static Locale randomLocale(Random random) { + Locale locales[] = Locale.getAvailableLocales(); + return locales[random.nextInt(locales.length)]; + } + + /** return a random TimeZone from the available timezones on the system */ + public static TimeZone randomTimeZone(Random random) { + String tzIds[] = TimeZone.getAvailableIDs(); + return TimeZone.getTimeZone(tzIds[random.nextInt(tzIds.length)]); + } + + /** return a Locale object equivalent to its programmatic name */ + public static Locale localeForName(String localeName) { + String elements[] = localeName.split("\\_"); + switch(elements.length) { + case 3: return new Locale(elements[0], elements[1], elements[2]); + case 2: return new Locale(elements[0], elements[1]); + case 1: return new Locale(elements[0]); + default: throw new IllegalArgumentException("Invalid Locale: " + localeName); + } + } + public String getName() { return this.name; } @@ -538,7 +580,10 @@ public class LuceneTestCaseJ4 { } System.out.println("NOTE: random codec of testcase '" + getName() + "' was: " + codec); - + if (TEST_LOCALE.equals("random")) + System.out.println("NOTE: random locale of testcase '" + getName() + "' was: " + locale); + if (TEST_TIMEZONE.equals("random")) + System.out.println("NOTE: random timezone of testcase '" + getName() + "' was: " + timeZone.getID()); if (seed != null) { System.out.println("NOTE: random seed of testcase '" + getName() + "' was: " + seed); } diff --git a/solr/build.xml b/solr/build.xml index 199e328ed3d..6a6893356eb 100644 --- a/solr/build.xml +++ b/solr/build.xml @@ -441,6 +441,8 @@ + + diff --git a/solr/common-build.xml b/solr/common-build.xml index 84181463521..489b040ae57 100644 --- a/solr/common-build.xml +++ b/solr/common-build.xml @@ -45,6 +45,8 @@ + +