Skip to content

Commit 94417f3

Browse files
authored
Check for logging messages and fix preAnalyseTest (#1)
* Remove duplicated junit dependencies from pom * Fix surefire plugin error by adding junit dependency * Remove unused imports * Add ability to check for logging messages * Format code * Fix preAnalyseTest
1 parent fc08f2b commit 94417f3

File tree

3 files changed

+41
-79
lines changed

3 files changed

+41
-79
lines changed

pom.xml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@
3535
<dependency>
3636
<groupId>org.junit.jupiter</groupId>
3737
<artifactId>junit-jupiter-api</artifactId>
38-
<version>5.10.1</version>
38+
<version>5.10.2</version>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>uk.org.webcompere</groupId>
44+
<artifactId>system-stubs-jupiter</artifactId>
45+
<version>2.1.6</version>
3946
<scope>test</scope>
4047
</dependency>
4148

@@ -121,13 +128,6 @@
121128
<version>2.20.0</version>
122129
</dependency>
123130

124-
<dependency>
125-
<groupId>org.mockito</groupId>
126-
<artifactId>mockito-core</artifactId>
127-
<version>3.3.3</version>
128-
<scope>compile</scope>
129-
</dependency>
130-
131131
<dependency>
132132
<groupId>org.junit.jupiter</groupId>
133133
<artifactId>junit-jupiter-engine</artifactId>
@@ -185,6 +185,13 @@
185185
<groupId>org.apache.maven.plugins</groupId>
186186
<artifactId>maven-surefire-plugin</artifactId>
187187
<version>3.2.5</version>
188+
<dependencies>
189+
<dependency>
190+
<groupId>org.junit.jupiter</groupId>
191+
<artifactId>junit-jupiter-engine</artifactId>
192+
<version>5.10.0</version>
193+
</dependency>
194+
</dependencies>
188195
</plugin>
189196
</plugins>
190197
</build>

src/main/java/gobpie/GobPieConfReader.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package gobpie;
22

3-
import analysis.GoblintAnalysis;
4-
import api.GoblintService;
53
import com.google.gson.*;
6-
import goblintserver.GoblintConfWatcher;
7-
import goblintserver.GoblintServer;
84
import magpiebridge.core.MagpieServer;
95
import org.apache.commons.lang3.exception.ExceptionUtils;
106
import org.apache.logging.log4j.LogManager;
@@ -18,8 +14,6 @@
1814
import java.nio.file.Files;
1915
import java.nio.file.Path;
2016

21-
import static org.mockito.Mockito.mock;
22-
2317
/**
2418
* The Class GobPieConfReader.
2519
* <p>

src/test/java/GoblintAnalysisTest.java

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,29 @@
99
import gobpie.GobPieConfiguration;
1010
import magpiebridge.core.AnalysisConsumer;
1111
import magpiebridge.core.MagpieServer;
12-
import nl.altindag.log.LogCaptor;
13-
import org.apache.logging.log4j.LogManager;
14-
import org.apache.logging.log4j.core.LogEvent;
1512
import 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;
1918
import java.io.IOException;
2019
import java.util.ArrayDeque;
21-
import java.util.ArrayList;
2220
import java.util.Collection;
23-
import java.util.List;
2421
import java.util.concurrent.CompletableFuture;
2522

2623
import static org.junit.jupiter.api.Assertions.assertTrue;
2724
import static org.mockito.Mockito.*;
2825

26+
@ExtendWith(SystemStubsExtension.class)
2927
class 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

Comments
 (0)