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    *      http://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.juneau.commons.io;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.io.*;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  class NoCloseOutputStream_Test extends TestBase {
27  
28  	//====================================================================================================
29  	// Basic write tests
30  	//====================================================================================================
31  	@Test void a01_writeInt() throws IOException {
32  		var baos = new ByteArrayOutputStream();
33  		var wrapper = new NoCloseOutputStream(baos);
34  		wrapper.write(65); // 'A'
35  		wrapper.write(66); // 'B'
36  		assertEquals("AB", baos.toString());
37  	}
38  
39  	@Test void a02_writeByteArray() throws IOException {
40  		var baos = new ByteArrayOutputStream();
41  		var wrapper = new NoCloseOutputStream(baos);
42  		wrapper.write("test".getBytes());
43  		assertEquals("test", baos.toString());
44  	}
45  
46  	@Test void a03_writeByteArrayWithOffset() throws IOException {
47  		var baos = new ByteArrayOutputStream();
48  		var wrapper = new NoCloseOutputStream(baos);
49  		var bytes = "hello".getBytes();
50  		wrapper.write(bytes, 1, 3);
51  		assertEquals("ell", baos.toString());
52  	}
53  
54  	//====================================================================================================
55  	// flush() tests
56  	//====================================================================================================
57  	@Test void b01_flush() throws IOException {
58  		var baos = new ByteArrayOutputStream();
59  		var wrapper = new NoCloseOutputStream(baos);
60  		wrapper.write("test".getBytes());
61  		wrapper.flush(); // Should not throw
62  		assertEquals("test", baos.toString());
63  	}
64  
65  	@Test void b02_flush_multipleTimes() throws IOException {
66  		var baos = new ByteArrayOutputStream();
67  		var wrapper = new NoCloseOutputStream(baos);
68  		wrapper.flush();
69  		wrapper.flush();
70  		wrapper.flush();
71  		// Should not throw
72  	}
73  
74  	//====================================================================================================
75  	// close() tests - should not actually close
76  	//====================================================================================================
77  	@Test void c01_close_doesNotClose() throws IOException {
78  		var baos = new ByteArrayOutputStream();
79  		var wrapper = new NoCloseOutputStream(baos);
80  		wrapper.write("test".getBytes());
81  		wrapper.close(); // Should flush but not close
82  		// Should still be able to write after close
83  		wrapper.write("more".getBytes());
84  		assertEquals("testmore", baos.toString());
85  	}
86  
87  	@Test void c02_close_flushes() throws IOException {
88  		var baos = new ByteArrayOutputStream();
89  		var wrapper = new NoCloseOutputStream(baos);
90  		wrapper.write("test".getBytes());
91  		wrapper.close();
92  		// Content should be flushed
93  		assertEquals("test", baos.toString());
94  	}
95  
96  	@Test void c03_close_multipleTimes() throws IOException {
97  		var baos = new ByteArrayOutputStream();
98  		var wrapper = new NoCloseOutputStream(baos);
99  		wrapper.close();
100 		wrapper.close();
101 		wrapper.close();
102 		// Should not throw
103 	}
104 
105 	//====================================================================================================
106 	// Integration tests
107 	//====================================================================================================
108 	@Test void d01_combinedOperations() throws IOException {
109 		var baos = new ByteArrayOutputStream();
110 		var wrapper = new NoCloseOutputStream(baos);
111 		wrapper.write(72); // 'H'
112 		wrapper.write("ello".getBytes());
113 		wrapper.write(" World".getBytes(), 0, 6);
114 		wrapper.flush();
115 		wrapper.close();
116 		wrapper.write(33); // '!'
117 		assertEquals("Hello World!", baos.toString());
118 	}
119 }
120