Skip to content

Commit d6e836d

Browse files
committed
Merge 2 similar test classes
1 parent b4caca0 commit d6e836d

2 files changed

Lines changed: 111 additions & 155 deletions

File tree

src/test/java/org/apache/commons/io/UncheckIOTest.java

Lines changed: 0 additions & 154 deletions
This file was deleted.

src/test/java/org/apache/commons/io/function/UncheckTest.java

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertThrows;
2222
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
import static org.junit.jupiter.api.Assertions.fail;
2324

25+
import java.io.ByteArrayInputStream;
2426
import java.io.IOException;
2527
import java.io.UncheckedIOException;
2628
import java.util.concurrent.atomic.AtomicInteger;
2729
import java.util.concurrent.atomic.AtomicReference;
30+
import java.util.function.Supplier;
2831

32+
import org.apache.commons.io.input.BrokenInputStream;
2933
import org.junit.jupiter.api.BeforeEach;
3034
import org.junit.jupiter.api.Test;
3135

@@ -34,11 +38,22 @@
3438
*/
3539
public class UncheckTest {
3640

41+
private static final byte[] BYTES = { 'a', 'b' };
42+
private static final String CAUSE_MESSAGE = "CauseMessage";
43+
private static final String CUSTOM_MESSAGE = "Custom message";
44+
45+
private AtomicInteger atomicInt;
3746
private AtomicReference<String> ref1;
3847
private AtomicReference<String> ref2;
3948
private AtomicReference<String> ref3;
4049
private AtomicReference<String> ref4;
41-
private AtomicInteger atomicInt;
50+
51+
private void assertUncheckedIOException(final IOException expected, final UncheckedIOException e) {
52+
assertEquals(CUSTOM_MESSAGE, e.getMessage());
53+
final IOException cause = e.getCause();
54+
assertEquals(expected.getClass(), cause.getClass());
55+
assertEquals(CAUSE_MESSAGE, cause.getMessage());
56+
}
4257

4358
@BeforeEach
4459
public void initEach() {
@@ -49,6 +64,20 @@ public void initEach() {
4964
atomicInt = new AtomicInteger();
5065
}
5166

67+
private ByteArrayInputStream newInputStream() {
68+
return new ByteArrayInputStream(BYTES);
69+
}
70+
71+
/**
72+
* Tests {@link Uncheck#accept(IOConsumer, Object)}.
73+
*/
74+
@Test
75+
public void testAccept() {
76+
final ByteArrayInputStream stream = newInputStream();
77+
Uncheck.accept(n -> stream.skip(n), 1);
78+
assertEquals('b', Uncheck.get(stream::read).intValue());
79+
}
80+
5281
@Test
5382
public void testAcceptIOBiConsumerOfTUTU() {
5483
assertThrows(UncheckedIOException.class, () -> Uncheck.accept((t, u) -> {
@@ -89,6 +118,38 @@ public void testAcceptIOTriConsumerOfTUVTUV() {
89118
assertEquals("new3", ref3.get());
90119
}
91120

121+
/**
122+
* Tests {@link Uncheck#apply(IOFunction, Object)}.
123+
*/
124+
@Test
125+
public void testApply1() {
126+
final ByteArrayInputStream stream = newInputStream();
127+
assertEquals(1, Uncheck.apply(n -> stream.skip(n), 1).intValue());
128+
assertEquals('b', Uncheck.get(stream::read).intValue());
129+
}
130+
131+
/**
132+
* Tests {@link Uncheck#apply(IOBiFunction, Object, Object)}.
133+
*/
134+
@Test
135+
public void testApply2() {
136+
final ByteArrayInputStream stream = newInputStream();
137+
final byte[] buf = new byte[BYTES.length];
138+
assertEquals(1, Uncheck.apply((o, l) -> stream.read(buf, o, l), 0, 1).intValue());
139+
assertEquals('a', buf[0]);
140+
}
141+
142+
/**
143+
* Tests {@link Uncheck#apply(IOTriFunction, Object, Object, Object)}.
144+
*/
145+
@Test
146+
public void testApply3() {
147+
final ByteArrayInputStream stream = newInputStream();
148+
final byte[] buf = new byte[BYTES.length];
149+
assertEquals(1, Uncheck.apply((b, o, l) -> stream.read(b, o, l), buf, 0, 1).intValue());
150+
assertEquals('a', buf[0]);
151+
}
152+
92153
@Test
93154
public void testApplyIOBiFunctionOfTURTU() {
94155
assertThrows(UncheckedIOException.class, () -> Uncheck.apply((t, u) -> {
@@ -150,8 +211,12 @@ public void testApplyIOTriFunctionOfTUVRTUV() {
150211
assertEquals("new3", ref3.get());
151212
}
152213

214+
/**
215+
* Tests {@link Uncheck#get(IOSupplier)}.
216+
*/
153217
@Test
154218
public void testGet() {
219+
assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue());
155220
assertThrows(UncheckedIOException.class, () -> Uncheck.get(() -> {
156221
throw new IOException();
157222
}));
@@ -170,8 +235,32 @@ public void testGetAsInt() {
170235
assertEquals(1, atomicInt.get());
171236
}
172237

238+
/**
239+
* Tests {@link Uncheck#get(IOSupplier, Supplier)}.
240+
*/
241+
@Test
242+
public void testGetMessage() {
243+
// No exception
244+
assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue(), () -> CUSTOM_MESSAGE);
245+
// Exception
246+
final IOException expected = new IOException(CAUSE_MESSAGE);
247+
try {
248+
Uncheck.get(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
249+
fail();
250+
} catch (final UncheckedIOException e) {
251+
assertUncheckedIOException(expected, e);
252+
}
253+
}
254+
255+
/**
256+
* Tests {@link Uncheck#run(IORunnable)}.
257+
*/
173258
@Test
174259
public void testRun() {
260+
final ByteArrayInputStream stream = newInputStream();
261+
Uncheck.run(() -> stream.skip(1));
262+
assertEquals('b', Uncheck.get(stream::read).intValue());
263+
//
175264
assertThrows(UncheckedIOException.class, () -> Uncheck.run(() -> {
176265
throw new IOException();
177266
}));
@@ -180,6 +269,27 @@ public void testRun() {
180269
assertEquals("new1", ref1.get());
181270
}
182271

272+
/**
273+
* Tests {@link Uncheck#run(IORunnable, Supplier))}.
274+
*
275+
* @throws IOException
276+
*/
277+
@Test
278+
public void testRunMessage() throws IOException {
279+
// No exception
280+
final ByteArrayInputStream stream = newInputStream();
281+
Uncheck.run(() -> stream.skip(1), () -> CUSTOM_MESSAGE);
282+
assertEquals('b', Uncheck.get(stream::read).intValue());
283+
final IOException expected = new IOException(CAUSE_MESSAGE);
284+
// Exception
285+
try {
286+
Uncheck.run(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
287+
fail();
288+
} catch (final UncheckedIOException e) {
289+
assertUncheckedIOException(expected, e);
290+
}
291+
}
292+
183293
@Test
184294
public void testTest() {
185295
assertThrows(UncheckedIOException.class, () -> Uncheck.test(t -> {

0 commit comments

Comments
 (0)