RegexFileFilter is no longer Serializable.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -171,6 +171,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-768" dev="ggregory" type="fix" due-to="Marcono1234, Michael Osipov">
Add reserved Windows file names CONIN$ and CONOUT$ to FileSystem #355.
</action>
+ <action issue="IO-773" dev="ggregory" type="fix" due-to="Dominik Reinarz, Gary Gregory">
+ RegexFileFilter is no longer Serializable.
+ </action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">
Add GitHub coverage.yml.

MODIFY
src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java
src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java
@@ -104,8 +104,9 @@ private static int toFlags(final IOCase ioCase) {
* @param pattern regular expression to match.
* @throws IllegalArgumentException if the pattern is null.
*/
+ @SuppressWarnings("unchecked")
public RegexFileFilter(final Pattern pattern) {
- this(pattern, p -> p.getFileName().toString());
+ this(pattern, (Function<Path, String> & Serializable) p -> p.getFileName().toString());
}
/**

MODIFY
src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTest.java
src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTest.java
package org.apache.commons.io.filefilter;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.File;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.function.Function;
import java.util.regex.Pattern;
import org.apache.commons.io.IOCase;
+import org.apache.commons.io.output.ByteArrayOutputStream;
import org.junit.jupiter.api.Test;
/**
@@ -65,9 +71,21 @@ public void assertFiltering(final IOFileFilter filter, final Path path, final bo
}
}
+ private RegexFileFilter assertSerializable(final RegexFileFilter serializable) throws IOException {
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
+ oos.writeObject(serializable);
+ }
+ baos.flush();
+ assertTrue(baos.toByteArray().length > 0);
+ }
+ return serializable;
+ }
+
@Test
- public void testRegex() {
- IOFileFilter filter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
+ public void testRegex() throws IOException {
+ RegexFileFilter filter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
+ assertSerializable(filter);
assertFiltering(filter, new File("Test.java"), true);
assertFiltering(filter, new File("test-10.java"), true);
assertFiltering(filter, new File("test-.java"), false);
@@ -77,6 +95,7 @@ public void testRegex() {
assertFiltering(filter, new File("test-.java").toPath(), false);
filter = new RegexFileFilter("^[Tt]est.java$");
+ assertSerializable(filter);
assertFiltering(filter, new File("Test.java"), true);
assertFiltering(filter, new File("test.java"), true);
assertFiltering(filter, new File("tEST.java"), false);
@@ -86,6 +105,7 @@ public void testRegex() {
assertFiltering(filter, new File("tEST.java").toPath(), false);
filter = new RegexFileFilter(Pattern.compile("^test.java$", Pattern.CASE_INSENSITIVE));
+ assertSerializable(filter);
assertFiltering(filter, new File("Test.java"), true);
assertFiltering(filter, new File("test.java"), true);
assertFiltering(filter, new File("tEST.java"), true);
@@ -95,6 +115,7 @@ public void testRegex() {
assertFiltering(filter, new File("tEST.java").toPath(), true);
filter = new RegexFileFilter("^test.java$", Pattern.CASE_INSENSITIVE);
+ assertSerializable(filter);
assertFiltering(filter, new File("Test.java"), true);
assertFiltering(filter, new File("test.java"), true);
assertFiltering(filter, new File("tEST.java"), true);
@@ -104,6 +125,7 @@ public void testRegex() {
assertFiltering(filter, new File("tEST.java").toPath(), true);
filter = new RegexFileFilter("^test.java$", IOCase.INSENSITIVE);
+ assertSerializable(filter);
assertFiltering(filter, new File("Test.java"), true);
assertFiltering(filter, new File("test.java"), true);
assertFiltering(filter, new File("tEST.java"), true);
@@ -115,21 +137,24 @@ public void testRegex() {
@Test
public void testRegexEdgeCases() {
- assertThrows(IllegalArgumentException.class, () -> new RegexFileFilter((String) null));
- assertThrows(IllegalArgumentException.class, () -> new RegexFileFilter(null, Pattern.CASE_INSENSITIVE));
- assertThrows(IllegalArgumentException.class, () -> new RegexFileFilter(null, IOCase.INSENSITIVE));
- assertThrows(IllegalArgumentException.class, () -> new RegexFileFilter((java.util.regex.Pattern) null));
+ assertThrows(IllegalArgumentException.class, () -> assertSerializable(new RegexFileFilter((String) null)));
+ assertThrows(IllegalArgumentException.class, () -> assertSerializable(new RegexFileFilter(null, Pattern.CASE_INSENSITIVE)));
+ assertThrows(IllegalArgumentException.class, () -> assertSerializable(new RegexFileFilter(null, IOCase.INSENSITIVE)));
+ assertThrows(IllegalArgumentException.class, () -> assertSerializable(new RegexFileFilter((java.util.regex.Pattern) null)));
}
/**
* Tests https://issues.apache.org/jira/browse/IO-733.
+ * @throws IOException
*/
+ @SuppressWarnings("unchecked")
@Test
- public void testRegexFileNameOnly() {
+ public void testRegexFileNameOnly() throws IOException {
final Path path = Paths.get("folder", "Foo.java");
final String patternStr = "Foo.*";
- assertFiltering(new RegexFileFilter(patternStr), path, true);
- assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), Path::toString), path, false);
+ assertFiltering(assertSerializable(new RegexFileFilter(patternStr)), path, true);
+ assertFiltering(assertSerializable(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) Path::toString)), path,
+ false);
}
}

[IO-773] RegexFileFilter is no longer Serializable.