Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/codan
diff options
context:
space:
mode:
authorDominic Scharfe2016-11-10 16:50:52 +0000
committerDominic Scharfe2016-11-11 10:18:55 +0000
commit56c496ba5a98aed9a30d5ad9db38cfd161f45458 (patch)
tree5fba2350d44eb2ab0fd4b5405dd321b6ce009908 /codan
parentdc6e3a06ff3734457340c62558863c34a0500440 (diff)
downloadorg.eclipse.cdt-56c496ba5a98aed9a30d5ad9db38cfd161f45458.tar.gz
org.eclipse.cdt-56c496ba5a98aed9a30d5ad9db38cfd161f45458.tar.xz
org.eclipse.cdt-56c496ba5a98aed9a30d5ad9db38cfd161f45458.zip
Bug 507148. Fixed opening/switching to editor.
- Reuse the TU from the open editor if present, otherwise load it from workspace - Added test plugin org.eclipse.cdt.codan.checkers.ui.test Change-Id: I725b7b8e462ffba39dd6c9a828ab72dd58f86b1f Signed-off-by: Dominic Scharfe <dominic.scharfe@coseda-tech.com>
Diffstat (limited to 'codan')
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui.test/.classpath7
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui.test/.project28
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui.test/.settings/org.eclipse.jdt.core.prefs7
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui.test/META-INF/MANIFEST.MF23
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui.test/OSGI-INF/l10n/bundle.properties13
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui.test/build.properties4
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui.test/pom.xml39
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui.test/src/org/eclipse/cdt/codan/checkers/ui/Activator.java41
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui.test/src/org/eclipse/cdt/codan/checkers/ui/quickfix/QuickFixCreateNewClassTest.java126
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF2
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCreateNewClass.java54
11 files changed, 335 insertions, 9 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui.test/.classpath b/codan/org.eclipse.cdt.codan.checkers.ui.test/.classpath
new file mode 100644
index 0000000000..eca7bdba8f
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui.test/.classpath
@@ -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.8"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui.test/.project b/codan/org.eclipse.cdt.codan.checkers.ui.test/.project
new file mode 100644
index 0000000000..031b00c652
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui.test/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.cdt.codan.checkers.ui.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>
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui.test/.settings/org.eclipse.jdt.core.prefs b/codan/org.eclipse.cdt.codan.checkers.ui.test/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000000..0c68a61dca
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui.test/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.compliance=1.8
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.8
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui.test/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.checkers.ui.test/META-INF/MANIFEST.MF
new file mode 100644
index 0000000000..6c8e44c760
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui.test/META-INF/MANIFEST.MF
@@ -0,0 +1,23 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: %Bundle-Name
+Bundle-SymbolicName: org.eclipse.cdt.codan.checkers.ui.test
+Bundle-Version: 3.2.0.qualifier
+Bundle-Activator: org.eclipse.cdt.codan.checkers.ui.Activator
+Require-Bundle: org.eclipse.core.runtime,
+ org.eclipse.cdt.codan.checkers.ui,
+ org.eclipse.core.resources,
+ org.eclipse.cdt.codan.ui,
+ org.eclipse.cdt.codan.ui.cxx,
+ org.eclipse.ui,
+ org.eclipse.ui.ide,
+ org.junit,
+ org.mockito,
+ org.hamcrest,
+ org.eclipse.cdt.core,
+ org.eclipse.jface.text,
+ org.eclipse.ui.workbench.texteditor,
+ org.eclipse.ui.editors
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Bundle-ActivationPolicy: lazy
+Bundle-Vendor: %Bundle-Vendor
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui.test/OSGI-INF/l10n/bundle.properties b/codan/org.eclipse.cdt.codan.checkers.ui.test/OSGI-INF/l10n/bundle.properties
new file mode 100644
index 0000000000..c796fcf3c0
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui.test/OSGI-INF/l10n/bundle.properties
@@ -0,0 +1,13 @@
+###############################################################################
+# Copyright (c) 2016 COSEDA Technologies GmbH 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:
+# Dominic Scharfe (COSEDA Technologies GmbH) - initial API and implementation
+###############################################################################
+#Properties file for org.eclipse.cdt.codan.checkers.ui
+Bundle-Vendor = Eclipse CDT
+Bundle-Name = Codan Checkers UI Tests
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui.test/build.properties b/codan/org.eclipse.cdt.codan.checkers.ui.test/build.properties
new file mode 100644
index 0000000000..34d2e4d2da
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui.test/build.properties
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui.test/pom.xml b/codan/org.eclipse.cdt.codan.checkers.ui.test/pom.xml
new file mode 100644
index 0000000000..8920e5cad8
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui.test/pom.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.eclipse.cdt</groupId>
+ <artifactId>cdt-parent</artifactId>
+ <version>9.1.0-SNAPSHOT</version>
+ <relativePath>../../pom.xml</relativePath>
+ </parent>
+
+ <version>3.2.0-SNAPSHOT</version>
+ <artifactId>org.eclipse.cdt.codan.checkers.ui.test</artifactId>
+ <packaging>eclipse-test-plugin</packaging>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.eclipse.tycho</groupId>
+ <artifactId>tycho-surefire-plugin</artifactId>
+ <version>${tycho-version}</version>
+ <configuration>
+ <!-- Need UI harness for quick fix tests -->
+ <useUIHarness>true</useUIHarness>
+ <argLine>${tycho.testArgLine} ${base.ui.test.vmargs}</argLine>
+ <testFailureIgnore>true</testFailureIgnore>
+ <dependencies>
+ <dependency>
+ <artifactId>org.eclipse.platform.feature.group</artifactId>
+ <type>p2-installable-unit</type>
+ </dependency>
+ </dependencies>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui.test/src/org/eclipse/cdt/codan/checkers/ui/Activator.java b/codan/org.eclipse.cdt.codan.checkers.ui.test/src/org/eclipse/cdt/codan/checkers/ui/Activator.java
new file mode 100644
index 0000000000..928d0408ae
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui.test/src/org/eclipse/cdt/codan/checkers/ui/Activator.java
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * Copyright (c) 2016 COSEDA Technologies GmbH
+ * 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:
+ * Dominic Scharfe (COSEDA Technologies GmbH) - initial implementation
+ *
+ *******************************************************************************/
+package org.eclipse.cdt.codan.checkers.ui;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+public class Activator implements BundleActivator {
+
+ private static BundleContext context;
+
+ static BundleContext getContext() {
+ return context;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+ */
+ public void start(BundleContext bundleContext) throws Exception {
+ Activator.context = bundleContext;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+ */
+ public void stop(BundleContext bundleContext) throws Exception {
+ Activator.context = null;
+ }
+
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui.test/src/org/eclipse/cdt/codan/checkers/ui/quickfix/QuickFixCreateNewClassTest.java b/codan/org.eclipse.cdt.codan.checkers.ui.test/src/org/eclipse/cdt/codan/checkers/ui/quickfix/QuickFixCreateNewClassTest.java
new file mode 100644
index 0000000000..687f1fd788
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui.test/src/org/eclipse/cdt/codan/checkers/ui/quickfix/QuickFixCreateNewClassTest.java
@@ -0,0 +1,126 @@
+/*******************************************************************************
+ * Copyright (c) 2016 COSEDA Technologies GmbH
+ * 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:
+ * Dominic Scharfe (COSEDA Technologies GmbH) - initial implementation
+ *
+ *******************************************************************************/
+package org.eclipse.cdt.codan.checkers.ui.quickfix;
+
+import static org.junit.Assert.*;
+
+import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateNewClass;
+import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import static org.mockito.Mockito.*;
+
+import java.util.function.Function;
+
+import org.eclipse.cdt.core.dom.ILinkage;
+import org.eclipse.cdt.core.model.ILanguage;
+import org.eclipse.cdt.core.model.ITranslationUnit;
+
+@RunWith(MockitoJUnitRunner.class)
+public class QuickFixCreateNewClassTest {
+
+ QuickFixCreateNewClass qut;
+
+ @Mock
+ IMarker marker;
+
+ /**
+ * TranslationUnit of the marker's resource which is received from the
+ * workspace.
+ */
+ @Mock
+ ITranslationUnit translationUnitViaWorkspace;
+
+ /**
+ * TranslationUnit of the marker's resource if there is an open editor.
+ */
+ @Mock
+ ITranslationUnit translationUnitWorkingCopy;
+
+ /**
+ * Function to get the working copy of translationUnitViaWorkspace
+ */
+ @Mock
+ Function<ITranslationUnit, ITranslationUnit> toWorkingCopy;
+
+ /**
+ * Language of the TranslationUnit
+ */
+ @Mock
+ ILanguage translationUnitLanguage;
+
+ @Before
+ public void setUp() throws Exception {
+ qut = new QuickFixCreateNewClass(toWorkingCopy) {
+ @Override
+ protected ITranslationUnit getTranslationUnitViaWorkspace(IMarker marker) {
+ if (QuickFixCreateNewClassTest.this.marker == marker) {
+ return translationUnitViaWorkspace;
+ }
+ throw new RuntimeException("Invalid marker");
+ };
+ };
+
+ when(toWorkingCopy.apply(translationUnitViaWorkspace)).thenReturn(translationUnitWorkingCopy);
+ when(translationUnitWorkingCopy.getLanguage()).thenReturn(translationUnitLanguage);
+ }
+
+ /**
+ * Test if the marker is applicable if
+ * {@link AbstractCodanCMarkerResolution#getTranslationUnitViaWorkspace}
+ * returns null.
+ */
+ @Test
+ public void isApplicableForUnresolvableMarker() {
+ translationUnitViaWorkspace = null;
+ assertTrue("Unresolvable marker is not applicable", qut.isApplicable(marker));
+ }
+
+ /**
+ * Test if the marker is applicable if
+ * {@link AbstractCodanCMarkerResolution#getTranslationUnitViaWorkspace}
+ * returns a marker valid marker.
+ */
+ @Test
+ public void isApplicableForResolvableMarker() {
+ assertTrue("Resolvable marker is not applicable", qut.isApplicable(marker));
+ }
+
+ /**
+ * Test if the marker is applicable if
+ * <code>tu.getLanguage().getLinkageID() == ILinkage.C_LINKAGE_ID</code>
+ */
+ @Test
+ public void isNotApplicableForResolvableMarkerWithCLinkage() {
+ when(translationUnitLanguage.getLinkageID()).thenReturn(ILinkage.C_LINKAGE_ID);
+ assertFalse("Resolvable marker with ILinkage.C_LINKAGE_ID must not be applicable", qut.isApplicable(marker));
+ }
+
+ /**
+ * Test if the marker is applicable if <code>tu.getLanguage()</code> throws
+ * a {@link CoreException}.
+ *
+ * @throws CoreException
+ */
+ @Test
+ public void isApplicableForResolvableMarkerWithCLinkageResolutionCoreException() throws CoreException {
+ CoreException coreException = new CoreException(mock(IStatus.class));
+ doThrow(coreException).when(translationUnitWorkingCopy).getLanguage();
+ assertTrue("Resolvable marker with invalid language must be applicable", qut.isApplicable(marker));
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF
index 62aa6c037e..ae0d4e0255 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF
@@ -20,5 +20,5 @@ Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Vendor: %Bundle-Vendor
Export-Package: org.eclipse.cdt.codan.internal.checkers.ui;x-internal:=true,
- org.eclipse.cdt.codan.internal.checkers.ui.quickfix;x-internal:=true
+ org.eclipse.cdt.codan.internal.checkers.ui.quickfix;x-friends:="org.eclipse.cdt.codan.checkers.ui.test"
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCreateNewClass.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCreateNewClass.java
index 0907027dc7..605484d03d 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCreateNewClass.java
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCreateNewClass.java
@@ -8,15 +8,20 @@
* Contributors:
* Alena Laskavaia - initial API and implementation,
* inspired by work of Erik Johansson <erik.johansson.979@gmail.com>
+ * Dominic Scharfe (COSEDA Technologies GmbH) - Fix for bug 507148
*
*******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
+import java.util.Optional;
+import java.util.function.Function;
+
import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ITranslationUnit;
+import org.eclipse.cdt.internal.corext.util.CModelUtil;
import org.eclipse.cdt.internal.ui.wizards.NewClassCreationWizard;
import org.eclipse.cdt.ui.CDTSharedImages;
import org.eclipse.core.resources.IMarker;
@@ -31,6 +36,27 @@ import org.eclipse.ui.IMarkerResolution2;
import org.eclipse.ui.PlatformUI;
public class QuickFixCreateNewClass extends AbstractCodanCMarkerResolution implements IMarkerResolution2 {
+ /**
+ * Returns the working copy TU of the given TU. If the TU is already a
+ * working copy or the TU has no working copy the input TU is returned.
+ */
+ private final Function<ITranslationUnit, ITranslationUnit> toWorkingCopy;
+
+ /**
+ * Default constructor.
+ * Uses {@link CModelUtil#toWorkingCopy(ITranslationUnit)} to get the
+ * working copy of a {@link ITranslationUnit}.
+ *
+ * TODO: Inject {@link #toWorkingCopy} as a dependency.
+ */
+ public QuickFixCreateNewClass() {
+ this(tu -> CModelUtil.toWorkingCopy(tu));
+ }
+
+ public QuickFixCreateNewClass(Function<ITranslationUnit, ITranslationUnit> toWorkingCopy) {
+ this.toWorkingCopy = toWorkingCopy;
+ }
+
@Override
public String getLabel() {
// TODO Should provide class name as message parameter
@@ -39,14 +65,26 @@ public class QuickFixCreateNewClass extends AbstractCodanCMarkerResolution imple
@Override
public boolean isApplicable(IMarker marker) {
- ITranslationUnit tu = getTranslationUnitViaEditor(marker);
- try {
- if (tu.getLanguage().getLinkageID() == ILinkage.C_LINKAGE_ID)
- return false;
- } catch (CoreException e) {
- // ignore
- }
- return true;
+ return getTranslationUnitViaEditorOrWorkspace(marker).map(tu -> {
+ try {
+ return tu.getLanguage().getLinkageID() != ILinkage.C_LINKAGE_ID;
+ } catch (CoreException e) {
+ return true;
+ }
+ }).orElse(true);
+ }
+
+ /**
+ * Receives a translation unit from a given marker. The TU is received from
+ * an editor (if opened),
+ * or by calling {@link #getTranslationUnitViaWorkspace(IMarker)}.
+ *
+ * @param marker
+ * A marker in a translation unit
+ * @return The translation unit
+ */
+ protected Optional<ITranslationUnit> getTranslationUnitViaEditorOrWorkspace(IMarker marker) {
+ return Optional.ofNullable(getTranslationUnitViaWorkspace(marker)).map(toWorkingCopy);
}
@Override

Back to the top