|
4 | 4 | import java.io.IOException; |
5 | 5 | import java.nio.file.Files; |
6 | 6 | import java.nio.file.Path; |
7 | | -import java.nio.file.StandardCopyOption; |
| 7 | +import java.util.stream.Stream; |
8 | 8 |
|
9 | | -import org.apache.commons.io.FileUtils; |
10 | 9 | import org.junit.jupiter.api.BeforeEach; |
11 | 10 | import org.junit.jupiter.api.Test; |
12 | 11 | import org.slf4j.Logger; |
@@ -35,10 +34,15 @@ void resetContext() { |
35 | 34 | } |
36 | 35 |
|
37 | 36 | @Test |
38 | | - void testConstructorWithValidDirectory() { |
| 37 | + void testProjectManagerFull() { |
39 | 38 |
|
40 | | - //No exception should be thrown |
41 | 39 | projectManager = new ProjectManager(ideRoot); |
| 40 | + |
| 41 | + assertThat(projectManager).isNotNull(); |
| 42 | + assertThat(projectManager.getProjectNames()).containsExactlyInAnyOrder("project-0", "project-1", "project-2", "project-3", "project-4", "project-5"); |
| 43 | + for (String projectName : projectManager.getProjectNames()) { |
| 44 | + assertThat(projectManager.getWorkspaceNames(projectName)).containsExactlyInAnyOrder("foo-test", "main"); |
| 45 | + } |
42 | 46 | } |
43 | 47 |
|
44 | 48 | @Test |
@@ -83,214 +87,87 @@ void testConstructorWithFile() throws IOException { |
83 | 87 | } |
84 | 88 | } |
85 | 89 |
|
86 | | - // ============ readProjects() Tests ============ |
87 | | - |
88 | 90 | @Test |
89 | | - void testReadProjectsWithEmptyProjectDirectory() { |
| 91 | + void testRefreshProjects() throws IOException { |
90 | 92 |
|
91 | | - context = newContext("emptyProjects", ""); |
92 | 93 | projectManager = new ProjectManager(ideRoot); |
93 | | - projectManager.refreshProjects(); |
| 94 | + assertThat(projectManager.getProjectNames()).containsExactlyInAnyOrder("project-0", "project-1", "project-2", "project-3", "project-4", "project-5"); |
94 | 95 |
|
95 | | - assertThat(projectManager.getProjectNames()).isEmpty(); |
96 | | - } |
| 96 | + Path project0 = ideRoot.resolve("project-0"); |
| 97 | + Path project6 = ideRoot.resolve("project-6"); |
| 98 | + copyDirectory(project0, project6); |
97 | 99 |
|
98 | | - @Test |
99 | | - void testReadProjectsWithValidProjects() { |
| 100 | + projectManager.refreshProjects(); |
100 | 101 |
|
101 | | - projectManager = new ProjectManager(ideRoot); |
| 102 | + // Verify that project-6 is now recognized |
| 103 | + assertThat(projectManager.getProjectNames()).containsExactlyInAnyOrder("project-0", "project-1", "project-2", "project-3", "project-4", "project-5", |
| 104 | + "project-6"); |
| 105 | + assertThat(projectManager.getWorkspaceNames("project-6")).containsExactlyInAnyOrder("foo-test", "main"); |
102 | 106 |
|
103 | | - //test folder contains baseProject to project-5 |
104 | | - assertThat(projectManager.getProjectNames()).hasSize(6).as("Should have 6 projects") |
105 | | - .containsExactlyInAnyOrder("project-0", "project-1", "project-2", "project-3", "project-4", "project-5"); |
| 107 | + // Cleanup |
| 108 | + deleteDirectory(project6); |
106 | 109 | } |
107 | 110 |
|
108 | 111 | @Test |
109 | | - void testReadProjectsIgnoresUnderscorePrefixed() throws Exception { |
| 112 | + void testReadProjectsExcludesFoldersWithoutWorkspaces() throws IOException { |
110 | 113 |
|
111 | | - // Create a project with underscore prefix |
112 | | - Path underscoreProject = ideRoot.resolve("_project"); |
113 | | - Files.createDirectories(underscoreProject); |
114 | | - Files.copy(ideRoot.resolve("project-0"), underscoreProject, StandardCopyOption.REPLACE_EXISTING); |
| 114 | + // Create a project folder without a workspaces subdirectory |
| 115 | + Path testProject = ideRoot.resolve("test-project-no-workspaces"); |
| 116 | + Files.createDirectory(testProject); |
115 | 117 |
|
116 | 118 | projectManager = new ProjectManager(ideRoot); |
117 | 119 |
|
118 | | - // Should not include _project, same as _ide directory should be ignored |
119 | | - assertThat(projectManager.getProjectNames()).doesNotContain("_project").hasSize(6); |
| 120 | + // Verify that test-project-no-workspaces is not recognized |
| 121 | + assertThat(projectManager.getProjectNames()).doesNotContain("test-project-no-workspaces"); |
| 122 | + assertThat(projectManager.getProjectNames()).containsExactlyInAnyOrder("project-0", "project-1", "project-2", "project-3", "project-4", "project-5"); |
120 | 123 |
|
121 | | - //clean up |
122 | | - Files.delete(underscoreProject); |
| 124 | + // Cleanup |
| 125 | + deleteDirectory(testProject); |
123 | 126 | } |
124 | 127 |
|
125 | 128 | @Test |
126 | | - void testReadProjectsIgnoresDirectoriesWithoutWorkspacesFolder() { |
| 129 | + void testReadProjectsExcludesUnderscorePrefixedFolders() { |
127 | 130 |
|
128 | | - context = newContext("noWorkspaces", "projectWithoutWorkspaces"); |
129 | 131 | projectManager = new ProjectManager(ideRoot); |
130 | 132 |
|
131 | | - // Should not include projectNoWorkspaces |
132 | | - assertThat(projectManager.getProjectNames()).doesNotContain("projectWithoutWorkspaces"); |
| 133 | + // Verify that _ide folder is not in the project names |
| 134 | + assertThat(projectManager.getProjectNames()).doesNotContain("_ide"); |
| 135 | + assertThat(projectManager.getProjectNames()).containsExactlyInAnyOrder("project-0", "project-1", "project-2", "project-3", "project-4", "project-5"); |
133 | 136 | } |
134 | 137 |
|
135 | | - // ============ readWorkspaces() Tests ============ |
136 | | - |
137 | | - @Test |
138 | | - void testReadWorkspacesWithMultipleWorkspaces() throws Exception { |
139 | | - |
140 | | - Path project = ideRoot.resolve("project-0"); |
141 | | - Path workspacesDir = project.resolve("workspaces"); |
142 | | - |
143 | | - // Create additional workspaces besides existing |
144 | | - Files.createDirectory(workspacesDir.resolve("dev")); |
145 | | - Files.createDirectory(workspacesDir.resolve("prod")); |
146 | | - |
147 | | - projectManager = new ProjectManager(ideRoot); |
148 | | - |
149 | | - // Should have main, dev, prod workspaces |
150 | | - assertThat(projectManager.getWorkspaceNames("project-0")).hasSize(4) |
151 | | - .containsExactlyInAnyOrder("main", "foo-test", "dev", "prod"); |
152 | | - } |
153 | | - |
154 | | - @Test |
155 | | - void testReadWorkspacesEmptyWorkspaceDirectory() { |
156 | | - |
157 | | - context = newContext("emptyWorkspaceFolders", "project-0"); |
158 | | - |
159 | | - projectManager = new ProjectManager(ideRoot); |
160 | | - |
161 | | - // Should return empty list for empty workspaces directory |
162 | | - assertThat(projectManager.getWorkspaceNames("project-0")).isEmpty(); |
163 | | - } |
| 138 | + private void copyDirectory(Path source, Path destination) throws IOException { |
164 | 139 |
|
165 | | - // ============ refreshProjects() Tests ============ |
166 | | - |
167 | | - @Test |
168 | | - void testRefreshProjectsClearsExistingData() throws Exception { |
169 | | - |
170 | | - projectManager = new ProjectManager(ideRoot); |
171 | | - |
172 | | - // Get initial projects |
173 | | - assertThat(projectManager.getProjectNames()).hasSize(6); |
174 | | - |
175 | | - // Delete a project |
176 | | - try (var stream = Files.walk(ideRoot.resolve("project-0"))) { |
177 | | - stream.forEach(p -> { |
| 140 | + Files.createDirectory(destination); |
| 141 | + try (Stream<Path> stream = Files.list(source)) { |
| 142 | + stream.forEach(sourcePath -> { |
178 | 143 | try { |
179 | | - FileUtils.deleteDirectory(p.toFile()); |
180 | | - } catch (Exception e) { |
181 | | - fail("Error deleting project for test case: {}", p, e); |
| 144 | + if (Files.isDirectory(sourcePath)) { |
| 145 | + copyDirectory(sourcePath, destination.resolve(sourcePath.getFileName())); |
| 146 | + } else { |
| 147 | + Files.copy(sourcePath, destination.resolve(sourcePath.getFileName())); |
| 148 | + } |
| 149 | + } catch (IOException e) { |
| 150 | + throw new RuntimeException("Failed to copy directory", e); |
182 | 151 | } |
183 | 152 | }); |
184 | 153 | } |
185 | | - |
186 | | - // Refresh should clear old data and reload |
187 | | - projectManager.refreshProjects(); |
188 | | - |
189 | | - assertThat(projectManager.getProjectNames()).hasSize(5).doesNotContain("project-0"); |
190 | | - } |
191 | | - |
192 | | - @Test |
193 | | - void testRefreshProjectsReloadsAll() throws Exception { |
194 | | - |
195 | | - projectManager = new ProjectManager(ideRoot); |
196 | | - |
197 | | - assertThat(projectManager.getProjectNames()).isEmpty(); |
198 | | - |
199 | | - Path newProject = ideRoot.resolve("newProject"); |
200 | | - Files.createDirectories(newProject.resolve("workspaces").resolve("main")); |
201 | | - |
202 | | - projectManager.refreshProjects(); |
203 | | - |
204 | | - assertThat(projectManager.getProjectNames()).hasSize(1).contains("newProject"); |
205 | | - |
206 | | - //clean up |
207 | | - Files.delete(newProject); |
208 | | - } |
209 | | - |
210 | | - @Test |
211 | | - void testGetWorkspaceNamesReturnsValidList() { |
212 | | - |
213 | | - projectManager = new ProjectManager(ideRoot); |
214 | | - |
215 | | - assertThat(projectManager.getWorkspaceNames("project-0")).containsExactly("foo-test", "main"); |
216 | | - } |
217 | | - |
218 | | - @Test |
219 | | - void testGetWorkspaceNamesWithInvalidProject() { |
220 | | - |
221 | | - projectManager = new ProjectManager(ideRoot); |
222 | | - |
223 | | - // Should return null for unknown project |
224 | | - assertThat(projectManager.getWorkspaceNames("unknownProject")).isNull(); |
225 | | - } |
226 | | - |
227 | | - |
228 | | - @Test |
229 | | - void testProjectWithMultipleWorkspaces() throws Exception { |
230 | | - |
231 | | - Path project = ideRoot.resolve("project-0"); |
232 | | - Path workspacesDir = project.resolve("workspaces"); |
233 | | - |
234 | | - // Create multiple workspaces |
235 | | - for (String workspace : new String[] { "dev", "test", "staging", "production" }) { |
236 | | - Files.createDirectory(workspacesDir.resolve(workspace)); |
237 | | - } |
238 | | - |
239 | | - projectManager = new ProjectManager(ideRoot); |
240 | | - |
241 | | - // Should have all workspaces including main |
242 | | - assertThat(projectManager.getWorkspaceNames("project-0")).hasSize(5) |
243 | | - .contains("main", "dev", "test", "staging", "production"); |
244 | 154 | } |
245 | 155 |
|
246 | | - @Test |
247 | | - void testMultipleProjectsWithWorkspaces() throws Exception { |
| 156 | + private void deleteDirectory(Path directory) throws IOException { |
248 | 157 |
|
249 | | - // Create different workspace structures for different projects |
250 | | - for (int i = 0; i < 6; i++) { |
251 | | - Path project = ideRoot.resolve("project-" + i); |
252 | | - Path workspacesDir = project.resolve("workspaces"); |
253 | | - |
254 | | - // Add additional workspaces to each project |
255 | | - for (int j = 1; j <= i; j++) { |
256 | | - Files.createDirectory(workspacesDir.resolve("workspace-" + j)); |
257 | | - } |
258 | | - } |
259 | | - |
260 | | - projectManager = new ProjectManager(ideRoot); |
261 | | - |
262 | | - // Verify each project has correct number of workspaces |
263 | | - for (int i = 0; i < 6; i++) { |
264 | | - String projectName = "project-" + i; |
265 | | - // Each project has "main" + i additional workspaces |
266 | | - assertThat(projectManager.getWorkspaceNames(projectName)).hasSize(i + 1); |
| 158 | + try (Stream<Path> stream = Files.list(directory)) { |
| 159 | + stream.forEach(path -> { |
| 160 | + try { |
| 161 | + if (Files.isDirectory(path)) { |
| 162 | + deleteDirectory(path); |
| 163 | + } else { |
| 164 | + Files.delete(path); |
| 165 | + } |
| 166 | + } catch (IOException e) { |
| 167 | + throw new RuntimeException("Failed to delete directory", e); |
| 168 | + } |
| 169 | + }); |
267 | 170 | } |
268 | | - } |
269 | | - |
270 | | - @Test |
271 | | - void testProjectNamesWithSpecialCharacters() throws Exception { |
272 | | - |
273 | | - Path specialProject = ideRoot.resolve("project-dash_underscore&"); |
274 | | - Files.createDirectories(specialProject.resolve("workspaces").resolve("main")); |
275 | | - |
276 | | - projectManager = new ProjectManager(ideRoot); |
277 | | - |
278 | | - assertThat(projectManager.getProjectNames()).contains("project-dash_underscore&"); |
279 | | - } |
280 | | - |
281 | | - @Test |
282 | | - void testWorkspaceNamesWithSpecialCharacters() throws Exception { |
283 | | - |
284 | | - Path project = ideRoot.resolve("project-0"); |
285 | | - Path workspacesDir = project.resolve("workspaces"); |
286 | | - |
287 | | - // Create workspaces with special characters |
288 | | - Files.createDirectory(workspacesDir.resolve("dev-environment")); |
289 | | - Files.createDirectory(workspacesDir.resolve("test_workspace")); |
290 | | - |
291 | | - projectManager = new ProjectManager(ideRoot); |
292 | | - |
293 | | - assertThat(projectManager.getWorkspaceNames("project-0")) |
294 | | - .contains("dev-environment", "test_workspace", "main"); |
| 171 | + Files.delete(directory); |
295 | 172 | } |
296 | 173 | } |
0 commit comments