LOG4J2-381 - modify unit test to verify this fix

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/8f8371e7
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/8f8371e7
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/8f8371e7

Branch: refs/heads/master
Commit: 8f8371e72aee06e7f1f86eaf04e31efb6ce9cf46
Parents: 3c84ef9
Author: Ralph Goers <[email protected]>
Authored: Wed Nov 18 23:16:28 2015 -0700
Committer: Ralph Goers <[email protected]>
Committed: Wed Nov 18 23:16:28 2015 -0700

----------------------------------------------------------------------
 .../appender/rolling/CronTriggeringPolicy.java  |  4 ++
 .../rolling/RollingAppenderCronTest.java        | 34 ++++++++++--
 .../src/test/resources/log4j-rolling-cron.xml   |  2 +-
 .../src/test/resources/log4j-rolling-cron2.xml  | 54 ++++++++++++++++++++
 4 files changed, 89 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8f8371e7/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicy.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicy.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicy.java
index b2c4dd7..ccb52a7 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicy.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicy.java
@@ -67,6 +67,10 @@ public final class CronTriggeringPolicy implements 
TriggeringPolicy {
         return false;
     }
 
+    public CronExpression getCronExpression() {
+        return cronExpression;
+    }
+
     /**
      * Creates a ScheduledTriggeringPolicy.
      * @param configuration the Configuration.

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8f8371e7/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCronTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCronTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCronTest.java
index 41ef6a2..dcdb9fd 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCronTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCronTest.java
@@ -17,6 +17,8 @@
 package org.apache.logging.log4j.core.appender.rolling;
 
 import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.appender.RollingFileAppender;
+import org.apache.logging.log4j.core.util.CronExpression;
 import org.apache.logging.log4j.junit.LoggerContextRule;
 import org.hamcrest.Matcher;
 import org.junit.Rule;
@@ -25,13 +27,17 @@ import org.junit.rules.ExternalResource;
 import org.junit.rules.RuleChain;
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
 
 import static org.apache.logging.log4j.hamcrest.Descriptors.that;
 import static org.apache.logging.log4j.hamcrest.FileMatchers.hasName;
 import static org.hamcrest.Matchers.endsWith;
 import static org.hamcrest.Matchers.hasItemInArray;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
 
 /**
  *
@@ -61,15 +67,35 @@ public class RollingAppenderCronTest {
 
         final int MAX_TRIES = 20;
         final Matcher<File[]> hasGzippedFile = 
hasItemInArray(that(hasName(that(endsWith(".gz")))));
+        boolean succeeded = false;
         for (int i = 0; i < MAX_TRIES; i++) {
             final File[] files = dir.listFiles();
             if (hasGzippedFile.matches(files)) {
-                return; // test succeeded
+                succeeded = true;
+                break;
             }
             logger.debug("Adding additional event " + i);
             Thread.sleep(100); // Allow time for rollover to complete
         }
-        fail("No compressed files found");
+        if (!succeeded) {
+            fail("No compressed files found");
+        }
+        Path src = 
FileSystems.getDefault().getPath("target/test-classes/log4j-rolling-cron2.xml");
+        OutputStream os = new 
FileOutputStream("target/test-classes/log4j-rolling-cron.xml");
+        Files.copy(src, os);
+        Thread.sleep(5000);
+        // force a reconfiguration
+        for (int i = 0; i < MAX_TRIES; ++i) {
+            logger.debug("Adding new event {}", i);
+        }
+        Thread.sleep(1000);
+        RollingFileAppender app = (RollingFileAppender) 
ctx.getContext().getConfiguration().getAppender("RollingFile");
+        TriggeringPolicy policy = app.getManager().getTriggeringPolicy();
+        assertNotNull("No triggering policy", policy);
+        assertTrue("Incorrect policy type", policy instanceof 
CronTriggeringPolicy);
+        CronExpression expression = ((CronTriggeringPolicy) 
policy).getCronExpression();
+        assertTrue("Incorrect triggering policy", 
expression.getCronExpression().equals("* * * ? * *"));
+
     }
 
     private static void deleteDir() {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8f8371e7/log4j-core/src/test/resources/log4j-rolling-cron.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/log4j-rolling-cron.xml 
b/log4j-core/src/test/resources/log4j-rolling-cron.xml
index b738a98..9579804 100644
--- a/log4j-core/src/test/resources/log4j-rolling-cron.xml
+++ b/log4j-core/src/test/resources/log4j-rolling-cron.xml
@@ -16,7 +16,7 @@
  limitations under the License.
 
 -->
-<Configuration status="ERROR" name="RollingCronTest">
+<Configuration status="ERROR" name="RollingCronTest" monitorInterval="1">
   <Properties>
     <Property name="filename">target/rolling-cron/rollingtest.log</Property>
   </Properties>

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8f8371e7/log4j-core/src/test/resources/log4j-rolling-cron2.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/log4j-rolling-cron2.xml 
b/log4j-core/src/test/resources/log4j-rolling-cron2.xml
new file mode 100644
index 0000000..a6a17ed
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-rolling-cron2.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<Configuration status="ERROR" name="RollingCronTest" monitorInterval="1">
+  <Properties>
+    <Property name="filename">target/rolling-cron/rollingtest.log</Property>
+  </Properties>
+  <Filters>
+    <ThresholdFilter level="debug"/>
+  </Filters>
+
+  <Appenders>
+    <Console name="STDOUT">
+      <PatternLayout pattern="%m%n"/>
+    </Console>
+    <RollingFile name="RollingFile" fileName="${filename}" 
filePattern="target/rolling-cron/test1-%d{MM-dd-yy-HH-mm-ss}.log.gz">
+      <PatternLayout>
+        <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
+      </PatternLayout>
+      <CronTriggeringPolicy schedule="* * * ? * *"/>
+    </RollingFile>
+    <List name="List">
+      <Filters>
+        <ThresholdFilter level="error"/>
+      </Filters>
+    </List>
+  </Appenders>
+
+  <Loggers>
+    <Logger name="org.apache.logging.log4j.core.appender.rolling" 
level="debug" additivity="false">
+      <AppenderRef ref="RollingFile"/>
+    </Logger>>
+
+    <Root level="error">
+      <AppenderRef ref="STDOUT"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file

Reply via email to