44import static org .mockito .Mockito .*;
55
66import java .util .ArrayList ;
7+ import java .util .Collections ;
78import java .util .List ;
8-
9+ import org .eclipse .ui .PlatformUI ;
10+ import org .eclipse .ui .IWorkbench ;
11+ import org .eclipse .core .runtime .CoreException ;
12+ import org .eclipse .core .resources .IWorkspaceRoot ;
13+
14+ import org .eclipse .core .resources .IFile ;
15+ import org .eclipse .core .resources .IMarker ;
16+ import org .eclipse .core .resources .IResource ;
17+ import org .eclipse .core .resources .IResourceProxyVisitor ;
18+ import org .eclipse .core .resources .IWorkspace ;
19+ import org .eclipse .core .resources .ResourcesPlugin ;
20+ import org .eclipse .e4 .core .services .events .IEventBroker ;
921import org .eclipse .jface .action .Action ;
1022import org .eclipse .jface .viewers .ComboViewer ;
1123import org .eclipse .jface .viewers .TreeViewer ;
1224import org .eclipse .swt .widgets .Combo ;
1325import org .junit .jupiter .api .Test ;
1426import org .mockito .MockedStatic ;
1527import org .mockito .Mockito ;
28+ import org .mockito .stubbing .Answer ;
1629
30+ import com .checkmarx .ast .results .result .Data ;
31+ import com .checkmarx .ast .results .result .Node ;
32+ import com .checkmarx .ast .results .result .Result ;
33+ import com .checkmarx .eclipse .enums .Severity ;
1734import com .checkmarx .eclipse .properties .Preferences ;
35+ import com .checkmarx .eclipse .utils .PluginConstants ;
1836import com .checkmarx .eclipse .utils .PluginUtils ;
1937import com .checkmarx .eclipse .views .DataProvider ;
2038import com .checkmarx .eclipse .views .DisplayModel ;
2139import com .checkmarx .eclipse .views .filters .FilterState ;
40+ import org .eclipse .core .runtime .IStatus ;
2241
2342public class PluginUtilsTest {
2443
@@ -118,7 +137,6 @@ void testMessageCreation() {
118137
119138 @ Test
120139 void testShowMessage () {
121-
122140 DisplayModel root = new DisplayModel .DisplayModelBuilder ("root" ).build ();
123141 TreeViewer viewer = mock (TreeViewer .class );
124142
@@ -167,4 +185,115 @@ void testAreCredentialsDefinedFalse() {
167185 assertFalse (result );
168186 }
169187 }
188+
189+ @ Test
190+ void testGetEventBroker () {
191+ IEventBroker mockBroker = mock (IEventBroker .class );
192+ IWorkbench mockWorkbench = mock (IWorkbench .class );
193+ try (MockedStatic <PlatformUI > platformUI = Mockito .mockStatic (PlatformUI .class )) {
194+ platformUI .when (PlatformUI ::getWorkbench ).thenReturn (mockWorkbench );
195+ when (mockWorkbench .getService (IEventBroker .class )).thenReturn (mockBroker );
196+ IEventBroker result = PluginUtils .getEventBroker ();
197+ assertSame (mockBroker , result );
198+ }
199+ }
200+
201+ @ Test
202+ void testAddVulnerabilitiesToProblemsView_normal () throws Exception {
203+ Result mockResult = mock (Result .class );
204+ Node mockNode = mock (Node .class );
205+ IFile mockFile = mock (IFile .class );
206+ IMarker mockMarker = mock (IMarker .class );
207+ List <Node > nodeList = Collections .singletonList (mockNode );
208+ List <IFile > fileList = Collections .singletonList (mockFile );
209+ List <Result > resultsList = Collections .singletonList (mockResult );
210+
211+ Data mockData = mock (Data .class );
212+ when (mockData .getNodes ()).thenReturn (nodeList );
213+ when (mockResult .getData ()).thenReturn (mockData );
214+ when (mockNode .getFileName ()).thenReturn ("file.java" );
215+ when (mockNode .getName ()).thenReturn ("VulnName" );
216+ when (mockNode .getLine ()).thenReturn (42 );
217+ when (mockFile .createMarker (IMarker .PROBLEM )).thenReturn (mockMarker );
218+ when (mockResult .getSeverity ()).thenReturn (Severity .HIGH .name ());
219+
220+ try (MockedStatic <PluginUtils > pu = Mockito .mockStatic (PluginUtils .class , Mockito .CALLS_REAL_METHODS )) {
221+ pu .when (() -> PluginUtils .findFileInWorkspace ("file.java" )).thenReturn (fileList );
222+ PluginUtils .addVulnerabilitiesToProblemsView (resultsList );
223+ verify (mockMarker ).setAttribute (IMarker .MESSAGE , "VulnName" );
224+ verify (mockMarker ).setAttribute (IMarker .LOCATION , "line 42" );
225+ verify (mockMarker ).setAttribute (IMarker .LINE_NUMBER , 42 );
226+ verify (mockMarker ).setAttribute (IMarker .SOURCE_ID , PluginConstants .PROBLEM_SOURCE_ID );
227+ verify (mockMarker ).setAttribute (IMarker .SEVERITY , IMarker .SEVERITY_ERROR );
228+ }
229+ }
230+
231+ @ Test
232+ void testFindFileInWorkspace_normal () throws Exception {
233+ IFile mockFile = mock (IFile .class );
234+ IWorkspaceRoot root = mock (IWorkspaceRoot .class );
235+ IWorkspace workspace = mock (IWorkspace .class );
236+ IResourceProxyVisitor [] visitorHolder = new IResourceProxyVisitor [1 ];
237+ try (MockedStatic <ResourcesPlugin > rp = Mockito .mockStatic (ResourcesPlugin .class )) {
238+ rp .when (ResourcesPlugin ::getWorkspace ).thenReturn (workspace );
239+ when (workspace .getRoot ()).thenReturn (root );
240+ doAnswer ((Answer <Void >) invocation -> {
241+ visitorHolder [0 ] = invocation .getArgument (0 );
242+ // Simulate visit
243+ return null ;
244+ }).when (root ).accept (any (IResourceProxyVisitor .class ), anyInt ());
245+ List <IFile > files = PluginUtils .findFileInWorkspace ("file.java" );
246+ assertNotNull (files );
247+ }
248+ }
249+
250+ @ Test
251+ void testFindFileInWorkspace_exception () throws Exception {
252+ IWorkspaceRoot root = mock (IWorkspaceRoot .class );
253+ IWorkspace workspace = mock (IWorkspace .class );
254+ try (MockedStatic <ResourcesPlugin > rp = Mockito .mockStatic (ResourcesPlugin .class )) {
255+ rp .when (ResourcesPlugin ::getWorkspace ).thenReturn (workspace );
256+ when (workspace .getRoot ()).thenReturn (root );
257+ doThrow (new RuntimeException ("fail" )).when (root ).accept (any (IResourceProxyVisitor .class ), anyInt ());
258+ List <IFile > files = PluginUtils .findFileInWorkspace ("file.java" );
259+ assertNotNull (files );
260+ assertTrue (files .isEmpty ());
261+ }
262+ }
263+
264+ @ Test
265+ void testClearVulnerabilitiesFromProblemsView_normal () throws Exception {
266+ IWorkspace workspace = mock (IWorkspace .class );
267+ IWorkspaceRoot resource = mock (IWorkspaceRoot .class );
268+ IMarker marker1 = mock (IMarker .class );
269+ IMarker marker2 = mock (IMarker .class );
270+ when (workspace .getRoot ()).thenReturn (resource );
271+ when (resource .findMarkers (IMarker .MARKER , true , IResource .DEPTH_INFINITE )).thenReturn (new IMarker []{marker1 , marker2 });
272+ when (marker1 .getAttribute (IMarker .SOURCE_ID )).thenReturn (PluginConstants .PROBLEM_SOURCE_ID );
273+ when (marker2 .getAttribute (IMarker .SOURCE_ID )).thenReturn ("other" );
274+ try (MockedStatic <ResourcesPlugin > rp = Mockito .mockStatic (ResourcesPlugin .class )) {
275+ rp .when (ResourcesPlugin ::getWorkspace ).thenReturn (workspace );
276+ PluginUtils .clearVulnerabilitiesFromProblemsView ();
277+ verify (marker1 ).delete ();
278+ verify (marker2 , never ()).delete ();
279+ }
280+ }
281+
282+ @ Test
283+ void testClearVulnerabilitiesFromProblemsView_coreException () throws Exception {
284+ IWorkspace workspace = mock (IWorkspace .class );
285+ IWorkspaceRoot resource = mock (IWorkspaceRoot .class );
286+ IMarker marker1 = mock (IMarker .class );
287+ when (workspace .getRoot ()).thenReturn (resource );
288+ when (resource .findMarkers (IMarker .MARKER , true , IResource .DEPTH_INFINITE )).thenReturn (new IMarker []{marker1 });
289+ when (marker1 .getAttribute (IMarker .SOURCE_ID )).thenReturn (PluginConstants .PROBLEM_SOURCE_ID );
290+ IStatus status = mock (IStatus .class );
291+ when (status .getMessage ()).thenReturn ("error" );
292+ doThrow (new CoreException (status )).when (marker1 ).delete ();
293+ try (MockedStatic <ResourcesPlugin > rp = Mockito .mockStatic (ResourcesPlugin .class )) {
294+ rp .when (ResourcesPlugin ::getWorkspace ).thenReturn (workspace );
295+ PluginUtils .clearVulnerabilitiesFromProblemsView ();
296+ // Should not throw
297+ }
298+ }
170299}
0 commit comments