From db91470f3ca844d0910df6cb195b131754d3cdc6 Mon Sep 17 00:00:00 2001 From: Alena Laskavaia Date: Wed, 23 Sep 2009 18:34:41 +0000 Subject: [PATCH] - test project for checkers --- .../.classpath | 7 + .../org.eclipse.cdt.codan.core.test/.project | 28 ++ .../.settings/org.eclipse.jdt.core.prefs | 8 + .../META-INF/MANIFEST.MF | 15 + .../build.properties | 5 + .../StatementHasNoEffectCheckerTest.java | 63 +++++ .../core/test/AutomatedIntegrationSuite.java | 48 ++++ .../cdt/codan/core/test/CheckerTestCase.java | 259 ++++++++++++++++++ .../core/test/CodanCoreTestActivator.java | 50 ++++ 9 files changed, 483 insertions(+) create mode 100644 codan/org.eclipse.cdt.codan.core.test/.classpath create mode 100644 codan/org.eclipse.cdt.codan.core.test/.project create mode 100644 codan/org.eclipse.cdt.codan.core.test/.settings/org.eclipse.jdt.core.prefs create mode 100644 codan/org.eclipse.cdt.codan.core.test/META-INF/MANIFEST.MF create mode 100644 codan/org.eclipse.cdt.codan.core.test/build.properties create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/checkers/sample/StatementHasNoEffectCheckerTest.java create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanCoreTestActivator.java diff --git a/codan/org.eclipse.cdt.codan.core.test/.classpath b/codan/org.eclipse.cdt.codan.core.test/.classpath new file mode 100644 index 00000000000..ad32c83a788 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/codan/org.eclipse.cdt.codan.core.test/.project b/codan/org.eclipse.cdt.codan.core.test/.project new file mode 100644 index 00000000000..657ca80b929 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/.project @@ -0,0 +1,28 @@ + + + org.eclipse.cdt.codan.core.test + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/codan/org.eclipse.cdt.codan.core.test/.settings/org.eclipse.jdt.core.prefs b/codan/org.eclipse.cdt.codan.core.test/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000000..8141101dbbd --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/codan/org.eclipse.cdt.codan.core.test/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.core.test/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..262a81bc8ee --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/META-INF/MANIFEST.MF @@ -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 diff --git a/codan/org.eclipse.cdt.codan.core.test/build.properties b/codan/org.eclipse.cdt.codan.core.test/build.properties new file mode 100644 index 00000000000..5c6aea6a46c --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + src/org/eclipse/cdt/codan/core/checkers/ diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/checkers/sample/StatementHasNoEffectCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/checkers/sample/StatementHasNoEffectCheckerTest.java new file mode 100644 index 00000000000..93e32702a88 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/checkers/sample/StatementHasNoEffectCheckerTest.java @@ -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 { + /*- + + main() { + int a; + +a; // error here on line 3 + } + + */ + public void test1() { + load("test1.c"); + runOnFile(); + checkErrorLine(3); + } + + /*- + + main() { + int a,b; + + b+a; // error here on line 4 + } + + */ + public void test2() { + load("test2.c"); + runOnFile(); + checkErrorLine(4); + } + + /*- + + main() { + int a,b; + + a=b+a; // no error here + } + + */ + public void test3() { + load("test3.c"); + runOnFile(); + checkNoErrors(); + } +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java new file mode 100644 index 00000000000..48e469c8c4d --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java @@ -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; + } + + + + +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java new file mode 100644 index 00000000000..0a89db1e4a8 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java @@ -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 tempFiles = new ArrayList(); + 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("")) { + 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()); + } + } +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanCoreTestActivator.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanCoreTestActivator.java new file mode 100644 index 00000000000..37ad4732921 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanCoreTestActivator.java @@ -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; + } + +}