mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-20 16:24:11 +01:00
added conditional ignore for JUnit tests
Former-commit-id: ed8a216bc31a7ac8f69b08d34a0ffc356f1cd912
This commit is contained in:
@@ -0,0 +1,79 @@
|
|||||||
|
/**
|
||||||
|
* *****************************************************************************
|
||||||
|
* Copyright (c) 2013 Rüdiger Herrmann 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: Rüdiger Herrmann - initial API and implementation
|
||||||
|
*****************************************************************************
|
||||||
|
*/
|
||||||
|
package com.codeaffine.junit.ignore;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
import org.junit.Assume;
|
||||||
|
import org.junit.rules.MethodRule;
|
||||||
|
import org.junit.runners.model.FrameworkMethod;
|
||||||
|
import org.junit.runners.model.Statement;
|
||||||
|
|
||||||
|
public class ConditionalIgnoreRule implements MethodRule {
|
||||||
|
|
||||||
|
public interface IgnoreCondition {
|
||||||
|
|
||||||
|
boolean isSatisfied();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.METHOD})
|
||||||
|
public @interface ConditionalIgnore {
|
||||||
|
|
||||||
|
Class<? extends IgnoreCondition> condition();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Statement apply(Statement base, FrameworkMethod method, Object target) {
|
||||||
|
Statement result = base;
|
||||||
|
if (hasConditionalIgnoreAnnotation(method)) {
|
||||||
|
IgnoreCondition condition = getIgnoreContition(method);
|
||||||
|
if (condition.isSatisfied()) {
|
||||||
|
result = new IgnoreStatement(condition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasConditionalIgnoreAnnotation(FrameworkMethod method) {
|
||||||
|
return method.getAnnotation(ConditionalIgnore.class) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IgnoreCondition getIgnoreContition(FrameworkMethod method) {
|
||||||
|
ConditionalIgnore annotation = method.getAnnotation(ConditionalIgnore.class);
|
||||||
|
return newCondition(annotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IgnoreCondition newCondition(ConditionalIgnore annotation) {
|
||||||
|
try {
|
||||||
|
return annotation.condition().newInstance();
|
||||||
|
} catch (RuntimeException re) {
|
||||||
|
throw re;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class IgnoreStatement extends Statement {
|
||||||
|
|
||||||
|
private IgnoreCondition condition;
|
||||||
|
|
||||||
|
IgnoreStatement(IgnoreCondition condition) {
|
||||||
|
this.condition = condition;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void evaluate() {
|
||||||
|
Assume.assumeTrue("Ignored by " + condition.getClass().getSimpleName(), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* *****************************************************************************
|
||||||
|
* Copyright (c) 2013 Rüdiger Herrmann 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: Rüdiger Herrmann - initial API and implementation
|
||||||
|
* ****************************************************************************
|
||||||
|
*/
|
||||||
|
package com.codeaffine.junit.ignore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The following NotRunningOnWindows class was taken from blog by Rüdiger Herrmann titled <a
|
||||||
|
* href="http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/">
|
||||||
|
* A JUnit Rule to Conditionally Ignore Tests</a>.
|
||||||
|
*
|
||||||
|
* @author Rüdiger Herrmann <rherrmann@codeaffine.com>
|
||||||
|
*/
|
||||||
|
public class NotRunningOnWindows implements ConditionalIgnoreRule.IgnoreCondition {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSatisfied() {
|
||||||
|
return !System.getProperty("os.name").startsWith("Windows");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user