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  package org.apache.commons.io.output;
18  
19  import static org.junit.jupiter.api.Assertions.assertSame;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Really not a lot to do here, but checking that no
26   * Exceptions are thrown.
27   */
28  class NullWriterTest {
29  
30      private static final String TEST_STRING = "ABC";
31      private static final char[] TEST_CHARS = TEST_STRING.toCharArray();
32  
33      @Test
34      void testAppendChar() {
35          try (NullWriter writer = NullWriter.INSTANCE) {
36              assertSame(writer, writer.append('X'));
37          }
38      }
39  
40      @Test
41      void testAppendCharSequence() {
42          try (NullWriter writer = NullWriter.INSTANCE) {
43              assertSame(writer, writer.append(TEST_STRING));
44              assertSame(writer, writer.append(null));
45          }
46      }
47  
48      @Test
49      void testAppendCharSequenceWithRange() {
50          try (NullWriter writer = NullWriter.INSTANCE) {
51              assertSame(writer, writer.append(TEST_STRING, 1, 2));
52              assertSame(writer, writer.append(null, 0, 4));
53              // Test argument validation
54              assertThrows(IndexOutOfBoundsException.class, () -> writer.append(TEST_STRING, -1, 2));
55              assertThrows(IndexOutOfBoundsException.class, () -> writer.append(TEST_STRING, 1, 5));
56              assertThrows(IndexOutOfBoundsException.class, () -> writer.append(TEST_STRING, 2, 1));
57          }
58      }
59  
60      @Test
61      void testCloseNoOp() {
62          final NullWriter writer = NullWriter.INSTANCE;
63          writer.close();
64          writer.write(TEST_CHARS);
65      }
66  
67      @Test
68      void testFlush() {
69          try (NullWriter writer = NullWriter.INSTANCE) {
70              writer.flush();
71          }
72      }
73  
74      @Test
75      void testWriteCharArray() {
76          try (NullWriter writer = NullWriter.INSTANCE) {
77              writer.write(TEST_CHARS);
78              // Test argument validation
79              assertThrows(NullPointerException.class, () -> writer.write((char[]) null));
80          }
81      }
82  
83      @Test
84      void testWriteCharArrayWithOffset() {
85          try (NullWriter writer = NullWriter.INSTANCE) {
86              writer.write(TEST_CHARS, 1, 2);
87              // Test argument validation
88              assertThrows(IndexOutOfBoundsException.class, () -> writer.write(TEST_CHARS, -1, 0));
89              assertThrows(IndexOutOfBoundsException.class, () -> writer.write(TEST_CHARS, 0, -1));
90              assertThrows(IndexOutOfBoundsException.class, () -> writer.write(TEST_CHARS, 0, 4));
91              assertThrows(NullPointerException.class, () -> writer.write((char[]) null, 0, 0));
92          }
93      }
94  
95      @Test
96      void testWriteInt() {
97          try (NullWriter writer = NullWriter.INSTANCE) {
98              writer.write(42);
99          }
100     }
101 
102     @Test
103     void testWriteString() {
104         try (NullWriter writer = NullWriter.INSTANCE) {
105             writer.write(TEST_STRING);
106             // Test argument validation
107             assertThrows(NullPointerException.class, () -> writer.write((String) null));
108         }
109     }
110 
111     @Test
112     void testWriteStringWithOffset() {
113         try (NullWriter writer = NullWriter.INSTANCE) {
114             writer.write(TEST_STRING, 1, 1);
115             // Test argument validation
116             assertThrows(IndexOutOfBoundsException.class, () -> writer.write(TEST_STRING, -1, 0));
117             assertThrows(IndexOutOfBoundsException.class, () -> writer.write(TEST_STRING, 0, -1));
118             assertThrows(IndexOutOfBoundsException.class, () -> writer.write(TEST_STRING, 0, 4));
119             assertThrows(NullPointerException.class, () -> writer.write((String) null, 0, 0));
120         }
121     }
122 }