This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 62eef61 [IO-605] Add class CanExecuteFileFilter. 62eef61 is described below commit 62eef61f85e15fddcb54e95a68f270fe3614684b Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Fri Apr 5 14:00:15 2019 -0400 [IO-605] Add class CanExecuteFileFilter. --- src/changes/changes.xml | 3 + .../io/filefilter/CanExecuteFileFilter.java | 78 ++++++++++++++++++++++ .../commons/io/filefilter/FileFilterTestCase.java | 16 +++++ 3 files changed, 97 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7f4563b..9a68de8 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -92,6 +92,9 @@ The <action> type attribute can be add,update,fix,remove. <action issue="IO-604" dev="ggregory" type="fix" due-to="Gary Gregory"> FileUtils.doCopyFile(File, File, boolean) can throw ClosedByInterruptException. </action> + <action issue="IO-605" dev="ggregory" type="add" due-to="Gary Gregory"> + Add class CanExecuteFileFilter. + </action> </release> <release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported."> diff --git a/src/main/java/org/apache/commons/io/filefilter/CanExecuteFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/CanExecuteFileFilter.java new file mode 100644 index 0000000..2aac689 --- /dev/null +++ b/src/main/java/org/apache/commons/io/filefilter/CanExecuteFileFilter.java @@ -0,0 +1,78 @@ +/* + * 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.commons.io.filefilter; + +import java.io.File; +import java.io.Serializable; + +/** + * This filter accepts <code>File</code>s that can be executed. + * <p> + * Example, showing how to print out a list of the + * current directory's <i>executable</i> files: + * + * <pre> + * File dir = new File("."); + * String[] files = dir.list( CanExecuteFileFilter.CAN_EXECUTE ); + * for ( int i = 0; i < files.length; i++ ) { + * System.out.println(files[i]); + * } + * </pre> + * + * <p> + * Example, showing how to print out a list of the + * current directory's <i>un-executable</i> files: + * + * <pre> + * File dir = new File("."); + * String[] files = dir.list( CanExecuteFileFilter.CANNOT_EXECUTE ); + * for ( int i = 0; i < files.length; i++ ) { + * System.out.println(files[i]); + * } + * </pre> + * + * @since 2.7 + * @version $Id$ + */ +public class CanExecuteFileFilter extends AbstractFileFilter implements Serializable { + + private static final long serialVersionUID = 3179904805251622989L; + + /** Singleton instance of <i>executable</i> filter */ + public static final IOFileFilter CAN_EXECUTE = new CanExecuteFileFilter(); + + /** Singleton instance of not <i>executable</i> filter */ + public static final IOFileFilter CANNOT_EXECUTE = new NotFileFilter(CAN_EXECUTE); + + /** + * Restrictive constructor. + */ + protected CanExecuteFileFilter() { + } + + /** + * Checks to see if the file can be executed. + * + * @param file the File to check. + * @return {@code true} if the file can be executed, otherwise {@code false}. + */ + @Override + public boolean accept(final File file) { + return file.canExecute(); + } + +} diff --git a/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java b/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java index 22bcf4c..e9f5889 100644 --- a/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java +++ b/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java @@ -940,6 +940,22 @@ public class FileFilterTestCase { } @Test + public void testCanExecute() throws Exception { + final File executableFile = File.createTempFile(getClass().getSimpleName(), ".temp"); + try { + try (final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(executableFile))) { + TestUtils.generateTestData(output, 32); + } + executableFile.setExecutable(true); + assertFiltering(CanExecuteFileFilter.CAN_EXECUTE, executableFile, true); + executableFile.setExecutable(false); + assertFiltering(CanExecuteFileFilter.CANNOT_EXECUTE, executableFile, false); + } finally { + executableFile.delete(); + } + } + + @Test public void testCanRead() throws Exception { final File readOnlyFile = new File(getTestDirectory(), "read-only-file1.txt"); if (!readOnlyFile.getParentFile().exists()) {