mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-09 17:25:38 +02:00
- test project for checkers
This commit is contained in:
parent
c59b224a64
commit
db91470f3c
9 changed files with 483 additions and 0 deletions
7
codan/org.eclipse.cdt.codan.core.test/.classpath
Normal file
7
codan/org.eclipse.cdt.codan.core.test/.classpath
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
28
codan/org.eclipse.cdt.codan.core.test/.project
Normal file
28
codan/org.eclipse.cdt.codan.core.test/.project
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>org.eclipse.cdt.codan.core.test</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.ManifestBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.SchemaBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.pde.PluginNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,8 @@
|
|||
#Wed Sep 23 11:54:31 EDT 2009
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
15
codan/org.eclipse.cdt.codan.core.test/META-INF/MANIFEST.MF
Normal file
15
codan/org.eclipse.cdt.codan.core.test/META-INF/MANIFEST.MF
Normal file
|
@ -0,0 +1,15 @@
|
|||
Manifest-Version: 1.0
|
||||
Bundle-ManifestVersion: 2
|
||||
Bundle-Name: Codan Core Tests
|
||||
Bundle-SymbolicName: org.eclipse.cdt.codan.core.test
|
||||
Bundle-Version: 1.0.0
|
||||
Bundle-Activator: org.eclipse.cdt.codan.core.test.CodanCoreTestActivator
|
||||
Require-Bundle: org.eclipse.core.runtime,
|
||||
org.eclipse.cdt.codan.checkers;bundle-version="1.0.0",
|
||||
org.eclipse.cdt.core;bundle-version="5.2.0",
|
||||
org.eclipse.core.resources;bundle-version="3.5.0",
|
||||
org.eclipse.cdt.core.tests;bundle-version="5.1.0",
|
||||
org.junit;bundle-version="3.8.2",
|
||||
org.eclipse.cdt.codan.core;bundle-version="1.0.0"
|
||||
Bundle-ActivationPolicy: lazy
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
|
5
codan/org.eclipse.cdt.codan.core.test/build.properties
Normal file
5
codan/org.eclipse.cdt.codan.core.test/build.properties
Normal file
|
@ -0,0 +1,5 @@
|
|||
source.. = src/
|
||||
output.. = bin/
|
||||
bin.includes = META-INF/,\
|
||||
.,\
|
||||
src/org/eclipse/cdt/codan/core/checkers/
|
|
@ -0,0 +1,63 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2009 Alena Laskavaia
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Alena Laskavaia - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.core.checkers.sample;
|
||||
|
||||
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
|
||||
|
||||
/**
|
||||
* Test for {@see StatementHasNoEffectChecker} class
|
||||
*
|
||||
*/
|
||||
public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
|
||||
/*-
|
||||
<code file="test1.c">
|
||||
main() {
|
||||
int a;
|
||||
+a; // error here on line 3
|
||||
}
|
||||
</code>
|
||||
*/
|
||||
public void test1() {
|
||||
load("test1.c");
|
||||
runOnFile();
|
||||
checkErrorLine(3);
|
||||
}
|
||||
|
||||
/*-
|
||||
<code file="test2.c">
|
||||
main() {
|
||||
int a,b;
|
||||
|
||||
b+a; // error here on line 4
|
||||
}
|
||||
</code>
|
||||
*/
|
||||
public void test2() {
|
||||
load("test2.c");
|
||||
runOnFile();
|
||||
checkErrorLine(4);
|
||||
}
|
||||
|
||||
/*-
|
||||
<code file="test3.c">
|
||||
main() {
|
||||
int a,b;
|
||||
|
||||
a=b+a; // no error here
|
||||
}
|
||||
</code>
|
||||
*/
|
||||
public void test3() {
|
||||
load("test3.c");
|
||||
runOnFile();
|
||||
checkNoErrors();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2004, 2007 QNX Software Systems and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* QNX Software Systems - initial API and implementation
|
||||
* Markus Schorn (Wind River Systems)
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.core.test;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.eclipse.cdt.codan.core.checkers.sample.StatementHasNoEffectCheckerTest;
|
||||
|
||||
public class AutomatedIntegrationSuite extends TestSuite {
|
||||
|
||||
public AutomatedIntegrationSuite() {
|
||||
}
|
||||
|
||||
public AutomatedIntegrationSuite(Class theClass, String name) {
|
||||
super(theClass, name);
|
||||
}
|
||||
|
||||
public AutomatedIntegrationSuite(Class theClass) {
|
||||
super(theClass);
|
||||
}
|
||||
|
||||
public AutomatedIntegrationSuite(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
|
||||
final AutomatedIntegrationSuite suite = new AutomatedIntegrationSuite();
|
||||
|
||||
suite.addTestSuite(StatementHasNoEffectCheckerTest.class);
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,259 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2009 Alena Laskavaia
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Alena Laskavaia - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.core.test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.eclipse.cdt.codan.core.CodanRuntime;
|
||||
import org.eclipse.cdt.codan.core.model.IProblemReporter;
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.IPDOMManager;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
import org.eclipse.cdt.core.testplugin.CProjectHelper;
|
||||
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
|
||||
import org.eclipse.core.resources.IMarker;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.resources.IWorkspace;
|
||||
import org.eclipse.core.resources.IWorkspaceRunnable;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
|
||||
/**
|
||||
* @author Alena
|
||||
*
|
||||
*/
|
||||
public class CheckerTestCase extends BaseTestCase {
|
||||
ArrayList<File> tempFiles = new ArrayList<File>();
|
||||
protected File tmpDir;
|
||||
private ICProject cproject;
|
||||
private IMarker[] markers;
|
||||
|
||||
/**
|
||||
* Override for c++
|
||||
*
|
||||
* @return is c++ tests
|
||||
*/
|
||||
public boolean isCpp() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
removeLeftOverProjects();
|
||||
cproject = createProject(isCpp());
|
||||
tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile();
|
||||
}
|
||||
|
||||
public File load(String file) {
|
||||
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
|
||||
String name = getClass().getName();
|
||||
String classFile = name.replaceAll("\\.", "/");
|
||||
classFile += ".java";
|
||||
InputStream st=null;
|
||||
File f = null;
|
||||
try {
|
||||
if (plugin != null) {
|
||||
URL resource = plugin.getBundle().getResource(
|
||||
"src/" + classFile);
|
||||
st = resource.openStream();
|
||||
} else {
|
||||
st = getClass().getResourceAsStream(classFile);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
fail("Cannot find java file: " + classFile);
|
||||
}
|
||||
try {
|
||||
f = saveFile(st, file);
|
||||
st.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
||||
static final Pattern filePattern = Pattern.compile("file=\"(.*)\"");
|
||||
|
||||
/**
|
||||
* @param st
|
||||
* @param file
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private File saveFile(InputStream st, String file) throws IOException {
|
||||
BufferedReader r = new BufferedReader(new InputStreamReader(st));
|
||||
String line;
|
||||
File testFile = new File(tmpDir, file);
|
||||
tempFiles.add(testFile);
|
||||
PrintStream wr = new PrintStream(testFile);
|
||||
try {
|
||||
boolean print = false;
|
||||
while ((line = r.readLine()) != null) {
|
||||
if (line.contains("<code ")) {
|
||||
Matcher m = filePattern.matcher(line);
|
||||
if (m.find()) {
|
||||
String userFile = m.group(1);
|
||||
if (userFile.equals(file)) {
|
||||
print = true;
|
||||
}
|
||||
}
|
||||
} else if (line.contains("</code>")) {
|
||||
print = false;
|
||||
} else if (print) {
|
||||
wr.println(line);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
wr.close();
|
||||
}
|
||||
return testFile;
|
||||
}
|
||||
|
||||
public void tearDown() throws CoreException {
|
||||
if (cproject != null) {
|
||||
try {
|
||||
cproject.getProject().delete(
|
||||
IResource.FORCE
|
||||
| IResource.ALWAYS_DELETE_PROJECT_CONTENT,
|
||||
new NullProgressMonitor());
|
||||
} catch (CoreException e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CoreException
|
||||
*/
|
||||
private void removeLeftOverProjects() throws CoreException {
|
||||
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
|
||||
IProject[] projects = workspace.getRoot().getProjects();
|
||||
for (int i = 0; i < projects.length; i++) {
|
||||
IProject p = projects[i];
|
||||
if (p.getName().startsWith("Codan")) {
|
||||
p.delete(
|
||||
IResource.FORCE
|
||||
| IResource.ALWAYS_DELETE_PROJECT_CONTENT,
|
||||
new NullProgressMonitor());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected ICProject createProject(final boolean cpp) throws CoreException {
|
||||
final ICProject cprojects[] = new ICProject[1];
|
||||
ModelJoiner mj = new ModelJoiner();
|
||||
try {
|
||||
// Create the cproject
|
||||
final String projectName = "CodanProjTest_" + System.currentTimeMillis();
|
||||
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
|
||||
workspace.run(new IWorkspaceRunnable() {
|
||||
public void run(IProgressMonitor monitor) throws CoreException {
|
||||
// Create the cproject
|
||||
ICProject cproject = cpp ? CProjectHelper.createCCProject(
|
||||
projectName, null, IPDOMManager.ID_NO_INDEXER)
|
||||
: CProjectHelper.createCProject(projectName, null,
|
||||
IPDOMManager.ID_NO_INDEXER);
|
||||
cprojects[0] = cproject;
|
||||
}
|
||||
}, null);
|
||||
mj.join();
|
||||
// Index the cproject
|
||||
CCorePlugin.getIndexManager().setIndexerId(cprojects[0],
|
||||
IPDOMManager.ID_FAST_INDEXER);
|
||||
// wait until the indexer is done
|
||||
assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000,
|
||||
new NullProgressMonitor()));
|
||||
} finally {
|
||||
mj.dispose();
|
||||
}
|
||||
return cprojects[0];
|
||||
}
|
||||
|
||||
protected void loadFiles() throws CoreException {
|
||||
ModelJoiner mj = new ModelJoiner();
|
||||
try {
|
||||
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
|
||||
workspace.run(new IWorkspaceRunnable() {
|
||||
public void run(IProgressMonitor monitor) throws CoreException {
|
||||
cproject.getProject().refreshLocal(1, monitor);
|
||||
}
|
||||
}, null);
|
||||
mj.join();
|
||||
// Index the cproject
|
||||
CCorePlugin.getIndexManager().setIndexerId(cproject,
|
||||
IPDOMManager.ID_FAST_INDEXER);
|
||||
// wait until the indexer is done
|
||||
assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000,
|
||||
new NullProgressMonitor()));
|
||||
} finally {
|
||||
mj.dispose();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param i - line
|
||||
*/
|
||||
public void checkErrorLine(int i) {
|
||||
assertTrue(markers!=null);
|
||||
assertTrue(markers.length>0);
|
||||
boolean found=false;
|
||||
for (int j = 0; j < markers.length; j++) {
|
||||
IMarker m = markers[j];
|
||||
Object line=null;
|
||||
try {
|
||||
line = m.getAttribute(IMarker.LINE_NUMBER);
|
||||
} catch (CoreException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
if (line.equals(i)) {
|
||||
found=true;
|
||||
}
|
||||
}
|
||||
assertTrue("Error on line "+i+" not found ",found);
|
||||
}
|
||||
public void checkNoErrors() {
|
||||
assertTrue("Found errors but should not",markers==null || markers.length==0);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void runOnFile() {
|
||||
try {
|
||||
loadFiles();
|
||||
} catch (CoreException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
CodanRuntime.getInstance().getBuilder().processResource(cproject.getProject(), NPM);
|
||||
try {
|
||||
markers = cproject.getProject().findMarkers(IProblemReporter.GENERIC_CODE_ANALYSIS_MARKER_TYPE, true, 1);
|
||||
} catch (CoreException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package org.eclipse.cdt.codan.core.test;
|
||||
|
||||
import org.eclipse.core.runtime.Plugin;
|
||||
import org.osgi.framework.BundleContext;
|
||||
|
||||
/**
|
||||
* The activator class controls the plug-in life cycle
|
||||
*/
|
||||
public class CodanCoreTestActivator extends Plugin {
|
||||
|
||||
// The plug-in ID
|
||||
public static final String PLUGIN_ID = "org.eclipse.cdt.codan.core.test";
|
||||
|
||||
// The shared instance
|
||||
private static CodanCoreTestActivator plugin;
|
||||
|
||||
/**
|
||||
* The constructor
|
||||
*/
|
||||
public CodanCoreTestActivator() {
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
|
||||
*/
|
||||
public void start(BundleContext context) throws Exception {
|
||||
super.start(context);
|
||||
plugin = this;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
|
||||
*/
|
||||
public void stop(BundleContext context) throws Exception {
|
||||
plugin = null;
|
||||
super.stop(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the shared instance
|
||||
*
|
||||
* @return the shared instance
|
||||
*/
|
||||
public static CodanCoreTestActivator getDefault() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue