Skip to content

Commit bfa6010

Browse files
vodorokgamesh411
authored andcommitted
Test the analysis results display (#163)
Add a new test to the integration test phase. This tests the result display function of the pluging. The Compilation logging is emulated by a utility method which needs to be called before a build. Extended The dummy CodeChecker accordingly.
1 parent 52d6141 commit bfa6010

File tree

14 files changed

+1059
-21
lines changed

14 files changed

+1059
-21
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ after_failure:
3333
- cat $TRAVIS_BUILD_DIR/tests/org.codechecker.eclipse.rcp.it.tests/target/work/data/.metadata/.log
3434
- cat $TRAVIS_BUILD_DIR/tests/org.codechecker.eclipse.rcp.it.tests/target/surefire-reports/*
3535
- cat $TRAVIS_BUILD_DIR/tests/org.codechecker.eclipse.rcp.unit.tests/target/work/configuration/*.log
36+
- cat $TRAVIS_BUILD_DIR/tests/org.codechecker.eclipse.rcp.unit.tests/target/surefire-reports/*
37+

bundles/org.codechecker.eclipse.plugin/src/org/codechecker/eclipse/plugin/config/project/CodeCheckerProject.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
* (and reports in the future), for one Eclipse project.
3636
*/
3737
public class CodeCheckerProject implements ConfigurationChangedListener {
38+
public static final String COMPILATION_COMMANDS = "compilation_commands.json.javarunner";
39+
3840
protected static final String STR_EMPTY = "";
39-
private static final String COMPILATION_COMMANDS = "compilation_commands.json.javarunner";
4041

4142
/**
4243
* The CodeChecker workspace is where the results folder will be created after an analyze.

bundles/org.codechecker.eclipse.plugin/src/org/codechecker/eclipse/plugin/init/StartupJob.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6+
import org.codechecker.eclipse.plugin.CodeCheckerNature;
7+
import org.codechecker.eclipse.plugin.ExternalLogger;
8+
import org.codechecker.eclipse.plugin.Logger;
9+
import org.codechecker.eclipse.plugin.config.CodeCheckerContext;
10+
import org.codechecker.eclipse.plugin.config.global.CcGlobalConfiguration;
11+
import org.codechecker.eclipse.plugin.config.project.CodeCheckerProject;
12+
import org.codechecker.eclipse.plugin.report.job.AnalyzeJob;
13+
import org.codechecker.eclipse.plugin.report.job.JobDoneChangeListener;
14+
import org.codechecker.eclipse.plugin.report.job.PlistParseJob;
15+
import org.codechecker.eclipse.plugin.runtime.SLogger;
616
import org.eclipse.cdt.core.model.CoreModel;
717
import org.eclipse.core.resources.IProject;
818
import org.eclipse.core.resources.IResource;
@@ -24,18 +34,6 @@
2434
import org.eclipse.ui.IWorkbenchWindow;
2535
import org.eclipse.ui.PlatformUI;
2636

27-
import org.codechecker.eclipse.plugin.CodeCheckerNature;
28-
import org.codechecker.eclipse.plugin.ExternalLogger;
29-
import org.codechecker.eclipse.plugin.Logger;
30-
import org.codechecker.eclipse.plugin.config.CcConfiguration;
31-
import org.codechecker.eclipse.plugin.config.CodeCheckerContext;
32-
import org.codechecker.eclipse.plugin.config.global.CcGlobalConfiguration;
33-
import org.codechecker.eclipse.plugin.config.project.CodeCheckerProject;
34-
import org.codechecker.eclipse.plugin.report.job.AnalyzeJob;
35-
import org.codechecker.eclipse.plugin.report.job.JobDoneChangeListener;
36-
import org.codechecker.eclipse.plugin.report.job.PlistParseJob;
37-
import org.codechecker.eclipse.plugin.runtime.SLogger;
38-
3937
/**
4038
* This eclipse job is responsible for registering the Build listener.
4139
*

tests/org.codechecker.eclipse.rcp.it.tests/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<classpath>
33
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
44
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
5-
<classpathentry kind="src" path="src">
5+
<classpathentry kind="src" output="tests/bin" path="src">
66
<attributes>
77
<attribute name="test" value="true"/>
88
</attributes>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/bin/
2+
/tests/

tests/org.codechecker.eclipse.rcp.it.tests/resources/cppTest/src/cppTest.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,27 @@
99
#include <iostream>
1010
using namespace std;
1111

12+
void test() {
13+
int *p = (int*)malloc(sizeof(int));
14+
free(p);
15+
*p = 1; // warn: use after free
16+
}
17+
18+
void test2() {
19+
long *p = (long*)malloc(sizeof(short));
20+
free(p);
21+
}
22+
23+
void f(int *p){};
24+
25+
void testUseMiddleArgAfterDelete(int *p) {
26+
delete p;
27+
f(p); // warn: use after free
28+
}
29+
1230
int main() {
13-
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
31+
int *i = new int;
32+
int a = *i;
33+
cout << "Hello world!" << endl; // prints !!!Hello World!!!
1434
return 0;
1535
}

tests/org.codechecker.eclipse.rcp.it.tests/src/org/codechecker/eclipse/plugin/AllTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
*/
1313
@RunWith(Suite.class)
1414
@SuiteClasses({ PluginTest.class,
15-
IndicatorTest.class,
16-
ConfigurationTest.class})
15+
IndicatorTest.class, ConfigurationTest.class,
16+
AnalysisTest.class, })
1717
public class AllTests {
1818

19+
private static SWTWorkbenchBot bot;
20+
1921
/**
2022
* Never called.
2123
*/
@@ -28,7 +30,7 @@ private AllTests() {}
2830
@BeforeClass
2931
public static void setup() {
3032
//clearWs();
31-
SWTWorkbenchBot bot = new SWTWorkbenchBot();
33+
bot = new SWTWorkbenchBot();
3234
GuiUtils.closeWelcomeIfPresent(bot);
3335
GuiUtils.changePerspectiveTo(GuiUtils.C_CPP_PESPECTIVE, bot);
3436
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
*
3+
*/
4+
package org.codechecker.eclipse.plugin;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.net.URISyntaxException;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
12+
import org.codechecker.eclipse.plugin.utils.CompilationLogHelper;
13+
import org.codechecker.eclipse.plugin.utils.GuiUtils;
14+
import org.codechecker.eclipse.plugin.utils.ProjectImporter;
15+
import org.codechecker.eclipse.rcp.shared.utils.Utils;
16+
import org.eclipse.core.resources.ResourcesPlugin;
17+
import org.eclipse.core.runtime.CoreException;
18+
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
19+
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
20+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
21+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
22+
import org.junit.BeforeClass;
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.rules.ExpectedException;
26+
27+
import static org.hamcrest.CoreMatchers.is;
28+
import static org.hamcrest.MatcherAssert.assertThat;
29+
30+
/**
31+
* Integration test for testing the Analysis functionality of the plugin.
32+
*/
33+
public class AnalysisTest {
34+
35+
private static final String CPP_PROJ = "cppTest";
36+
private static final String SRC = "src";
37+
private static final String CPP_SRC = "cppTest.cpp";
38+
private static final String EDITOR = CPP_SRC;
39+
private static final String UNIX = "unix";
40+
private static final String UNIX_MALLOC = "unix.MallocSizeof";
41+
private static final String REPORT = "#1: cppTest.cpp [19:20]";
42+
private static final String REPORT_LONG = "cppTest.cpp : 19 : Result of 'malloc' is converted to a pointer of type 'long', which is incompatible with sizeof operand type 'short'";
43+
44+
private static final String ERROR_MISSING_CHECKERS = "Reports were missing";
45+
private static Path ccDir = Utils.prepareCodeChecker();
46+
private static SWTBotTreeItem project;
47+
48+
private static SWTWorkbenchBot bot;
49+
50+
@Rule
51+
public ExpectedException thrown = ExpectedException.none();
52+
53+
/**
54+
* Import project - Set nature.
55+
*/
56+
@BeforeClass
57+
public static void setUpBeforeClass() {
58+
bot = new SWTWorkbenchBot();
59+
60+
// Import project
61+
Path file = null;
62+
try {
63+
file = Utils.loadFileFromBundle("org.codechecker.eclipse.rcp.it.tests", Utils.RES + CPP_PROJ);
64+
} catch (URISyntaxException | IOException e) {
65+
e.printStackTrace();
66+
}
67+
68+
Utils.copyFolder(file,
69+
Paths.get(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString() + File.separator));
70+
71+
File projectFile = new File(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString() + File.separator
72+
+ CPP_PROJ + File.separator + GuiUtils.DOT_PROJECT);
73+
try {
74+
ProjectImporter.importProject(projectFile.toPath(), CPP_PROJ);
75+
} catch (CoreException e1) {
76+
e1.printStackTrace();
77+
}
78+
79+
// Add CodeChecker Nature.
80+
project = bot.tree().getTreeItem(CPP_PROJ);
81+
project.contextMenu(GuiUtils.ADD_NATURE_MENU).click();
82+
}
83+
84+
/**
85+
* Test the analysis.
86+
*/
87+
@Test
88+
public void test() {
89+
SWTBotShell preferencesShell = GuiUtils.getPreferencesTab(GuiUtils.CODECHECKER, bot);
90+
GuiUtils.setCCBinDir(ccDir, bot);
91+
GuiUtils.applyClosePreferences(preferencesShell, bot);
92+
93+
bot.tree().getTreeItem(CPP_PROJ).select();
94+
bot.tree().getTreeItem(CPP_PROJ).doubleClick();
95+
bot.tree().getTreeItem(CPP_PROJ).getNode(SRC).expand();
96+
bot.tree().getTreeItem(CPP_PROJ).getNode(SRC).getNode(CPP_SRC).select();
97+
bot.tree().getTreeItem(CPP_PROJ).getNode(SRC).getNode(CPP_SRC).doubleClick();
98+
bot.editorByTitle(CPP_SRC).show();
99+
100+
GuiUtils.changePerspectiveTo(GuiUtils.CODECHECKER, bot);
101+
CompilationLogHelper.createCompilationLog(CPP_PROJ);
102+
103+
assertThat(ERROR_MISSING_CHECKERS, bot.tree(1).hasItems(), is(false));
104+
105+
// Build the project
106+
bot.viewByTitle(GuiUtils.CURRENT_PROJ).show();
107+
project.contextMenu(GuiUtils.BUILD_PROJ).click();
108+
109+
bot.waitUntil(new DefaultCondition() {
110+
@Override
111+
public boolean test() throws Exception {
112+
return bot.tree(1).getTreeItem(UNIX) != null;
113+
}
114+
115+
@Override
116+
public String getFailureMessage() {
117+
return null;
118+
}
119+
});
120+
121+
bot.tree(1).getTreeItem(UNIX).doubleClick();
122+
assertThat(ERROR_MISSING_CHECKERS, bot.tree(1).hasItems(), is(true));
123+
124+
bot.tree(1).getTreeItem(UNIX).getNode(UNIX_MALLOC).expand();
125+
bot.tree(1).getTreeItem(UNIX).getNode(UNIX_MALLOC).getNode(REPORT).expand();
126+
bot.tree(1).getTreeItem(UNIX).getNode(UNIX_MALLOC).getNode(REPORT).getNode(REPORT_LONG).select();
127+
bot.tree(1).getTreeItem(UNIX).getNode(UNIX_MALLOC).getNode(REPORT).getNode(REPORT_LONG).doubleClick();
128+
129+
assertThat("Editor did not get active upon report selection",
130+
bot.activeEditor().getTitle().equals(EDITOR));
131+
}
132+
133+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.codechecker.eclipse.plugin.utils;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
import org.codechecker.eclipse.plugin.config.project.CodeCheckerProject;
9+
import org.eclipse.core.resources.ResourcesPlugin;
10+
11+
/**
12+
* This class can be used to create an (dummy) analyze log to the specified
13+
* project.
14+
*/
15+
public class CompilationLogHelper {
16+
17+
/**
18+
* Never called.
19+
*/
20+
private CompilationLogHelper() {
21+
}
22+
23+
/**
24+
* Creates an empty compilation commands .json file, into the correct
25+
* destination with the correct name. This can be forwarded to the dummy
26+
* analysis tests.
27+
*
28+
* @param projectName
29+
* The project name to be used.
30+
*/
31+
public static void createCompilationLog(String projectName) {
32+
// get the correct location which is eclipseWs/.codechecker/{$projectName}/
33+
Path workspaceRoot = Paths.get(ResourcesPlugin.getWorkspace().getRoot().getLocation().toString(),
34+
GuiUtils.DOT_CODECHECKER, projectName, CodeCheckerProject.COMPILATION_COMMANDS);
35+
// create a compilation log to the project.
36+
try {
37+
Files.createFile(workspaceRoot);
38+
} catch (IOException e) {
39+
e.printStackTrace();
40+
}
41+
}
42+
}

tests/org.codechecker.eclipse.rcp.it.tests/src/org/codechecker/eclipse/plugin/utils/GuiUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ public final class GuiUtils {
2727
public static final String DOT_PROJECT = ".project";
2828
public static final String APPLY = "Apply";
2929
public static final String APPLY_AND_CLOSE = "Apply and Close";
30+
public static final String BUILD_PROJ = "Build Project";
3031
public static final String CANCEL = "Cancel";
3132
public static final String C_CPP_BUILD = "C/C++ Build";
3233
public static final String C_CPP_PESPECTIVE = "C/C++";
3334
public static final String CPP_PROJECT = "C++ Project";
3435
public static final String DELETE_RESOURCES = "Delete Resources";
3536
public static final String ENVIRONMENT = "Environment";
3637
public static final String FINISH = "Finish";
38+
public static final String OK = "OK";
3739
public static final String OPEN_PERSP = "Open Perspective";
3840
public static final String OTHER_MENU = "Other...";
3941
public static final String PERSP_MENU = "Perspective";
@@ -45,6 +47,7 @@ public final class GuiUtils {
4547
public static final String DOT_CODECHECKER = ".codechecker";
4648
public static final String ADD_NATURE_MENU = "Add CodeChecker Nature";
4749
public static final String COMP_COMMANDS = "compilation_commands.json.javarunner";
50+
public static final String CURRENT_PROJ = "Current Project";
4851
public static final String BIN = "bin";
4952
public static final String CC_DIR_WIDGET = "CodeChecker binary:";
5053
public static final String CODECHECKER = "CodeChecker";
@@ -54,6 +57,7 @@ public final class GuiUtils {
5457
public static final String THREAD_WIDGET = "Number of analysis threads";
5558
public static final String GLOBAL_RADIO = "Use global configuration";
5659
public static final String PROJECT_RADIO = "Use project configuration";
60+
public static final String TOGGLE_CHECKERS = "Toggle enabled checkers";
5761

5862
public static final int SHORT_WAIT_TIME = 500; // in milliseconds
5963
public static final int LONG_TIME_OUT = 10000;
@@ -171,7 +175,7 @@ public static void deleteProject(String projectName, boolean deleteFromDisk, SWT
171175
shell.activate();
172176
if (deleteFromDisk)
173177
bot.checkBox("Delete project contents on disk (cannot be undone)").select();
174-
bot.button("OK").click();
178+
bot.button(OK).click();
175179
bot.waitUntil(Conditions.shellCloses(shell), LONG_TIME_OUT);
176180
}
177181

0 commit comments

Comments
 (0)