You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/24 17:21:42 UTC

[commons-io] 01/02: Merge 2 similar test classes

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 d6e836d453c75d3ff5de3860fb863ed7277ddd52
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 13:02:32 2023 -0400

    Merge 2 similar test classes
---
 .../java/org/apache/commons/io/UncheckIOTest.java  | 154 ---------------------
 .../apache/commons/io/function/UncheckTest.java    | 112 ++++++++++++++-
 2 files changed, 111 insertions(+), 155 deletions(-)

diff --git a/src/test/java/org/apache/commons/io/UncheckIOTest.java b/src/test/java/org/apache/commons/io/UncheckIOTest.java
deleted file mode 100644
index 3611cc3c..00000000
--- a/src/test/java/org/apache/commons/io/UncheckIOTest.java
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * 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;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.UncheckedIOException;
-
-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.IORunnable;
-import org.apache.commons.io.function.IOSupplier;
-import org.apache.commons.io.function.IOTriFunction;
-import org.apache.commons.io.function.Uncheck;
-import org.apache.commons.io.input.BrokenInputStream;
-import org.junit.jupiter.api.Test;
-
-/**
- * Tests {@link Uncheck}.
- */
-public class UncheckIOTest {
-
-    private static final String CAUSE_MESSAGE = "CauseMessage";
-    private static final String CUSTOM_MESSAGE = "Custom message";
-    private static final byte[] BYTES = { 'a', 'b' };
-
-    private void assertUncheckedIOException(final IOException expected, final UncheckedIOException e) {
-        assertEquals(CUSTOM_MESSAGE, e.getMessage());
-        final IOException cause = e.getCause();
-        assertEquals(expected.getClass(), cause.getClass());
-        assertEquals(CAUSE_MESSAGE, cause.getMessage());
-    }
-
-    private ByteArrayInputStream newInputStream() {
-        return new ByteArrayInputStream(BYTES);
-    }
-
-    /**
-     * Tests {@link Uncheck#accept(IOConsumer, Object)}.
-     */
-    @Test
-    public void testAccept() {
-        final ByteArrayInputStream stream = newInputStream();
-        Uncheck.accept(n -> stream.skip(n), 1);
-        assertEquals('b', Uncheck.get(stream::read).intValue());
-    }
-
-    /**
-     * Tests {@link Uncheck#apply(IOFunction, Object)}.
-     */
-    @Test
-    public void testApply1() {
-        final ByteArrayInputStream stream = newInputStream();
-        assertEquals(1, Uncheck.apply(n -> stream.skip(n), 1).intValue());
-        assertEquals('b', Uncheck.get(stream::read).intValue());
-    }
-
-    /**
-     * Tests {@link Uncheck#apply(IOBiFunction, Object, Object)}.
-     */
-    @Test
-    public void testApply2() {
-        final ByteArrayInputStream stream = newInputStream();
-        final byte[] buf = new byte[BYTES.length];
-        assertEquals(1, Uncheck.apply((o, l) -> stream.read(buf, o, l), 0, 1).intValue());
-        assertEquals('a', buf[0]);
-    }
-
-    /**
-     * Tests {@link Uncheck#apply(IOTriFunction, Object, Object, Object)}.
-     */
-    @Test
-    public void testApply3() {
-        final ByteArrayInputStream stream = newInputStream();
-        final byte[] buf = new byte[BYTES.length];
-        assertEquals(1, Uncheck.apply((b, o, l) -> stream.read(b, o, l), buf, 0, 1).intValue());
-        assertEquals('a', buf[0]);
-    }
-
-    /**
-     * Tests {@link Uncheck#get(IOSupplier)}.
-     */
-    @Test
-    public void testGet() {
-        assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue());
-    }
-
-    /**
-     * Tests {@link Uncheck#get(IOSupplier, Supplier)}.
-     */
-    @Test
-    public void testGetMessage() {
-        // No exception
-        assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue(), () -> CUSTOM_MESSAGE);
-        // Exception
-        final IOException expected = new IOException(CAUSE_MESSAGE);
-        try {
-            Uncheck.get(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
-            fail();
-        } catch (final UncheckedIOException e) {
-            assertUncheckedIOException(expected, e);
-        }
-    }
-
-    /**
-     * Tests {@link Uncheck#run(IORunnable)}.
-     */
-    @Test
-    public void testRun() {
-        final ByteArrayInputStream stream = newInputStream();
-        Uncheck.run(() -> stream.skip(1));
-        assertEquals('b', Uncheck.get(stream::read).intValue());
-    }
-
-    /**
-     * Tests {@link Uncheck#run(IORunnable, Supplier))}.
-     *
-     * @throws IOException
-     */
-    @Test
-    public void testRunMessage() throws IOException {
-        // No exception
-        final ByteArrayInputStream stream = newInputStream();
-        Uncheck.run(() -> stream.skip(1), () -> CUSTOM_MESSAGE);
-        assertEquals('b', Uncheck.get(stream::read).intValue());
-        final IOException expected = new IOException(CAUSE_MESSAGE);
-        // Exception
-        try {
-            Uncheck.run(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
-            fail();
-        } catch (final UncheckedIOException e) {
-            assertUncheckedIOException(expected, e);
-        }
-    }
-}
diff --git a/src/test/java/org/apache/commons/io/function/UncheckTest.java b/src/test/java/org/apache/commons/io/function/UncheckTest.java
index fedd08a2..67186897 100644
--- a/src/test/java/org/apache/commons/io/function/UncheckTest.java
+++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java
@@ -20,12 +20,16 @@ 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 static org.junit.jupiter.api.Assertions.fail;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
 
+import org.apache.commons.io.input.BrokenInputStream;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -34,11 +38,22 @@ import org.junit.jupiter.api.Test;
  */
 public class UncheckTest {
 
+    private static final byte[] BYTES = { 'a', 'b' };
+    private static final String CAUSE_MESSAGE = "CauseMessage";
+    private static final String CUSTOM_MESSAGE = "Custom message";
+
+    private AtomicInteger atomicInt;
     private AtomicReference<String> ref1;
     private AtomicReference<String> ref2;
     private AtomicReference<String> ref3;
     private AtomicReference<String> ref4;
-    private AtomicInteger atomicInt;
+
+    private void assertUncheckedIOException(final IOException expected, final UncheckedIOException e) {
+        assertEquals(CUSTOM_MESSAGE, e.getMessage());
+        final IOException cause = e.getCause();
+        assertEquals(expected.getClass(), cause.getClass());
+        assertEquals(CAUSE_MESSAGE, cause.getMessage());
+    }
 
     @BeforeEach
     public void initEach() {
@@ -49,6 +64,20 @@ public class UncheckTest {
         atomicInt = new AtomicInteger();
     }
 
+    private ByteArrayInputStream newInputStream() {
+        return new ByteArrayInputStream(BYTES);
+    }
+
+    /**
+     * Tests {@link Uncheck#accept(IOConsumer, Object)}.
+     */
+    @Test
+    public void testAccept() {
+        final ByteArrayInputStream stream = newInputStream();
+        Uncheck.accept(n -> stream.skip(n), 1);
+        assertEquals('b', Uncheck.get(stream::read).intValue());
+    }
+
     @Test
     public void testAcceptIOBiConsumerOfTUTU() {
         assertThrows(UncheckedIOException.class, () -> Uncheck.accept((t, u) -> {
@@ -89,6 +118,38 @@ public class UncheckTest {
         assertEquals("new3", ref3.get());
     }
 
+    /**
+     * Tests {@link Uncheck#apply(IOFunction, Object)}.
+     */
+    @Test
+    public void testApply1() {
+        final ByteArrayInputStream stream = newInputStream();
+        assertEquals(1, Uncheck.apply(n -> stream.skip(n), 1).intValue());
+        assertEquals('b', Uncheck.get(stream::read).intValue());
+    }
+
+    /**
+     * Tests {@link Uncheck#apply(IOBiFunction, Object, Object)}.
+     */
+    @Test
+    public void testApply2() {
+        final ByteArrayInputStream stream = newInputStream();
+        final byte[] buf = new byte[BYTES.length];
+        assertEquals(1, Uncheck.apply((o, l) -> stream.read(buf, o, l), 0, 1).intValue());
+        assertEquals('a', buf[0]);
+    }
+
+    /**
+     * Tests {@link Uncheck#apply(IOTriFunction, Object, Object, Object)}.
+     */
+    @Test
+    public void testApply3() {
+        final ByteArrayInputStream stream = newInputStream();
+        final byte[] buf = new byte[BYTES.length];
+        assertEquals(1, Uncheck.apply((b, o, l) -> stream.read(b, o, l), buf, 0, 1).intValue());
+        assertEquals('a', buf[0]);
+    }
+
     @Test
     public void testApplyIOBiFunctionOfTURTU() {
         assertThrows(UncheckedIOException.class, () -> Uncheck.apply((t, u) -> {
@@ -150,8 +211,12 @@ public class UncheckTest {
         assertEquals("new3", ref3.get());
     }
 
+    /**
+     * Tests {@link Uncheck#get(IOSupplier)}.
+     */
     @Test
     public void testGet() {
+        assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue());
         assertThrows(UncheckedIOException.class, () -> Uncheck.get(() -> {
             throw new IOException();
         }));
@@ -170,8 +235,32 @@ public class UncheckTest {
         assertEquals(1, atomicInt.get());
     }
 
+    /**
+     * Tests {@link Uncheck#get(IOSupplier, Supplier)}.
+     */
+    @Test
+    public void testGetMessage() {
+        // No exception
+        assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue(), () -> CUSTOM_MESSAGE);
+        // Exception
+        final IOException expected = new IOException(CAUSE_MESSAGE);
+        try {
+            Uncheck.get(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+            fail();
+        } catch (final UncheckedIOException e) {
+            assertUncheckedIOException(expected, e);
+        }
+    }
+
+    /**
+     * Tests {@link Uncheck#run(IORunnable)}.
+     */
     @Test
     public void testRun() {
+        final ByteArrayInputStream stream = newInputStream();
+        Uncheck.run(() -> stream.skip(1));
+        assertEquals('b', Uncheck.get(stream::read).intValue());
+        //
         assertThrows(UncheckedIOException.class, () -> Uncheck.run(() -> {
             throw new IOException();
         }));
@@ -180,6 +269,27 @@ public class UncheckTest {
         assertEquals("new1", ref1.get());
     }
 
+    /**
+     * Tests {@link Uncheck#run(IORunnable, Supplier))}.
+     *
+     * @throws IOException
+     */
+    @Test
+    public void testRunMessage() throws IOException {
+        // No exception
+        final ByteArrayInputStream stream = newInputStream();
+        Uncheck.run(() -> stream.skip(1), () -> CUSTOM_MESSAGE);
+        assertEquals('b', Uncheck.get(stream::read).intValue());
+        final IOException expected = new IOException(CAUSE_MESSAGE);
+        // Exception
+        try {
+            Uncheck.run(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+            fail();
+        } catch (final UncheckedIOException e) {
+            assertUncheckedIOException(expected, e);
+        }
+    }
+
     @Test
     public void testTest() {
         assertThrows(UncheckedIOException.class, () -> Uncheck.test(t -> {