Repository: logging-log4j2 Updated Branches: refs/heads/master a212f6b77 -> cc2b074b1
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b87975b6/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 new file mode 100644 index 0000000..41ef6a2 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCronTest.java @@ -0,0 +1,85 @@ +/* + * 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. + */ +package org.apache.logging.log4j.core.appender.rolling; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.hamcrest.Matcher; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExternalResource; +import org.junit.rules.RuleChain; + +import java.io.File; + +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; + +/** + * + */ +public class RollingAppenderCronTest { + + private static final String CONFIG = "log4j-rolling-cron.xml"; + private static final String DIR = "target/rolling-cron"; + + private final LoggerContextRule ctx = new LoggerContextRule(CONFIG); + + @Rule + public RuleChain chain = RuleChain.outerRule(new ExternalResource() { + @Override + protected void before() throws Throwable { + deleteDir(); + } + }).around(ctx); + + @Test + public void testAppender() throws Exception { + final Logger logger = ctx.getLogger(); + logger.debug("This is test message number 1"); + Thread.sleep(2500); + final File dir = new File(DIR); + assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0); + + final int MAX_TRIES = 20; + final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz"))))); + for (int i = 0; i < MAX_TRIES; i++) { + final File[] files = dir.listFiles(); + if (hasGzippedFile.matches(files)) { + return; // test succeeded + } + logger.debug("Adding additional event " + i); + Thread.sleep(100); // Allow time for rollover to complete + } + fail("No compressed files found"); + } + + private static void deleteDir() { + final File dir = new File(DIR); + if (dir.exists()) { + final File[] files = dir.listFiles(); + for (final File file : files) { + file.delete(); + } + dir.delete(); + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b87975b6/log4j-core/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java new file mode 100644 index 0000000..2e73d9c --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java @@ -0,0 +1,68 @@ +/* + * 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. + */ +package org.apache.logging.log4j.core.util; + +import org.junit.Test; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +import static org.junit.Assert.*; + +/** + * Class Description goes here. + * Created by rgoers on 11/15/15 + */ +public class CronExpressionTest { + + @Test + public void testDayOfMonth() throws Exception { + CronExpression parser = new CronExpression("0 */15,12 7-11,13-17 * * ?"); + Date date = new GregorianCalendar(2015, 11, 2).getTime(); + Date fireDate = parser.getNextValidTimeAfter(date); + Date expected = new GregorianCalendar(2015, 11, 2, 7, 0, 0).getTime(); + assertEquals("Dates not equal.", expected, fireDate); + } + + @Test + public void testDayOfWeek() throws Exception { + CronExpression parser = new CronExpression("0 */15,12 7-11,13-17 ? * Fri"); + Date date = new GregorianCalendar(2015, 11, 2).getTime(); + Date fireDate = parser.getNextValidTimeAfter(date); + Date expected = new GregorianCalendar(2015, 11, 4, 7, 0, 0).getTime(); + assertEquals("Dates not equal.", expected, fireDate); + } + + @Test + public void testNextMonth() throws Exception { + CronExpression parser = new CronExpression("0 */15,12 7-11,13-17 1 * ?"); + Date date = new GregorianCalendar(2015, 11, 2).getTime(); + Date fireDate = parser.getNextValidTimeAfter(date); + Date expected = new GregorianCalendar(2016, 0, 1, 7, 0, 0).getTime(); + assertEquals("Dates not equal.", expected, fireDate); + } + + @Test + public void testLastDayOfMonth() throws Exception { + CronExpression parser = new CronExpression("0 */15,12 7-11,13-17 L * ?"); + Date date = new GregorianCalendar(2015, 10, 2).getTime(); + Date fireDate = parser.getNextValidTimeAfter(date); + Date expected = new GregorianCalendar(2015, 10, 30, 7, 0, 0).getTime(); + assertEquals("Dates not equal.", expected, fireDate); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b87975b6/log4j-core/src/test/java/org/apache/logging/log4j/core/util/WatchManagerTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/WatchManagerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/WatchManagerTest.java index 1c9677a..17fdc0c 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/WatchManagerTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/WatchManagerTest.java @@ -31,6 +31,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import org.apache.logging.log4j.core.config.ConfigurationScheduler; import org.apache.logging.log4j.util.PropertiesUtil; import org.junit.Assume; import org.junit.Test; @@ -49,10 +50,11 @@ public class WatchManagerTest { @Test public void testWatchManager() throws Exception { Assume.assumeFalse(IS_WINDOWS); - ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1); - WatchManager watchManager = new WatchManager(); + ConfigurationScheduler scheduler = new ConfigurationScheduler(); + scheduler.incrementScheduledItems(); + scheduler.start(); + WatchManager watchManager = new WatchManager(scheduler); watchManager.setIntervalSeconds(1); - watchManager.setExecutorService(executorService); watchManager.start(); try { File sourceFile = new File(originalFile); @@ -71,7 +73,7 @@ public class WatchManagerTest { assertNotNull("File change not detected", f); } finally { watchManager.stop(); - executorService.shutdown(); + scheduler.stop(); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b87975b6/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 new file mode 100644 index 0000000..b738a98 --- /dev/null +++ b/log4j-core/src/test/resources/log4j-rolling-cron.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"> + <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
