mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-16 05:22:22 +00:00
Make @TestExtension work with JenkinsRule.
This commit is contained in:
1 parent
769692cd80
commit
5dd905f2a2
3 files changed
+46
-17
No files matched your search
@@ -121,8 +121,6 @@ import net.sourceforge.htmlunit.corejs.javascript.ContextFactory;
|
||||
import org.acegisecurity.AuthenticationException;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.context.SecurityContext;
|
||||
import org.acegisecurity.context.SecurityContextHolder;
|
||||
import org.acegisecurity.userdetails.UserDetails;
|
||||
import org.acegisecurity.userdetails.UsernameNotFoundException;
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
@@ -133,7 +131,6 @@ import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
|
||||
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.rules.MethodRule;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
@@ -205,10 +202,8 @@ import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.hasXPath;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.isA;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -224,7 +219,7 @@ import static org.junit.matchers.JUnitMatchers.containsString;
|
||||
*/
|
||||
public class JenkinsRule implements TestRule, MethodRule, RootAction {
|
||||
|
||||
private final TestEnvironment env = new TestEnvironment(null);
|
||||
private TestEnvironment env;
|
||||
|
||||
private Description testDescription;
|
||||
|
||||
@@ -315,6 +310,7 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction {
|
||||
* @throws Throwable if setup fails (which will disable {@code after}
|
||||
*/
|
||||
protected void before() throws Throwable {
|
||||
env = new TestEnvironment(testDescription);
|
||||
env.pin();
|
||||
recipe();
|
||||
AbstractProject.WORKSPACE.toString();
|
||||
@@ -439,6 +435,12 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction {
|
||||
ExtensionList.clearLegacyInstances();
|
||||
DescriptorExtensionList.clearLegacyInstances();
|
||||
|
||||
try {
|
||||
env.dispose();
|
||||
} catch (Exception x) {
|
||||
x.printStackTrace();
|
||||
}
|
||||
|
||||
// Hudson creates ClassLoaders for plugins that hold on to file descriptors of its jar files,
|
||||
// but because there's no explicit dispose method on ClassLoader, they won't get GC-ed until
|
||||
// at some later point, leading to possible file descriptor overflow. So encourage GC now.
|
||||
|
||||
@@ -26,6 +26,9 @@ package org.jvnet.hudson.test;
|
||||
import hudson.model.Computer;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.annotation.CheckForNull;
|
||||
import javax.annotation.Nonnull;
|
||||
import org.junit.runner.Description;
|
||||
|
||||
/**
|
||||
* TODO: deprecate this, and just consolidate this to {@link HudsonTestCase}.
|
||||
@@ -35,14 +38,35 @@ import java.io.IOException;
|
||||
*/
|
||||
public class TestEnvironment {
|
||||
/**
|
||||
* Current test case being run.
|
||||
* Current test case being run (null for a JUnit 4 test).
|
||||
*/
|
||||
public final HudsonTestCase testCase;
|
||||
public final @CheckForNull HudsonTestCase testCase;
|
||||
|
||||
private final @CheckForNull Description description;
|
||||
|
||||
public final TemporaryDirectoryAllocator temporaryDirectoryAllocator = new TemporaryDirectoryAllocator();
|
||||
|
||||
public TestEnvironment(HudsonTestCase testCase) {
|
||||
public TestEnvironment(@Nonnull HudsonTestCase testCase) {
|
||||
this.testCase = testCase;
|
||||
this.description = null;
|
||||
}
|
||||
|
||||
public TestEnvironment(@Nonnull Description description) {
|
||||
this.testCase = null;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Current test case being run (works for JUnit 3 or 4).
|
||||
*/
|
||||
public @Nonnull Description description() {
|
||||
if (description != null) {
|
||||
return description;
|
||||
} else {
|
||||
assert testCase != null;
|
||||
// Initialize lazily; testCase.getName() may be null in the TestEnvironment constructor.
|
||||
return Description.createTestDescription(testCase.getClass(), testCase.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public void pin() {
|
||||
|
||||
@@ -29,6 +29,7 @@ import hudson.ExtensionFinder.GuiceExtensionAnnotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import org.junit.runner.Description;
|
||||
|
||||
/**
|
||||
* Loads {@link TestExtension}s.
|
||||
@@ -54,32 +55,34 @@ public class TestExtensionLoader extends GuiceExtensionAnnotation<TestExtension>
|
||||
@Override
|
||||
protected boolean isActive(AnnotatedElement e) {
|
||||
TestEnvironment env = TestEnvironment.get();
|
||||
if (env == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TestExtension a = e.getAnnotation(TestExtension.class);
|
||||
if (a==null) return false; // stale index
|
||||
String testName = a.value();
|
||||
if (testName.length()>0 && env!=null && env.testCase!=null && !env.testCase.getName().equals(testName))
|
||||
Description description = env.description();
|
||||
if (testName.length()>0 && !testName.equals(description.getMethodName()))
|
||||
return false; // doesn't apply to this test
|
||||
|
||||
if (e instanceof Class) {
|
||||
return isActive(env, (Class)e);
|
||||
return isActive(description, (Class)e);
|
||||
}
|
||||
if (e instanceof Field) {
|
||||
Field f = (Field) e;
|
||||
return f.getDeclaringClass().isInstance(env.testCase);
|
||||
return f.getDeclaringClass() == description.getTestClass();
|
||||
}
|
||||
if (e instanceof Method) {
|
||||
Method m = (Method) e;
|
||||
return m.getDeclaringClass().isInstance(env.testCase);
|
||||
return m.getDeclaringClass() == description.getTestClass();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isActive(TestEnvironment env, Class<?> extType) {
|
||||
if (env == null || env.testCase == null)
|
||||
return false;
|
||||
private boolean isActive(Description description, Class<?> extType) {
|
||||
for (Class<?> outer = extType; outer!=null; outer=outer.getEnclosingClass())
|
||||
if (outer.isInstance(env.testCase))
|
||||
if (outer == description.getTestClass())
|
||||
return true; // enclosed
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user