Author: ggregory
Date: Thu May  8 16:19:29 2014
New Revision: 1593318

URL: http://svn.apache.org/r1593318
Log:
Test order should not matter. When this test class is invoked from Eclipse for 
example, while running all tests in the project, it will fail. This change 
extracts the static block code into a method to be invoked from the test set up 
and tear down methods. Also make sure we do disable everything in the test. The 
refactoring also renames the svar all (all what?) to disableAll. I'm not sure 
why we even need this ivar...

Modified:
    
logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
    
logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java

Modified: 
logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java?rev=1593318&r1=1593317&r2=1593318&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
 Thu May  8 16:19:29 2014
@@ -60,7 +60,7 @@ public final class ThreadContext  {
     private static final String DISABLE_ALL = "disableThreadContext";
     private static final String THREAD_CONTEXT_KEY = "log4j2.threadContextMap";
 
-    private static boolean all;
+    private static boolean disableAll;
     private static boolean useMap;
     private static boolean useStack;
     private static ThreadContextMap contextMap;
@@ -68,12 +68,20 @@ public final class ThreadContext  {
     private static final Logger LOGGER = StatusLogger.getLogger();
 
     static {
+        init();
+    }
+        
+    /**
+     * <em>Consider private, used for testing.</em>
+     */
+    static void init() {
+        contextMap = null;
         final PropertiesUtil managerProps = PropertiesUtil.getProperties();
-        all = managerProps.getBooleanProperty(DISABLE_ALL);
-        useStack = !(managerProps.getBooleanProperty(DISABLE_STACK) || all);
-        contextStack = new DefaultThreadContextStack(useStack);
+        disableAll = managerProps.getBooleanProperty(DISABLE_ALL);
+        useStack = !(managerProps.getBooleanProperty(DISABLE_STACK) || 
disableAll);
+        useMap = !(managerProps.getBooleanProperty(DISABLE_MAP) || disableAll);
 
-        useMap = !(managerProps.getBooleanProperty(DISABLE_MAP) || all);
+        contextStack = new DefaultThreadContextStack(useStack);
         String threadContextMapName = 
managerProps.getStringProperty(THREAD_CONTEXT_KEY);
         final ClassLoader cl = ProviderUtil.findClassLoader();
         if (threadContextMapName != null) {
@@ -116,7 +124,7 @@ public final class ThreadContext  {
     }
 
     private ThreadContext() {
-
+        // empty
     }
 
     /**

Modified: 
logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java?rev=1593318&r1=1593317&r2=1593318&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java
 Thu May  8 16:19:29 2014
@@ -23,18 +23,26 @@ import org.junit.Test;
 import static org.junit.Assert.assertNull;
 
 /**
- *
+ * Tests {@link ThreadContext}.
  */
 public class NoopThreadContextTest {
 
+    private static final String TRUE = "true";
+    private static final String PROPERY_KEY_ALL = "disableThreadContext";
+    private static final String PROPERY_KEY_MAP = "disableThreadContextMap";
+
     @BeforeClass
     public static void before() {
-        System.setProperty("disableThreadContext", "true");
+        System.setProperty(PROPERY_KEY_ALL, TRUE);
+        System.setProperty(PROPERY_KEY_MAP, TRUE);
+        ThreadContext.init();
     }
 
     @AfterClass
     public static void after() {
-        System.clearProperty("disableThreadContext");
+        System.clearProperty(PROPERY_KEY_ALL);
+        System.clearProperty(PROPERY_KEY_MAP);
+        ThreadContext.init();
     }
 
     @Test


Reply via email to