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.apache.juneau.commons.utils.IoUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.io.*;
23  import java.nio.file.*;
24  import java.util.*;
25  
26  import org.apache.juneau.*;
27  import org.junit.jupiter.api.*;
28  
29  class LocalFile_Test extends TestBase {
30  
31  	private static final Path TEST_FILE = Paths.get("src/test/resources/files/Test3.properties");
32  
33  	//====================================================================================================
34  	// Constructor tests - filesystem
35  	//====================================================================================================
36  	@Test void a01_constructorWithPath() {
37  		var file = new LocalFile(TEST_FILE);
38  		assertEquals("Test3.properties", file.getName());
39  	}
40  
41  	@Test void a02_constructorWithPath_null() {
42  		assertThrows(IllegalArgumentException.class, () -> {
43  			new LocalFile((Path)null);
44  		});
45  	}
46  
47  	@Test void a03_constructorWithPath_rootPath() {
48  		var path = Paths.get("/");
49  		assertThrows(IllegalArgumentException.class, () -> {
50  			new LocalFile(path);
51  		});
52  	}
53  
54  	//====================================================================================================
55  	// Constructor tests - classpath
56  	//====================================================================================================
57  	@Test void b01_constructorWithClassAndPath() {
58  		var file = new LocalFile(LocalFile_Test.class, "files/Test3.properties");
59  		assertEquals("Test3.properties", file.getName());
60  	}
61  
62  	@Test void b02_constructorWithClassAndPath_noSlash() {
63  		var file = new LocalFile(LocalFile_Test.class, "Test3.properties");
64  		assertEquals("Test3.properties", file.getName());
65  	}
66  
67  	@Test void b03_constructorWithClassAndPath_nestedPath() {
68  		var file = new LocalFile(LocalFile_Test.class, "files/subdir/Test3.properties");
69  		assertEquals("Test3.properties", file.getName());
70  	}
71  
72  	@Test void b04_constructorWithClassAndPath_nullClass() {
73  		assertThrows(IllegalArgumentException.class, () -> {
74  			new LocalFile((Class<?>)null, "path");
75  		});
76  	}
77  
78  	@Test void b05_constructorWithClassAndPath_nullPath() {
79  		assertThrows(IllegalArgumentException.class, () -> {
80  			new LocalFile(LocalFile_Test.class, null);
81  		});
82  	}
83  
84  	//====================================================================================================
85  	// getName() tests
86  	//====================================================================================================
87  	@Test void c01_getName_filesystem() {
88  		var file = new LocalFile(TEST_FILE);
89  		assertEquals("Test3.properties", file.getName());
90  	}
91  
92  	@Test void c02_getName_classpath() {
93  		var file = new LocalFile(LocalFile_Test.class, "files/Test3.properties");
94  		assertEquals("Test3.properties", file.getName());
95  	}
96  
97  	@Test void c03_getName_classpath_noSlash() {
98  		var file = new LocalFile(LocalFile_Test.class, "Test3.properties");
99  		assertEquals("Test3.properties", file.getName());
100 	}
101 
102 	//====================================================================================================
103 	// read() tests - filesystem
104 	//====================================================================================================
105 	@Test void d01_read_filesystem() throws IOException {
106 		var file = new LocalFile(TEST_FILE);
107 		try (var is = file.read()) {
108 			var p = new Properties();
109 			p.load(new StringReader(read(is, Files.size(TEST_FILE))));
110 			assertEquals("files/Test3.properties", p.get("file"));
111 		}
112 	}
113 
114 	@Test void d02_read_filesystem_multipleTimes() throws IOException {
115 		var file = new LocalFile(TEST_FILE);
116 		String content1;
117 		try (var is1 = file.read()) {
118 			content1 = read(is1, Files.size(TEST_FILE));
119 			assertNotNull(content1);
120 		}
121 		try (var is2 = file.read()) {
122 			var content2 = read(is2, Files.size(TEST_FILE));
123 			assertNotNull(content2);
124 			assertEquals(content1, content2);
125 		}
126 	}
127 
128 	//====================================================================================================
129 	// read() tests - classpath
130 	//====================================================================================================
131 	@Test void d03_read_classpath() throws IOException {
132 		var file = new LocalFile(LocalFile_Test.class, "/files/Test3.properties");
133 		try (var is = file.read()) {
134 			assertNotNull(is);
135 			var p = new Properties();
136 			p.load(new StringReader(read(is)));
137 			assertEquals("files/Test3.properties", p.get("file"));
138 		}
139 	}
140 
141 	@Test void d04_read_classpath_nonexistent() {
142 		var file = new LocalFile(LocalFile_Test.class, "/nonexistent.properties");
143 		assertThrows(IOException.class, () -> {
144 			file.read();
145 		});
146 	}
147 
148 	//====================================================================================================
149 	// cache() tests
150 	//====================================================================================================
151 	@Test void e01_cache_filesystem() throws IOException {
152 		var file = new LocalFile(TEST_FILE);
153 		var cached = file.cache();
154 		assertSame(file, cached);
155 		// After caching, read should return cached content
156 		String content1;
157 		try (var is1 = file.read()) {
158 			content1 = read(is1, Files.size(TEST_FILE));
159 		}
160 		try (var is2 = file.read()) {
161 			var content2 = read(is2, Files.size(TEST_FILE));
162 			assertEquals(content1, content2);
163 		}
164 	}
165 
166 	@Test void e02_cache_classpath() throws IOException {
167 		var file = new LocalFile(LocalFile_Test.class, "/files/Test3.properties");
168 		var cached = file.cache();
169 		assertSame(file, cached);
170 		// After caching, read should return cached content
171 		String content1;
172 		try (var is1 = file.read()) {
173 			content1 = read(is1, 1000);
174 		}
175 		try (var is2 = file.read()) {
176 			var content2 = read(is2, 1000);
177 			assertEquals(content1, content2);
178 		}
179 	}
180 
181 	@Test void e03_cache_multipleCalls() throws IOException {
182 		var file = new LocalFile(TEST_FILE);
183 		file.cache();
184 		file.cache(); // Should be safe to call multiple times
185 		file.cache();
186 		try (var is = file.read()) {
187 			assertNotNull(is);
188 		}
189 	}
190 
191 	//====================================================================================================
192 	// size() tests
193 	//====================================================================================================
194 	@Test void f01_size_filesystem() throws IOException {
195 		var file = new LocalFile(TEST_FILE);
196 		var size = file.size();
197 		assertTrue(size > 0);
198 		assertEquals(Files.size(TEST_FILE), size);
199 	}
200 
201 	@Test void f02_size_classpath() throws IOException {
202 		var file = new LocalFile(LocalFile_Test.class, "files/Test3.properties");
203 		var size = file.size();
204 		assertEquals(-1, size); // Classpath files return -1
205 	}
206 }
207