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.nio.file.*;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  class LocalDir_Test extends TestBase {
27  
28  	private static final Path TEST_DIR = Paths.get("src/test/resources/files");
29  
30  	//====================================================================================================
31  	// Constructor tests - filesystem
32  	//====================================================================================================
33  	@Test void a01_constructorWithPath() {
34  		var dir = new LocalDir(TEST_DIR);
35  		assertNotNull(dir);
36  	}
37  
38  	@Test void a02_constructorWithPath_null() {
39  		assertThrows(IllegalArgumentException.class, () -> {
40  			new LocalDir((Path)null);
41  		});
42  	}
43  
44  	//====================================================================================================
45  	// Constructor tests - classpath
46  	//====================================================================================================
47  	@Test void b01_constructorWithClassAndPath_null() {
48  		var dir = new LocalDir(LocalDir_Test.class, null);
49  		assertNotNull(dir);
50  	}
51  
52  	@Test void b02_constructorWithClassAndPath_empty() {
53  		var dir = new LocalDir(LocalDir_Test.class, "");
54  		assertNotNull(dir);
55  	}
56  
57  	@Test void b03_constructorWithClassAndPath_absolute() {
58  		var dir = new LocalDir(LocalDir_Test.class, "/files");
59  		assertNotNull(dir);
60  	}
61  
62  	@Test void b04_constructorWithClassAndPath_relative() {
63  		var dir = new LocalDir(LocalDir_Test.class, "files");
64  		assertNotNull(dir);
65  	}
66  
67  	@Test void b05_constructorWithClassAndPath_root() {
68  		var dir = new LocalDir(LocalDir_Test.class, "/");
69  		assertNotNull(dir);
70  	}
71  
72  	@Test void b06_constructorWithClassAndPath_nullClass() {
73  		assertThrows(IllegalArgumentException.class, () -> {
74  			new LocalDir((Class<?>)null, "path");
75  		});
76  	}
77  
78  	@Test void b07_constructorWithClassAndPath_trailingSlashes() {
79  		var dir = new LocalDir(LocalDir_Test.class, "files/");
80  		assertNotNull(dir);
81  		// Trailing slashes should be trimmed
82  	}
83  
84  	//====================================================================================================
85  	// resolve() tests - filesystem
86  	//====================================================================================================
87  	@Test void c01_resolve_filesystem() {
88  		var dir = new LocalDir(TEST_DIR);
89  		var file = dir.resolve("Test3.properties");
90  		assertNotNull(file);
91  		assertEquals("Test3.properties", file.getName());
92  	}
93  
94  	@Test void c02_resolve_filesystem_nonexistent() {
95  		var dir = new LocalDir(TEST_DIR);
96  		var file = dir.resolve("nonexistent.properties");
97  		assertNull(file);
98  	}
99  
100 	@Test void c03_resolve_filesystem_directory() {
101 		var dir = new LocalDir(TEST_DIR);
102 		// Resolving a directory should return null
103 		assertDoesNotThrow(()->dir.resolve("."));
104 		// May be null if "." is treated as a directory
105 	}
106 
107 	//====================================================================================================
108 	// resolve() tests - classpath
109 	//====================================================================================================
110 	@Test void d01_resolve_classpath() {
111 		var dir = new LocalDir(LocalDir_Test.class, "/files");
112 		var file = dir.resolve("Test3.properties");
113 		assertNotNull(file);
114 		assertEquals("Test3.properties", file.getName());
115 	}
116 
117 	@Test void d02_resolve_classpath_nonexistent() {
118 		var dir = new LocalDir(LocalDir_Test.class, "files");
119 		var file = dir.resolve("nonexistent.properties");
120 		assertNull(file);
121 	}
122 
123 	@Test void d03_resolve_classpath_nullPath() {
124 		var dir = new LocalDir(LocalDir_Test.class, null);
125 		assertDoesNotThrow(()->dir.resolve("files/Test3.properties"));
126 		// May or may not be null depending on classpath structure
127 	}
128 
129 	@Test void d04_resolve_classpath_absolutePath() {
130 		var dir = new LocalDir(LocalDir_Test.class, "/");
131 		var file = dir.resolve("files/Test3.properties");
132 		assertNotNull(file);
133 	}
134 
135 	@Test void d05_resolve_classpath_directory() {
136 		// Resolving a directory should return null (covers line 139 in LocalDir)
137 		var dir = new LocalDir(LocalDir_Test.class, "/");
138 		var file = dir.resolve("files");
139 		// When unpackaged, "files" is a directory, so resolve should return null
140 		// This tests the isClasspathFile check that returns false for directories
141 		assertNull(file);
142 	}
143 
144 	//====================================================================================================
145 	// equals() and hashCode() tests
146 	//====================================================================================================
147 	@Test void e01_equals_filesystem() {
148 		var dir1 = new LocalDir(TEST_DIR);
149 		var dir2 = new LocalDir(TEST_DIR);
150 		assertEquals(dir1, dir2);
151 		assertEquals(dir1.hashCode(), dir2.hashCode());
152 	}
153 
154 	@Test void e02_equals_filesystem_different() {
155 		var dir1 = new LocalDir(TEST_DIR);
156 		var dir2 = new LocalDir(Paths.get("src/test/resources"));
157 		assertNotEquals(dir1, dir2);
158 	}
159 
160 	@Test void e03_equals_classpath() {
161 		var dir1 = new LocalDir(LocalDir_Test.class, "files");
162 		var dir2 = new LocalDir(LocalDir_Test.class, "files");
163 		assertEquals(dir1, dir2);
164 		assertEquals(dir1.hashCode(), dir2.hashCode());
165 	}
166 
167 	@Test void e04_equals_classpath_different() {
168 		var dir1 = new LocalDir(LocalDir_Test.class, "files");
169 		var dir2 = new LocalDir(LocalDir_Test.class, "other");
170 		assertNotEquals(dir1, dir2);
171 	}
172 
173 	@Test void e05_equals_differentTypes() {
174 		var dir1 = new LocalDir(TEST_DIR);
175 		var dir2 = new LocalDir(LocalDir_Test.class, "files");
176 		assertNotEquals(dir1, dir2);
177 	}
178 
179 	@Test void e06_equals_notLocalDir() {
180 		var dir = new LocalDir(TEST_DIR);
181 		assertNotEquals(dir, "not a LocalDir");
182 		assertNotEquals(dir, null);
183 		assertNotEquals(dir, new Object());
184 	}
185 
186 	//====================================================================================================
187 	// toString() tests
188 	//====================================================================================================
189 	@Test void f01_toString_filesystem() {
190 		var dir = new LocalDir(TEST_DIR);
191 		var str = dir.toString();
192 		assertTrue(str.contains("files") || str.contains("test"));
193 	}
194 
195 	@Test void f02_toString_classpath() {
196 		var dir = new LocalDir(LocalDir_Test.class, "files");
197 		var str = dir.toString();
198 		assertTrue(str.contains("LocalDir_Test") || str.contains("files"));
199 	}
200 
201 	@Test void f03_toString_classpath_null() {
202 		var dir = new LocalDir(LocalDir_Test.class, null);
203 		var str = dir.toString();
204 		assertTrue(str.contains("LocalDir_Test"));
205 	}
206 
207 	@Test void f04_toString_classpath_root() {
208 		var dir = new LocalDir(LocalDir_Test.class, "/");
209 		var str = dir.toString();
210 		assertTrue(str.contains("LocalDir_Test") && str.contains("/"));
211 	}
212 }
213