Author: mattsicker
Date: Wed Mar 26 03:56:14 2014
New Revision: 1581681

URL: http://svn.apache.org/r1581681
Log:
Add InitialLoggerContext JUnit test rule.

  - Implements LOG4J2-497 in a more direct manner.
  - Will update some unit tests to demonstrate how to use this, too.

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

Added: 
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/InitialLoggerContext.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/InitialLoggerContext.java?rev=1581681&view=auto
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/InitialLoggerContext.java
 (added)
+++ 
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/InitialLoggerContext.java
 Wed Mar 26 03:56:14 2014
@@ -0,0 +1,44 @@
+package org.apache.logging.log4j.junit;
+
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * JUnit {@link TestRule} for constructing a new LoggerContext using a 
specified configuration file.
+ */
+public class InitialLoggerContext implements TestRule {
+
+    private final String configLocation;
+
+    private LoggerContext context;
+
+    public InitialLoggerContext(String configLocation) {
+        this.configLocation = configLocation;
+    }
+
+    @Override
+    public Statement apply(final Statement base, final Description 
description) {
+        return new Statement() {
+            @Override
+            public void evaluate() throws Throwable {
+                context = Configurator.initialize(
+                        description.getDisplayName(),
+                        description.getTestClass().getClassLoader(),
+                        configLocation
+                );
+                try {
+                    base.evaluate();
+                } finally {
+                    Configurator.shutdown(context);
+                }
+            }
+        };
+    }
+
+    public LoggerContext getContext() {
+        return context;
+    }
+}

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


Reply via email to