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 83ac402f Move new class and test it explicitly 83ac402f is described below commit 83ac402ffefbee8adf33843e81c4cd5bda35b410 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Tue Jul 26 10:34:09 2022 -0400 Move new class and test it explicitly As opposed to testing it by running other code. --- src/main/java/org/apache/commons/io/FileUtils.java | 1 + .../org/apache/commons/io/file/FilesUncheck.java | 2 +- .../java/org/apache/commons/io/file/PathUtils.java | 2 +- .../apache/commons/io/function/IOPredicate.java | 2 - .../apache/commons/io/{ => function}/Uncheck.java | 13 +- .../commons/io/input/UncheckedBufferedReader.java | 2 +- .../io/input/UncheckedFilterInputStream.java | 2 +- .../commons/io/input/UncheckedFilterReader.java | 2 +- .../commons/io/output/UncheckedAppendableImpl.java | 2 +- .../io/output/UncheckedFilterOutputStream.java | 2 +- .../commons/io/output/UncheckedFilterWriter.java | 2 +- .../commons/io/UncheckedIOExceptionsTest.java | 1 + .../org/apache/commons/io/UncheckedIOTest.java | 1 + .../apache/commons/io/file/FilesUncheckTest.java | 2 +- .../apache/commons/io/function/IOConsumerTest.java | 1 - .../apache/commons/io/function/TestConstants.java | 73 ++++++++ .../apache/commons/io/function/UncheckTest.java | 188 +++++++++++++++++++++ 17 files changed, 274 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index 12767d13..7c4a9913 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -80,6 +80,7 @@ import org.apache.commons.io.filefilter.IOFileFilter; import org.apache.commons.io.filefilter.SuffixFileFilter; import org.apache.commons.io.filefilter.TrueFileFilter; import org.apache.commons.io.function.IOConsumer; +import org.apache.commons.io.function.Uncheck; /** * General file manipulation utilities. diff --git a/src/main/java/org/apache/commons/io/file/FilesUncheck.java b/src/main/java/org/apache/commons/io/file/FilesUncheck.java index f3cd712d..44545dab 100644 --- a/src/main/java/org/apache/commons/io/file/FilesUncheck.java +++ b/src/main/java/org/apache/commons/io/file/FilesUncheck.java @@ -44,7 +44,7 @@ import java.util.Map; import java.util.Set; import java.util.stream.Stream; -import org.apache.commons.io.Uncheck; +import org.apache.commons.io.function.Uncheck; /** * Delegates to {@link Files} to uncheck calls by throwing {@link UncheckedIOException} instead of {@link IOException}. diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java index b504c4a3..3a90a279 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -71,11 +71,11 @@ import org.apache.commons.io.Charsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; -import org.apache.commons.io.Uncheck; import org.apache.commons.io.file.Counters.PathCounters; import org.apache.commons.io.file.attribute.FileTimes; import org.apache.commons.io.filefilter.IOFileFilter; import org.apache.commons.io.function.IOFunction; +import org.apache.commons.io.function.Uncheck; /** * NIO Path utilities. diff --git a/src/main/java/org/apache/commons/io/function/IOPredicate.java b/src/main/java/org/apache/commons/io/function/IOPredicate.java index f12b1f97..6cedbd4c 100644 --- a/src/main/java/org/apache/commons/io/function/IOPredicate.java +++ b/src/main/java/org/apache/commons/io/function/IOPredicate.java @@ -22,8 +22,6 @@ import java.io.UncheckedIOException; import java.util.Objects; import java.util.function.Predicate; -import org.apache.commons.io.Uncheck; - /** * Like {@link Predicate} but throws {@link IOException}. * diff --git a/src/main/java/org/apache/commons/io/Uncheck.java b/src/main/java/org/apache/commons/io/function/Uncheck.java similarity index 93% rename from src/main/java/org/apache/commons/io/Uncheck.java rename to src/main/java/org/apache/commons/io/function/Uncheck.java index 64b620d6..81b3b4ad 100644 --- a/src/main/java/org/apache/commons/io/Uncheck.java +++ b/src/main/java/org/apache/commons/io/function/Uncheck.java @@ -15,22 +15,11 @@ * limitations under the License. */ -package org.apache.commons.io; +package org.apache.commons.io.function; import java.io.IOException; import java.io.UncheckedIOException; -import org.apache.commons.io.function.IOBiConsumer; -import org.apache.commons.io.function.IOBiFunction; -import org.apache.commons.io.function.IOConsumer; -import org.apache.commons.io.function.IOFunction; -import org.apache.commons.io.function.IOPredicate; -import org.apache.commons.io.function.IOQuadFunction; -import org.apache.commons.io.function.IORunnable; -import org.apache.commons.io.function.IOSupplier; -import org.apache.commons.io.function.IOTriConsumer; -import org.apache.commons.io.function.IOTriFunction; - /** * Unchecks calls by throwing {@link UncheckedIOException} instead of {@link IOException}. * diff --git a/src/main/java/org/apache/commons/io/input/UncheckedBufferedReader.java b/src/main/java/org/apache/commons/io/input/UncheckedBufferedReader.java index 8ab8b544..6664f5ad 100644 --- a/src/main/java/org/apache/commons/io/input/UncheckedBufferedReader.java +++ b/src/main/java/org/apache/commons/io/input/UncheckedBufferedReader.java @@ -23,7 +23,7 @@ import java.io.Reader; import java.io.UncheckedIOException; import java.nio.CharBuffer; -import org.apache.commons.io.Uncheck; +import org.apache.commons.io.function.Uncheck; /** * A {@link BufferedReader} that throws {@link UncheckedIOException} instead of {@link IOException}. diff --git a/src/main/java/org/apache/commons/io/input/UncheckedFilterInputStream.java b/src/main/java/org/apache/commons/io/input/UncheckedFilterInputStream.java index 60d4bb46..f617e93b 100644 --- a/src/main/java/org/apache/commons/io/input/UncheckedFilterInputStream.java +++ b/src/main/java/org/apache/commons/io/input/UncheckedFilterInputStream.java @@ -24,7 +24,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; -import org.apache.commons.io.Uncheck; +import org.apache.commons.io.function.Uncheck; /** * A {@link BufferedReader} that throws {@link UncheckedIOException} instead of {@link IOException}. diff --git a/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java b/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java index a91088bf..794630a0 100644 --- a/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java +++ b/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java @@ -23,7 +23,7 @@ import java.io.Reader; import java.io.UncheckedIOException; import java.nio.CharBuffer; -import org.apache.commons.io.Uncheck; +import org.apache.commons.io.function.Uncheck; /** * A {@link FilterReader} that throws {@link UncheckedIOException} instead of {@link IOException}. diff --git a/src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java b/src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java index 53afcc92..17cbe327 100644 --- a/src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java +++ b/src/main/java/org/apache/commons/io/output/UncheckedAppendableImpl.java @@ -21,7 +21,7 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.util.Objects; -import org.apache.commons.io.Uncheck; +import org.apache.commons.io.function.Uncheck; /** * An {@link Appendable} implementation that throws {@link UncheckedIOException} instead of {@link IOException}. diff --git a/src/main/java/org/apache/commons/io/output/UncheckedFilterOutputStream.java b/src/main/java/org/apache/commons/io/output/UncheckedFilterOutputStream.java index e73ac541..31dd5ef7 100644 --- a/src/main/java/org/apache/commons/io/output/UncheckedFilterOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/UncheckedFilterOutputStream.java @@ -22,7 +22,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.UncheckedIOException; -import org.apache.commons.io.Uncheck; +import org.apache.commons.io.function.Uncheck; /** * A {@link FilterOutputStream} that throws {@link UncheckedIOException} instead of {@link UncheckedIOException}. diff --git a/src/main/java/org/apache/commons/io/output/UncheckedFilterWriter.java b/src/main/java/org/apache/commons/io/output/UncheckedFilterWriter.java index c6f9599c..e64a4ced 100644 --- a/src/main/java/org/apache/commons/io/output/UncheckedFilterWriter.java +++ b/src/main/java/org/apache/commons/io/output/UncheckedFilterWriter.java @@ -22,7 +22,7 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.io.Writer; -import org.apache.commons.io.Uncheck; +import org.apache.commons.io.function.Uncheck; /** * A {@link FilterWriter} that throws {@link UncheckedIOException} instead of {@link IOException}. diff --git a/src/test/java/org/apache/commons/io/UncheckedIOExceptionsTest.java b/src/test/java/org/apache/commons/io/UncheckedIOExceptionsTest.java index 4e0f83f9..7eaf23f5 100644 --- a/src/test/java/org/apache/commons/io/UncheckedIOExceptionsTest.java +++ b/src/test/java/org/apache/commons/io/UncheckedIOExceptionsTest.java @@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; import java.io.UncheckedIOException; +import org.apache.commons.io.function.Uncheck; import org.junit.jupiter.api.Test; /** diff --git a/src/test/java/org/apache/commons/io/UncheckedIOTest.java b/src/test/java/org/apache/commons/io/UncheckedIOTest.java index 5830862d..38aae531 100644 --- a/src/test/java/org/apache/commons/io/UncheckedIOTest.java +++ b/src/test/java/org/apache/commons/io/UncheckedIOTest.java @@ -27,6 +27,7 @@ import org.apache.commons.io.function.IOFunction; import org.apache.commons.io.function.IORunnable; import org.apache.commons.io.function.IOSupplier; import org.apache.commons.io.function.IOTriFunction; +import org.apache.commons.io.function.Uncheck; import org.junit.jupiter.api.Test; /** diff --git a/src/test/java/org/apache/commons/io/file/FilesUncheckTest.java b/src/test/java/org/apache/commons/io/file/FilesUncheckTest.java index 6951c62b..18823532 100644 --- a/src/test/java/org/apache/commons/io/file/FilesUncheckTest.java +++ b/src/test/java/org/apache/commons/io/file/FilesUncheckTest.java @@ -52,7 +52,7 @@ import java.util.HashSet; import java.util.Set; import org.apache.commons.io.IOUtils; -import org.apache.commons.io.Uncheck; +import org.apache.commons.io.function.Uncheck; import org.apache.commons.io.input.NullInputStream; import org.apache.commons.io.output.NullOutputStream; import org.apache.commons.lang3.ArrayUtils; diff --git a/src/test/java/org/apache/commons/io/function/IOConsumerTest.java b/src/test/java/org/apache/commons/io/function/IOConsumerTest.java index 22eac031..67a8e6ce 100644 --- a/src/test/java/org/apache/commons/io/function/IOConsumerTest.java +++ b/src/test/java/org/apache/commons/io/function/IOConsumerTest.java @@ -27,7 +27,6 @@ import java.nio.file.Files; import java.util.concurrent.atomic.AtomicReference; import org.apache.commons.io.IOUtils; -import org.apache.commons.io.Uncheck; import org.apache.commons.io.file.PathUtils; import org.apache.commons.io.test.ThrowOnCloseReader; import org.junit.jupiter.api.Test; diff --git a/src/test/java/org/apache/commons/io/function/TestConstants.java b/src/test/java/org/apache/commons/io/function/TestConstants.java new file mode 100644 index 00000000..545e71e3 --- /dev/null +++ b/src/test/java/org/apache/commons/io/function/TestConstants.java @@ -0,0 +1,73 @@ +/* + * 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.function; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.function.Predicate; + +/** + * Test fixtures for this package. + */ +class TestConstants { + + static IOConsumer<Object> THROWING_IO_CONSUMER = t -> { + throw new IOException("Failure"); + }; + + static IOFunction<Object, Object> THROWING_IO_FUNCTION = t -> { + throw new IOException("Failure"); + }; + + static IOBiFunction<Object, Object, Object> THROWING_IO_BI_FUNCTION = (t, u) -> { + throw new IOException("Failure"); + }; + + static IOTriFunction<Object, Object, Object, Object> THROWING_IO_TRI_FUNCTION = (t, u, v) -> { + throw new IOException("Failure"); + }; + + static IOQuadFunction<Object, Object, Object, Object, Object> THROWING_IO_QUAD_FUNCTION = (t, u, v, w) -> { + throw new IOException("Failure"); + }; + + static IOSupplier<Object> THROWING_IO_SUPPLIER = () -> { + throw new IOException("Failure"); + }; + + static IORunnable THROWING_IO_RUNNABLE = () -> { + throw new IOException("Failure"); + }; + + static IOBiConsumer<Object, Object> THROWING_IO_BI_CONSUMER = (t, u) -> { + throw new IOException("Failure"); + }; + + static IOTriConsumer<Object, Object, Object> THROWING_IO_TRI_CONSUMER = (t, u, v) -> { + throw new IOException("Failure"); + }; + + static IOPredicate<Object> THROWING_IO_PREDICATE = t -> { + throw new IOException("Failure"); + }; + + static Predicate<Object> THROWING_PREDICATE = t -> { + throw new UncheckedIOException(new IOException("Failure")); + }; + +} diff --git a/src/test/java/org/apache/commons/io/function/UncheckTest.java b/src/test/java/org/apache/commons/io/function/UncheckTest.java new file mode 100644 index 00000000..a379208a --- /dev/null +++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java @@ -0,0 +1,188 @@ +/* + * 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.function; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link Uncheck}. + */ +public class UncheckTest { + + private AtomicReference<String> ref1; + private AtomicReference<String> ref2; + private AtomicReference<String> ref3; + private AtomicReference<String> ref4; + + @BeforeEach + public void initEach() { + ref1 = new AtomicReference<>(); + ref2 = new AtomicReference<>(); + ref3 = new AtomicReference<>(); + ref4 = new AtomicReference<>(); + } + + @Test + public void testAcceptIOBiConsumerOfTUTU() { + assertThrows(UncheckedIOException.class, () -> Uncheck.accept((t, u) -> { + throw new IOException(); + }, null, null)); + assertThrows(UncheckedIOException.class, () -> Uncheck.accept(TestConstants.THROWING_IO_BI_CONSUMER, null, null)); + Uncheck.accept((t, u) -> { + compareAndSet(ref1, t); + compareAndSet(ref2, u); + }, "new1", "new2"); + assertEquals("new1", ref1.get()); + assertEquals("new2", ref2.get()); + } + + @Test + public void testAcceptIOConsumerOfTT() { + assertThrows(UncheckedIOException.class, () -> Uncheck.accept(t -> { + throw new IOException(); + }, null)); + assertThrows(UncheckedIOException.class, () -> Uncheck.accept(TestConstants.THROWING_IO_CONSUMER, null)); + Uncheck.accept(t -> compareAndSet(ref1, t), "new1"); + assertEquals("new1", ref1.get()); + } + + private <T> T compareAndSet(final AtomicReference<T> ref, final T t) throws IOException { + if (!ref.compareAndSet(null, t)) { + // Tell the compiler this method throws a checked exception. + throw new IOException("Unexpected1"); + } + return ref.get(); // same as t + } + + @Test + public void testAcceptIOTriConsumerOfTUVTUV() { + assertThrows(UncheckedIOException.class, () -> Uncheck.accept((t, u, v) -> { + throw new IOException(); + }, null, null, null)); + assertThrows(UncheckedIOException.class, () -> Uncheck.accept(TestConstants.THROWING_IO_TRI_CONSUMER, null, null, null)); + Uncheck.accept((t, u, v) -> { + compareAndSet(ref1, t); + compareAndSet(ref2, u); + compareAndSet(ref3, v); + }, "new1", "new2", "new3"); + assertEquals("new1", ref1.get()); + assertEquals("new2", ref2.get()); + assertEquals("new3", ref3.get()); + } + + @Test + public void testApplyIOBiFunctionOfTURTU() { + assertThrows(UncheckedIOException.class, () -> Uncheck.apply((t, u) -> { + throw new IOException(); + }, null, null)); + assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_BI_FUNCTION, null, null)); + assertEquals("new0", Uncheck.apply((t, u) -> { + compareAndSet(ref1, t); + compareAndSet(ref2, u); + return "new0"; + }, "new1", "new2")); + assertEquals("new1", ref1.get()); + assertEquals("new2", ref2.get()); + } + + @Test + public void testApplyIOFunctionOfTRT() { + assertThrows(UncheckedIOException.class, () -> Uncheck.apply(t -> { + throw new IOException(); + }, null)); + assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_FUNCTION, null)); + Uncheck.apply(t -> compareAndSet(ref1, t), "new1"); + assertEquals("new1", ref1.get()); + } + + @Test + public void testApplyIOQuadFunctionOfTUVWRTUVW() { + assertThrows(UncheckedIOException.class, () -> Uncheck.apply((t, u, v, w) -> { + throw new IOException(); + }, null, null, null, null)); + assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_QUAD_FUNCTION, null, null, null, null)); + assertEquals("new0", Uncheck.apply((t, u, v, w) -> { + compareAndSet(ref1, t); + compareAndSet(ref2, u); + compareAndSet(ref3, v); + compareAndSet(ref4, w); + return "new0"; + }, "new1", "new2", "new3", "new4")); + assertEquals("new1", ref1.get()); + assertEquals("new2", ref2.get()); + assertEquals("new3", ref3.get()); + assertEquals("new4", ref4.get()); + } + + @Test + public void testApplyIOTriFunctionOfTUVRTUV() { + assertThrows(UncheckedIOException.class, () -> Uncheck.apply((t, u, v) -> { + throw new IOException(); + }, null, null, null)); + assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_TRI_FUNCTION, null, null, null)); + assertEquals("new0", Uncheck.apply((t, u, v) -> { + compareAndSet(ref1, t); + compareAndSet(ref2, u); + compareAndSet(ref3, v); + return "new0"; + }, "new1", "new2", "new3")); + assertEquals("new1", ref1.get()); + assertEquals("new2", ref2.get()); + assertEquals("new3", ref3.get()); + } + + @Test + public void testGet() { + assertThrows(UncheckedIOException.class, () -> Uncheck.get(() -> { + throw new IOException(); + })); + assertThrows(UncheckedIOException.class, () -> Uncheck.get(TestConstants.THROWING_IO_SUPPLIER)); + assertEquals("new1", Uncheck.get(() -> compareAndSet(ref1, "new1"))); + assertEquals("new1", ref1.get()); + } + + @Test + public void testRun() { + assertThrows(UncheckedIOException.class, () -> Uncheck.run(() -> { + throw new IOException(); + })); + assertThrows(UncheckedIOException.class, () -> Uncheck.run(TestConstants.THROWING_IO_RUNNABLE)); + Uncheck.run(() -> compareAndSet(ref1, "new1")); + assertEquals("new1", ref1.get()); + } + + @Test + public void testTest() { + assertThrows(UncheckedIOException.class, () -> Uncheck.test(t -> { + throw new IOException(); + }, null)); + assertThrows(UncheckedIOException.class, () -> Uncheck.test(TestConstants.THROWING_IO_PREDICATE, null)); + assertTrue(Uncheck.test(t -> compareAndSet(ref1, t).equals(t), "new1")); + assertEquals("new1", ref1.get()); + } + +}