Skip to content

Commit

Permalink
Merge pull request #207 from pitest/task_205-Uncovered_UI_code
Browse files Browse the repository at this point in the history
Code coverage of pitclipse.ui
  • Loading branch information
LorenzoBettini authored Aug 29, 2022
2 parents d4a663e + 62d7009 commit a75a63e
Show file tree
Hide file tree
Showing 24 changed files with 419 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,15 +41,15 @@ static <T> Optional<T> forEditorInputDo(IEditorInput i, Function<ITypeRoot, Opti

static Function<ITypeRoot, Optional<IResource>> getCorrespondingResource() {
return t ->
Optional.ofNullable(PitclipseLaunchUiUtils.executeSafelyOrElse(
Optional.ofNullable(PitclipseUiUtils.executeSafelyOrElse(
t::getCorrespondingResource, null));
}

static Optional<IJavaElement> 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<ILaunchConfiguration> emptyLaunchConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IJavaElement> selected = asJavaElement(elements[0]);
Optional<IJavaElement> launchElement =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -349,7 +349,7 @@ private Function<IJavaElement, List<ILaunchConfiguration>> findLaunchConfigurati
Optional<IJavaElement> launchElement = getLaunchElementFor(element);
return launchElement
.map(javaElement ->
PitclipseLaunchUiUtils.executeSafelyOrElse(
PitclipseUiUtils.executeSafelyOrElse(
() -> findExistingLaunchConfigurations
(createLaunchConfiguration(javaElement)),
emptyLaunchConfiguration()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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> {
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.
Expand All @@ -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> T executeSafelyOrElse(ProviderWithCoreException<T> 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 <T>
* @param o
* @param type
* @return
*/
public static <T extends IAdaptable> T tryToAdapt(Object o, Class<T> type) {
if (o instanceof IAdaptable) {
return ((IAdaptable) o).getAdapter(type);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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 <T>
*/
public interface MutationsModelVisitorAdapter<T> extends MutationsModelVisitor<T> {

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,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 All @@ -40,6 +43,131 @@ private PitclipseUiUtils() {
// Only static utility methods
}

@FunctionalInterface
public static interface RunnableWithException<T extends Exception> {
void run() throws T;
}

@FunctionalInterface
public static interface RunnableWithCoreException {
void run() throws CoreException;
}

@FunctionalInterface
public static interface ProviderWithCoreException<T> {
T get() throws CoreException;
}

@FunctionalInterface
public static interface PredicateWithCoreException {
boolean test() throws CoreException;
}

@FunctionalInterface
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 runnable
* @param andFinally
*/
public static <T extends Exception > void executeSafelyAndFinally(RunnableWithException<T> 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.
*
* @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> T executeSafelyOrElse(ProviderWithCoreException<T> 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 <T>
* @param o
* @param type
* @return
*/
public static <T extends IAdaptable> T tryToAdapt(Object o, Class<T> 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.
Expand Down
Loading

0 comments on commit a75a63e

Please sign in to comment.