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