From b7d55d5c1ae281219137ae6869be8efa3a933311 Mon Sep 17 00:00:00 2001 From: Karoliine Holter Date: Fri, 12 Jan 2024 15:22:25 +0200 Subject: [PATCH] Write test for aborting analysis --- src/test/java/GoblintAnalysisTest.java | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/test/java/GoblintAnalysisTest.java diff --git a/src/test/java/GoblintAnalysisTest.java b/src/test/java/GoblintAnalysisTest.java new file mode 100644 index 0000000..5374949 --- /dev/null +++ b/src/test/java/GoblintAnalysisTest.java @@ -0,0 +1,54 @@ +import analysis.GoblintAnalysis; +import api.GoblintService; +import api.messages.params.AnalyzeParams; +import com.ibm.wala.classLoader.Module; +import goblintserver.GoblintConfWatcher; +import goblintserver.GoblintServer; +import gobpie.GobPieConfiguration; +import magpiebridge.core.AnalysisConsumer; +import magpiebridge.core.MagpieServer; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.Collection; +import java.util.concurrent.CompletableFuture; + +import static org.mockito.Mockito.*; + +class GoblintAnalysisTest { + + @Test + void abortAnalysis() throws IOException { + + // Mock everything needed for creating GoblintAnalysis + MagpieServer magpieServer = mock(MagpieServer.class); + GoblintServer goblintServer = mock(GoblintServer.class); + GoblintService goblintService = mock(GoblintService.class); + GobPieConfiguration gobPieConfiguration = mock(GobPieConfiguration.class); + GoblintConfWatcher goblintConfWatcher = mock(GoblintConfWatcher.class); + + GoblintAnalysis goblintAnalysis = new GoblintAnalysis(magpieServer, goblintServer, goblintService, gobPieConfiguration, goblintConfWatcher); + + // Mock that GoblintServer is alive and everything is fine with Goblint's configuration file + when(goblintServer.isAlive()).thenReturn(true); + when(goblintConfWatcher.refreshGoblintConfig()).thenReturn(true); + + // Mock that the analyses of goblint have started but not completed (still run) + when(goblintService.analyze(new AnalyzeParams(false))).thenReturn(new CompletableFuture<>()); + + // Mock that the incremental analysis is turned off (TODO: not sure why this is checked in reanalyze?) + when(gobPieConfiguration.useIncrementalAnalysis()).thenReturn(true); + + // Mock the arguments for calling the goblintAnalyze.analyze method + // And call the method twice + Collection files = new ArrayDeque<>(); + AnalysisConsumer analysisConsumer = mock(AnalysisConsumer.class); + goblintAnalysis.analyze(files, analysisConsumer, true); + goblintAnalysis.analyze(files, analysisConsumer, true); + + // Verify that abortAnalysis was indeed called once + verify(goblintServer).abortAnalysis(); + } + +} \ No newline at end of file