View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link ProxyReader}.
30   */
31  class ProxyReaderTest {
32  
33      /** Custom NullReader implementation. */
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      /** ProxyReader implementation. */
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  }