LOG4J2-435 unit tests for the new actions and path filters Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f14edc22 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f14edc22 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f14edc22
Branch: refs/heads/master Commit: f14edc228f16ced6788765b36c21a2eddb7e1c3d Parents: 7b7bdfa Author: rpopma <[email protected]> Authored: Sun Nov 15 04:23:40 2015 +0900 Committer: rpopma <[email protected]> Committed: Sun Nov 15 04:23:40 2015 +0900 ---------------------------------------------------------------------- .../core/appender/rolling/action/AndTest.java | 41 ++++++ .../rolling/action/DeleteActionTest.java | 101 +++++++++++++++ .../rolling/action/DeletingVisitorTest.java | 120 ++++++++++++++++++ .../rolling/action/DummyFileAttributes.java | 86 +++++++++++++ .../appender/rolling/action/DurationTest.java | 125 +++++++++++++++++++ .../action/FileLastModifiedFilterTest.java | 65 ++++++++++ .../rolling/action/FileNameFilterTest.java | 106 ++++++++++++++++ .../appender/rolling/action/FixedFilter.java | 41 ++++++ .../core/appender/rolling/action/NotTest.java | 39 ++++++ .../core/appender/rolling/action/OrTest.java | 41 ++++++ 10 files changed, 765 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/AndTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/AndTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/AndTest.java new file mode 100644 index 0000000..1f2d826 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/AndTest.java @@ -0,0 +1,41 @@ +/* + * 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.apache.logging.log4j.core.appender.rolling.action.And; +import org.apache.logging.log4j.core.appender.rolling.action.PathFilter; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the And composite filter. + */ +public class AndTest { + + @Test + public void test() { + final PathFilter TRUE = new FixedFilter(true); + final PathFilter FALSE = new FixedFilter(false); + assertTrue(And.createAndFilter(TRUE, TRUE).accept(null, null, null)); + assertFalse(And.createAndFilter(FALSE, TRUE).accept(null, null, null)); + assertFalse(And.createAndFilter(TRUE, FALSE).accept(null, null, null)); + assertFalse(And.createAndFilter(FALSE, FALSE).accept(null, null, null)); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteActionTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteActionTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteActionTest.java new file mode 100644 index 0000000..9be8bda --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteActionTest.java @@ -0,0 +1,101 @@ +/* + * 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 java.nio.file.FileSystems; +import java.nio.file.FileVisitOption; +import java.nio.file.FileVisitor; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumSet; + +import org.apache.logging.log4j.core.BasicConfigurationFactory; +import org.apache.logging.log4j.core.appender.rolling.action.DeleteAction; +import org.apache.logging.log4j.core.appender.rolling.action.DeletingVisitor; +import org.apache.logging.log4j.core.appender.rolling.action.PathFilter; +import org.apache.logging.log4j.core.config.Configuration; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the {@code DeleteAction} class. + */ +public class DeleteActionTest { + + private static DeleteAction createAnyFilter(String path, boolean followLinks, int maxDepth) { + PathFilter[] pathFilters = {new FixedFilter(true)}; + return create(path, followLinks, maxDepth, pathFilters); + } + + private static DeleteAction create(String path, boolean followLinks, int maxDepth, PathFilter[] filters) { + Configuration config = new BasicConfigurationFactory().new BasicConfiguration(); + DeleteAction delete = DeleteAction.createDeleteAction(path, followLinks, maxDepth, filters, config); + return delete; + } + + @Test + public void testGetBasePathResolvesLookups() { + DeleteAction delete = createAnyFilter("${sys:user.home}/a/b/c", false, 1); + + Path actual = delete.getBasePath(); + String expected = System.getProperty("user.home") + "/a/b/c"; + + assertEquals(FileSystems.getDefault().getPath(expected), actual); + } + + @Test + public void testGetBasePathStringReturnsOriginalParam() { + DeleteAction delete = createAnyFilter("${sys:user.home}/a/b/c", false, 1); + assertEquals("${sys:user.home}/a/b/c", delete.getBasePathString()); + } + + @Test + public void testGetMaxDepthReturnsConstructorValue() { + DeleteAction delete = createAnyFilter("any", false, 23); + assertEquals(23, delete.getMaxDepth()); + } + + @Test + public void testGetOptionsReturnsEmptySetIfNotFollowingLinks() { + DeleteAction delete = createAnyFilter("any", false, 0); + assertEquals(Collections.emptySet(), delete.getOptions()); + } + + @Test + public void testGetOptionsReturnsSetWithFollowLinksIfFollowingLinks() { + DeleteAction delete = createAnyFilter("any", true, 0); + assertEquals(EnumSet.of(FileVisitOption.FOLLOW_LINKS), delete.getOptions()); + } + + @Test + public void testGetFiltersReturnsConstructorValue() { + PathFilter[] filters = {new FixedFilter(true), new FixedFilter(false)}; + + DeleteAction delete = create("any", true, 0, filters); + assertEquals(Arrays.asList(filters), delete.getPathFilters()); + } + + @Test + public void testCreateFileVisitorReturnsDeletingVisitor() { + DeleteAction delete = createAnyFilter("any", true, 0); + FileVisitor<Path> visitor = delete.createFileVisitor(delete.getBasePath(), delete.getPathFilters()); + assertTrue(visitor instanceof DeletingVisitor); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeletingVisitorTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeletingVisitorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeletingVisitorTest.java new file mode 100644 index 0000000..f72573e --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeletingVisitorTest.java @@ -0,0 +1,120 @@ +/* + * 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 java.io.IOException; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.logging.log4j.core.appender.rolling.action.DeletingVisitor; +import org.apache.logging.log4j.core.appender.rolling.action.PathFilter; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the {@code DeletingVisitor} class. + */ +public class DeletingVisitorTest { + /** + * Modifies {@code DeletingVisitor} for testing: + * instead of actually deleting a file, it adds the path to a list for later verification. + */ + static class DeletingVisitorHelper extends DeletingVisitor { + List<Path> deleted = new ArrayList<Path>(); + + public DeletingVisitorHelper(final Path basePath, final List<? extends PathFilter> pathFilters) { + super(basePath, pathFilters); + } + + @Override + protected void delete(final Path file) throws IOException { + deleted.add(file); + } + } + + @Test + public void testAcceptedFilesAreDeleted() throws IOException { + Path base = FileSystems.getDefault().getPath("/a/b/c"); + final FixedFilter ACCEPT_ALL = new FixedFilter(true); + DeletingVisitorHelper visitor = new DeletingVisitorHelper(base, Arrays.asList(ACCEPT_ALL)); + + Path any = FileSystems.getDefault().getPath("/a/b/c/any"); + visitor.visitFile(any, null); + assertTrue(visitor.deleted.contains(any)); + } + + @Test + public void testRejectedFilesAreNotDeleted() throws IOException { + Path base = FileSystems.getDefault().getPath("/a/b/c"); + final FixedFilter REJECT_ALL = new FixedFilter(false); + DeletingVisitorHelper visitor = new DeletingVisitorHelper(base, Arrays.asList(REJECT_ALL)); + + Path any = FileSystems.getDefault().getPath("/a/b/c/any"); + visitor.visitFile(any, null); + assertFalse(visitor.deleted.contains(any)); + } + + @Test + public void testAllFiltersMustAcceptOrFileIsNotDeleted() throws IOException { + Path base = FileSystems.getDefault().getPath("/a/b/c"); + final FixedFilter ACCEPT_ALL = new FixedFilter(true); + final FixedFilter REJECT_ALL = new FixedFilter(false); + List<? extends PathFilter> filters = Arrays.asList(ACCEPT_ALL, ACCEPT_ALL, REJECT_ALL); + DeletingVisitorHelper visitor = new DeletingVisitorHelper(base, filters); + + Path any = FileSystems.getDefault().getPath("/a/b/c/any"); + visitor.visitFile(any, null); + assertFalse(visitor.deleted.contains(any)); + } + + @Test + public void testIfAllFiltersAcceptFileIsDeleted() throws IOException { + Path base = FileSystems.getDefault().getPath("/a/b/c"); + final FixedFilter ACCEPT_ALL = new FixedFilter(true); + List<? extends PathFilter> filters = Arrays.asList(ACCEPT_ALL, ACCEPT_ALL, ACCEPT_ALL); + DeletingVisitorHelper visitor = new DeletingVisitorHelper(base, filters); + + Path any = FileSystems.getDefault().getPath("/a/b/c/any"); + visitor.visitFile(any, null); + assertTrue(visitor.deleted.contains(any)); + } + + @Test + public void testVisitFileRelativizesAgainstBase() throws IOException { + + PathFilter filter = new PathFilter() { + + @Override + public boolean accept(Path baseDir, Path relativePath, BasicFileAttributes attrs) { + Path expected = FileSystems.getDefault().getPath("relative"); + assertEquals(expected, relativePath); + return true; + } + }; + Path base = FileSystems.getDefault().getPath("/a/b/c"); + DeletingVisitorHelper visitor = new DeletingVisitorHelper(base, Arrays.asList(filter)); + + Path child = FileSystems.getDefault().getPath("/a/b/c/relative"); + visitor.visitFile(child, null); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DummyFileAttributes.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DummyFileAttributes.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DummyFileAttributes.java new file mode 100644 index 0000000..81e316f --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DummyFileAttributes.java @@ -0,0 +1,86 @@ +/* + * 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 java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileTime; + +/** + * Test helper class: file attributes. + */ +public class DummyFileAttributes implements BasicFileAttributes { + + public FileTime lastModified; + public FileTime lastAccessTime; + public FileTime creationTime; + public boolean isRegularFile; + public boolean isDirectory; + public boolean isSymbolicLink; + public boolean isOther; + public long size; + public Object fileKey; + + public DummyFileAttributes() { + } + + @Override + public FileTime lastModifiedTime() { + return lastModified; + } + + @Override + public FileTime lastAccessTime() { + return lastAccessTime; + } + + @Override + public FileTime creationTime() { + return creationTime; + } + + @Override + public boolean isRegularFile() { + return isRegularFile; + } + + @Override + public boolean isDirectory() { + return isDirectory; + } + + @Override + public boolean isSymbolicLink() { + return isSymbolicLink; + } + + @Override + public boolean isOther() { + return isOther; + } + + @Override + public long size() { + return size; + } + + @Override + public Object fileKey() { + return fileKey; + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DurationTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DurationTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DurationTest.java new file mode 100644 index 0000000..61d3ccd --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DurationTest.java @@ -0,0 +1,125 @@ +/* + * 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.apache.logging.log4j.core.appender.rolling.action.Duration; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the Duration class. + */ +public class DurationTest { + + @Test(expected = NullPointerException.class) + public void testParseFailsIfNullText() { + Duration.parse(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseFailsIfInvalidPattern() { + Duration.parse("abc"); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseFailsIfSectionsOutOfOrder() { + Duration.parse("P4DT2M1S3H"); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseFailsIfMissingTForTime() { + Duration.parse("P1S"); + } + + @Test + public void testParseIsCaseInsensitive() { + assertEquals("P4DT3H2M1S", Duration.parse("p4dt3h2m1s").toString()); + } + + @Test + public void testParseAllowsOverflows() { + assertEquals(1000 * 70, Duration.parse("PT70S").toMillis()); + assertEquals(1000 * 70 * 60, Duration.parse("PT70M").toMillis()); + assertEquals(1000 * 25 * 60 * 60, Duration.parse("PT25H").toMillis()); + } + + @Test + public void testToMillis() { + assertEquals(0, Duration.ZERO.toMillis()); + assertEquals(1000, Duration.parse("PT1S").toMillis()); + assertEquals(1000 * 2 * 60, Duration.parse("PT2M").toMillis()); + assertEquals(1000 * 3 * 60 * 60, Duration.parse("PT3H").toMillis()); + assertEquals(1000 * 4 * 24 * 60 * 60, Duration.parse("P4D").toMillis()); + long expected = (1000 * 4 * 24 * 60 * 60) + (1000 * 3 * 60 * 60) + (1000 * 2 * 60) + 1000; + assertEquals(expected, Duration.parse("P4DT3H2M1S").toMillis()); + } + + @Test + public void testToString() { + assertEquals("PT0S", Duration.ZERO.toString()); + assertEquals("PT1S", Duration.parse("PT1S").toString()); + assertEquals("PT2M1S", Duration.parse("PT2M1S").toString()); + assertEquals("PT3H2M1S", Duration.parse("PT3H2M1S").toString()); + assertEquals("P4DT3H2M1S", Duration.parse("P4DT3H2M1S").toString()); + } + + @Test + public void testCompareTo() { + assertEquals(-1, Duration.parse("PT1S").compareTo(Duration.parse("PT2S"))); + assertEquals(-1, Duration.parse("PT1M").compareTo(Duration.parse("PT2M"))); + assertEquals(-1, Duration.parse("PT1H").compareTo(Duration.parse("PT2H"))); + assertEquals(-1, Duration.parse("P1D").compareTo(Duration.parse("P2D"))); + + assertEquals(0, Duration.parse("PT1S").compareTo(Duration.parse("PT1S"))); + assertEquals(0, Duration.parse("PT1M").compareTo(Duration.parse("PT1M"))); + assertEquals(0, Duration.parse("PT1H").compareTo(Duration.parse("PT1H"))); + assertEquals(0, Duration.parse("P1D").compareTo(Duration.parse("P1D"))); + + assertEquals(1, Duration.parse("PT2S").compareTo(Duration.parse("PT1S"))); + assertEquals(1, Duration.parse("PT2M").compareTo(Duration.parse("PT1M"))); + assertEquals(1, Duration.parse("PT2H").compareTo(Duration.parse("PT1H"))); + assertEquals(1, Duration.parse("P2D").compareTo(Duration.parse("P1D"))); + + assertEquals(0, Duration.parse("PT1M").compareTo(Duration.parse("PT60S"))); + assertEquals(0, Duration.parse("PT1H").compareTo(Duration.parse("PT60M"))); + assertEquals(0, Duration.parse("PT1H").compareTo(Duration.parse("PT3600S"))); + assertEquals(0, Duration.parse("P1D").compareTo(Duration.parse("PT24H"))); + assertEquals(0, Duration.parse("P1D").compareTo(Duration.parse("PT1440M"))); + } + + @Test + public void testEquals() { + assertNotEquals(Duration.parse("PT1S"),(Duration.parse("PT2S"))); + assertNotEquals(Duration.parse("PT1M"),(Duration.parse("PT2M"))); + assertNotEquals(Duration.parse("PT1H"),(Duration.parse("PT2H"))); + assertNotEquals(Duration.parse("P1D"),(Duration.parse("P2D"))); + + assertEquals( Duration.parse("PT1S"),(Duration.parse("PT1S"))); + assertEquals( Duration.parse("PT1M"),(Duration.parse("PT1M"))); + assertEquals( Duration.parse("PT1H"),(Duration.parse("PT1H"))); + assertEquals( Duration.parse("P1D"),(Duration.parse("P1D"))); + + assertEquals( Duration.parse("PT1M"),(Duration.parse("PT60S"))); + assertEquals( Duration.parse("PT1H"),(Duration.parse("PT60M"))); + assertEquals( Duration.parse("PT1H"),(Duration.parse("PT3600S"))); + assertEquals( Duration.parse("P1D"),(Duration.parse("PT24H"))); + assertEquals(Duration.parse("P1D"), (Duration.parse("PT1440M"))); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileLastModifiedFilterTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileLastModifiedFilterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileLastModifiedFilterTest.java new file mode 100644 index 0000000..28a3ba8 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileLastModifiedFilterTest.java @@ -0,0 +1,65 @@ +/* + * 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 java.nio.file.attribute.FileTime; + +import org.apache.logging.log4j.core.appender.rolling.action.Duration; +import org.apache.logging.log4j.core.appender.rolling.action.FileLastModifiedFilter; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the FileAgeFilter class. + */ +public class FileLastModifiedFilterTest { + + @Test + public void testGetDurationReturnsConstructorValue() { + FileLastModifiedFilter filter = FileLastModifiedFilter.createAgeFilter(Duration.parse("P7D")); + assertEquals(0, filter.getDuration().compareTo(Duration.parse("P7D"))); + } + + @Test + public void testAcceptsIfFileAgeEqualToDuration() { + FileLastModifiedFilter filter = FileLastModifiedFilter.createAgeFilter(Duration.parse("PT33S")); + DummyFileAttributes attrs = new DummyFileAttributes(); + final long age = 33 * 1000; + attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age); + assertTrue(filter.accept(null, null, attrs)); + } + + @Test + public void testAcceptsIfFileAgeExceedsDuration() { + FileLastModifiedFilter filter = FileLastModifiedFilter.createAgeFilter(Duration.parse("PT33S")); + DummyFileAttributes attrs = new DummyFileAttributes(); + final long age = 33 * 1000 + 5; + attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age); + assertTrue(filter.accept(null, null, attrs)); + } + + @Test + public void testDoesNotAcceptIfFileAgeLessThanDuration() { + FileLastModifiedFilter filter = FileLastModifiedFilter.createAgeFilter(Duration.parse("PT33S")); + DummyFileAttributes attrs = new DummyFileAttributes(); + final long age = 33 * 1000 - 5; + attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age); + assertFalse(filter.accept(null, null, attrs)); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilterTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilterTest.java new file mode 100644 index 0000000..45469a4 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilterTest.java @@ -0,0 +1,106 @@ +/* + * 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 java.nio.file.FileSystems; +import java.nio.file.Path; + +import org.apache.logging.log4j.core.appender.rolling.action.FileNameFilter; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class FileNameFilterTest { + + @Test(expected = IllegalArgumentException.class) + public void testCreateNameFilterFailsIfBothRegexAndPathAreNull() { + FileNameFilter.createNameFilter(null, null); + } + + @Test() + public void testCreateNameFilterAcceptsIfEitherRegexOrPathOrBothAreNonNull() { + FileNameFilter.createNameFilter("bar", null); + FileNameFilter.createNameFilter(null, "foo"); + FileNameFilter.createNameFilter("bar", "foo"); + } + + @Test + public void testGetRegexReturnsConstructorValue() { + assertEquals("bar", FileNameFilter.createNameFilter(null, "bar").getRegex().pattern()); + assertEquals(null, FileNameFilter.createNameFilter("path", null).getRegex()); + } + + @Test + public void testGetPathReturnsConstructorValue() { + assertEquals("path", FileNameFilter.createNameFilter("path", null).getPathPattern()); + assertEquals(null, FileNameFilter.createNameFilter(null, "bar").getPathPattern()); + } + + @Test + public void testAcceptUsesPathPatternIfExists() { + final FileNameFilter filter = FileNameFilter.createNameFilter("path", "regex"); + final Path relativePath = FileSystems.getDefault().getPath("path"); + assertTrue(filter.accept(null, relativePath, null)); + + final Path pathMatchingRegex = FileSystems.getDefault().getPath("regex"); + assertFalse(filter.accept(null, pathMatchingRegex, null)); + } + + @Test + public void testAcceptUsesRegexIfNoPathPatternExists() { + final FileNameFilter regexFilter = FileNameFilter.createNameFilter(null, "regex"); + final Path pathMatchingRegex = FileSystems.getDefault().getPath("regex"); + assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); + + final Path noMatch = FileSystems.getDefault().getPath("nomatch"); + assertFalse(regexFilter.accept(null, noMatch, null)); + } + + @Test + public void testAcceptIgnoresBasePathAndAttributes() { + final FileNameFilter pathFilter = FileNameFilter.createNameFilter("path", null); + final Path relativePath = FileSystems.getDefault().getPath("path"); + assertTrue(pathFilter.accept(null, relativePath, null)); + + final FileNameFilter regexFilter = FileNameFilter.createNameFilter(null, "regex"); + final Path pathMatchingRegex = FileSystems.getDefault().getPath("regex"); + assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); + } + + @Test + public void testIsMatch() { + assertTrue(FileNameFilter.isMatch("abc", "???")); + assertTrue(FileNameFilter.isMatch("abc", "a??")); + assertTrue(FileNameFilter.isMatch("abc", "?b?")); + assertTrue(FileNameFilter.isMatch("abc", "??c")); + assertTrue(FileNameFilter.isMatch("abc", "ab?")); + assertTrue(FileNameFilter.isMatch("abc", "?bc")); + assertTrue(FileNameFilter.isMatch("abc", "*")); + assertTrue(FileNameFilter.isMatch("abc", "*bc")); + assertTrue(FileNameFilter.isMatch("abc", "*c")); + assertTrue(FileNameFilter.isMatch("abc", "*c*")); + assertTrue(FileNameFilter.isMatch("abc", "a*")); + assertTrue(FileNameFilter.isMatch("abc", "*a*")); + assertTrue(FileNameFilter.isMatch("abc", "*b*")); + + assertFalse(FileNameFilter.isMatch("abc", "????")); + assertFalse(FileNameFilter.isMatch("abc", "b*")); + assertFalse(FileNameFilter.isMatch("abc", "*b")); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FixedFilter.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FixedFilter.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FixedFilter.java new file mode 100644 index 0000000..b86f067 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FixedFilter.java @@ -0,0 +1,41 @@ +/* + * 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 java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; + +import org.apache.logging.log4j.core.appender.rolling.action.PathFilter; + +/** + * Test helper class. + */ +public class FixedFilter implements PathFilter { + + private final boolean accept; + + public FixedFilter(boolean accept) { + this.accept = accept; + } + + @Override + public boolean accept(Path baseDir, Path path, BasicFileAttributes attrs) { + return accept; + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/NotTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/NotTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/NotTest.java new file mode 100644 index 0000000..ce50dd4 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/NotTest.java @@ -0,0 +1,39 @@ +/* + * 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.apache.logging.log4j.core.appender.rolling.action.Not; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the Not composite filter. + */ +public class NotTest { + + @Test + public void test() { + assertTrue(new FixedFilter(true).accept(null, null, null)); + assertFalse(Not.createNotFilter(new FixedFilter(true)).accept(null, null, null)); + + assertFalse(new FixedFilter(false).accept(null, null, null)); + assertTrue(Not.createNotFilter(new FixedFilter(false)).accept(null, null, null)); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f14edc22/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/OrTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/OrTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/OrTest.java new file mode 100644 index 0000000..fa9c419 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/OrTest.java @@ -0,0 +1,41 @@ +/* + * 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.apache.logging.log4j.core.appender.rolling.action.Or; +import org.apache.logging.log4j.core.appender.rolling.action.PathFilter; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the Or composite filter. + */ +public class OrTest { + + @Test + public void test() { + final PathFilter TRUE = new FixedFilter(true); + final PathFilter FALSE = new FixedFilter(false); + assertTrue(Or.createOrFilter(TRUE, TRUE).accept(null, null, null)); + assertTrue(Or.createOrFilter(FALSE, TRUE).accept(null, null, null)); + assertTrue(Or.createOrFilter(TRUE, FALSE).accept(null, null, null)); + assertFalse(Or.createOrFilter(FALSE, FALSE).accept(null, null, null)); + } + +}
