Skip to content

Commit

Permalink
Add CodeChecker stub to the test resources
Browse files Browse the repository at this point in the history
CodeChecker resources in a new shared project that both unit and
integration test suites reach. org.codechecker.eclipse.rcp.shared

For this to work, the unit test needed to be changed to
"JUnit Plug-in test" Because cross bundle resource loading only works
in an osgi context, which plain standard JUnit isn't. This results in a
slightly slower execution, because an osgi context is needed to be
created (basically a headless eclipse). This difference of execution time
will diminish when the test case count rises in the unit tests.
The benefit is much more flexible code maintainability.

Also moved the checkstyle "fail on warning" switch to the root test folder,
and explicitly allowed the unit tests to violate until future clean up.
This way the new shared project is also protected as well as the
integration tests.

Commonly used functions like resource loading for the tests, or
CodeChecker preparation can be reached from this module, as well as
commonly used resources, like the new CodeChecker stub.

The CodeChecker added in this commit is utilized the in the new
Indicator test which is for to test the configuration indicator in the
CodeChecker preferences page.

This new Test is now a part of a test Suite: AllTests.java
The common loading and workspace related works can be performed in this
class.

Added a lot of utility methods for navigating the eclipse gui.
  • Loading branch information
vodorok committed Jun 26, 2019
1 parent 6875c89 commit 599896d
Show file tree
Hide file tree
Showing 26 changed files with 769 additions and 205 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.swo

# Eclipse
bin/
.idea
*.iml
target/
Expand Down
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<module name="TreeWalker">
<module name="AvoidStarImport"/>
<module name="AvoidStaticImport">
<property name="excludes" value="org.hamcrest.MatcherAssert.*,org.hamcrest.Matchers.*,org.mockito.Mockito.*"/>
<property name="excludes" value="org.hamcrest.MatcherAssert.*,org.hamcrest.Matchers.*,org.hamcrest.CoreMatchers.*,org.mockito.Mockito.*"/>
</module>
<module name="ConstantName"/>
<module name="DeclarationOrder"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<unit id="org.eclipse.swtbot.forms.feature.group" version="2.7.0.201806111355"/>
<unit id="org.eclipse.swtbot.generator.feature.feature.group" version="2.7.0.201806111355"/>
<unit id="org.eclipse.swtbot.ide.feature.group" version="2.7.0.201806111355"/>
<repository location="http://download.eclipse.org/technology/swtbot/releases/latest"/>
<repository location="http://download.eclipse.org/technology/swtbot/releases/2.7.0"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.tm.terminal.control.feature.feature.group" version="4.0.0.201506040610"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Require-Bundle: org.junit;bundle-version="4.12.0",
org.eclipse.cdt.make.ui;bundle-version="7.3.0",
org.eclipse.cdt.build.gcc.core;bundle-version="1.0.0",
org.eclipse.cdt.gdb.ui;bundle-version="7.0.0",
org.eclipse.swtbot.eclipse.finder;bundle-version="2.7.0"
org.eclipse.swtbot.eclipse.finder;bundle-version="2.7.0",
org.codechecker.eclipse.rcp.shared;bundle-version="1.0.0"
3 changes: 2 additions & 1 deletion tests/org.codechecker.eclipse.rcp.it.tests/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
fragment.xml
fragment.xml,\
resources/
14 changes: 1 addition & 13 deletions tests/org.codechecker.eclipse.rcp.it.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,9 @@
<useUIThread>false</useUIThread>
<product>org.codechecker.eclipse.feature</product>
<application>org.eclipse.ui.ide.workbench</application>
<testClass>org.codechecker.eclipse.plugin.AllTests</testClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>validate</id>
<configuration>
<failOnViolation>true</failOnViolation>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.codechecker.eclipse.plugin;

import org.codechecker.eclipse.plugin.utils.GuiUtils;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

/**
* Test Suite for running the gui tests. Add your class to the Suite class list.
*/
@RunWith(Suite.class)
@SuiteClasses({ PluginTest.class, IndicatorTest.class })
public class AllTests {

/**
* Never called.
*/
private AllTests() {}

/**
* Import cpp project into workspace, and setup SWTBot.
*
*/
@BeforeClass
public static void setup() {
//clearWs();
SWTWorkbenchBot bot = new SWTWorkbenchBot();
GuiUtils.closeWelcomeIfPresent(bot);
GuiUtils.changePerspectiveTo(GuiUtils.C_CPP_PESPECTIVE, bot);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.codechecker.eclipse.plugin;

import java.nio.file.Path;

import org.codechecker.eclipse.plugin.utils.GuiUtils;
import org.codechecker.eclipse.rcp.shared.utils.Utils;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCLabel;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.hamcrest.core.IsNull;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* CodeChecker plugin preferences indicator tests.
*/
public class IndicatorTest {

private static final int SHORT_WAIT_TIME = 500; // in milliseconds

private static final String CODECHECKER = "CodeChecker";

private static SWTWorkbenchBot bot;

@Rule
public ExpectedException thrown = ExpectedException.none();

private SWTBotShell preferencesShell;

/**
* Import cpp project into workspace, and setup SWTBot.
*
*/
@BeforeClass
public static void setup() {
bot = new SWTWorkbenchBot();
}

/**
* Open preferences, CodeChecker page before every test.
*/
@Before
public void openPreferences() {
preferencesShell = GuiUtils.getPreferencesTab(CODECHECKER, bot);
}

/**
* Test that with unconfigured CodeChecker, a warning message is displayed.
*/
@Test
public void testNoCodeCheckerFound() {
SWTBotCLabel label = null;
try {
label = bot.clabel("CodeChecker package directory is invalid");
} catch (WidgetNotFoundException e) {
System.out.println(e.getMessage());
}
assertThat("There was no invalid CodeChecker message displayed", label, is(IsNull.notNullValue()));

preferencesShell.close();
}

/**
* Test that with CodeChecker configured, a confirmation message is displayed.
*/
@Test
public void testCodeCheckerFound() {
Path ccDir = Utils.prepareCodeChecker();

SWTBotText text = bot.textWithLabel("CodeChecker package root directory");
text.setText(ccDir.toString());
text.setFocus();
bot.textWithLabel("Python virtualenv root directory (optional)").setFocus();

bot.sleep(SHORT_WAIT_TIME);

SWTBotCLabel label = null;
try {
label = bot.clabel("CodeChecker package directory is valid");
} catch (WidgetNotFoundException e) {
System.out.println(e.getMessage());
}
assertThat("There was no valid CodeChecker message displayed", label, is(IsNull.notNullValue()));

preferencesShell.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,21 @@
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.codechecker.eclipse.plugin.utils.Utils;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.codechecker.eclipse.plugin.utils.ProjectImporter;
import org.codechecker.eclipse.rcp.shared.utils.Utils;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.ui.PlatformUI;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.osgi.framework.Bundle;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -38,10 +29,6 @@
public class PluginTest {

private static final String CPP_PROJ = "cppTest";
private static final String WINDOW_MENU = "Window";
private static final String PERSP_MENU = "Perspective";
private static final String OPEN_PERSP = "Open Perspective";
private static final String OTHER_MENU = "Other...";
private static final String ADD_NATURE_MENU = "Add CodeChecker Nature";

private static SWTWorkbenchBot bot;
Expand All @@ -51,56 +38,30 @@ public class PluginTest {

/**
* Import cpp project into workspace, and setup SWTBot.
*
*/
@BeforeClass
public static void setup() {

// http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/
Bundle bundle = Platform.getBundle("org.codechecker.eclipse.rcp.it.tests");
URL fileURL = bundle.getEntry("resources/cppTest");
File file = null;
bot = new SWTWorkbenchBot();

Path file = null;
try {
file = new File(FileLocator.resolve(fileURL).toURI());
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
file = Utils.loadFileFromBundle("org.codechecker.eclipse.rcp.it.tests",
Utils.RES + CPP_PROJ);
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
assertThat("File not exists.", file.exists());
Utils.copyFolder(file.toPath(),

Utils.copyFolder(file,
Paths.get(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString() + File.separator));

File project = new File(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString() + File.separator
+ CPP_PROJ + File.separator + ".project");
try {
importProject(project, CPP_PROJ);
ProjectImporter.importProject(project.toPath(), CPP_PROJ);
} catch (CoreException e1) {
e1.printStackTrace();
}

bot = new SWTWorkbenchBot();
UIThreadRunnable.syncExec(new VoidResult() {
public void run() {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().forceActive();
}
});

try {
bot.viewByTitle("Welcome").close();
} catch (WidgetNotFoundException e) {
System.out.println("Welcome Screen wasn't present.");
}

// Change the perspective via the Open Perspective dialog
bot.menu(WINDOW_MENU).menu(PERSP_MENU).menu(OPEN_PERSP).menu(OTHER_MENU).click();
SWTBotShell openPerspectiveShell = bot.shell(OPEN_PERSP);
openPerspectiveShell.activate();

// select the dialog
bot.table().select("C/C++");
bot.button("Open").click();

}

/**
Expand All @@ -121,37 +82,4 @@ public void testAddNatureDisappears() {
thrown.expectMessage(containsString("Could not find"));
project.contextMenu(ADD_NATURE_MENU);
}


/**
* Imports a project into workspace.
* https://www.eclipse.org/forums/index.php/t/560903/
*
* @param projectFile
* The project file to be imported.
* @param projectName
* The project name that will be used to create the project
* @throws CoreException
* Project cannot be created: if this method fails. Reasons include:
* - This project already exists in the workspace. - The name of
* this resource is not valid (according to
* IWorkspace.validateName). - The project location is not valid
* (according to IWorkspace.validateProjectLocation). - The project
* description file could not be created in the project content
* area. - Resource changes are disallowed during certain types of
* resource change event notification. See IResourceChangeEvent for
* more details. .project file has troubles. Reasons include: - The
* project description file does not exist. - The file cannot be
* opened or read. - The file cannot be parsed as a legal project
* description. or during opening - Resource changes are disallowed
* during certain types of resource change event notification. See
* IResourceChangeEvent for more details.
*/
private static void importProject(final File projectFile, final String projectName) throws CoreException {
IProjectDescription description = ResourcesPlugin.getWorkspace()
.loadProjectDescription(new Path(projectFile.getAbsolutePath()));
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName());
project.create(description, null);
project.open(null);
}
}
}
Loading

0 comments on commit 599896d

Please sign in to comment.