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.utils;
18  
19  import static org.apache.juneau.common.utils.IOUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.io.*;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class IOUtils_Test extends TestBase {
28  
29  	//====================================================================================================
30  	// read(File)
31  	// read(InputStream, Charset)
32  	// read(InputStream)
33  	// read(Reader, int, int)
34  	//====================================================================================================
35  	@Test void a01_read() throws Exception {
36  		var out = new TestWriter();
37  		var in = new TestReader("foobar");
38  
39  		pipe(in, out);
40  		assertTrue(in.closed);
41  		assertFalse(out.closed);
42  		assertEquals("foobar", out.toString());
43  	}
44  
45  	@Test void a02_loadSystemResourceAsString() throws Exception {
46  		assertNotNull(loadSystemResourceAsString("test1.txt", "."));
47  		assertNull(loadSystemResourceAsString("test2.txt", "."));
48  		assertNull(loadSystemResourceAsString("test3.txt", "sub"));
49  		assertNull(loadSystemResourceAsString("test3.txt", "sub2"));
50  		assertNotNull(loadSystemResourceAsString("test3.txt", "."));
51  		assertNotNull(loadSystemResourceAsString("test4.txt", ".", "sub"));
52  		assertNotNull(loadSystemResourceAsString("test4.txt", "sub"));
53  	}
54  
55  	public static class TestReader extends StringReader {
56  		boolean closed;
57  
58  		public TestReader(String s) {
59  			super(s);
60  		}
61  
62  		@Override /* Reader */
63  		public void close() {
64  			closed = true;
65  		}
66  	}
67  
68  	public static class TestWriter extends StringWriter {
69  		boolean closed;
70  
71  		public TestWriter() { /* no-op */ }
72  
73  		@Override /* Writer */
74  		public void close() {
75  			closed = true;
76  		}
77  	}
78  
79  	public static class TestInputStream extends ByteArrayInputStream {
80  		boolean closed;
81  
82  		public TestInputStream(String s) {
83  			super(s.getBytes());
84  		}
85  
86  		@Override /* InputStream */
87  		public void close() throws IOException {
88  			super.close();
89  			closed = true;
90  		}
91  	}
92  
93  	public static class TestOutputStream extends ByteArrayOutputStream {
94  		boolean closed;
95  
96  		public TestOutputStream() { /* no-op */ }
97  
98  		@Override /* OutputStream */
99  		public void close() throws IOException {
100 			super.close();
101 			closed = true;
102 		}
103 
104 		@Override /* Object */
105 		public String toString() {
106 			return new String(this.toByteArray(), UTF8);
107 		}
108 	}
109 }