forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceUtilsTest.java
More file actions
160 lines (128 loc) · 5.52 KB
/
Copy pathResourceUtilsTest.java
File metadata and controls
160 lines (128 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.utils;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import com.microsoft.copilot.eclipse.core.utils.FileUtils;
/**
* Tests for ResourceUtils core functionality
*/
@ExtendWith(MockitoExtension.class)
class ResourceUtilsTest {
@Mock
private IFile mockValidFile;
@Mock
private IFile mockInvalidFile;
@Mock
private IFolder mockFolder;
@Mock
private IProject mockProjectA;
@Mock
private IProject mockProjectB;
private MockedStatic<FileUtils> mockedFileUtils;
private MockedStatic<LSPEclipseUtils> mockedLspUtils;
private static final String nameProjectA= "ProjectA";
private static final String nameProjectB= "ProjectB";
@BeforeEach
void setUp() {
mockedFileUtils = mockStatic(FileUtils.class);
mockedFileUtils.when(() -> FileUtils.isExcludedFromReferencedFiles(mockValidFile)).thenReturn(false);
mockedFileUtils.when(() -> FileUtils.isExcludedFromReferencedFiles(mockInvalidFile)).thenReturn(true);
mockedLspUtils = mockStatic(LSPEclipseUtils.class);
mockedLspUtils.when(() -> LSPEclipseUtils.toWorkspaceFolder(mockProjectA))
.thenReturn(new WorkspaceFolder("file:///" + nameProjectA, nameProjectA));
mockedLspUtils.when(() -> LSPEclipseUtils.toWorkspaceFolder(mockProjectB))
.thenReturn(new WorkspaceFolder("file:///" + nameProjectB, nameProjectB));
}
@AfterEach
void tearDown() {
if (mockedFileUtils != null) {
mockedFileUtils.close();
}
if (mockedLspUtils != null) {
mockedLspUtils.close();
}
}
@Test
void testComplexMixedSelectionWithMocks() {
var complexSelection = new StructuredSelection(Arrays.asList(mockValidFile, mockInvalidFile, mockFolder,
"just a string", Integer.valueOf(42), new Object()));
ResourceUtils.SelectionStats stats = ResourceUtils.analyzeSelection(complexSelection);
assertEquals(1, stats.fileCount, "Should have 1 valid file");
assertEquals(1, stats.folderCount, "Should have 1 folder");
assertEquals(4, stats.invalidCount,
"Should have 4 invalid objects (excluded file + string + number + Object)");
assertFalse(stats.hasOnlyFiles(), "Should not have only files (has folders and invalid objects)");
assertFalse(stats.hasOnlyFolders(), "Should not have only folders (has files and invalid objects)");
assertFalse(stats.hasOnlyValidResources(), "Should not have only valid resources (has invalid objects)");
}
@Test
void testCollectValidResourcesWithMocks() {
var mixedSelection = new StructuredSelection(
Arrays.asList(mockValidFile, mockInvalidFile, mockFolder, "invalid object"));
var validResources = ResourceUtils.collectValidResources(mixedSelection);
assertNotNull(validResources);
assertEquals(2, validResources.size(), "Should contain valid file and folder");
assertTrue(validResources.contains(mockValidFile), "Should contain valid file");
assertTrue(validResources.contains(mockFolder), "Should contain folder");
assertFalse(validResources.contains(mockInvalidFile), "Should not contain excluded file");
}
private static Stream<List<IResource>> provideResourcesForNeverNullTest() {
return Stream.of(
null,
List.of(),
Arrays.asList((IResource) null),
List.of(mock(IFolder.class)),
List.of(mock(IProject.class))
);
}
@ParameterizedTest
@MethodSource("provideResourcesForNeverNullTest")
void testDeriveWorkspaceFoldersReturnsNeverNull(List<IResource> resources) {
List<WorkspaceFolder> result = ResourceUtils.deriveWorkspaceFoldersFrom(resources);
assertNotNull(result);
assertTrue(result.isEmpty());
}
@Test
void testDeriveWorkspaceFoldersWithMultipleResources() {
when(mockValidFile.getProject()).thenReturn(mockProjectA);
when(mockFolder.getProject()).thenReturn(mockProjectB);
when(mockProjectA.isAccessible()).thenReturn(true);
when(mockProjectB.isAccessible()).thenReturn(true);
List<WorkspaceFolder> result = ResourceUtils.deriveWorkspaceFoldersFrom(List.of(mockValidFile, mockFolder));
assertNotNull(result);
assertFalse(result.isEmpty());
assertEquals(2, result.size(), "Both projects from both resources should be derived as workspace folders");
}
@Test
void testDeriveWorkspaceFoldersDoesNotReturnDuplicates() {
when(mockValidFile.getProject()).thenReturn(mockProjectA);
when(mockFolder.getProject()).thenReturn(mockProjectA);
when(mockProjectA.isAccessible()).thenReturn(true);
when(mockProjectA.getName()).thenReturn(nameProjectA);
List<WorkspaceFolder> result = ResourceUtils.deriveWorkspaceFoldersFrom(List.of(mockValidFile, mockFolder));
assertNotNull(result);
assertFalse(result.isEmpty());
assertEquals(1, result.size(), "Projects in derived workspaces folders should be unique, no duplicates.");
assertEquals(mockProjectA.getName(), result.get(0).getName());
}
}