1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.input;
19
20 import static org.junit.jupiter.api.Assertions.assertSame;
21
22 import java.io.IOException;
23 import java.io.Reader;
24 import java.nio.CharBuffer;
25
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 class ProxyReaderTest {
32
33
34 private static final class CustomNullReader extends NullReader {
35
36 CustomNullReader(final int len) {
37 super(len);
38 }
39
40 @Override
41 public int read(final char[] chars) throws IOException {
42 return chars == null ? 0 : super.read(chars);
43 }
44
45 @Override
46 public int read(final char[] chars, final int offset, final int length) throws IOException {
47 return chars == null ? 0 : super.read(chars, offset, length);
48 }
49
50 @Override
51 public int read(final CharBuffer target) throws IOException {
52 return target == null ? 0 : super.read(target);
53 }
54 }
55
56
57 private static final class ProxyReaderImpl extends ProxyReader {
58
59 ProxyReaderImpl(final Reader proxy) {
60 super(proxy);
61 }
62 }
63
64 @Test
65 void testNullCharArray() throws Exception {
66 try (ProxyReader proxy = new ProxyReaderImpl(new CustomNullReader(0))) {
67 proxy.read((char[]) null);
68 proxy.read(null, 0, 0);
69 }
70 }
71
72 @Test
73 void testNullCharBuffer() throws Exception {
74 try (ProxyReader proxy = new ProxyReaderImpl(new CustomNullReader(0))) {
75 proxy.read((CharBuffer) null);
76 }
77 }
78
79 @Test
80 void testSetReference() throws Exception {
81 try (CustomNullReader wrapped = new CustomNullReader(0); ProxyReader proxy = new ProxyReaderImpl(wrapped)) {
82 assertSame(wrapped, proxy.unwrap());
83 proxy.setReference(NullReader.INSTANCE);
84 assertSame(NullReader.INSTANCE, proxy.unwrap());
85 }
86 }
87
88 @Test
89 void testUnwrap() throws Exception {
90 try (CustomNullReader wrapped = new CustomNullReader(0); ProxyReader proxy = new ProxyReaderImpl(wrapped)) {
91 assertSame(wrapped, proxy.unwrap());
92 }
93 }
94 }