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>
This commit is contained in:
Dominic Scharfe 2016-11-10 17:50:52 +01:00
parent dc6e3a06ff
commit 56c496ba5a
13 changed files with 337 additions and 10 deletions

View 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.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View file

@ -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>

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.

View file

@ -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>

View file

@ -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;
}
}

View file

@ -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));
}
}

View file

@ -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"

View file

@ -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

View file

@ -11,7 +11,7 @@ Export-Package: org.eclipse.cdt.internal.corext;x-internal:=true,
org.eclipse.cdt.internal.corext.fix;x-internal:=true,
org.eclipse.cdt.internal.corext.refactoring.code.flow;x-internal:=true,
org.eclipse.cdt.internal.corext.template.c;x-internal:=true,
org.eclipse.cdt.internal.corext.util;x-friends:="org.eclipse.cdt.codan.ui",
org.eclipse.cdt.internal.corext.util;x-friends:="org.eclipse.cdt.codan.ui,org.eclipse.cdt.codan.checkers.ui",
org.eclipse.cdt.internal.ui;x-friends:="org.eclipse.cdt.debug.edc.tests",
org.eclipse.cdt.internal.ui.actions;x-internal:=true,
org.eclipse.cdt.internal.ui.browser.opentype;x-internal:=true,

View file

@ -175,6 +175,7 @@
<module>core/org.eclipse.cdt.core.tests</module>
<module>core/org.eclipse.cdt.ui.tests</module>
<module>codan/org.eclipse.cdt.codan.core.test</module>
<module>codan/org.eclipse.cdt.codan.checkers.ui.test</module>
<module>build/org.eclipse.cdt.managedbuilder.core.tests</module>
<module>build/org.eclipse.cdt.managedbuilder.ui.tests</module>
<module>build/org.eclipse.cdt.make.core.tests</module>