logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattsic...@apache.org
Subject svn commit: r1582153 - in /logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j: core/config/FileOutputTest.java junit/CleanFiles.java
Date Thu, 27 Mar 2014 02:26:09 GMT
Author: mattsicker
Date: Thu Mar 27 02:26:09 2014
New Revision: 1582153

URL: http://svn.apache.org/r1582153
Log:
Update test to use InitialLoggerContext rule.

  - Also added a CleanFiles rule. It does what it sounds like.

Added:
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java
  (with props)
Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java?rev=1582153&r1=1582152&r2=1582153&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java
Thu Mar 27 02:26:09 2014
@@ -18,13 +18,11 @@ package org.apache.logging.log4j.core.co
 
 import java.io.File;
 
-import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.xml.XMLConfiguration;
-import org.apache.logging.log4j.status.StatusLogger;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.apache.logging.log4j.junit.CleanFiles;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.junit.*;
 
 import static org.junit.Assert.assertTrue;
 
@@ -36,12 +34,15 @@ public class FileOutputTest {
     private static final String CONFIG = "log4j-filetest.xml";
     private static final String STATUS_LOG = "target/status.log";
 
-    @BeforeClass
-    public static void setupClass() {
-        final File file = new File(STATUS_LOG);
-        file.delete();
-        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
-        final LoggerContext ctx = (LoggerContext) LogManager.getContext();
+    @Rule
+    public CleanFiles cleanFiles = new CleanFiles(STATUS_LOG);
+
+    @Rule
+    public InitialLoggerContext init = new InitialLoggerContext(CONFIG);
+
+    @Before
+    public void setupClass() {
+        final LoggerContext ctx = this.init.getContext();
         final Configuration config = ctx.getConfiguration();
         if (config instanceof XMLConfiguration) {
             final String name = config.getName();
@@ -53,16 +54,6 @@ public class FileOutputTest {
         }
     }
 
-    @AfterClass
-    public static void cleanupClass() {
-        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        final LoggerContext ctx = (LoggerContext) LogManager.getContext();
-        ctx.reconfigure();
-        StatusLogger.getLogger().reset();
-        final File file = new File(STATUS_LOG);
-        file.delete();
-    }
-
     @Test
     public void testConfig() {
         final File file = new File(STATUS_LOG);

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java?rev=1582153&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java
Thu Mar 27 02:26:09 2014
@@ -0,0 +1,50 @@
+package org.apache.logging.log4j.junit;
+
+import org.junit.rules.ExternalResource;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * A JUnit test rule to automatically delete certain files before and after a test is run.
+ */
+public class CleanFiles extends ExternalResource {
+    private final List<File> files;
+
+    public CleanFiles(final File... files) {
+        this.files = Arrays.asList(files);
+    }
+
+    public CleanFiles(final String... fileNames) {
+        this.files = new ArrayList<File>(fileNames.length);
+        for (final String fileName : fileNames) {
+            this.files.add(new File(fileName));
+        }
+    }
+
+    private void clean() {
+        for (final File file : files) {
+            delete(file);
+        }
+    }
+
+    private static void delete(final File file) {
+        if (file.exists()) {
+            assertTrue(file.delete());
+        }
+    }
+
+    @Override
+    protected void before() throws Throwable {
+        this.clean();
+    }
+
+    @Override
+    protected void after() {
+        this.clean();
+    }
+}

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message