|
| 1 | +package io.snyk.eclipse.plugin.utils; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.mockStatic; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.eclipse.core.resources.IProject; |
| 13 | +import org.eclipse.core.resources.IWorkspace; |
| 14 | +import org.eclipse.core.resources.IWorkspaceRoot; |
| 15 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 16 | +import org.eclipse.core.runtime.IPath; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.mockito.MockedStatic; |
| 19 | + |
| 20 | +class ResourceUtilsTest { |
| 21 | + |
| 22 | + private IProject createMockProject(String name, String absolutePath) { |
| 23 | + IProject project = mock(IProject.class); |
| 24 | + when(project.getName()).thenReturn(name); |
| 25 | + when(project.isAccessible()).thenReturn(true); |
| 26 | + when(project.isDerived()).thenReturn(false); |
| 27 | + when(project.isHidden()).thenReturn(false); |
| 28 | + |
| 29 | + IPath location = mock(IPath.class); |
| 30 | + when(location.toPath()).thenReturn(Path.of(absolutePath)); |
| 31 | + when(project.getLocation()).thenReturn(location); |
| 32 | + |
| 33 | + return project; |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testIndependentProjectsAreAllReturned() { |
| 38 | + IProject projectA = createMockProject("GCASharedServicesMain", |
| 39 | + "/Users/test/github/repo-apc-common/GCASharedServicesMain"); |
| 40 | + IProject projectB = createMockProject("ei.core.service", |
| 41 | + "/Users/test/github/repo-core-services/ei.core.service"); |
| 42 | + IProject projectC = createMockProject("gc-ap-portfolio-foundation-service", |
| 43 | + "/Users/test/github/repo-portfolio-foundation-service/gc-ap-portfolio-foundation-service"); |
| 44 | + |
| 45 | + IWorkspaceRoot root = mock(IWorkspaceRoot.class); |
| 46 | + when(root.getProjects()).thenReturn(new IProject[] { projectA, projectB, projectC }); |
| 47 | + |
| 48 | + IWorkspace workspace = mock(IWorkspace.class); |
| 49 | + when(workspace.getRoot()).thenReturn(root); |
| 50 | + |
| 51 | + try (MockedStatic<ResourcesPlugin> resourcesPluginMock = mockStatic(ResourcesPlugin.class); |
| 52 | + MockedStatic<SnykLogger> loggerMock = mockStatic(SnykLogger.class)) { |
| 53 | + resourcesPluginMock.when(ResourcesPlugin::getWorkspace).thenReturn(workspace); |
| 54 | + |
| 55 | + List<IProject> result = ResourceUtils.getAccessibleTopLevelProjects(); |
| 56 | + |
| 57 | + assertEquals(3, result.size(), "All 3 independent projects should be returned"); |
| 58 | + assertTrue(result.contains(projectA)); |
| 59 | + assertTrue(result.contains(projectB)); |
| 60 | + assertTrue(result.contains(projectC)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testSubProjectIsFilteredOut() { |
| 66 | + IProject parent = createMockProject("parent", "/Users/test/repos/parent"); |
| 67 | + IProject child = createMockProject("child", "/Users/test/repos/parent/modules/child"); |
| 68 | + |
| 69 | + IWorkspaceRoot root = mock(IWorkspaceRoot.class); |
| 70 | + when(root.getProjects()).thenReturn(new IProject[] { parent, child }); |
| 71 | + |
| 72 | + IWorkspace workspace = mock(IWorkspace.class); |
| 73 | + when(workspace.getRoot()).thenReturn(root); |
| 74 | + |
| 75 | + try (MockedStatic<ResourcesPlugin> resourcesPluginMock = mockStatic(ResourcesPlugin.class); |
| 76 | + MockedStatic<SnykLogger> loggerMock = mockStatic(SnykLogger.class)) { |
| 77 | + resourcesPluginMock.when(ResourcesPlugin::getWorkspace).thenReturn(workspace); |
| 78 | + |
| 79 | + List<IProject> result = ResourceUtils.getAccessibleTopLevelProjects(); |
| 80 | + |
| 81 | + assertEquals(1, result.size(), "Only the parent project should be returned"); |
| 82 | + assertTrue(result.contains(parent)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testProjectAfterSubProjectIsNotDropped() { |
| 88 | + // This is the exact bug scenario from IDE-1083: |
| 89 | + // After sorting by path, if a sub-project sets add=false and the flag is never |
| 90 | + // reset, all subsequent independent projects are silently dropped. |
| 91 | + IProject parent = createMockProject("parent", "/Users/test/repos/aaa-parent"); |
| 92 | + IProject child = createMockProject("child", "/Users/test/repos/aaa-parent/modules/child"); |
| 93 | + IProject independent = createMockProject("independent", "/Users/test/repos/zzz-independent"); |
| 94 | + |
| 95 | + IWorkspaceRoot root = mock(IWorkspaceRoot.class); |
| 96 | + when(root.getProjects()).thenReturn(new IProject[] { parent, child, independent }); |
| 97 | + |
| 98 | + IWorkspace workspace = mock(IWorkspace.class); |
| 99 | + when(workspace.getRoot()).thenReturn(root); |
| 100 | + |
| 101 | + try (MockedStatic<ResourcesPlugin> resourcesPluginMock = mockStatic(ResourcesPlugin.class); |
| 102 | + MockedStatic<SnykLogger> loggerMock = mockStatic(SnykLogger.class)) { |
| 103 | + resourcesPluginMock.when(ResourcesPlugin::getWorkspace).thenReturn(workspace); |
| 104 | + |
| 105 | + List<IProject> result = ResourceUtils.getAccessibleTopLevelProjects(); |
| 106 | + |
| 107 | + assertEquals(2, result.size(), "Parent and independent project should both be returned"); |
| 108 | + assertTrue(result.contains(parent), "Parent project must be present"); |
| 109 | + assertTrue(result.contains(independent), "Independent project must not be dropped after sub-project filtering"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testInaccessibleProjectIsFiltered() { |
| 115 | + IProject accessible = createMockProject("accessible", "/Users/test/repos/accessible"); |
| 116 | + IProject inaccessible = createMockProject("inaccessible", "/Users/test/repos/inaccessible"); |
| 117 | + when(inaccessible.isAccessible()).thenReturn(false); |
| 118 | + |
| 119 | + IWorkspaceRoot root = mock(IWorkspaceRoot.class); |
| 120 | + when(root.getProjects()).thenReturn(new IProject[] { accessible, inaccessible }); |
| 121 | + |
| 122 | + IWorkspace workspace = mock(IWorkspace.class); |
| 123 | + when(workspace.getRoot()).thenReturn(root); |
| 124 | + |
| 125 | + try (MockedStatic<ResourcesPlugin> resourcesPluginMock = mockStatic(ResourcesPlugin.class); |
| 126 | + MockedStatic<SnykLogger> loggerMock = mockStatic(SnykLogger.class)) { |
| 127 | + resourcesPluginMock.when(ResourcesPlugin::getWorkspace).thenReturn(workspace); |
| 128 | + |
| 129 | + List<IProject> result = ResourceUtils.getAccessibleTopLevelProjects(); |
| 130 | + |
| 131 | + assertEquals(1, result.size()); |
| 132 | + assertTrue(result.contains(accessible)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testDerivedProjectIsFiltered() { |
| 138 | + IProject normal = createMockProject("normal", "/Users/test/repos/normal"); |
| 139 | + IProject derived = createMockProject("derived", "/Users/test/repos/derived"); |
| 140 | + when(derived.isDerived()).thenReturn(true); |
| 141 | + |
| 142 | + IWorkspaceRoot root = mock(IWorkspaceRoot.class); |
| 143 | + when(root.getProjects()).thenReturn(new IProject[] { normal, derived }); |
| 144 | + |
| 145 | + IWorkspace workspace = mock(IWorkspace.class); |
| 146 | + when(workspace.getRoot()).thenReturn(root); |
| 147 | + |
| 148 | + try (MockedStatic<ResourcesPlugin> resourcesPluginMock = mockStatic(ResourcesPlugin.class); |
| 149 | + MockedStatic<SnykLogger> loggerMock = mockStatic(SnykLogger.class)) { |
| 150 | + resourcesPluginMock.when(ResourcesPlugin::getWorkspace).thenReturn(workspace); |
| 151 | + |
| 152 | + List<IProject> result = ResourceUtils.getAccessibleTopLevelProjects(); |
| 153 | + |
| 154 | + assertEquals(1, result.size()); |
| 155 | + assertTrue(result.contains(normal)); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void testHiddenProjectIsFiltered() { |
| 161 | + IProject visible = createMockProject("visible", "/Users/test/repos/visible"); |
| 162 | + IProject hidden = createMockProject("hidden", "/Users/test/repos/hidden"); |
| 163 | + when(hidden.isHidden()).thenReturn(true); |
| 164 | + |
| 165 | + IWorkspaceRoot root = mock(IWorkspaceRoot.class); |
| 166 | + when(root.getProjects()).thenReturn(new IProject[] { visible, hidden }); |
| 167 | + |
| 168 | + IWorkspace workspace = mock(IWorkspace.class); |
| 169 | + when(workspace.getRoot()).thenReturn(root); |
| 170 | + |
| 171 | + try (MockedStatic<ResourcesPlugin> resourcesPluginMock = mockStatic(ResourcesPlugin.class); |
| 172 | + MockedStatic<SnykLogger> loggerMock = mockStatic(SnykLogger.class)) { |
| 173 | + resourcesPluginMock.when(ResourcesPlugin::getWorkspace).thenReturn(workspace); |
| 174 | + |
| 175 | + List<IProject> result = ResourceUtils.getAccessibleTopLevelProjects(); |
| 176 | + |
| 177 | + assertEquals(1, result.size()); |
| 178 | + assertTrue(result.contains(visible)); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments