mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-01-13 23:33:37 +01:00
Changed from using the ConditionalIgnoreRule to using junit's core assumeFalse
Former-commit-id: fa9e77a19adeda13aa30c48c3ffa903ec50ed762
This commit is contained in:
@@ -1,79 +0,0 @@
|
||||
/**
|
||||
* *****************************************************************************
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/**
|
||||
* *****************************************************************************
|
||||
* 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");
|
||||
}
|
||||
}
|
||||
@@ -17,16 +17,12 @@
|
||||
*/
|
||||
package org.owasp.dependencycheck.analyzer;
|
||||
|
||||
import com.codeaffine.junit.ignore.ConditionalIgnoreRule;
|
||||
import com.codeaffine.junit.ignore.ConditionalIgnoreRule.ConditionalIgnore;
|
||||
import com.codeaffine.junit.ignore.NotRunningOnWindows;
|
||||
import java.io.File;
|
||||
import org.junit.After;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assume.assumeFalse;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.owasp.dependencycheck.dependency.Confidence;
|
||||
import org.owasp.dependencycheck.dependency.Dependency;
|
||||
@@ -88,12 +84,12 @@ public class AssemblyAnalyzerTest {
|
||||
analyzer.analyze(d, null);
|
||||
}
|
||||
|
||||
@Rule
|
||||
public ConditionalIgnoreRule rule = new ConditionalIgnoreRule();
|
||||
|
||||
@Test()
|
||||
@ConditionalIgnore(condition = NotRunningOnWindows.class)
|
||||
@Test(expected = AnalysisException.class)
|
||||
public void testWithSettingMono() throws Exception {
|
||||
|
||||
//This test doesn't work on Windows.
|
||||
assumeFalse(System.getProperty("os.name").startsWith("Windows"));
|
||||
|
||||
String oldValue = Settings.getString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH);
|
||||
// if oldValue is null, that means that neither the system property nor the setting has
|
||||
// been set. If that's the case, then we have to make it such that when we recover,
|
||||
@@ -105,14 +101,10 @@ public class AssemblyAnalyzerTest {
|
||||
Settings.setString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH, "/yooser/bine/mono");
|
||||
}
|
||||
|
||||
//** @Test(expected = AnalysisException) doesn't seem to work with the conditional test **//
|
||||
AnalysisException aex = null;
|
||||
try {
|
||||
// Have to make a NEW analyzer because during setUp, it would have gotten the correct one
|
||||
AssemblyAnalyzer aanalyzer = new AssemblyAnalyzer();
|
||||
aanalyzer.initialize();
|
||||
} catch (AnalysisException ex) {
|
||||
aex = ex;
|
||||
} finally {
|
||||
// Now recover the way we came in. If we had to set a System property, delete it. Otherwise,
|
||||
// reset the old value
|
||||
@@ -122,7 +114,6 @@ public class AssemblyAnalyzerTest {
|
||||
Settings.setString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH, oldValue);
|
||||
}
|
||||
}
|
||||
assertNull("Excpted exception: org.owasp.dependencycheck.analyzer.AnalysisException", aex);
|
||||
}
|
||||
|
||||
@After
|
||||
|
||||
Reference in New Issue
Block a user