diff options
author | Gary Gregory <gardgregory@gmail.com> | 2022-08-07 23:02:08 -0400 |
---|---|---|
committer | Gary Gregory <gardgregory@gmail.com> | 2022-08-07 23:02:08 -0400 |
commit | 596902ccaed90e0f1d72945e7eeb5c928dabfec1 (patch) | |
tree | 05f26c7e9c3afe4436b9a91f1952d675bc67d13b /src/test | |
parent | 4ce3d39b065db87c7b8e9ced1bbf897deec6b4ce (diff) | |
download | apache-commons-io-596902ccaed90e0f1d72945e7eeb5c928dabfec1.tar.gz |
Add IOUnaryOperator
Diffstat (limited to 'src/test')
3 files changed, 66 insertions, 3 deletions
diff --git a/src/test/java/org/apache/commons/io/function/IOUnaryOperatorTest.java b/src/test/java/org/apache/commons/io/function/IOUnaryOperatorTest.java new file mode 100644 index 00000000..11dbae0f --- /dev/null +++ b/src/test/java/org/apache/commons/io/function/IOUnaryOperatorTest.java @@ -0,0 +1,56 @@ +/* + * 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.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link IOUnaryOperator}. + */ +public class IOUnaryOperatorTest { + + @Test + public void testAsUnaryOperator() { + final List<Path> list = Arrays.asList(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A); + final IOUnaryOperator<Path> throwingIOUnaryOperator = TestUtils.throwingIOUnaryOperator(); + assertThrows(UncheckedIOException.class, () -> list.replaceAll(throwingIOUnaryOperator.asUnaryOperator())); + assertEquals("a", Optional.of("a").map(IOUnaryOperator.identity().asUnaryOperator()).get()); + assertEquals("a", Optional.of("a").map(IOUnaryOperator.identity().asFunction()).get()); + } + + @Test + public void testIdentity() throws IOException { + assertEquals(IOUnaryOperator.identity(), IOUnaryOperator.identity()); + final IOUnaryOperator<byte[]> identityFunction = IOUnaryOperator.identity(); + final byte[] buf = {(byte) 0xa, (byte) 0xb, (byte) 0xc}; + assertEquals(buf, identityFunction.apply(buf)); + assertArrayEquals(buf, identityFunction.apply(buf)); + } + +} diff --git a/src/test/java/org/apache/commons/io/function/TestConstants.java b/src/test/java/org/apache/commons/io/function/TestConstants.java index c361e878..482d8389 100644 --- a/src/test/java/org/apache/commons/io/function/TestConstants.java +++ b/src/test/java/org/apache/commons/io/function/TestConstants.java @@ -74,6 +74,8 @@ class TestConstants { throw new IOException("Failure"); }; + static IOUnaryOperator<?> THROWING_IO_UNARY_OPERATOR = t -> throwIOException(); + static Predicate<Object> THROWING_PREDICATE = t -> { throw new UncheckedIOException(new IOException("Failure")); }; diff --git a/src/test/java/org/apache/commons/io/function/TestUtils.java b/src/test/java/org/apache/commons/io/function/TestUtils.java index 47889bee..1338ee79 100644 --- a/src/test/java/org/apache/commons/io/function/TestUtils.java +++ b/src/test/java/org/apache/commons/io/function/TestUtils.java @@ -22,6 +22,10 @@ import java.util.concurrent.atomic.AtomicReference; class TestUtils { + static <T> T compareAndSetThrows(final AtomicReference<T> ref, final T update) throws IOException { + return compareAndSetThrows(ref, null, update); + } + static <T> T compareAndSetThrows(final AtomicReference<T> ref, final T expected, final T update) throws IOException { if (!ref.compareAndSet(expected, update)) { throw new IOException("Unexpected"); @@ -29,8 +33,9 @@ class TestUtils { return ref.get(); // same as update } - static <T> T compareAndSetThrows(final AtomicReference<T> ref, final T update) throws IOException { - return compareAndSetThrows(ref, null, update); - } + @SuppressWarnings("unchecked") + static <T> IOUnaryOperator<T> throwingIOUnaryOperator() { + return (IOUnaryOperator<T>) TestConstants.THROWING_IO_UNARY_OPERATOR; + } } |