Skip to content

Commit

Permalink
136: covered PitViewFinder
Browse files Browse the repository at this point in the history
Task-Url: #136
  • Loading branch information
LorenzoBettini committed Aug 29, 2022
1 parent b4dd65d commit 62d7009
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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> T executeViewSafelyOrThrow(ProviderWithCoreException<T> 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 <T extends Exception > void executeSafelyAndFinally(RunnableWithException<T> runnable,
Runnable andFinally) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,30 @@

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.
*/
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<String> initialisedViews = newHashSet();
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 62d7009

Please sign in to comment.