From 1c30b28d1607f17d9073cd03dded5c2819dcf661 Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 12:52:55 +0200 Subject: [PATCH 01/10] Revert "Revert "205: PitMutationsViewContentProvider"" This reverts commit d4a663e1f9a9b1ee236c9b7f4ab1d82afaeeb3e3. --- .../ui/view/mutations/PitMutationsView.java | 2 +- ...a => PitMutationsViewContentProvider.java} | 25 ++++++------------- 2 files changed, 9 insertions(+), 18 deletions(-) rename bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/{ViewContentProvider.java => PitMutationsViewContentProvider.java} (84%) diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java index a80110ec..6f662c36 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java @@ -67,7 +67,7 @@ public void run() { private void createTreeViewer(Composite parent) { viewer = new TreeViewer(parent, TREE_STYLE); - viewer.setContentProvider(new ViewContentProvider()); + viewer.setContentProvider(new PitMutationsViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); viewer.addDoubleClickListener(ExpandingDoubleClick.LISTENER); viewer.addDoubleClickListener(OpenMutationDoubleClick.LISTENER); diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/ViewContentProvider.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsViewContentProvider.java similarity index 84% rename from bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/ViewContentProvider.java rename to bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsViewContentProvider.java index ee12d2cb..88a79e3a 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/ViewContentProvider.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsViewContentProvider.java @@ -26,7 +26,7 @@ import org.pitest.pitclipse.runner.model.Status; import org.pitest.pitclipse.runner.model.Visitable; -public class ViewContentProvider implements ITreeContentProvider { +public class PitMutationsViewContentProvider implements ITreeContentProvider { @Override public Object[] getElements(Object element) { @@ -39,30 +39,21 @@ public Object[] getChildren(Object element) { } private Object[] handleElementsOrChildren(Object element) { - if (element instanceof Visitable) { - Visitable visitable = (Visitable) element; - return visitable.accept(Structure.VISITOR); - } - return nothing(); + Visitable visitable = (Visitable) element; + return visitable.accept(Structure.VISITOR); } @Override public Object getParent(Object element) { - if (element instanceof Visitable) { - Visitable visitable = (Visitable) element; - return visitable.accept(Parent.VISITOR); - } - return null; + Visitable visitable = (Visitable) element; + return visitable.accept(Parent.VISITOR); } @Override public boolean hasChildren(Object element) { - if (element instanceof Visitable) { - Visitable visitable = (Visitable) element; - Object[] children = visitable.accept(Structure.VISITOR); - return children.length > 0; - } - return false; + Visitable visitable = (Visitable) element; + Object[] children = visitable.accept(Structure.VISITOR); + return children.length > 0; } private enum Structure implements MutationsModelVisitor { From 9c29d688dd64cfa4c31b1d30fbc64c0bce889c86 Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 12:57:26 +0200 Subject: [PATCH 02/10] 205: MutationsModelVisitorAdapter Task-Url: http://github.com/pitest/pitclipse/issues/205 --- .../model/MutationsModelVisitorAdapter.java | 57 +++++++++++++++++++ pom.xml | 1 + 2 files changed, 58 insertions(+) create mode 100644 bundles/org.pitest.pitclipse.runner/src/org/pitest/pitclipse/runner/model/MutationsModelVisitorAdapter.java diff --git a/bundles/org.pitest.pitclipse.runner/src/org/pitest/pitclipse/runner/model/MutationsModelVisitorAdapter.java b/bundles/org.pitest.pitclipse.runner/src/org/pitest/pitclipse/runner/model/MutationsModelVisitorAdapter.java new file mode 100644 index 00000000..52c4d503 --- /dev/null +++ b/bundles/org.pitest.pitclipse.runner/src/org/pitest/pitclipse/runner/model/MutationsModelVisitorAdapter.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright 2022 Lorenzo Bettini and contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + ******************************************************************************/ +package org.pitest.pitclipse.runner.model; + +/** + * Default implementation returning null. + * + * @author Lorenzo Bettini + * + * @param + */ +public interface MutationsModelVisitorAdapter extends MutationsModelVisitor { + + @Override + default T visitModel(MutationsModel mutationsModel) { + return null; + } + + @Override + default T visitProject(ProjectMutations projectMutations) { + return null; + } + + @Override + default T visitPackage(PackageMutations packageMutations) { + return null; + } + + @Override + default T visitClass(ClassMutations classMutations) { + return null; + } + + @Override + default T visitMutation(Mutation mutation) { + return null; + } + + @Override + default T visitStatus(Status status) { + return null; + } + +} diff --git a/pom.xml b/pom.xml index 4d88c8bd..2e1f7341 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,7 @@ **/PitclipseUiUtils.java, **/PitclipseLaunchUiUtils.java, **/LinkSelectionAdapter.java, + **/MutationsModelVisitorAdapter.java, ${basedir}/../../tests/org.pitest.pitclipse.tests.coverage.report/target/site/jacoco-aggregate/jacoco.xml From 34b7418767e652d02ed306f0b2f32c5c5658caaf Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 14:07:39 +0200 Subject: [PATCH 03/10] 205: test project with default package Task-Url: http://github.com/pitest/pitclipse/issues/205 --- .../.classpath | 7 +++++++ .../.project | 17 +++++++++++++++++ .../.settings/org.eclipse.core.resources.prefs | 2 ++ .../.settings/org.eclipse.jdt.core.prefs | 15 +++++++++++++++ .../.settings/org.eclipse.jdt.launching.prefs | 3 +++ .../README | 4 ++++ .../src/DefaultPackageFoo.java | 13 +++++++++++++ .../src/foobar/Bar1.java | 13 +++++++++++++ .../src/foobar/BarTest.java | 10 ++++++++++ .../src/foobar/Foo1.java | 13 +++++++++++++ .../src/foobar/FooTest.java | 10 ++++++++++ 11 files changed, 107 insertions(+) create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.classpath create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.project create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.core.resources.prefs create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.jdt.core.prefs create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.jdt.launching.prefs create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/README create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/DefaultPackageFoo.java create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/Bar1.java create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/BarTest.java create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/Foo1.java create mode 100644 tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/FooTest.java diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.classpath b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.classpath new file mode 100644 index 00000000..373dce40 --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.project b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.project new file mode 100644 index 00000000..c537dced --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.project @@ -0,0 +1,17 @@ + + + org.pitest.pitclipse.testprojects.threeclasses + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.core.resources.prefs b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000..99f26c02 --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.jdt.core.prefs b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..0fee6a9c --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,15 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.jdt.launching.prefs b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.jdt.launching.prefs new file mode 100644 index 00000000..d177941e --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/.settings/org.eclipse.jdt.launching.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.launching.PREF_COMPILER_COMPLIANCE_DOES_NOT_MATCH_JRE=ignore +org.eclipse.jdt.launching.PREF_STRICTLY_COMPATIBLE_JRE_NOT_AVAILABLE=ignore diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/README b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/README new file mode 100644 index 00000000..af042335 --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/README @@ -0,0 +1,4 @@ +This is a project imported by our SWTBot tests (org.pitest.pitclipse.ui.tests) +in particular, by the PitclipseOptionsTest and PitclipsePitMutationsViewTest. + +This one also has a default package with a class diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/DefaultPackageFoo.java b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/DefaultPackageFoo.java new file mode 100644 index 00000000..4c8e0fb2 --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/DefaultPackageFoo.java @@ -0,0 +1,13 @@ + + +public class DefaultPackageFoo { + + public int f(int i) { + java.util.ArrayList pointless = new java.util.ArrayList<>(); + if (pointless.size() == 1) + return i + 1; + else + return 0; + } + +} diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/Bar1.java b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/Bar1.java new file mode 100644 index 00000000..6982e7c6 --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/Bar1.java @@ -0,0 +1,13 @@ +package foobar; + +public class Bar1 { + + public int f(int i) { + java.util.ArrayList pointless = new java.util.ArrayList<>(); + if (pointless.size() == 1) + return i + 1; + else + return 0; + } + +} diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/BarTest.java b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/BarTest.java new file mode 100644 index 00000000..6e99ac89 --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/BarTest.java @@ -0,0 +1,10 @@ +package foobar; + +public class BarTest { + + @org.junit.Test public void badTest() { + Bar1 x = new Bar1(); + x.f(1); + } + +} diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/Foo1.java b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/Foo1.java new file mode 100644 index 00000000..f3d252bf --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/Foo1.java @@ -0,0 +1,13 @@ +package foobar; + +public class Foo1 { + + public int f(int i) { + java.util.ArrayList pointless = new java.util.ArrayList<>(); + if (pointless.size() == 1) + return i + 1; + else + return 0; + } + +} diff --git a/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/FooTest.java b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/FooTest.java new file mode 100644 index 00000000..75a45e68 --- /dev/null +++ b/tests/testprojects/org.pitest.pitclipse.testprojects.threeclasses/src/foobar/FooTest.java @@ -0,0 +1,10 @@ +package foobar; + +public class FooTest { + + @org.junit.Test public void badTest() { + Foo1 x = new Foo1(); + x.f(1); + } + +} From 72aa6ec30622d4dced2ba1680d1002ed4bf00a8b Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 14:07:56 +0200 Subject: [PATCH 04/10] 205: PitMutationsViewLabelProvider Task-Url: http://github.com/pitest/pitclipse/issues/205 --- .../ui/view/mutations/PitMutationsView.java | 2 +- ...ava => PitMutationsViewLabelProvider.java} | 63 +++++++------------ .../tests/PitclipsePitMutationsViewTest.java | 5 +- 3 files changed, 27 insertions(+), 43 deletions(-) rename bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/{ViewLabelProvider.java => PitMutationsViewLabelProvider.java} (82%) diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java index 6f662c36..c1b26928 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java @@ -68,7 +68,7 @@ public void run() { private void createTreeViewer(Composite parent) { viewer = new TreeViewer(parent, TREE_STYLE); viewer.setContentProvider(new PitMutationsViewContentProvider()); - viewer.setLabelProvider(new ViewLabelProvider()); + viewer.setLabelProvider(new PitMutationsViewLabelProvider()); viewer.addDoubleClickListener(ExpandingDoubleClick.LISTENER); viewer.addDoubleClickListener(OpenMutationDoubleClick.LISTENER); viewer.setInput(MutationsModel.EMPTY_MODEL); diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/ViewLabelProvider.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsViewLabelProvider.java similarity index 82% rename from bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/ViewLabelProvider.java rename to bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsViewLabelProvider.java index 69b566f7..11d7892b 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/ViewLabelProvider.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsViewLabelProvider.java @@ -16,13 +16,24 @@ package org.pitest.pitclipse.ui.view.mutations; +import static com.google.common.collect.Sets.immutableEnumSet; +import static org.pitest.pitclipse.runner.results.DetectionStatus.KILLED; +import static org.pitest.pitclipse.runner.results.DetectionStatus.MEMORY_ERROR; +import static org.pitest.pitclipse.runner.results.DetectionStatus.NON_VIABLE; +import static org.pitest.pitclipse.runner.results.DetectionStatus.NOT_STARTED; +import static org.pitest.pitclipse.runner.results.DetectionStatus.RUN_ERROR; +import static org.pitest.pitclipse.runner.results.DetectionStatus.STARTED; +import static org.pitest.pitclipse.runner.results.DetectionStatus.TIMED_OUT; + +import java.net.URL; +import java.util.Set; + import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Path; import org.eclipse.jdt.ui.JavaUI; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.swt.graphics.Image; -import org.eclipse.ui.ISharedImages; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.IDE.SharedImages; import org.osgi.framework.Bundle; @@ -30,28 +41,14 @@ import org.pitest.pitclipse.runner.model.ClassMutations; import org.pitest.pitclipse.runner.model.Countable; import org.pitest.pitclipse.runner.model.Mutation; -import org.pitest.pitclipse.runner.model.MutationsModel; -import org.pitest.pitclipse.runner.model.MutationsModelVisitor; +import org.pitest.pitclipse.runner.model.MutationsModelVisitorAdapter; import org.pitest.pitclipse.runner.model.PackageMutations; import org.pitest.pitclipse.runner.model.ProjectMutations; import org.pitest.pitclipse.runner.model.Status; import org.pitest.pitclipse.runner.model.Visitable; import org.pitest.pitclipse.runner.results.DetectionStatus; -import java.net.URL; -import java.util.Set; - -import static com.google.common.base.Strings.isNullOrEmpty; -import static com.google.common.collect.Sets.immutableEnumSet; -import static org.pitest.pitclipse.runner.results.DetectionStatus.KILLED; -import static org.pitest.pitclipse.runner.results.DetectionStatus.MEMORY_ERROR; -import static org.pitest.pitclipse.runner.results.DetectionStatus.NON_VIABLE; -import static org.pitest.pitclipse.runner.results.DetectionStatus.NOT_STARTED; -import static org.pitest.pitclipse.runner.results.DetectionStatus.RUN_ERROR; -import static org.pitest.pitclipse.runner.results.DetectionStatus.STARTED; -import static org.pitest.pitclipse.runner.results.DetectionStatus.TIMED_OUT; - -public class ViewLabelProvider extends LabelProvider { +public class PitMutationsViewLabelProvider extends LabelProvider { private static final Image MUTATION_DETECTED = getBundleImage("detected.gif"); private static final Image MUTATION_NOT_DETECTED = getBundleImage("not_detected.gif"); @@ -60,37 +57,26 @@ public class ViewLabelProvider extends LabelProvider { @Override public String getText(Object element) { - if (element instanceof Visitable) { - Visitable visitable = (Visitable) element; - return visitable.accept(LabelVisitor.INSTANCE); - } - return ""; + Visitable visitable = (Visitable) element; + return visitable.accept(LabelVisitor.INSTANCE); } @Override public Image getImage(Object element) { - if (element instanceof Visitable) { - Visitable visitable = (Visitable) element; - return visitable.accept(ImageVisitor.INSTANCE); - } - return null; + Visitable visitable = (Visitable) element; + return visitable.accept(ImageVisitor.INSTANCE); } private static Image getBundleImage(String file) { - Bundle bundle = FrameworkUtil.getBundle(ViewLabelProvider.class); + Bundle bundle = FrameworkUtil.getBundle(PitMutationsViewLabelProvider.class); URL url = FileLocator.find(bundle, new Path("icons/" + file), null); ImageDescriptor image = ImageDescriptor.createFromURL(url); return image.createImage(); } - private enum LabelVisitor implements MutationsModelVisitor { + private enum LabelVisitor implements MutationsModelVisitorAdapter { INSTANCE; - @Override - public String visitModel(MutationsModel mutationsModel) { - return "Mutations"; - } - @Override public String visitProject(ProjectMutations projectMutations) { return projectMutations.getProjectName() + countString(projectMutations); @@ -99,7 +85,7 @@ public String visitProject(ProjectMutations projectMutations) { @Override public String visitPackage(PackageMutations packageMutations) { String label = packageMutations.getPackageName(); - if (isNullOrEmpty(label)) { + if (label.isEmpty()) { label = "(default package)"; } return label + countString(packageMutations); @@ -125,14 +111,9 @@ private String countString(Countable countable) { } } - private enum ImageVisitor implements MutationsModelVisitor { + private enum ImageVisitor implements MutationsModelVisitorAdapter { INSTANCE; - @Override - public Image visitModel(MutationsModel mutationsModel) { - return getPlatformIcon(ISharedImages.IMG_OBJ_ELEMENT); - } - @Override public Image visitProject(ProjectMutations projectMutations) { return getPlatformIcon(SharedImages.IMG_OBJ_PROJECT); diff --git a/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipsePitMutationsViewTest.java b/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipsePitMutationsViewTest.java index 713b1edf..f829f294 100644 --- a/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipsePitMutationsViewTest.java +++ b/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipsePitMutationsViewTest.java @@ -23,7 +23,9 @@ public class PitclipsePitMutationsViewTest extends AbstractPitclipseSWTBotTest { private static final String TEST_PROJECT = "org.pitest.pitclipse.testprojects.twoclasses";; + private static final String TEST_PROJECT_WITH_DEFAULT_PACKAGE = "org.pitest.pitclipse.testprojects.threeclasses";; private static final String FOO_BAR_PACKAGE = "foo.bar"; + private static final String FOOBAR_PACKAGE = "foobar"; private static final String FOO_CLASS = "Foo"; private static final String FOO_TEST_CLASS = "FooTest"; private static final String BAR_CLASS = "Bar"; @@ -32,6 +34,7 @@ public class PitclipsePitMutationsViewTest extends AbstractPitclipseSWTBotTest { @BeforeClass public static void setupJavaProject() throws CoreException { importTestProject(TEST_PROJECT); + importTestProject(TEST_PROJECT_WITH_DEFAULT_PACKAGE); } @Test @@ -55,7 +58,7 @@ public void selectMutationOpensTheClassAtTheRightLineNumber() throws CoreExcepti @Test public void expandAndCollapse() throws CoreException { - runTest(BAR_TEST_CLASS, FOO_BAR_PACKAGE, TEST_PROJECT); + runPackageTest(FOOBAR_PACKAGE, TEST_PROJECT_WITH_DEFAULT_PACKAGE); final PitMutationsViewPageObject pitMutationsView = new PitMutationsViewPageObject(bot); SWTBotTree mutationTreeRoot = pitMutationsView.mutationTreeRoot(); final SWTBotTreeItem firstItem = mutationTreeRoot.getAllItems()[0]; From 2981162fe88594b093f17646bd28f339af6d48a5 Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 14:19:52 +0200 Subject: [PATCH 05/10] 205: started simplifying OpenMutationDoubleClick Task-Url: http://github.com/pitest/pitclipse/issues/205 --- .../mutations/OpenMutationDoubleClick.java | 40 ++----------------- 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java index e2a871a2..aef7f58e 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java @@ -45,13 +45,8 @@ import org.eclipse.ui.progress.UIJob; import org.eclipse.ui.texteditor.IDocumentProvider; import org.eclipse.ui.texteditor.ITextEditor; -import org.pitest.pitclipse.runner.model.ClassMutations; import org.pitest.pitclipse.runner.model.Mutation; -import org.pitest.pitclipse.runner.model.MutationsModel; -import org.pitest.pitclipse.runner.model.MutationsModelVisitor; -import org.pitest.pitclipse.runner.model.PackageMutations; -import org.pitest.pitclipse.runner.model.ProjectMutations; -import org.pitest.pitclipse.runner.model.Status; +import org.pitest.pitclipse.runner.model.MutationsModelVisitorAdapter; import org.pitest.pitclipse.runner.model.Visitable; public enum OpenMutationDoubleClick implements IDoubleClickListener { @@ -70,39 +65,17 @@ public enum OpenMutationDoubleClick implements IDoubleClickListener { public void doubleClick(DoubleClickEvent event) { IStructuredSelection selection = selectionFrom(event); Object element = selection.getFirstElement(); - if (element instanceof Visitable) { - Visitable visitable = (Visitable) element; - visitable.accept(MutationSource.VIEWER); - } + Visitable visitable = (Visitable) element; + visitable.accept(MutationSource.VIEWER); } private IStructuredSelection selectionFrom(DoubleClickEvent event) { return (IStructuredSelection) event.getSelection(); } - private enum MutationSource implements MutationsModelVisitor { + private enum MutationSource implements MutationsModelVisitorAdapter { VIEWER; - @Override - public Void visitModel(MutationsModel mutationsModel) { - return null; - } - - @Override - public Void visitProject(ProjectMutations projectMutations) { - return null; - } - - @Override - public Void visitPackage(PackageMutations packageMutations) { - return null; - } - - @Override - public Void visitClass(ClassMutations classMutations) { - return null; - } - @Override public Void visitMutation(Mutation mutation) { final String projectName = mutation.getClassMutations().getPackageMutations().getProjectMutations() @@ -114,11 +87,6 @@ public Void visitMutation(Mutation mutation) { return null; } - @Override - public Void visitStatus(Status status) { - return null; - } - private static final class MutationSelectingJob extends UIJob { private final String projectName; private final int lineNumber; From 38c5583e14ec37724dfa818b1b58badfc63d0411 Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 14:33:47 +0200 Subject: [PATCH 06/10] 205: moved from LaunchUiUtils to UiUtils apart from something specific of launch ui Task-Url: http://github.com/pitest/pitclipse/issues/205 --- .../pitclipse/launch/ui/LaunchShortcut.java | 6 +- .../launch/ui/PitLaunchShortcut.java | 8 +- .../ui/utils/PitclipseLaunchUiUtils.java | 80 +----------------- .../pitclipse/ui/utils/PitclipseUiUtils.java | 81 +++++++++++++++++++ 4 files changed, 89 insertions(+), 86 deletions(-) diff --git a/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/LaunchShortcut.java b/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/LaunchShortcut.java index e7c3e8c2..9c16a657 100644 --- a/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/LaunchShortcut.java +++ b/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/LaunchShortcut.java @@ -28,7 +28,7 @@ import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.ITypeRoot; import org.eclipse.ui.IEditorInput; -import org.pitest.pitclipse.launch.ui.utils.PitclipseLaunchUiUtils; +import org.pitest.pitclipse.ui.utils.PitclipseUiUtils; import com.google.common.collect.ImmutableList; @@ -41,7 +41,7 @@ static Optional forEditorInputDo(IEditorInput i, Function> getCorrespondingResource() { return t -> - Optional.ofNullable(PitclipseLaunchUiUtils.executeSafelyOrElse( + Optional.ofNullable(PitclipseUiUtils.executeSafelyOrElse( t::getCorrespondingResource, null)); } @@ -49,7 +49,7 @@ static Optional asJavaElement(Object o) { if (o instanceof IJavaElement) { return Optional.of((IJavaElement) o); } - return Optional.ofNullable(PitclipseLaunchUiUtils.tryToAdapt(o, IJavaElement.class)); + return Optional.ofNullable(PitclipseUiUtils.tryToAdapt(o, IJavaElement.class)); } static ImmutableList emptyLaunchConfiguration() { diff --git a/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/PitLaunchShortcut.java b/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/PitLaunchShortcut.java index bc93586f..e0756d3a 100644 --- a/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/PitLaunchShortcut.java +++ b/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/PitLaunchShortcut.java @@ -72,8 +72,8 @@ import org.eclipse.ui.IEditorPart; import org.eclipse.ui.dialogs.ElementListSelectionDialog; import org.pitest.pitclipse.core.PitCoreActivator; -import org.pitest.pitclipse.launch.ui.utils.PitclipseLaunchUiUtils; import org.pitest.pitclipse.runner.config.PitConfiguration; +import org.pitest.pitclipse.ui.utils.PitclipseUiUtils; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; @@ -104,7 +104,7 @@ public void launch(ISelection selection, String mode) { } private void launch(Object[] elements, String mode) { - PitclipseLaunchUiUtils.executeSafely(() -> { + PitclipseUiUtils.executeSafely(() -> { if (elements.length == 1) { Optional selected = asJavaElement(elements[0]); Optional launchElement = @@ -235,7 +235,7 @@ protected String[] getAttributeNamesToCompare() { } private static boolean hasSameAttributes(ILaunchConfiguration config1, ILaunchConfiguration config2, String[] attributeToCompare) { - return PitclipseLaunchUiUtils.executeSafely(() -> { + return PitclipseUiUtils.executeSafely(() -> { for (String element : attributeToCompare) { String val1 = config1.getAttribute(element, EMPTY_STRING); String val2 = config2.getAttribute(element, EMPTY_STRING); @@ -349,7 +349,7 @@ private Function> findLaunchConfigurati Optional launchElement = getLaunchElementFor(element); return launchElement .map(javaElement -> - PitclipseLaunchUiUtils.executeSafelyOrElse( + PitclipseUiUtils.executeSafelyOrElse( () -> findExistingLaunchConfigurations (createLaunchConfiguration(javaElement)), emptyLaunchConfiguration())) diff --git a/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/utils/PitclipseLaunchUiUtils.java b/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/utils/PitclipseLaunchUiUtils.java index 4d5f78b1..36e61246 100644 --- a/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/utils/PitclipseLaunchUiUtils.java +++ b/bundles/org.pitest.pitclipse.launch.ui/src/org/pitest/pitclipse/launch/ui/utils/PitclipseLaunchUiUtils.java @@ -16,8 +16,8 @@ package org.pitest.pitclipse.launch.ui.utils; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IAdaptable; import org.pitest.pitclipse.launch.ui.PitMutatorsTab; +import org.pitest.pitclipse.ui.utils.PitclipseUiUtils.RunnableWithCoreException; /** * A few utilities for the UI. @@ -31,26 +31,6 @@ private PitclipseLaunchUiUtils() { // Only static utility methods } - @FunctionalInterface - public static interface RunnableWithCoreException { - void run() throws CoreException; - } - - @FunctionalInterface - public static interface ProviderWithCoreException { - T get() throws CoreException; - } - - @FunctionalInterface - public static interface PredicateWithCoreException { - boolean test() throws CoreException; - } - - @FunctionalInterface - public static interface RunnableWithCoreExceptionInterruptable { - void run() throws CoreException, InterruptedException; - } - /** * Executes the passed lambda and in case of {@link CoreException} sets the * error message. @@ -66,62 +46,4 @@ public static void executeSafely(RunnableWithCoreException runnable, PitMutators } } - /** - * Executes the passed predicate and in case of {@link CoreException} returns - * false. - * - * @param predicate - */ - public static boolean executeSafely(PredicateWithCoreException predicate) { - try { - return predicate.test(); - } catch (CoreException ce) { - return false; - } - } - - /** - * Returns the result of the passed provider and in case of {@link CoreException} returns - * the orElse argument. - * - * @param predicate - */ - public static T executeSafelyOrElse(ProviderWithCoreException provider, T orElse) { - try { - return provider.get(); - } catch (CoreException ce) { - return orElse; - } - } - - /** - * Executes the passed lambda and ignores exceptions. - * - * @param runnable - */ - public static void executeSafely(RunnableWithCoreExceptionInterruptable runnable) { - try { - runnable.run(); - } catch (InterruptedException e) { // NOSONAR - // OK, silently move on - } catch (CoreException e) { // NOSONAR - // OK, silently move on - } - } - - /** - * Try to adapt the object to the {@link IAdaptable} type, or returns null - * if it's not an {@link IAdaptable}. - * - * @param - * @param o - * @param type - * @return - */ - public static T tryToAdapt(Object o, Class type) { - if (o instanceof IAdaptable) { - return ((IAdaptable) o).getAdapter(type); - } - return null; - } } diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java index 85b2edc9..bd038242 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java @@ -17,7 +17,9 @@ import java.net.URL; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.Path; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.SWT; @@ -40,6 +42,85 @@ private PitclipseUiUtils() { // Only static utility methods } + @FunctionalInterface + public static interface RunnableWithCoreException { + void run() throws CoreException; + } + + @FunctionalInterface + public static interface ProviderWithCoreException { + T get() throws CoreException; + } + + @FunctionalInterface + public static interface PredicateWithCoreException { + boolean test() throws CoreException; + } + + @FunctionalInterface + public static interface RunnableWithCoreExceptionInterruptable { + void run() throws CoreException, InterruptedException; + } + + /** + * Executes the passed predicate and in case of {@link CoreException} returns + * false. + * + * @param predicate + */ + public static boolean executeSafely(PredicateWithCoreException predicate) { + try { + return predicate.test(); + } catch (CoreException ce) { + return false; + } + } + + /** + * Returns the result of the passed provider and in case of {@link CoreException} returns + * the orElse argument. + * + * @param predicate + */ + public static T executeSafelyOrElse(ProviderWithCoreException provider, T orElse) { + try { + return provider.get(); + } catch (CoreException ce) { + return orElse; + } + } + + /** + * Executes the passed lambda and ignores exceptions. + * + * @param runnable + */ + public static void executeSafely(RunnableWithCoreExceptionInterruptable runnable) { + try { + runnable.run(); + } catch (InterruptedException e) { // NOSONAR + // OK, silently move on + } catch (CoreException e) { // NOSONAR + // OK, silently move on + } + } + + /** + * Try to adapt the object to the {@link IAdaptable} type, or returns null + * if it's not an {@link IAdaptable}. + * + * @param + * @param o + * @param type + * @return + */ + public static T tryToAdapt(Object o, Class type) { + if (o instanceof IAdaptable) { + return ((IAdaptable) o).getAdapter(type); + } + return null; + } + /** * Only performs the operation if the {@link Composite} is not null and not * disposed. From a4e38fdc10269d2195eac1f2e98138c10ff6988a Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 14:44:57 +0200 Subject: [PATCH 07/10] 205: simplified OpenMutationDoubleClick findClass(String, String) Task-Url: http://github.com/pitest/pitclipse/issues/205 --- .../mutations/OpenMutationDoubleClick.java | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java index aef7f58e..45586774 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java @@ -18,8 +18,10 @@ import static org.eclipse.ui.ide.IDE.openEditor; +import java.util.Objects; import java.util.Optional; import java.util.function.Function; +import java.util.stream.Stream; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; @@ -28,10 +30,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; -import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; @@ -48,6 +47,7 @@ import org.pitest.pitclipse.runner.model.Mutation; import org.pitest.pitclipse.runner.model.MutationsModelVisitorAdapter; import org.pitest.pitclipse.runner.model.Visitable; +import org.pitest.pitclipse.ui.utils.PitclipseUiUtils; public enum OpenMutationDoubleClick implements IDoubleClickListener { @@ -113,20 +113,15 @@ public boolean belongsTo(Object family) { private Optional findClass(final String projectName, final String className) { IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); - for (IProject project : root.getProjects()) { - if (project.getName().equals(projectName) && project.isOpen()) { - IJavaProject javaProject = JavaCore.create(project); - if (javaProject != null) { - try { - IType type = javaProject.findType(className); - return Optional.ofNullable(root.getFile(type.getPath())); - } catch (JavaModelException e) { - // Maybe type no longer exists. Do nothing - } - } - } - } - return Optional.empty(); + return Stream.of(root.getProjects()) + .filter(IProject::isOpen) + .filter(project -> project.getName().equals(projectName)) + .map(JavaCore::create) + .filter(Objects::nonNull) + .map(javaProject -> PitclipseUiUtils.executeSafelyOrElse(() -> + root.getFile(javaProject.findType(className).getPath()), null)) + .filter(Objects::nonNull) + .findFirst(); } private static final class OpenFileInEditorAtLine implements Function { From 5d15bc88b83395d33fb5dbf904796dfb27b108aa Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 14:56:16 +0200 Subject: [PATCH 08/10] 205: OpenMutationDoubleClick ignore impossible situations Task-Url: http://github.com/pitest/pitclipse/issues/205 --- .../pitclipse/ui/utils/PitclipseUiUtils.java | 22 ++++++++++ .../mutations/OpenMutationDoubleClick.java | 40 ++++++++----------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java index bd038242..fd2a48f1 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java @@ -42,6 +42,11 @@ private PitclipseUiUtils() { // Only static utility methods } + @FunctionalInterface + public static interface RunnableWithException { + void run() throws T; + } + @FunctionalInterface public static interface RunnableWithCoreException { void run() throws CoreException; @@ -62,6 +67,23 @@ public static interface RunnableWithCoreExceptionInterruptable { void run() throws CoreException, InterruptedException; } + /** + * Executes the passed runnable and in case of {@link Exception} ignores that + * and finally executes the andFinally Runnable. + * + * @param predicate + */ + public static void executeSafelyAndFinally(RunnableWithException runnable, + Runnable andFinally) { + try { + runnable.run(); + } catch (Exception e) { + // ignore that + } finally { + andFinally.run(); + } + } + /** * Executes the passed predicate and in case of {@link CoreException} returns * false. diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java index 45586774..e5d10da3 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/OpenMutationDoubleClick.java @@ -31,7 +31,6 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.viewers.DoubleClickEvent; @@ -105,7 +104,7 @@ public IStatus runInUIThread(IProgressMonitor arg0) { .map(new OpenFileInEditorAtLine(lineNumber)) .orElse(org.eclipse.core.runtime.Status.OK_STATUS); } - + @Override public boolean belongsTo(Object family) { return JOB_FAMILY.equals(family); @@ -134,37 +133,30 @@ public OpenFileInEditorAtLine(int lineNumber) { @Override public IStatus apply(final IFile file) { IWorkbenchWindow workbench = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); - if (workbench != null) { - try { - tryToOpen(workbench, file); - } catch (CoreException e) { - return org.eclipse.core.runtime.Status.CANCEL_STATUS; - } - } - return org.eclipse.core.runtime.Status.OK_STATUS; + return PitclipseUiUtils.executeSafelyOrElse(() -> { + tryToOpen(workbench, file); + return org.eclipse.core.runtime.Status.OK_STATUS; + }, org.eclipse.core.runtime.Status.CANCEL_STATUS); } private void tryToOpen(IWorkbenchWindow workbench, final IFile file) throws CoreException { IEditorPart editorPart = openEditor(workbench.getActivePage(), file); - if (editorPart instanceof ITextEditor && lineNumber >= 0) { - ITextEditor textEditor = (ITextEditor) editorPart; - IEditorInput editorInput = textEditor.getEditorInput(); - openEditorAtLine(textEditor, editorInput); - } + ITextEditor textEditor = (ITextEditor) editorPart; + IEditorInput editorInput = textEditor.getEditorInput(); + openEditorAtLine(textEditor, editorInput); } private void openEditorAtLine(ITextEditor textEditor, IEditorInput editorInput) throws CoreException { IDocumentProvider provider = textEditor.getDocumentProvider(); provider.connect(editorInput); - try { - IDocument document = provider.getDocument(editorInput); - IRegion line = document.getLineInformation(lineNumber); - textEditor.selectAndReveal(line.getOffset(), line.getLength()); - } catch (BadLocationException e) { - // Invalid line number - perhaps file has since changed. Do nothing - } finally { - provider.disconnect(editorInput); - } + PitclipseUiUtils.executeSafelyAndFinally( + () -> { + IDocument document = provider.getDocument(editorInput); + IRegion line = document.getLineInformation(lineNumber); + textEditor.selectAndReveal(line.getOffset(), line.getLength()); + }, + () -> provider.disconnect(editorInput) + ); } } } From b4dd65d6954c612b5d9f8fea35cef9a1900daaad Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 15:06:40 +0200 Subject: [PATCH 09/10] 205: fully tested ExpandingDoubleClick Task-Url: http://github.com/pitest/pitclipse/issues/205 --- .../pitclipse/ui/tests/PitclipsePitMutationsViewTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipsePitMutationsViewTest.java b/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipsePitMutationsViewTest.java index f829f294..cda857e7 100644 --- a/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipsePitMutationsViewTest.java +++ b/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipsePitMutationsViewTest.java @@ -63,11 +63,17 @@ public void expandAndCollapse() throws CoreException { SWTBotTree mutationTreeRoot = pitMutationsView.mutationTreeRoot(); final SWTBotTreeItem firstItem = mutationTreeRoot.getAllItems()[0]; assertFalse("should be collapsed", firstItem.isExpanded()); + // with toolbar buttons bot.toolbarButtonWithTooltip (PitMutationsView.EXPAND_ALL_BUTTON_TEXT).click(); assertTrue("should be expanded", firstItem.isExpanded()); bot.toolbarButtonWithTooltip (PitMutationsView.COLLAPSE_ALL_BUTTON_TEXT).click(); assertFalse("should be collapsed", firstItem.isExpanded()); + // with double-click + firstItem.doubleClick(); + assertTrue("should be expanded", firstItem.isExpanded()); + firstItem.doubleClick(); + assertFalse("should be collapsed", firstItem.isExpanded()); } } From 62d700916e95a27e9de173b13b01e117a114f65d Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Mon, 29 Aug 2022 17:11:23 +0200 Subject: [PATCH 10/10] 136: covered PitViewFinder Task-Url: https://github.com/pitest/pitclipse/issues/136 --- .../pitclipse/ui/utils/PitclipseUiUtils.java | 29 ++++++++++++++-- .../pitclipse/ui/view/PitViewFinder.java | 33 +++++++------------ .../ui/view/mutations/PitMutationsView.java | 2 ++ .../ui/tests/PitclipseUiRunnerTest.java | 29 ++++++++++++++++ 4 files changed, 70 insertions(+), 23 deletions(-) diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java index fd2a48f1..866d7065 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/utils/PitclipseUiUtils.java @@ -31,7 +31,8 @@ import org.pitest.pitclipse.ui.view.mutations.PitMutationsView; /** - * A few utilities for the UI. + * A few utilities for the UI not meant to be subject of code coverage, + * since they deal with borderline situations that are very likely not to happen. * * @author Lorenzo Bettini * @@ -67,11 +68,35 @@ public static interface RunnableWithCoreExceptionInterruptable { void run() throws CoreException, InterruptedException; } + private static final class MissingViewException extends RuntimeException { + private static final long serialVersionUID = -2200645197888948647L; + + public MissingViewException(Exception e) { + super(e); + } + } + + /** + * Executes the passed runnable and in case of {@link Exception} throws a + * {@link RuntimeException} (a {@link MissingViewException}). + * + * @param runnable + * @return + */ + public static T executeViewSafelyOrThrow(ProviderWithCoreException provider) { + try { + return provider.get(); + } catch (Exception e) { + throw new MissingViewException(e); + } + } + /** * Executes the passed runnable and in case of {@link Exception} ignores that * and finally executes the andFinally Runnable. * - * @param predicate + * @param runnable + * @param andFinally */ public static void executeSafelyAndFinally(RunnableWithException runnable, Runnable andFinally) { diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/PitViewFinder.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/PitViewFinder.java index 1a25b3fa..4373367a 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/PitViewFinder.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/PitViewFinder.java @@ -16,19 +16,21 @@ package org.pitest.pitclipse.ui.view; +import static com.google.common.collect.Sets.newHashSet; + +import java.io.File; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; + import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IViewPart; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.pitest.pitclipse.runner.model.MutationsModel; +import org.pitest.pitclipse.ui.utils.PitclipseUiUtils; import org.pitest.pitclipse.ui.view.mutations.MutationsView; - -import java.io.File; -import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; - -import static com.google.common.collect.Sets.newHashSet; +import org.pitest.pitclipse.ui.view.mutations.PitMutationsView; /** * Singleton making easier to find Pitclipse views. @@ -36,16 +38,8 @@ public enum PitViewFinder { INSTANCE; - private static final String PIT_SUMMARY_VIEW = "org.pitest.pitclipse.ui.view.PitView"; - private static final String PIT_MUTATIONS_VIEW = "org.pitest.pitclipse.ui.view.mutations.PitMutationsView"; - - private static final class MissingViewException extends RuntimeException { - private static final long serialVersionUID = 6672829886156086528L; - - public MissingViewException(Exception e) { - super(e); - } - } + private static final String PIT_SUMMARY_VIEW = PitView.VIEW_ID; + private static final String PIT_MUTATIONS_VIEW = PitMutationsView.VIEW_ID; private static final class ViewSearch implements Runnable { private static Set initialisedViews = newHashSet(); @@ -63,11 +57,8 @@ public void run() { } private IViewPart findView(String viewId) { - try { - return tryFindView(viewId); - } catch (PartInitException e) { - throw new MissingViewException(e); - } + return PitclipseUiUtils.executeViewSafelyOrThrow( + () -> tryFindView(viewId)); } private IViewPart tryFindView(String viewId) throws PartInitException { diff --git a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java index c1b26928..ec91f68f 100644 --- a/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java +++ b/bundles/org.pitest.pitclipse.ui/src/org/pitest/pitclipse/ui/view/mutations/PitMutationsView.java @@ -30,6 +30,8 @@ public class PitMutationsView extends ViewPart implements MutationsView { + public static final String VIEW_ID = "org.pitest.pitclipse.ui.view.mutations.PitMutationsView"; + private static final int TREE_STYLE = SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL; private TreeViewer viewer; diff --git a/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipseUiRunnerTest.java b/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipseUiRunnerTest.java index a1e5a670..229f3346 100644 --- a/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipseUiRunnerTest.java +++ b/tests/org.pitest.pitclipse.ui.tests/src/org/pitest/pitclipse/ui/tests/PitclipseUiRunnerTest.java @@ -12,6 +12,8 @@ import org.junit.runner.RunWith; import org.pitest.pitclipse.ui.behaviours.pageobjects.NoTestsFoundDialog; import org.pitest.pitclipse.ui.behaviours.pageobjects.TestConfigurationSelectorDialog; +import org.pitest.pitclipse.ui.view.PitView; +import org.pitest.pitclipse.ui.view.mutations.PitMutationsView; /** * @author Lorenzo Bettini @@ -301,4 +303,31 @@ public void runWithRunToolbarButtonFromEditor() throws CoreException { mutationsAre(Collections.emptyList()); noCoverageReportGenerated(); } + + @Test + public void withViewsClosed() throws CoreException, InterruptedException { + try { + closeViewById(PitView.VIEW_ID); + closeViewById(PitMutationsView.VIEW_ID); + + removeMethods(FOO_CLASS, FOO_BAR_PACKAGE, TEST_PROJECT); + removeMethods(FOO_TEST_CLASS, FOO_BAR_PACKAGE, TEST_PROJECT); + runTest(FOO_TEST_CLASS, FOO_BAR_PACKAGE, TEST_PROJECT); + + // to make sure our views are not opened by our code (see below) + // we run the PIT tests twice + closeViewById(PitView.VIEW_ID); + closeViewById(PitMutationsView.VIEW_ID); + runTest(FOO_TEST_CLASS, FOO_BAR_PACKAGE, TEST_PROJECT); + + // we just verify that nothing bad happens, but we cannot + // assert mutations because depending on the order of the tests + // our views are still closed + // see org.pitest.pitclipse.ui.view.PitViewFinder.ViewSearch.activateViewOnceAndOnceOnly(String) + } finally { + // of course, for the other tests the views must be open + openViewById(PitView.VIEW_ID); + openViewById(PitMutationsView.VIEW_ID); + } + } }