99import gobpie .GobPieConfiguration ;
1010import magpiebridge .core .AnalysisConsumer ;
1111import magpiebridge .core .MagpieServer ;
12- import nl .altindag .log .LogCaptor ;
13- import org .apache .logging .log4j .LogManager ;
14- import org .apache .logging .log4j .core .LogEvent ;
1512import org .junit .jupiter .api .Test ;
13+ import org .junit .jupiter .api .extension .ExtendWith ;
14+ import uk .org .webcompere .systemstubs .jupiter .SystemStub ;
15+ import uk .org .webcompere .systemstubs .jupiter .SystemStubsExtension ;
16+ import uk .org .webcompere .systemstubs .stream .SystemOut ;
1617
17- import javax .swing .*;
18- import java .io .ByteArrayOutputStream ;
1918import java .io .IOException ;
2019import java .util .ArrayDeque ;
21- import java .util .ArrayList ;
2220import java .util .Collection ;
23- import java .util .List ;
2421import java .util .concurrent .CompletableFuture ;
2522
2623import static org .junit .jupiter .api .Assertions .assertTrue ;
2724import static org .mockito .Mockito .*;
2825
26+ @ ExtendWith (SystemStubsExtension .class )
2927class GoblintAnalysisTest {
3028
31- private final List <String > logMessages = new ArrayList <>();
32-
33-
34-
35- public void append (LogEvent event ) {
36- logMessages .add (event .getMessage ().getFormattedMessage ());
37- }
38-
39- public boolean contains (String logMessage ) {
40- return logMessages .contains (logMessage );
41- }
29+ @ SystemStub
30+ private SystemOut systemOut ;
4231
4332 /**
4433 * Mock test to ensure @analyze function
4534 * behaviour in abort situation
46- *
4735 */
4836 @ Test
4937 void abortAnalysis () throws IOException {
@@ -54,19 +42,14 @@ void abortAnalysis() throws IOException {
5442 GoblintService goblintService = mock (GoblintService .class );
5543 GobPieConfiguration gobPieConfiguration = mock (GobPieConfiguration .class );
5644 GoblintConfWatcher goblintConfWatcher = mock (GoblintConfWatcher .class );
57- org .apache .logging .log4j .Logger log = LogManager .getLogger (GoblintAnalysis .class );
58-
59-
60- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
61- String consoleOutput = byteArrayOutputStream .toString ();
6245
6346 GoblintAnalysis goblintAnalysis = new GoblintAnalysis (magpieServer , goblintServer , goblintService , gobPieConfiguration , goblintConfWatcher );
6447
6548 // Mock that GoblintServer is alive and everything is fine with Goblint's configuration file
6649 when (goblintServer .isAlive ()).thenReturn (true );
6750 when (goblintConfWatcher .refreshGoblintConfig ()).thenReturn (true );
6851
69- // Mock that the analyses of goblint have started but not completed (still run)
52+ // Mock that the analyses of Goblint have started but not completed (still run)
7053 when (goblintService .analyze (new AnalyzeParams (false ))).thenReturn (new CompletableFuture <>());
7154
7255 // Mock that the incremental analysis is turned off (TODO: not sure why this is checked in reanalyze?)
@@ -79,67 +62,57 @@ void abortAnalysis() throws IOException {
7962 goblintAnalysis .analyze (files , analysisConsumer , true );
8063 goblintAnalysis .analyze (files , analysisConsumer , true );
8164
82- System .out .println (consoleOutput );
83-
84- System .out .println ("--------------- This analysis has been aborted -------------" );
85- System .out .println (log .getName ());
86-
87- //assertTrue(consoleOutput, "--------------- This analysis has been aborted -------------");
88-
8965 // Verify that abortAnalysis was indeed called once
9066 verify (goblintServer ).abortAnalysis ();
67+ assertTrue (systemOut .getLines ().anyMatch (line -> line .contains ("--------------- This analysis has been aborted -------------" )));
9168 }
69+
9270 /**
9371 * Mock test to ensure @preAnalyse function
9472 * is functional and is called out in @analyze function
95- *
9673 */
9774
9875
9976 @ Test
100- void preAnalysetest () {
77+ void preAnalyseTest () {
10178 // Mock everything needed for creating preAnalysis
10279 MagpieServer magpieServer = mock (MagpieServer .class );
103- GoblintServer goblintServer = mock (GoblintServer .class );
10480 GoblintService goblintService = mock (GoblintService .class );
105- GobPieConfiguration gobPieConfigurationMock = mock (GobPieConfiguration .class );
81+ GobPieConfiguration gobPieConfiguration = mock (GobPieConfiguration .class );
82+ GoblintServer goblintServer = spy (new GoblintServer (magpieServer , gobPieConfiguration ));
10683 GoblintConfWatcher goblintConfWatcher = mock (GoblintConfWatcher .class );
10784
108-
109- GoblintAnalysis goblintAnalysis = new GoblintAnalysis (magpieServer , goblintServer , goblintService , gobPieConfigurationMock , goblintConfWatcher );
85+ GoblintAnalysis goblintAnalysis = new GoblintAnalysis (magpieServer , goblintServer , goblintService , gobPieConfiguration , goblintConfWatcher );
11086
11187 // Mock that GoblintServer is alive and everything is fine with Goblint's configuration file
112- when (goblintServer .isAlive ()). thenReturn ( true );
88+ doReturn ( true ). when (goblintServer ) .isAlive ();
11389 when (goblintConfWatcher .refreshGoblintConfig ()).thenReturn (true );
11490
11591 // Mock that the command to execute is not empty
116- String [] preAnalyzeCommand = new String []{"cmake" , "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" , "-B" , "build" };
117- when ( gobPieConfigurationMock .getPreAnalyzeCommand ()).thenReturn (new String []{ "cmake" , "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" , "-B" , "build" } );
92+ String [] preAnalyzeCommand = new String []{"cmake" , "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" , "-B" , "build" };
93+ when (gobPieConfiguration .getPreAnalyzeCommand ()).thenReturn (preAnalyzeCommand );
11894
119- // Mock that the analyses of goblint have started but not completed (still run)
95+ // Mock that the analyses of Goblint have started but not completed (still run)
12096 when (goblintService .analyze (new AnalyzeParams (false ))).thenReturn (new CompletableFuture <>());
12197
12298 // Mock that the incremental analysis is turned off (TODO: not sure why this is checked in reanalyze?)
123- when (gobPieConfigurationMock .useIncrementalAnalysis ()).thenReturn (true );
99+ when (gobPieConfiguration .useIncrementalAnalysis ()).thenReturn (true );
124100
125101 // Mock the arguments for calling the goblintAnalyze.analyze method
126102 Collection <? extends Module > files = new ArrayDeque <>();
127103 AnalysisConsumer analysisConsumer = mock (AnalysisConsumer .class );
128104 goblintAnalysis .analyze (files , analysisConsumer , true );
129105
130-
131106 // Verify that preAnalysis was indeed called once
132107 verify (goblintServer ).preAnalyse ();
108+ assertTrue (systemOut .getLines ().anyMatch (line -> line .contains ("Preanalyze command ran: " )));
133109 }
134110
135111 /**
136- ** Mock test to ensure @preAnalyse function
137- *
138- *
112+ * * Mock test to ensure @preAnalyse function
113+ * <p>
114+ * <p>
139115 * is functional and is called out in @analyze function
140- *
141- *
142- *
143116 */
144117 @ Test
145118 void preanalyzeError () {
@@ -161,8 +134,8 @@ void preanalyzeError() {
161134
162135 // Mock that the command to execute is not empty
163136 // Make mistake to catch exception - correct - {"cmake", "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON", "-B", "build"}
164- String [] preAnalyzeCommand = new String []{"cmake" , "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" , "-V" , "bdghdhgfjhy" };
165- when (gobPieConfiguration .getPreAnalyzeCommand ()).thenReturn (preAnalyzeCommand );
137+ String [] preAnalyzeCommand = new String []{"cmake" , "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" , "-V" , "bdghdhgfjhy" };
138+ when (gobPieConfiguration .getPreAnalyzeCommand ()).thenReturn (preAnalyzeCommand );
166139
167140 // Mock that the analyses of goblint have started but not completed (still run)
168141 when (goblintService .analyze (new AnalyzeParams (false ))).thenReturn (new CompletableFuture <>());
@@ -179,7 +152,7 @@ void preanalyzeError() {
179152
180153 System .out .println (logger .getLoggerContext ().toString ());
181154
182- assert (logger .getLoggerContext ().toString ().contains ("Running preanalysis command failed. " ));
155+ assert (logger .getLoggerContext ().toString ().contains ("Running preanalysis command failed. " ));
183156
184157 //verify(log, times (1)).info("Running preanalysis command failed. ");
185158
@@ -188,16 +161,4 @@ void preanalyzeError() {
188161 }
189162
190163
191-
192-
193-
194-
195-
196-
197-
198-
199-
200-
201-
202-
203164}
0 commit comments