poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From one...@apache.org
Subject svn commit: r1751641 - in /poi/trunk/src: java/org/apache/poi/util/LocaleUtil.java testcases/org/apache/poi/util/TestLocaleUtil.java
Date Wed, 06 Jul 2016 09:23:34 GMT
Author: onealj
Date: Wed Jul  6 09:23:34 2016
New Revision: 1751641

URL: http://svn.apache.org/viewvc?rev=1751641&view=rev
Log:
bug 59805: add LocaleUtil#resetUserTimeZone and #resetUserLocale methods that call ThreadLocal.remove
to fix PermGen memory leaks for long-running threaded applications; patch from apptaro

Modified:
    poi/trunk/src/java/org/apache/poi/util/LocaleUtil.java
    poi/trunk/src/testcases/org/apache/poi/util/TestLocaleUtil.java

Modified: poi/trunk/src/java/org/apache/poi/util/LocaleUtil.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/LocaleUtil.java?rev=1751641&r1=1751640&r2=1751641&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/LocaleUtil.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/LocaleUtil.java Wed Jul  6 09:23:34 2016
@@ -85,6 +85,10 @@ public final class LocaleUtil {
     public static TimeZone getUserTimeZone() {
         return userTimeZone.get();
     }
+    
+    public static void resetUserTimeZone() {
+        userTimeZone.remove();
+    }
 
     /**
      * Sets default user locale.
@@ -93,6 +97,10 @@ public final class LocaleUtil {
     public static void setUserLocale(Locale locale) {
         userLocale.set(locale);
     }
+    
+    public static void resetUserLocale() {
+        userLocale.remove();
+    }
 
     /**
      * @return the default user locale, defaults to {@link Locale#ROOT}
@@ -105,7 +113,7 @@ public final class LocaleUtil {
      * @return a calendar for the user locale and time zone
      */
     public static Calendar getLocaleCalendar() {
-        return getLocaleCalendar(getUserTimeZone());        
+        return getLocaleCalendar(getUserTimeZone());
     }
 
     /**
@@ -142,7 +150,7 @@ public final class LocaleUtil {
      * @return a calendar for the user locale and time zone
      */
     public static Calendar getLocaleCalendar(TimeZone timeZone) {
-        return Calendar.getInstance(timeZone, getUserLocale());        
+        return Calendar.getInstance(timeZone, getUserLocale());
     }
 }
 

Modified: poi/trunk/src/testcases/org/apache/poi/util/TestLocaleUtil.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/util/TestLocaleUtil.java?rev=1751641&r1=1751640&r2=1751641&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/util/TestLocaleUtil.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/util/TestLocaleUtil.java Wed Jul  6 09:23:34 2016
@@ -19,29 +19,45 @@ package org.apache.poi.util;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assume.assumeFalse;
 
 import java.util.Calendar;
 import java.util.Locale;
 import java.util.TimeZone;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
 
 public class TestLocaleUtil {
+    // This unit test assumes that the user's locale isn't ja-JP and timezone isn't Asia/Tokyo
+    // If this is the case, change the values to something else
     private static final Locale ja_JP = Locale.JAPAN;
     private static final TimeZone TOKYO = TimeZone.getTimeZone("Asia/Tokyo");
     private static final Calendar JAPAN_CALENDAR = Calendar.getInstance(TOKYO, ja_JP);
     
+    /**
+     * Reset the Locale to the user default before the test so that it isn't influenced
+     * by the LocaleUtil's state being changed by previous tests.
+     * 
+     * Reset the Locale to the user default after the test so that it doesn't influence
+     * other tests.
+     */
     @Before
-    public void setUp() {
+    @After
+    @SuppressForbidden("implementation around default locales in POI")
+    public void reset() {
         // clear the user locale and time zone so that tests do not interfere with each other
         // the other way and better way would be to run each test in its own thread since
         // LocaleUtil uses per-thread settings.
         // Helpful, but not ASL 2.0 licensed:
         // http://www.codeaffine.com/2014/07/21/a-junit-rule-to-run-a-test-in-its-own-thread/
-        LocaleUtil.setUserLocale(Locale.GERMANY);
-        LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
+        LocaleUtil.setUserLocale(Locale.getDefault());
+        LocaleUtil.setUserTimeZone(TimeZone.getDefault());
+        
+        assumeFalse(ja_JP.equals(LocaleUtil.getUserLocale()));
+        assumeFalse(TOKYO.equals(LocaleUtil.getUserTimeZone()));
     }
     
     @Test
@@ -54,6 +70,9 @@ public class TestLocaleUtil {
         
         LocaleUtil.setUserLocale(ja_JP);
         assertEquals(ja_JP, LocaleUtil.getUserLocale());
+        
+        LocaleUtil.resetUserLocale();
+        assertEquals(DEFAULT_LOCALE, LocaleUtil.getUserLocale());
     }
     
     @Test
@@ -85,15 +104,18 @@ public class TestLocaleUtil {
         LocaleUtil.setUserLocale(ja_JP);
         LocaleUtil.setUserTimeZone(TOKYO);
         assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar());
+        assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar(TOKYO));
         // FIXME: These might affect the time zone due to daylight savings:
-        //assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar(2016, 01, 01));
-        //assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar(2016, 01, 01,
00, 00, 00));
+        //assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar(2016, 00, 01));
+        //assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar(2016, 00, 01,
00, 00, 00));
     }
     
     private static void assertCalendarNotEquals(Calendar expected, Calendar actual) {
+        // FIXME: add more tests to compare calendars, ignoring whether the dates are equal
         assertNotEquals("time zone", expected.getTimeZone(), actual.getTimeZone());
     }
     private static void assertCalendarEquals(Calendar expected, Calendar actual) {
+        // FIXME: add more tests to compare calendars, ignoring whether the set dates are
equal
         assertEquals("time zone", expected.getTimeZone(), actual.getTimeZone());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message