Skip to content

Commit

Permalink
[IO-605] Add class CanExecuteFileFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Gregory committed Apr 5, 2019
1 parent 94aaf06 commit 62eef61
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.">
Expand Down
Original file line number Diff line number Diff line change
@@ -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 &lt; 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 &lt; 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,22 @@ public void testHidden() throws Exception {
assertFiltering(HiddenFileFilter.VISIBLE, getTestDirectory(), true);
}

@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");
Expand Down

0 comments on commit 62eef61

Please sign in to comment.