LOG4J2-435 added test for IfAccumulatedFileSize Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/8152f193 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/8152f193 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/8152f193
Branch: refs/heads/master Commit: 8152f19351f19ec51cf047b79b80543fb7317120 Parents: f496c0a Author: rpopma <[email protected]> Authored: Fri Nov 27 00:29:36 2015 +0900 Committer: rpopma <[email protected]> Committed: Fri Nov 27 00:29:36 2015 +0900 ---------------------------------------------------------------------- .../rolling/action/IfAccumulatedFileSize.java | 2 +- .../action/IfAccumulatedFileSizeTest.java | 94 ++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8152f193/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java index 1820ec9..c1378e6 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java @@ -79,7 +79,7 @@ public final class IfAccumulatedFileSize implements PathCondition { * @return An IfAccumulatedFileSize condition. */ @PluginFactory - public static IfAccumulatedFileSize createAgeCondition( // + public static IfAccumulatedFileSize createFileSizeCondition( // @PluginAttribute("exceeds") final String size) { if (size == null) { LOGGER.error("IfAccumulatedFileSize missing mandatory size threshold."); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8152f193/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java new file mode 100644 index 0000000..d756a95 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSizeTest.java @@ -0,0 +1,94 @@ +/* + * 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.action; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the IfAccumulatedFileSize class. + */ +public class IfAccumulatedFileSizeTest { + + @Test + public void testGetThresholdBytes() { + assertEquals(2, create("2B").getThresholdBytes()); + assertEquals(3, create("3 B").getThresholdBytes()); + assertEquals(2 * 1024, create("2KB").getThresholdBytes()); + assertEquals(3 * 1024, create("3 KB").getThresholdBytes()); + assertEquals(2 * 1024 * 1024, create("2MB").getThresholdBytes()); + assertEquals(3 * 1024 * 1024, create("3 MB").getThresholdBytes()); + assertEquals(2L * 1024 * 1024 * 1024, create("2GB").getThresholdBytes()); + assertEquals(3L * 1024 * 1024 * 1024, create("3 GB").getThresholdBytes()); + } + + private static IfAccumulatedFileSize create(final String size) { + return IfAccumulatedFileSize.createFileSizeCondition(size); + } + + @Test + public void testNotAcceptOnExactMatch() { + String[] sizes = {"2KB", "3MB", "4GB"}; + for (String size : sizes) { + IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition(size); + DummyFileAttributes attribs = new DummyFileAttributes(); + attribs.size = condition.getThresholdBytes(); + assertFalse(condition.accept(null, null, attribs)); + } + } + + @Test + public void testAcceptIfExceedThreshold() { + String[] sizes = {"2KB", "3MB", "4GB"}; + for (String size : sizes) { + IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition(size); + DummyFileAttributes attribs = new DummyFileAttributes(); + attribs.size = condition.getThresholdBytes() + 1; + assertTrue(condition.accept(null, null, attribs)); + } + } + + @Test + public void testNotAcceptIfBelowThreshold() { + String[] sizes = {"2KB", "3MB", "4GB"}; + for (String size : sizes) { + IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition(size); + DummyFileAttributes attribs = new DummyFileAttributes(); + attribs.size = condition.getThresholdBytes() - 1; + assertFalse(condition.accept(null, null, attribs)); + } + } + + @Test + public void testAcceptOnceThresholdExceeded() { + DummyFileAttributes attribs = new DummyFileAttributes(); + String[] sizes = {"2KB", "3MB", "4GB"}; + for (String size : sizes) { + IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition(size); + long quarter = condition.getThresholdBytes() / 4; + attribs.size = quarter; + assertFalse(condition.accept(null, null, attribs)); + assertFalse(condition.accept(null, null, attribs)); + assertFalse(condition.accept(null, null, attribs)); + assertFalse(condition.accept(null, null, attribs)); + assertTrue(condition.accept(null, null, attribs)); + } + } + +}
