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
commit 30451321618e4e915092a38d7030719717c1c404 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Wed Jun 15 09:36:37 2022 -0400 Use Objects.requireNonNull() Update Javadoc for existing calls. --- .../java/org/apache/commons/io/ByteOrderMark.java | 21 +++--- .../org/apache/commons/io/FileSystemUtils.java | 28 ++++---- src/main/java/org/apache/commons/io/HexDump.java | 12 ++-- .../io/comparator/ReverseFileComparator.java | 6 +- .../commons/io/filefilter/AbstractFileFilter.java | 7 -- .../commons/io/filefilter/DelegateFileFilter.java | 5 +- .../commons/io/filefilter/FileFilterUtils.java | 24 ++----- .../io/filefilter/MagicNumberFileFilter.java | 11 ++-- .../commons/io/filefilter/NameFileFilter.java | 16 ++--- .../commons/io/filefilter/NotFileFilter.java | 5 +- .../commons/io/filefilter/PrefixFileFilter.java | 11 ++-- .../commons/io/filefilter/RegexFileFilter.java | 11 ++-- .../commons/io/filefilter/SuffixFileFilter.java | 12 ++-- .../commons/io/filefilter/WildcardFileFilter.java | 12 ++-- .../commons/io/filefilter/WildcardFilter.java | 12 ++-- .../org/apache/commons/io/monitor/FileEntry.java | 6 +- .../io/serialization/RegexpClassNameMatcher.java | 8 +-- .../org/apache/commons/io/ByteOrderMarkTest.java | 75 +++++++++++----------- .../org/apache/commons/io/FileSystemUtilsTest.java | 6 +- .../java/org/apache/commons/io/HexDumpTest.java | 5 +- .../commons/io/filefilter/FileFilterTest.java | 66 +++++++++---------- .../commons/io/filefilter/RegexFileFilterTest.java | 8 +-- .../io/filefilter/WildcardFileFilterTest.java | 6 +- .../org/apache/commons/io/input/TailerTest.java | 6 +- .../serialization/RegexpClassNameMatcherTest.java | 6 +- 25 files changed, 176 insertions(+), 209 deletions(-) diff --git a/src/main/java/org/apache/commons/io/ByteOrderMark.java b/src/main/java/org/apache/commons/io/ByteOrderMark.java index 75da194d..18a3b045 100644 --- a/src/main/java/org/apache/commons/io/ByteOrderMark.java +++ b/src/main/java/org/apache/commons/io/ByteOrderMark.java @@ -18,6 +18,7 @@ package org.apache.commons.io; import java.io.Serializable; import java.util.Locale; +import java.util.Objects; /** * Byte Order Mark (BOM) representation - see {@link org.apache.commons.io.input.BOMInputStream}. @@ -67,20 +68,20 @@ public class ByteOrderMark implements Serializable { private final int[] bytes; /** - * Constructs a new BOM. + * Constructs a new instance. * * @param charsetName The name of the charset the BOM represents * @param bytes The BOM's bytes - * @throws IllegalArgumentException if the charsetName is null or - * zero length - * @throws IllegalArgumentException if the bytes are null or zero - * length + * @throws IllegalArgumentException if the charsetName is zero length + * @throws IllegalArgumentException if the bytes are zero length */ public ByteOrderMark(final String charsetName, final int... bytes) { - if (charsetName == null || charsetName.isEmpty()) { + Objects.requireNonNull(charsetName, "charsetName"); + Objects.requireNonNull(bytes, "bytes"); + if (charsetName.isEmpty()) { throw new IllegalArgumentException("No charsetName specified"); } - if (bytes == null || bytes.length == 0) { + if (bytes.length == 0) { throw new IllegalArgumentException("No bytes specified"); } this.charsetName = charsetName; @@ -88,7 +89,7 @@ public class ByteOrderMark implements Serializable { } /** - * Indicates if this BOM's bytes equals another. + * Indicates if this instance's bytes equals another. * * @param obj The object to compare to * @return true if the bom's bytes are equal, otherwise @@ -99,7 +100,7 @@ public class ByteOrderMark implements Serializable { if (!(obj instanceof ByteOrderMark)) { return false; } - final ByteOrderMark bom = (ByteOrderMark)obj; + final ByteOrderMark bom = (ByteOrderMark) obj; if (bytes.length != bom.length()) { return false; } @@ -129,7 +130,7 @@ public class ByteOrderMark implements Serializable { public byte[] getBytes() { final byte[] copy = IOUtils.byteArray(bytes.length); for (int i = 0; i < bytes.length; i++) { - copy[i] = (byte)bytes[i]; + copy[i] = (byte) bytes[i]; } return copy; } diff --git a/src/main/java/org/apache/commons/io/FileSystemUtils.java b/src/main/java/org/apache/commons/io/FileSystemUtils.java index 42fce02c..a92da946 100644 --- a/src/main/java/org/apache/commons/io/FileSystemUtils.java +++ b/src/main/java/org/apache/commons/io/FileSystemUtils.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Locale; +import java.util.Objects; import java.util.StringTokenizer; /** @@ -262,25 +263,22 @@ public class FileSystemUtils { * is zero or less * @return the amount of free drive space on the drive or volume * @throws IllegalArgumentException if the path is invalid - * @throws IllegalStateException if an error occurred in initialisation + * @throws IllegalStateException if an error occurred in initialization * @throws IOException if an error occurs when finding the free space */ long freeSpaceOS(final String path, final int os, final boolean kb, final Duration timeout) throws IOException { - if (path == null) { - throw new IllegalArgumentException("Path must not be null"); - } + Objects.requireNonNull(path, "path"); switch (os) { - case WINDOWS: - return kb ? freeSpaceWindows(path, timeout) / FileUtils.ONE_KB : freeSpaceWindows(path, timeout); - case UNIX: - return freeSpaceUnix(path, kb, false, timeout); - case POSIX_UNIX: - return freeSpaceUnix(path, kb, true, timeout); - case OTHER: - throw new IllegalStateException("Unsupported operating system"); - default: - throw new IllegalStateException( - "Exception caught when determining operating system"); + case WINDOWS: + return kb ? freeSpaceWindows(path, timeout) / FileUtils.ONE_KB : freeSpaceWindows(path, timeout); + case UNIX: + return freeSpaceUnix(path, kb, false, timeout); + case POSIX_UNIX: + return freeSpaceUnix(path, kb, true, timeout); + case OTHER: + throw new IllegalStateException("Unsupported operating system"); + default: + throw new IllegalStateException("Exception caught when determining operating system"); } } diff --git a/src/main/java/org/apache/commons/io/HexDump.java b/src/main/java/org/apache/commons/io/HexDump.java index d31a63f7..ab2ae8d6 100644 --- a/src/main/java/org/apache/commons/io/HexDump.java +++ b/src/main/java/org/apache/commons/io/HexDump.java @@ -19,6 +19,7 @@ package org.apache.commons.io; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; +import java.util.Objects; /** * Dumps data in hexadecimal format. @@ -74,22 +75,17 @@ public class HexDump { * the data to stream * @throws ArrayIndexOutOfBoundsException if the index is * outside the data array's bounds - * @throws IllegalArgumentException if the output stream is null + * @throws NullPointerException if the output stream is null */ - public static void dump(final byte[] data, final long offset, final OutputStream stream, final int index) - throws IOException, ArrayIndexOutOfBoundsException, - IllegalArgumentException { - + throws IOException, ArrayIndexOutOfBoundsException { + Objects.requireNonNull(stream, "stream"); if (index < 0 || index >= data.length) { throw new ArrayIndexOutOfBoundsException( "illegal index: " + index + " into array of length " + data.length); } - if (stream == null) { - throw new IllegalArgumentException("cannot write to nullstream"); - } long display_offset = offset + index; final StringBuilder buffer = new StringBuilder(74); diff --git a/src/main/java/org/apache/commons/io/comparator/ReverseFileComparator.java b/src/main/java/org/apache/commons/io/comparator/ReverseFileComparator.java index c5315feb..eed5bfef 100644 --- a/src/main/java/org/apache/commons/io/comparator/ReverseFileComparator.java +++ b/src/main/java/org/apache/commons/io/comparator/ReverseFileComparator.java @@ -19,6 +19,7 @@ package org.apache.commons.io.comparator; import java.io.File; import java.io.Serializable; import java.util.Comparator; +import java.util.Objects; /** * Reverses the result of comparing two {@link File} objects using the delegate {@link Comparator}. @@ -36,10 +37,7 @@ class ReverseFileComparator extends AbstractFileComparator implements Serializab * @param delegate The comparator to delegate to. */ public ReverseFileComparator(final Comparator<File> delegate) { - if (delegate == null) { - throw new IllegalArgumentException("Delegate comparator is missing"); - } - this.delegate = delegate; + this.delegate = Objects.requireNonNull(delegate, "delegate"); } /** diff --git a/src/main/java/org/apache/commons/io/filefilter/AbstractFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/AbstractFileFilter.java index 1d424704..06dfbcef 100644 --- a/src/main/java/org/apache/commons/io/filefilter/AbstractFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/AbstractFileFilter.java @@ -39,13 +39,6 @@ import org.apache.commons.io.file.PathVisitor; */ public abstract class AbstractFileFilter implements IOFileFilter, PathVisitor { - static <T> T requireNonNull(final T obj, final String message) { - if (obj == null) { - throw new IllegalArgumentException(message); - } - return obj; - } - static FileVisitResult toDefaultFileVisitResult(final boolean accept) { return accept ? FileVisitResult.CONTINUE : FileVisitResult.TERMINATE; } diff --git a/src/main/java/org/apache/commons/io/filefilter/DelegateFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/DelegateFileFilter.java index 7d0bb282..edc15d70 100644 --- a/src/main/java/org/apache/commons/io/filefilter/DelegateFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/DelegateFileFilter.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.io.Serializable; +import java.util.Objects; /** * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter. @@ -42,7 +43,7 @@ public class DelegateFileFilter extends AbstractFileFilter implements Serializab * @param fileFilter the filter to decorate */ public DelegateFileFilter(final FileFilter fileFilter) { - requireNonNull(fileFilter, "filter"); + Objects.requireNonNull(fileFilter, "filter"); this.fileFilter = fileFilter; this.filenameFilter = null; } @@ -53,7 +54,7 @@ public class DelegateFileFilter extends AbstractFileFilter implements Serializab * @param filenameFilter the filter to decorate */ public DelegateFileFilter(final FilenameFilter filenameFilter) { - requireNonNull(filenameFilter, "filter"); + Objects.requireNonNull(filenameFilter, "filter"); this.filenameFilter = filenameFilter; this.fileFilter = null; } diff --git a/src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java b/src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java index 37c56da5..0b8995f4 100644 --- a/src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java +++ b/src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java @@ -234,15 +234,13 @@ public class FileFilterUtils { * * @return a subset of {@code files} that is accepted by the * file filter. - * @throws IllegalArgumentException if the filter is {@code null} + * @throws NullPointerException if the filter is {@code null} * or {@code files} contains a {@code null} value. * * @since 2.0 */ public static File[] filter(final IOFileFilter filter, final File... files) { - if (filter == null) { - throw new IllegalArgumentException("file filter is null"); - } + Objects.requireNonNull(filter, "filter"); if (files == null) { return FileUtils.EMPTY_FILE_ARRAY; } @@ -291,15 +289,12 @@ public class FileFilterUtils { * @param <R> the return type. * @param <A> the mutable accumulation type of the reduction operation (often hidden as an implementation detail) * @return a subset of files from the stream that is accepted by the filter. - * @throws IllegalArgumentException if the filter is {@code null}. + * @throws NullPointerException if the filter is {@code null}. */ private static <R, A> R filterFiles(final IOFileFilter filter, final Stream<File> stream, final Collector<? super File, A, R> collector) { - //Objects.requireNonNull(filter, "filter"); + Objects.requireNonNull(filter, "filter"); Objects.requireNonNull(collector, "collector"); - if (filter == null) { - throw new IllegalArgumentException("file filter is null"); - } if (stream == null) { return Stream.<File>empty().collect(collector); } @@ -723,20 +718,15 @@ public class FileFilterUtils { * * @param filters The file filters * @return The list of file filters - * @throws IllegalArgumentException if the filters are null or contain a + * @throws NullPointerException if the filters are null or contain a * null value. * @since 2.0 */ public static List<IOFileFilter> toList(final IOFileFilter... filters) { - if (filters == null) { - throw new IllegalArgumentException("The filters must not be null"); - } + Objects.requireNonNull(filters, "filters"); final List<IOFileFilter> list = new ArrayList<>(filters.length); for (int i = 0; i < filters.length; i++) { - if (filters[i] == null) { - throw new IllegalArgumentException("The filter[" + i + "] is null"); - } - list.add(filters[i]); + list.add(Objects.requireNonNull(filters[i], "filters[i]")); } return list; } diff --git a/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java index 9e705856..88bf64f3 100644 --- a/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java @@ -28,6 +28,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; +import java.util.Objects; import org.apache.commons.io.IOUtils; import org.apache.commons.io.RandomAccessFileMode; @@ -159,12 +160,12 @@ public class MagicNumberFileFilter extends AbstractFileFilter implements * @param magicNumbers the magic number to look for in the file. * @param offset the byte offset in the file to start comparing bytes. * - * @throws IllegalArgumentException if {@code magicNumber} is - * {@code null}, or contains no bytes, or {@code offset} + * @throws IllegalArgumentException if {@code magicNumber} + * contains no bytes, or {@code offset} * is a negative number. */ public MagicNumberFileFilter(final byte[] magicNumbers, final long offset) { - requireNonNull(magicNumbers, "magicNumbers"); + Objects.requireNonNull(magicNumbers, "magicNumbers"); if (magicNumbers.length == 0) { throw new IllegalArgumentException("The magic number must contain at least one byte"); } @@ -218,11 +219,11 @@ public class MagicNumberFileFilter extends AbstractFileFilter implements * @param offset the byte offset in the file to start comparing bytes. * * @throws IllegalArgumentException if {@code magicNumber} is - * {@code null} or the empty String, or {@code offset} is + * the empty String, or {@code offset} is * a negative number. */ public MagicNumberFileFilter(final String magicNumber, final long offset) { - requireNonNull(magicNumber, "magicNumber"); + Objects.requireNonNull(magicNumber, "magicNumber"); if (magicNumber.isEmpty()) { throw new IllegalArgumentException("The magic number must contain at least one byte"); } diff --git a/src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java index 079aa0f2..08740538 100644 --- a/src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java @@ -90,11 +90,11 @@ public class NameFileFilter extends AbstractFileFilter implements Serializable { * * @param names the names to allow, must not be null * @param ioCase how to handle case sensitivity, null means case-sensitive - * @throws IllegalArgumentException if the name list is null + * @throws NullPointerException if the name list is null * @throws ClassCastException if the list does not contain Strings */ public NameFileFilter(final List<String> names, final IOCase ioCase) { - requireNonNull(names, "names"); + Objects.requireNonNull(names, "names"); this.names = names.toArray(EMPTY_STRING_ARRAY); this.ioCase = toIOCase(ioCase); } @@ -128,12 +128,10 @@ public class NameFileFilter extends AbstractFileFilter implements Serializable { * * @param name the name to allow, must not be null * @param ioCase how to handle case sensitivity, null means case-sensitive - * @throws IllegalArgumentException if the name is null + * @throws NullPointerException if the name is null */ public NameFileFilter(final String name, final IOCase ioCase) { - if (name == null) { - throw new IllegalArgumentException("The wildcard must not be null"); - } + Objects.requireNonNull(name, "name"); this.names = new String[] {name}; this.ioCase = toIOCase(ioCase); } @@ -143,12 +141,10 @@ public class NameFileFilter extends AbstractFileFilter implements Serializable { * * @param names the names to allow, must not be null * @param ioCase how to handle case sensitivity, null means case-sensitive - * @throws IllegalArgumentException if the names array is null + * @throws NullPointerException if the names array is null */ public NameFileFilter(final String[] names, final IOCase ioCase) { - if (names == null) { - throw new IllegalArgumentException("The array of names must not be null"); - } + Objects.requireNonNull(names, "names"); this.names = names.clone(); this.ioCase = toIOCase(ioCase); } diff --git a/src/main/java/org/apache/commons/io/filefilter/NotFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/NotFileFilter.java index ab04b0ac..1cc5d4f9 100644 --- a/src/main/java/org/apache/commons/io/filefilter/NotFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/NotFileFilter.java @@ -21,6 +21,7 @@ import java.io.Serializable; import java.nio.file.FileVisitResult; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; +import java.util.Objects; /** * This filter produces a logical NOT of the filters specified. @@ -39,10 +40,10 @@ public class NotFileFilter extends AbstractFileFilter implements Serializable { * Constructs a new file filter that NOTs the result of another filter. * * @param filter the filter, must not be null - * @throws IllegalArgumentException if the filter is null + * @throws NullPointerException if the filter is null */ public NotFileFilter(final IOFileFilter filter) { - requireNonNull(filter, "filter"); + Objects.requireNonNull(filter, "filter"); this.filter = filter; } diff --git a/src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java index d900dea3..807e3f47 100644 --- a/src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java @@ -22,6 +22,7 @@ import java.nio.file.FileVisitResult; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.util.List; +import java.util.Objects; import org.apache.commons.io.IOCase; @@ -77,7 +78,7 @@ public class PrefixFileFilter extends AbstractFileFilter implements Serializable * Constructs a new Prefix file filter for a list of prefixes. * * @param prefixes the prefixes to allow, must not be null - * @throws IllegalArgumentException if the prefix list is null + * @throws NullPointerException if the prefix list is null * @throws ClassCastException if the list does not contain Strings */ public PrefixFileFilter(final List<String> prefixes) { @@ -90,12 +91,12 @@ public class PrefixFileFilter extends AbstractFileFilter implements Serializable * * @param prefixes the prefixes to allow, must not be null * @param ioCase how to handle case sensitivity, null means case-sensitive - * @throws IllegalArgumentException if the prefix list is null + * @throws NullPointerException if the prefix list is null * @throws ClassCastException if the list does not contain Strings * @since 1.4 */ public PrefixFileFilter(final List<String> prefixes, final IOCase ioCase) { - requireNonNull(prefixes, "prefixes"); + Objects.requireNonNull(prefixes, "prefixes"); this.prefixes = prefixes.toArray(EMPTY_STRING_ARRAY); this.isCase = IOCase.value(ioCase, IOCase.SENSITIVE); } @@ -133,7 +134,7 @@ public class PrefixFileFilter extends AbstractFileFilter implements Serializable * @since 1.4 */ public PrefixFileFilter(final String prefix, final IOCase ioCase) { - requireNonNull(prefix, "prefix"); + Objects.requireNonNull(prefix, "prefix"); this.prefixes = new String[] {prefix}; this.isCase = IOCase.value(ioCase, IOCase.SENSITIVE); } @@ -148,7 +149,7 @@ public class PrefixFileFilter extends AbstractFileFilter implements Serializable * @since 1.4 */ public PrefixFileFilter(final String[] prefixes, final IOCase ioCase) { - requireNonNull(prefixes, "prefixes"); + Objects.requireNonNull(prefixes, "prefixes"); this.prefixes = prefixes.clone(); this.isCase = IOCase.value(ioCase, IOCase.SENSITIVE); } diff --git a/src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java index 0cf2a282..ea16c3b1 100644 --- a/src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java @@ -21,6 +21,7 @@ import java.io.Serializable; import java.nio.file.FileVisitResult; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; +import java.util.Objects; import java.util.function.Function; import java.util.regex.Pattern; @@ -78,7 +79,7 @@ public class RegexFileFilter extends AbstractFileFilter implements Serializable * @return a new Pattern. */ private static Pattern compile(final String pattern, final int flags) { - requireNonNull(pattern, "pattern"); + Objects.requireNonNull(pattern, "pattern"); return Pattern.compile(pattern, flags); } @@ -102,7 +103,7 @@ public class RegexFileFilter extends AbstractFileFilter implements Serializable * Constructs a new regular expression filter for a compiled regular expression * * @param pattern regular expression to match. - * @throws IllegalArgumentException if the pattern is null. + * @throws NullPointerException if the pattern is null. */ @SuppressWarnings("unchecked") public RegexFileFilter(final Pattern pattern) { @@ -114,11 +115,11 @@ public class RegexFileFilter extends AbstractFileFilter implements Serializable * * @param pattern regular expression to match. * @param pathToString How convert a path to a string. - * @throws IllegalArgumentException if the pattern is null. + * @throws NullPointerException if the pattern is null. * @since 2.10.0 */ public RegexFileFilter(final Pattern pattern, final Function<Path, String> pathToString) { - requireNonNull(pattern, "pattern"); + Objects.requireNonNull(pattern, "pattern"); this.pattern = pattern; this.pathToString = pathToString; } @@ -127,7 +128,7 @@ public class RegexFileFilter extends AbstractFileFilter implements Serializable * Constructs a new regular expression filter. * * @param pattern regular string expression to match - * @throws IllegalArgumentException if the pattern is null + * @throws NullPointerException if the pattern is null */ public RegexFileFilter(final String pattern) { this(pattern, 0); diff --git a/src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java index 55e4e407..def7f1ba 100644 --- a/src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java @@ -97,7 +97,7 @@ public class SuffixFileFilter extends AbstractFileFilter implements Serializable * @since 1.4 */ public SuffixFileFilter(final List<String> suffixes, final IOCase ioCase) { - requireNonNull(suffixes, "suffixes"); + Objects.requireNonNull(suffixes, "suffixes"); this.suffixes = suffixes.toArray(EMPTY_STRING_ARRAY); this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); } @@ -119,7 +119,7 @@ public class SuffixFileFilter extends AbstractFileFilter implements Serializable * instance. This would be inadvisable however. * * @param suffixes the suffixes to allow, must not be null - * @throws IllegalArgumentException if the suffix array is null + * @throws NullPointerException if the suffix array is null */ public SuffixFileFilter(final String... suffixes) { this(suffixes, IOCase.SENSITIVE); @@ -131,11 +131,11 @@ public class SuffixFileFilter extends AbstractFileFilter implements Serializable * * @param suffix the suffix to allow, must not be null * @param ioCase how to handle case sensitivity, null means case-sensitive - * @throws IllegalArgumentException if the suffix is null + * @throws NullPointerException if the suffix is null * @since 1.4 */ public SuffixFileFilter(final String suffix, final IOCase ioCase) { - requireNonNull(suffix, "suffix"); + Objects.requireNonNull(suffix, "suffix"); this.suffixes = new String[] {suffix}; this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); } @@ -146,11 +146,11 @@ public class SuffixFileFilter extends AbstractFileFilter implements Serializable * * @param suffixes the suffixes to allow, must not be null * @param ioCase how to handle case sensitivity, null means case-sensitive - * @throws IllegalArgumentException if the suffix array is null + * @throws NullPointerException if the suffix array is null * @since 1.4 */ public SuffixFileFilter(final String[] suffixes, final IOCase ioCase) { - requireNonNull(suffixes, "suffixes"); + Objects.requireNonNull(suffixes, "suffixes"); this.suffixes = suffixes.clone(); this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); } diff --git a/src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java index b10c0c7c..ff2ef142 100644 --- a/src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java @@ -104,7 +104,7 @@ public class WildcardFileFilter extends AbstractFileFilter implements Serializab * @throws ClassCastException if the list does not contain Strings */ public WildcardFileFilter(final List<String> wildcards, final IOCase ioCase) { - requireNonNull(wildcards, "wildcards"); + Objects.requireNonNull(wildcards, "wildcards"); this.wildcards = wildcards.toArray(EMPTY_STRING_ARRAY); this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); } @@ -123,7 +123,7 @@ public class WildcardFileFilter extends AbstractFileFilter implements Serializab * Constructs a new case-sensitive wildcard filter for an array of wildcards. * * @param wildcards the array of wildcards to match - * @throws IllegalArgumentException if the pattern array is null + * @throws NullPointerException if the pattern array is null */ public WildcardFileFilter(final String... wildcards) { this(wildcards, IOCase.SENSITIVE); @@ -134,10 +134,10 @@ public class WildcardFileFilter extends AbstractFileFilter implements Serializab * * @param wildcard the wildcard to match, not null * @param ioCase how to handle case sensitivity, null means case-sensitive - * @throws IllegalArgumentException if the pattern is null + * @throws NullPointerException if the pattern is null */ public WildcardFileFilter(final String wildcard, final IOCase ioCase) { - requireNonNull(wildcard, "wildcard"); + Objects.requireNonNull(wildcard, "wildcard"); this.wildcards = new String[] {wildcard}; this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); } @@ -147,10 +147,10 @@ public class WildcardFileFilter extends AbstractFileFilter implements Serializab * * @param wildcards the array of wildcards to match, not null * @param ioCase how to handle case sensitivity, null means case-sensitive - * @throws IllegalArgumentException if the pattern array is null + * @throws NullPointerException if the pattern array is null */ public WildcardFileFilter(final String[] wildcards, final IOCase ioCase) { - requireNonNull(wildcards, "wildcards"); + Objects.requireNonNull(wildcards, "wildcards"); this.wildcards = wildcards.clone(); this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); } diff --git a/src/main/java/org/apache/commons/io/filefilter/WildcardFilter.java b/src/main/java/org/apache/commons/io/filefilter/WildcardFilter.java index 38bb6c65..47f45c74 100644 --- a/src/main/java/org/apache/commons/io/filefilter/WildcardFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/WildcardFilter.java @@ -88,11 +88,11 @@ public class WildcardFilter extends AbstractFileFilter implements Serializable { * Constructs a new case-sensitive wildcard filter for a list of wildcards. * * @param wildcards the list of wildcards to match - * @throws IllegalArgumentException if the pattern list is null + * @throws NullPointerException if the pattern list is null * @throws ClassCastException if the list does not contain Strings */ public WildcardFilter(final List<String> wildcards) { - requireNonNull(wildcards, "wildcards"); + Objects.requireNonNull(wildcards, "wildcards"); this.wildcards = wildcards.toArray(EMPTY_STRING_ARRAY); } @@ -100,10 +100,10 @@ public class WildcardFilter extends AbstractFileFilter implements Serializable { * Constructs a new case-sensitive wildcard filter for a single wildcard. * * @param wildcard the wildcard to match - * @throws IllegalArgumentException if the pattern is null + * @throws NullPointerException if the pattern is null */ public WildcardFilter(final String wildcard) { - requireNonNull(wildcard, "wildcard"); + Objects.requireNonNull(wildcard, "wildcard"); this.wildcards = new String[] { wildcard }; } @@ -111,10 +111,10 @@ public class WildcardFilter extends AbstractFileFilter implements Serializable { * Constructs a new case-sensitive wildcard filter for an array of wildcards. * * @param wildcards the array of wildcards to match - * @throws IllegalArgumentException if the pattern array is null + * @throws NullPointerException if the pattern array is null */ public WildcardFilter(final String... wildcards) { - requireNonNull(wildcards, "wildcards"); + Objects.requireNonNull(wildcards, "wildcards"); this.wildcards = wildcards.clone(); } diff --git a/src/main/java/org/apache/commons/io/monitor/FileEntry.java b/src/main/java/org/apache/commons/io/monitor/FileEntry.java index e58e76e6..7c55f32c 100644 --- a/src/main/java/org/apache/commons/io/monitor/FileEntry.java +++ b/src/main/java/org/apache/commons/io/monitor/FileEntry.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.Serializable; import java.nio.file.Files; import java.nio.file.attribute.FileTime; +import java.util.Objects; import org.apache.commons.io.FileUtils; import org.apache.commons.io.file.attribute.FileTimes; @@ -77,10 +78,7 @@ public class FileEntry implements Serializable { * @param file The file being monitored */ public FileEntry(final FileEntry parent, final File file) { - if (file == null) { - throw new IllegalArgumentException("File is null."); - } - this.file = file; + this.file = Objects.requireNonNull(file, "file"); this.parent = parent; this.name = file.getName(); } diff --git a/src/main/java/org/apache/commons/io/serialization/RegexpClassNameMatcher.java b/src/main/java/org/apache/commons/io/serialization/RegexpClassNameMatcher.java index 535f94f2..c2c21f54 100644 --- a/src/main/java/org/apache/commons/io/serialization/RegexpClassNameMatcher.java +++ b/src/main/java/org/apache/commons/io/serialization/RegexpClassNameMatcher.java @@ -18,6 +18,7 @@ */ package org.apache.commons.io.serialization; +import java.util.Objects; import java.util.regex.Pattern; /** @@ -34,13 +35,10 @@ final class RegexpClassNameMatcher implements ClassNameMatcher { * Constructs an object based on the specified pattern. * * @param pattern a pattern for evaluating acceptable class names - * @throws IllegalArgumentException if {@code pattern} is null + * @throws NullPointerException if {@code pattern} is null */ public RegexpClassNameMatcher(final Pattern pattern) { - if (pattern == null) { - throw new IllegalArgumentException("Null pattern"); - } - this.pattern = pattern; + this.pattern = Objects.requireNonNull(pattern, "pattern"); } /** diff --git a/src/test/java/org/apache/commons/io/ByteOrderMarkTest.java b/src/test/java/org/apache/commons/io/ByteOrderMarkTest.java index eadc74d9..1cc156bc 100644 --- a/src/test/java/org/apache/commons/io/ByteOrderMarkTest.java +++ b/src/test/java/org/apache/commons/io/ByteOrderMarkTest.java @@ -29,7 +29,6 @@ import org.junit.jupiter.api.Test; /** * Test for {@link ByteOrderMark}. - * */ public class ByteOrderMarkTest { @@ -37,17 +36,9 @@ public class ByteOrderMarkTest { private static final ByteOrderMark TEST_BOM_2 = new ByteOrderMark("test2", 1, 2); private static final ByteOrderMark TEST_BOM_3 = new ByteOrderMark("test3", 1, 2, 3); - /** Test {@link ByteOrderMark#getCharsetName()} */ - @Test - public void charsetName() { - assertEquals("test1", TEST_BOM_1.getCharsetName(), "test1 name"); - assertEquals("test2", TEST_BOM_2.getCharsetName(), "test2 name"); - assertEquals("test3", TEST_BOM_3.getCharsetName(), "test3 name"); - } - /** Tests that {@link ByteOrderMark#getCharsetName()} can be loaded as a {@link java.nio.charset.Charset} as advertised. */ @Test - public void constantCharsetNames() { + public void testConstantCharsetNames() { assertNotNull(Charset.forName(ByteOrderMark.UTF_8.getCharsetName())); assertNotNull(Charset.forName(ByteOrderMark.UTF_16BE.getCharsetName())); assertNotNull(Charset.forName(ByteOrderMark.UTF_16LE.getCharsetName())); @@ -55,29 +46,33 @@ public class ByteOrderMarkTest { assertNotNull(Charset.forName(ByteOrderMark.UTF_32LE.getCharsetName())); } - /** Test Errors */ + /** Tests Exceptions */ @Test - public void errors() { - assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark(null, 1, 2, 3)); + public void testConstructorExceptions() { + assertThrows(NullPointerException.class, () -> new ByteOrderMark(null, 1, 2, 3)); assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("", 1, 2, 3)); - assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("a", (int[]) null)); + assertThrows(NullPointerException.class, () -> new ByteOrderMark("a", (int[]) null)); assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("b")); } - /** Test {@link ByteOrderMark#get(int)} */ + /** Tests {@link ByteOrderMark#equals(Object)} */ + @SuppressWarnings("EqualsWithItself") @Test - public void get() { - assertEquals(1, TEST_BOM_1.get(0), "test1 get(0)"); - assertEquals(1, TEST_BOM_2.get(0), "test2 get(0)"); - assertEquals(2, TEST_BOM_2.get(1), "test2 get(1)"); - assertEquals(1, TEST_BOM_3.get(0), "test3 get(0)"); - assertEquals(2, TEST_BOM_3.get(1), "test3 get(1)"); - assertEquals(3, TEST_BOM_3.get(2), "test3 get(2)"); + public void testEquals() { + assertEquals(TEST_BOM_1, TEST_BOM_1, "test1 equals"); + assertEquals(TEST_BOM_2, TEST_BOM_2, "test2 equals"); + assertEquals(TEST_BOM_3, TEST_BOM_3, "test3 equals"); + + assertNotEquals(TEST_BOM_1, new Object(), "Object not equal"); + assertNotEquals(TEST_BOM_1, new ByteOrderMark("1a", 2), "test1-1 not equal"); + assertNotEquals(TEST_BOM_1, new ByteOrderMark("1b", 1, 2), "test1-2 not test2"); + assertNotEquals(TEST_BOM_2, new ByteOrderMark("2", 1, 1), "test2 not equal"); + assertNotEquals(TEST_BOM_3, new ByteOrderMark("3", 1, 2, 4), "test3 not equal"); } - /** Test {@link ByteOrderMark#getBytes()} */ + /** Tests {@link ByteOrderMark#getBytes()} */ @Test - public void getBytes() { + public void testGetBytes() { assertArrayEquals(TEST_BOM_1.getBytes(), new byte[]{(byte) 1}, "test1 bytes"); TEST_BOM_1.getBytes()[0] = 2; assertArrayEquals(TEST_BOM_1.getBytes(), new byte[]{(byte) 1}, "test1 bytes"); @@ -85,22 +80,26 @@ public class ByteOrderMarkTest { assertArrayEquals(TEST_BOM_3.getBytes(), new byte[]{(byte) 1, (byte) 2, (byte) 3}, "test1 bytes"); } - /** Test {@link ByteOrderMark#equals(Object)} */ - @SuppressWarnings("EqualsWithItself") + /** Tests {@link ByteOrderMark#getCharsetName()} */ @Test - public void testEquals() { - assertEquals(TEST_BOM_1, TEST_BOM_1, "test1 equals"); - assertEquals(TEST_BOM_2, TEST_BOM_2, "test2 equals"); - assertEquals(TEST_BOM_3, TEST_BOM_3, "test3 equals"); + public void testGetCharsetName() { + assertEquals("test1", TEST_BOM_1.getCharsetName(), "test1 name"); + assertEquals("test2", TEST_BOM_2.getCharsetName(), "test2 name"); + assertEquals("test3", TEST_BOM_3.getCharsetName(), "test3 name"); + } - assertNotEquals(TEST_BOM_1, new Object(), "Object not equal"); - assertNotEquals(TEST_BOM_1, new ByteOrderMark("1a", 2), "test1-1 not equal"); - assertNotEquals(TEST_BOM_1, new ByteOrderMark("1b", 1, 2), "test1-2 not test2"); - assertNotEquals(TEST_BOM_2, new ByteOrderMark("2", 1, 1), "test2 not equal"); - assertNotEquals(TEST_BOM_3, new ByteOrderMark("3", 1, 2, 4), "test3 not equal"); + /** Tests {@link ByteOrderMark#get(int)} */ + @Test + public void testGetInt() { + assertEquals(1, TEST_BOM_1.get(0), "test1 get(0)"); + assertEquals(1, TEST_BOM_2.get(0), "test2 get(0)"); + assertEquals(2, TEST_BOM_2.get(1), "test2 get(1)"); + assertEquals(1, TEST_BOM_3.get(0), "test3 get(0)"); + assertEquals(2, TEST_BOM_3.get(1), "test3 get(1)"); + assertEquals(3, TEST_BOM_3.get(2), "test3 get(2)"); } - /** Test {@link ByteOrderMark#hashCode()} */ + /** Tests {@link ByteOrderMark#hashCode()} */ @Test public void testHashCode() { final int bomClassHash = ByteOrderMark.class.hashCode(); @@ -109,7 +108,7 @@ public class ByteOrderMarkTest { assertEquals(bomClassHash + 6, TEST_BOM_3.hashCode(), "hash test3 "); } - /** Test {@link ByteOrderMark#length()} */ + /** Tests {@link ByteOrderMark#length()} */ @Test public void testLength() { assertEquals(1, TEST_BOM_1.length(), "test1 length"); @@ -117,7 +116,7 @@ public class ByteOrderMarkTest { assertEquals(3, TEST_BOM_3.length(), "test3 length"); } - /** Test {@link ByteOrderMark#toString()} */ + /** Tests {@link ByteOrderMark#toString()} */ @Test public void testToString() { assertEquals("ByteOrderMark[test1: 0x1]", TEST_BOM_1.toString(), "test1 "); diff --git a/src/test/java/org/apache/commons/io/FileSystemUtilsTest.java b/src/test/java/org/apache/commons/io/FileSystemUtilsTest.java index 8a9d6200..642c9a80 100644 --- a/src/test/java/org/apache/commons/io/FileSystemUtilsTest.java +++ b/src/test/java/org/apache/commons/io/FileSystemUtilsTest.java @@ -153,15 +153,15 @@ public class FileSystemUtilsTest { @Test public void testGetFreeSpaceOS_String_NullPath() throws Exception { final FileSystemUtils fsu = new FileSystemUtils(); - assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceOS(null, 1, false, NEG_1_TIMEOUT)); - assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceOS(null, 1, true, NEG_1_TIMEOUT)); + assertThrows(NullPointerException.class, () -> fsu.freeSpaceOS(null, 1, false, NEG_1_TIMEOUT)); + assertThrows(NullPointerException.class, () -> fsu.freeSpaceOS(null, 1, true, NEG_1_TIMEOUT)); } @Test public void testGetFreeSpaceOS_String_Other() throws Exception { final FileSystemUtils fsu = new FileSystemUtils(); assertThrows(IllegalStateException.class, () -> fsu.freeSpaceOS("", 0, false, NEG_1_TIMEOUT)); - assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceOS(null, 1, true, NEG_1_TIMEOUT)); + assertThrows(NullPointerException.class, () -> fsu.freeSpaceOS(null, 1, true, NEG_1_TIMEOUT)); assertThrows(IllegalStateException.class, () -> fsu.freeSpaceOS("", 0, true, NEG_1_TIMEOUT)); } diff --git a/src/test/java/org/apache/commons/io/HexDumpTest.java b/src/test/java/org/apache/commons/io/HexDumpTest.java index bb8c072e..e47e8396 100644 --- a/src/test/java/org/apache/commons/io/HexDumpTest.java +++ b/src/test/java/org/apache/commons/io/HexDumpTest.java @@ -30,8 +30,7 @@ import org.junit.jupiter.api.Test; public class HexDumpTest { @Test - public void testDump() - throws IOException { + public void testDump() throws IOException { final byte[] testArray = new byte[256]; for (int j = 0; j < 256; j++) { @@ -188,7 +187,7 @@ public class HexDumpTest { assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(), testArray.length)); // verify proper behavior with null stream - assertThrows(IllegalArgumentException.class, () -> HexDump.dump(testArray, 0x10000000, null, 0)); + assertThrows(NullPointerException.class, () -> HexDump.dump(testArray, 0x10000000, null, 0)); } private char toAscii(final int c) { diff --git a/src/test/java/org/apache/commons/io/filefilter/FileFilterTest.java b/src/test/java/org/apache/commons/io/filefilter/FileFilterTest.java index b82f98f7..3b8f4bcd 100644 --- a/src/test/java/org/apache/commons/io/filefilter/FileFilterTest.java +++ b/src/test/java/org/apache/commons/io/filefilter/FileFilterTest.java @@ -264,8 +264,8 @@ public class FileFilterTest extends AbstractFilterTest { assertFiltering(filter, testFile, false); assertNotNull(filter.toString()); // TODO better test - assertThrows(IllegalArgumentException.class, () -> new DelegateFileFilter((FileFilter) null)); - assertThrows(IllegalArgumentException.class, () -> new DelegateFileFilter((FilenameFilter) null)); + assertThrows(NullPointerException.class, () -> new DelegateFileFilter((FileFilter) null)); + assertThrows(NullPointerException.class, () -> new DelegateFileFilter((FilenameFilter) null)); } @Test @@ -344,9 +344,9 @@ public class FileFilterTest extends AbstractFilterTest { assertEquals(FileVisitResult.TERMINATE, listFilter.accept(bmpPath, null)); assertEquals(FileVisitResult.TERMINATE, listFilter.accept(dirPath, null)); - assertThrows(IllegalArgumentException.class, () -> new WildcardFilter((String) null)); - assertThrows(IllegalArgumentException.class, () -> new WildcardFilter((String[]) null)); - assertThrows(IllegalArgumentException.class, () -> new WildcardFilter((List<String>) null)); + assertThrows(NullPointerException.class, () -> new WildcardFilter((String) null)); + assertThrows(NullPointerException.class, () -> new WildcardFilter((String[]) null)); + assertThrows(NullPointerException.class, () -> new WildcardFilter((List<String>) null)); } @Test @@ -567,7 +567,7 @@ public class FileFilterTest extends AbstractFilterTest { public void testFilterFilesArrayNullParameters() throws Exception { final File fileA = TestUtils.newFile(temporaryFolder, "A"); final File fileB = TestUtils.newFile(temporaryFolder, "B"); - assertThrows(IllegalArgumentException.class, () -> FileFilterUtils.filter(null, fileA, fileB)); + assertThrows(NullPointerException.class, () -> FileFilterUtils.filter(null, fileA, fileB)); final IOFileFilter filter = FileFilterUtils.trueFileFilter(); FileFilterUtils.filter(filter, fileA, null); @@ -617,7 +617,7 @@ public class FileFilterTest extends AbstractFilterTest { */ @Test public void testFilterListNullParameters() { - assertThrows(IllegalArgumentException.class, () -> FileFilterUtils.filterList(null, Collections.emptyList())); + assertThrows(NullPointerException.class, () -> FileFilterUtils.filterList(null, Collections.emptyList())); final IOFileFilter filter = FileFilterUtils.trueFileFilter(); List<File> filteredList = FileFilterUtils.filterList(filter, Collections.singletonList(null)); @@ -685,7 +685,7 @@ public class FileFilterTest extends AbstractFilterTest { */ @Test public void testFilterSetNullParameters() { - assertThrows(IllegalArgumentException.class, () -> FileFilterUtils.filterSet(null, Collections.emptySet())); + assertThrows(NullPointerException.class, () -> FileFilterUtils.filterSet(null, Collections.emptySet())); final IOFileFilter filter = FileFilterUtils.trueFileFilter(); FileFilterUtils.filterSet(filter, new HashSet<>(Collections.singletonList(null))); @@ -862,10 +862,10 @@ public class FileFilterTest extends AbstractFilterTest { @Test public void testMagicNumberFileFilterValidation() { - assertThrows(IllegalArgumentException.class, () -> new MagicNumberFileFilter((String) null, 0)); + assertThrows(NullPointerException.class, () -> new MagicNumberFileFilter((String) null, 0)); assertThrows(IllegalArgumentException.class, () -> new MagicNumberFileFilter("0", -1)); assertThrows(IllegalArgumentException.class, () -> new MagicNumberFileFilter("", 0)); - assertThrows(IllegalArgumentException.class, () -> new MagicNumberFileFilter((byte[]) null, 0)); + assertThrows(NullPointerException.class, () -> new MagicNumberFileFilter((byte[]) null, 0)); assertThrows(IllegalArgumentException.class, () -> new MagicNumberFileFilter(new byte[] {0}, -1)); assertThrows(IllegalArgumentException.class, () -> new MagicNumberFileFilter(new byte[] {}, 0)); } @@ -1040,23 +1040,19 @@ public class FileFilterTest extends AbstractFilterTest { public void testNameFilterNullArgument() { final String test = null; final String failMessage = "constructing a NameFileFilter with a null String argument should fail."; - assertThrows(IllegalArgumentException.class, ()->new NameFileFilter(test), - failMessage ); - - assertThrows(IllegalArgumentException.class, - ()-> FileFilterUtils.nameFileFilter(test, IOCase.INSENSITIVE), - failMessage); + assertThrows(NullPointerException.class, () -> new NameFileFilter(test), failMessage); + assertThrows(NullPointerException.class, () -> FileFilterUtils.nameFileFilter(test, IOCase.INSENSITIVE), failMessage); } @Test public void testNameFilterNullArrayArgument() { - assertThrows(IllegalArgumentException.class, () -> new NameFileFilter((String[]) null)); + assertThrows(NullPointerException.class, () -> new NameFileFilter((String[]) null)); } @Test public void testNameFilterNullListArgument() { final List<String> test = null; - assertThrows(IllegalArgumentException.class, () -> new NameFileFilter(test)); + assertThrows(NullPointerException.class, () -> new NameFileFilter(test)); } @Test @@ -1066,13 +1062,13 @@ public class FileFilterTest extends AbstractFilterTest { assertFiltering(filter, new File("foo"), false); assertFiltering(filter.negate(), new File("foo"), true); assertFiltering(filter, (File) null, false); - assertThrows(IllegalArgumentException.class, () -> new NotFileFilter(null)); + assertThrows(NullPointerException.class, () -> new NotFileFilter(null)); } @Test public void testNullFilters() { - assertThrows(IllegalArgumentException.class, ()-> FileFilterUtils.toList((IOFileFilter) null)); - assertThrows(IllegalArgumentException.class, ()-> FileFilterUtils.toList(new IOFileFilter[] {null})); + assertThrows(NullPointerException.class, () -> FileFilterUtils.toList((IOFileFilter) null)); + assertThrows(NullPointerException.class, () -> FileFilterUtils.toList(new IOFileFilter[] {null})); } @Test @@ -1179,9 +1175,9 @@ public class FileFilterTest extends AbstractFilterTest { assertEquals(FileVisitResult.TERMINATE, listFilter.accept(testPath, null)); assertEquals(FileVisitResult.CONTINUE, listFilter.accept(fredPath, null)); - assertThrows(IllegalArgumentException.class, () -> new PrefixFileFilter((String) null)); - assertThrows(IllegalArgumentException.class, () -> new PrefixFileFilter((String[]) null)); - assertThrows(IllegalArgumentException.class, () -> new PrefixFileFilter((List<String>) null)); + assertThrows(NullPointerException.class, () -> new PrefixFileFilter((String) null)); + assertThrows(NullPointerException.class, () -> new PrefixFileFilter((String[]) null)); + assertThrows(NullPointerException.class, () -> new PrefixFileFilter((List<String>) null)); } @Test @@ -1206,9 +1202,9 @@ public class FileFilterTest extends AbstractFilterTest { assertFiltering(filter, new File("FOO.test3"), true); // case-insensitive assertFiltering(filter, new File("BAR.test3"), true); // case-insensitive - assertThrows(IllegalArgumentException.class, () -> new PrefixFileFilter((String) null, IOCase.INSENSITIVE)); - assertThrows(IllegalArgumentException.class, () -> new PrefixFileFilter((String[]) null, IOCase.INSENSITIVE)); - assertThrows(IllegalArgumentException.class, () -> new PrefixFileFilter((List<String>) null, IOCase.INSENSITIVE)); + assertThrows(NullPointerException.class, () -> new PrefixFileFilter((String) null, IOCase.INSENSITIVE)); + assertThrows(NullPointerException.class, () -> new PrefixFileFilter((String[]) null, IOCase.INSENSITIVE)); + assertThrows(NullPointerException.class, () -> new PrefixFileFilter((List<String>) null, IOCase.INSENSITIVE)); // FileFilterUtils.prefixFileFilter(String, IOCase) tests filter = FileFilterUtils.prefixFileFilter("bar", IOCase.INSENSITIVE); assertFiltering(filter, new File("foo.test2"), false); @@ -1216,7 +1212,7 @@ public class FileFilterTest extends AbstractFilterTest { assertFiltering(filter, new File("FOO.test2"), false); // case-insensitive assertFiltering(filter, new File("BAR.test2"), true); // case-insensitive - assertThrows(IllegalArgumentException.class, () -> FileFilterUtils.prefixFileFilter(null, IOCase.INSENSITIVE)); + assertThrows(NullPointerException.class, () -> FileFilterUtils.prefixFileFilter(null, IOCase.INSENSITIVE)); } @Test @@ -1362,9 +1358,9 @@ public class FileFilterTest extends AbstractFilterTest { assertEquals(FileVisitResult.TERMINATE, listFilter.accept(testPath, null)); assertEquals(FileVisitResult.CONTINUE, listFilter.accept(fredPath, null)); - assertThrows(IllegalArgumentException.class, () -> new SuffixFileFilter((String) null)); - assertThrows(IllegalArgumentException.class, () -> new SuffixFileFilter((String[]) null)); - assertThrows(IllegalArgumentException.class, () -> new SuffixFileFilter((List<String>) null)); + assertThrows(NullPointerException.class, () -> new SuffixFileFilter((String) null)); + assertThrows(NullPointerException.class, () -> new SuffixFileFilter((String[]) null)); + assertThrows(NullPointerException.class, () -> new SuffixFileFilter((List<String>) null)); } @Test @@ -1389,16 +1385,16 @@ public class FileFilterTest extends AbstractFilterTest { assertFiltering(filter, new File("bar.TES"), true); // case-sensitive assertFiltering(filter, new File("bar.exe"), false); - assertThrows(IllegalArgumentException.class, () -> new SuffixFileFilter((String) null, IOCase.INSENSITIVE)); - assertThrows(IllegalArgumentException.class, () -> new SuffixFileFilter((String[]) null, IOCase.INSENSITIVE)); - assertThrows(IllegalArgumentException.class, () -> new SuffixFileFilter((List<String>) null, IOCase.INSENSITIVE)); + assertThrows(NullPointerException.class, () -> new SuffixFileFilter((String) null, IOCase.INSENSITIVE)); + assertThrows(NullPointerException.class, () -> new SuffixFileFilter((String[]) null, IOCase.INSENSITIVE)); + assertThrows(NullPointerException.class, () -> new SuffixFileFilter((List<String>) null, IOCase.INSENSITIVE)); // FileFilterUtils.suffixFileFilter(String, IOCase) tests filter = FileFilterUtils.suffixFileFilter("est", IOCase.INSENSITIVE); assertFiltering(filter, new File("test"), true); assertFiltering(filter, new File("TEST"), true); - assertThrows(IllegalArgumentException.class, () -> FileFilterUtils.suffixFileFilter(null, IOCase.INSENSITIVE)); + assertThrows(NullPointerException.class, () -> FileFilterUtils.suffixFileFilter(null, IOCase.INSENSITIVE)); } @Test diff --git a/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTest.java b/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTest.java index e04a5a82..dc0c094d 100644 --- a/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTest.java +++ b/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTest.java @@ -137,10 +137,10 @@ public class RegexFileFilterTest { @Test public void testRegexEdgeCases() { - 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))); + assertThrows(NullPointerException.class, () -> assertSerializable(new RegexFileFilter((String) null))); + assertThrows(NullPointerException.class, () -> assertSerializable(new RegexFileFilter(null, Pattern.CASE_INSENSITIVE))); + assertThrows(NullPointerException.class, () -> assertSerializable(new RegexFileFilter(null, IOCase.INSENSITIVE))); + assertThrows(NullPointerException.class, () -> assertSerializable(new RegexFileFilter((java.util.regex.Pattern) null))); } /** diff --git a/src/test/java/org/apache/commons/io/filefilter/WildcardFileFilterTest.java b/src/test/java/org/apache/commons/io/filefilter/WildcardFileFilterTest.java index 3d855ae1..f3c8ed61 100644 --- a/src/test/java/org/apache/commons/io/filefilter/WildcardFileFilterTest.java +++ b/src/test/java/org/apache/commons/io/filefilter/WildcardFileFilterTest.java @@ -141,8 +141,8 @@ public class WildcardFileFilterTest extends AbstractFilterTest { assertEquals(FileVisitResult.TERMINATE, listFilter.accept(bmpPath, null)); assertEquals(FileVisitResult.TERMINATE, listFilter.accept(dirPath, null)); - assertThrows(IllegalArgumentException.class, () -> new WildcardFileFilter((String) null)); - assertThrows(IllegalArgumentException.class, () -> new WildcardFileFilter((String[]) null)); - assertThrows(IllegalArgumentException.class, () -> new WildcardFileFilter((List<String>) null)); + assertThrows(NullPointerException.class, () -> new WildcardFileFilter((String) null)); + assertThrows(NullPointerException.class, () -> new WildcardFileFilter((String[]) null)); + assertThrows(NullPointerException.class, () -> new WildcardFileFilter((List<String>) null)); } } diff --git a/src/test/java/org/apache/commons/io/input/TailerTest.java b/src/test/java/org/apache/commons/io/input/TailerTest.java index 5369b759..6f487f00 100644 --- a/src/test/java/org/apache/commons/io/input/TailerTest.java +++ b/src/test/java/org/apache/commons/io/input/TailerTest.java @@ -696,7 +696,7 @@ public class TailerTest { } } - private void validateTailer(final TestTailerListener listener, final Tailer tailer, final File file) throws Exception { + private void validateTailer(final TestTailerListener listener, final Tailer tailer, final File file) throws IOException, InterruptedException { write(file, "foo"); final int timeout = 30; final TimeUnit timeoutUnit = TimeUnit.SECONDS; @@ -705,7 +705,7 @@ public class TailerTest { } /** Appends lines to a file */ - private void write(final File file, final String... lines) throws Exception { + private void write(final File file, final String... lines) throws IOException { try (Writer writer = Files.newBufferedWriter(file.toPath(), StandardOpenOption.APPEND)) { for (final String line : lines) { writer.write(line + "\n"); @@ -714,7 +714,7 @@ public class TailerTest { } /** Appends strings to a file */ - private void writeString(final File file, final String... strings) throws Exception { + private void writeString(final File file, final String... strings) throws IOException { try (Writer writer = Files.newBufferedWriter(file.toPath(), StandardOpenOption.APPEND)) { for (final String string : strings) { writer.write(string); diff --git a/src/test/java/org/apache/commons/io/serialization/RegexpClassNameMatcherTest.java b/src/test/java/org/apache/commons/io/serialization/RegexpClassNameMatcherTest.java index 1d856959..c90c92db 100644 --- a/src/test/java/org/apache/commons/io/serialization/RegexpClassNameMatcherTest.java +++ b/src/test/java/org/apache/commons/io/serialization/RegexpClassNameMatcherTest.java @@ -30,12 +30,12 @@ public class RegexpClassNameMatcherTest { @Test public void testNullPatternPattern() { - assertThrows(IllegalArgumentException.class, () -> new RegexpClassNameMatcher((Pattern)null)); + assertThrows(NullPointerException.class, () -> new RegexpClassNameMatcher((Pattern) null)); } @Test public void testNullStringPattern() { - assertThrows(NullPointerException.class, () -> new RegexpClassNameMatcher((String)null)); + assertThrows(NullPointerException.class, () -> new RegexpClassNameMatcher((String) null)); } @Test @@ -59,4 +59,4 @@ public class RegexpClassNameMatcherTest { assertTrue(ca.matches("foo.should.match")); assertFalse(ca.matches("bar.should.not.match")); } -} \ No newline at end of file +}