Duplicate Code refactoring
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/58a6a773 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/58a6a773 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/58a6a773 Branch: refs/heads/master Commit: 58a6a7739fa41e8f665ba60a97f3ae16909bcfb7 Parents: 3e56a75 Author: prabh.simran <[email protected]> Authored: Wed Sep 28 11:12:45 2016 +0400 Committer: Mikael Ståldal <[email protected]> Committed: Sat Jul 8 21:32:47 2017 +0200 ---------------------------------------------------------------------- .../log4j/ThreadContextInheritanceTest.java | 72 ++----------- .../apache/logging/log4j/ThreadContextTest.java | 68 ++----------- .../org/apache/logging/log4j/UtilityClass.java | 100 +++++++++++++++++++ 3 files changed, 118 insertions(+), 122 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/58a6a773/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextInheritanceTest.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextInheritanceTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextInheritanceTest.java index eff9b05..474725c 100644 --- a/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextInheritanceTest.java +++ b/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextInheritanceTest.java @@ -29,13 +29,13 @@ import static org.junit.Assert.*; * Tests {@link ThreadContext}. */ public class ThreadContextInheritanceTest { - + @BeforeClass public static void setupClass() { System.setProperty(DefaultThreadContextMap.INHERITABLE_MAP, "true"); ThreadContext.init(); } - + @AfterClass public static void tearDownClass() { System.clearProperty(DefaultThreadContextMap.INHERITABLE_MAP); @@ -80,94 +80,42 @@ public class ThreadContextInheritanceTest { @Test public void perfTest() throws Exception { - ThreadContext.clearMap(); - final Timer complete = new Timer("ThreadContextTest"); - complete.start(); - ThreadContext.put("Var1", "value 1"); - ThreadContext.put("Var2", "value 2"); - ThreadContext.put("Var3", "value 3"); - ThreadContext.put("Var4", "value 4"); - ThreadContext.put("Var5", "value 5"); - ThreadContext.put("Var6", "value 6"); - ThreadContext.put("Var7", "value 7"); - ThreadContext.put("Var8", "value 8"); - ThreadContext.put("Var9", "value 9"); - ThreadContext.put("Var10", "value 10"); - final int loopCount = 1000000; - final Timer timer = new Timer("ThreadContextCopy", loopCount); - timer.start(); - for (int i = 0; i < loopCount; ++i) { - final Map<String, String> map = ThreadContext.getImmutableContext(); - assertNotNull(map); - } - timer.stop(); - complete.stop(); - System.out.println(timer.toString()); - System.out.println(complete.toString()); + UtilityClass.perfTest(); } @Test public void testGetContextReturnsEmptyMapIfEmpty() { - ThreadContext.clearMap(); - assertTrue(ThreadContext.getContext().isEmpty()); + UtilityClass.testGetContextReturnsEmptyMapIfEmpty(); } @Test public void testGetContextReturnsMutableCopy() { - ThreadContext.clearMap(); - final Map<String, String> map1 = ThreadContext.getContext(); - assertTrue(map1.isEmpty()); - map1.put("K", "val"); // no error - assertEquals("val", map1.get("K")); - - // adding to copy does not affect thread context map - assertTrue(ThreadContext.getContext().isEmpty()); - - ThreadContext.put("key", "val2"); - final Map<String, String> map2 = ThreadContext.getContext(); - assertEquals(1, map2.size()); - assertEquals("val2", map2.get("key")); - map2.put("K", "val"); // no error - assertEquals("val", map2.get("K")); - - // first copy is not affected - assertNotSame(map1, map2); - assertEquals(1, map1.size()); + UtilityClass.testGetContextReturnsMutableCopy(); } @Test public void testGetImmutableContextReturnsEmptyMapIfEmpty() { - ThreadContext.clearMap(); - assertTrue(ThreadContext.getImmutableContext().isEmpty()); + UtilityClass.testGetImmutableContextReturnsEmptyMapIfEmpty(); } @Test(expected = UnsupportedOperationException.class) public void testGetImmutableContextReturnsImmutableMapIfNonEmpty() { - ThreadContext.clearMap(); - ThreadContext.put("key", "val"); - final Map<String, String> immutable = ThreadContext.getImmutableContext(); - immutable.put("otherkey", "otherval"); + UtilityClass.testGetImmutableContextReturnsImmutableMapIfNonEmpty(); } @Test(expected = UnsupportedOperationException.class) public void testGetImmutableContextReturnsImmutableMapIfEmpty() { - ThreadContext.clearMap(); - final Map<String, String> immutable = ThreadContext.getImmutableContext(); - immutable.put("otherkey", "otherval"); + UtilityClass.testGetImmutableContextReturnsImmutableMapIfEmpty(); } @Test public void testGetImmutableStackReturnsEmptyStackIfEmpty() { - ThreadContext.clearStack(); - assertTrue(ThreadContext.getImmutableStack().asList().isEmpty()); + UtilityClass.testGetImmutableStackReturnsEmptyStackIfEmpty(); } @Test public void testPut() { - ThreadContext.clearMap(); - assertNull(ThreadContext.get("testKey")); - ThreadContext.put("testKey", "testValue"); - assertEquals("testValue", ThreadContext.get("testKey")); + UtilityClass.testPut(); } @Test http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/58a6a773/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java index e0731b3..45aa098 100644 --- a/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java +++ b/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java @@ -62,94 +62,42 @@ public class ThreadContextTest { @Test public void perfTest() throws Exception { - ThreadContext.clearMap(); - final Timer complete = new Timer("ThreadContextTest"); - complete.start(); - ThreadContext.put("Var1", "value 1"); - ThreadContext.put("Var2", "value 2"); - ThreadContext.put("Var3", "value 3"); - ThreadContext.put("Var4", "value 4"); - ThreadContext.put("Var5", "value 5"); - ThreadContext.put("Var6", "value 6"); - ThreadContext.put("Var7", "value 7"); - ThreadContext.put("Var8", "value 8"); - ThreadContext.put("Var9", "value 9"); - ThreadContext.put("Var10", "value 10"); - final int loopCount = 1000000; - final Timer timer = new Timer("ThreadContextCopy", loopCount); - timer.start(); - for (int i = 0; i < loopCount; ++i) { - final Map<String, String> map = ThreadContext.getImmutableContext(); - assertNotNull(map); - } - timer.stop(); - complete.stop(); - System.out.println(timer.toString()); - System.out.println(complete.toString()); + UtilityClass.perfTest(); } @Test public void testGetContextReturnsEmptyMapIfEmpty() { - ThreadContext.clearMap(); - assertTrue(ThreadContext.getContext().isEmpty()); + UtilityClass.testGetContextReturnsEmptyMapIfEmpty(); } @Test public void testGetContextReturnsMutableCopy() { - ThreadContext.clearMap(); - final Map<String, String> map1 = ThreadContext.getContext(); - assertTrue(map1.isEmpty()); - map1.put("K", "val"); // no error - assertEquals("val", map1.get("K")); - - // adding to copy does not affect thread context map - assertTrue(ThreadContext.getContext().isEmpty()); - - ThreadContext.put("key", "val2"); - final Map<String, String> map2 = ThreadContext.getContext(); - assertEquals(1, map2.size()); - assertEquals("val2", map2.get("key")); - map2.put("K", "val"); // no error - assertEquals("val", map2.get("K")); - - // first copy is not affected - assertNotSame(map1, map2); - assertEquals(1, map1.size()); + UtilityClass.testGetContextReturnsMutableCopy(); } @Test public void testGetImmutableContextReturnsEmptyMapIfEmpty() { - ThreadContext.clearMap(); - assertTrue(ThreadContext.getImmutableContext().isEmpty()); + UtilityClass.testGetImmutableContextReturnsEmptyMapIfEmpty(); } @Test(expected = UnsupportedOperationException.class) public void testGetImmutableContextReturnsImmutableMapIfNonEmpty() { - ThreadContext.clearMap(); - ThreadContext.put("key", "val"); - final Map<String, String> immutable = ThreadContext.getImmutableContext(); - immutable.put("otherkey", "otherval"); + UtilityClass.testGetImmutableContextReturnsImmutableMapIfNonEmpty(); } @Test(expected = UnsupportedOperationException.class) public void testGetImmutableContextReturnsImmutableMapIfEmpty() { - ThreadContext.clearMap(); - final Map<String, String> immutable = ThreadContext.getImmutableContext(); - immutable.put("otherkey", "otherval"); + UtilityClass.testGetImmutableContextReturnsImmutableMapIfEmpty(); } @Test public void testGetImmutableStackReturnsEmptyStackIfEmpty() { - ThreadContext.clearStack(); - assertTrue(ThreadContext.getImmutableStack().asList().isEmpty()); + UtilityClass.testGetImmutableStackReturnsEmptyStackIfEmpty(); } @Test public void testPut() { - ThreadContext.clearMap(); - assertNull(ThreadContext.get("testKey")); - ThreadContext.put("testKey", "testValue"); - assertEquals("testValue", ThreadContext.get("testKey")); + UtilityClass.testPut(); } @Test http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/58a6a773/log4j-api/src/test/java/org/apache/logging/log4j/UtilityClass.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/UtilityClass.java b/log4j-api/src/test/java/org/apache/logging/log4j/UtilityClass.java new file mode 100644 index 0000000..4ba7624 --- /dev/null +++ b/log4j-api/src/test/java/org/apache/logging/log4j/UtilityClass.java @@ -0,0 +1,100 @@ +package org.apache.logging.log4j; + +import java.util.Map; + +import org.apache.logging.log4j.Timer; +import org.apache.logging.log4j.spi.DefaultThreadContextMap; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.Assert.*; +import org.apache.logging.log4j.ThreadContext; +import static org.junit.Assert.*; + + + +public class UtilityClass { + + public static void perfTest() throws Exception { + ThreadContext.clearMap(); + final Timer complete = new Timer("ThreadContextTest"); + complete.start(); + ThreadContext.put("Var1", "value 1"); + ThreadContext.put("Var2", "value 2"); + ThreadContext.put("Var3", "value 3"); + ThreadContext.put("Var4", "value 4"); + ThreadContext.put("Var5", "value 5"); + ThreadContext.put("Var6", "value 6"); + ThreadContext.put("Var7", "value 7"); + ThreadContext.put("Var8", "value 8"); + ThreadContext.put("Var9", "value 9"); + ThreadContext.put("Var10", "value 10"); + final int loopCount = 1000000; + final Timer timer = new Timer("ThreadContextCopy", loopCount); + timer.start(); + for (int i = 0; i < loopCount; ++i) { + final Map<String, String> map = ThreadContext.getImmutableContext(); + assertNotNull(map); + } + timer.stop(); + complete.stop(); + System.out.println(timer.toString()); + System.out.println(complete.toString()); + } + + + public static void testGetContextReturnsEmptyMapIfEmpty() { + ThreadContext.clearMap(); + assertTrue(ThreadContext.getContext().isEmpty()); + } + + + public static void testGetContextReturnsMutableCopy() { + ThreadContext.clearMap(); + final Map<String, String> map1 = ThreadContext.getContext(); + assertTrue(map1.isEmpty()); + map1.put("K", "val"); + assertEquals("val", map1.get("K")); + assertTrue(ThreadContext.getContext().isEmpty()); + ThreadContext.put("key", "val2"); + final Map<String, String> map2 = ThreadContext.getContext(); + assertEquals(1, map2.size()); + assertEquals("val2", map2.get("key")); + map2.put("K", "val"); + assertEquals("val", map2.get("K")); + assertNotSame(map1, map2); + assertEquals(1, map1.size()); + } + + public static void testGetImmutableContextReturnsEmptyMapIfEmpty() { + ThreadContext.clearMap(); + assertTrue(ThreadContext.getImmutableContext().isEmpty()); + } + + + public static void testGetImmutableContextReturnsImmutableMapIfNonEmpty() { + ThreadContext.clearMap(); + ThreadContext.put("key", "val"); + final Map<String, String> immutable = ThreadContext.getImmutableContext(); + immutable.put("otherkey", "otherval"); + } + + public static void testGetImmutableContextReturnsImmutableMapIfEmpty() { + ThreadContext.clearMap(); + final Map<String, String> immutable = ThreadContext.getImmutableContext(); + immutable.put("otherkey", "otherval"); + } + + public static void testGetImmutableStackReturnsEmptyStackIfEmpty() { + ThreadContext.clearStack(); + assertTrue(ThreadContext.getImmutableStack().asList().isEmpty()); + } + + + public static void testPut() { + ThreadContext.clearMap(); + assertNull(ThreadContext.get("testKey")); + ThreadContext.put("testKey", "testValue"); + assertEquals("testValue", ThreadContext.get("testKey")); + } +}
