Skip to content
Permalink
Browse files

[FIXED JENKINS-38306] Add a parameter to LocalData and use that inste…

…ad of method name
  • Loading branch information...
ikedam committed Sep 17, 2016
1 parent 2407c77 commit 6cd95132487a1188f1adcffb8aaea6990e75c5c6
@@ -25,13 +25,16 @@

import hudson.FilePath;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;

import javax.annotation.CheckForNull;

/**
* Controls how a {@link HudsonTestCase} initializes <tt>JENKINS_HOME</tt>.
*
@@ -106,9 +109,11 @@ public File allocate() throws Exception {
*/
final class Local implements HudsonHomeLoader {
private final Method testMethod;
private final String alterName;

public Local(Method testMethod) {
public Local(Method testMethod, String alterName) {
this.testMethod = testMethod;
this.alterName = alterName;
}

public File allocate() throws Exception {
@@ -121,11 +126,31 @@ public File allocate() throws Exception {
return new CopyExisting(home).allocate();
}

public static boolean isJavaIdentifier(@CheckForNull String name) {
if (StringUtils.isEmpty(name)) {
return false;
}
if (!Character.isJavaIdentifierStart(name.charAt(0))) {
return false;
}
for (int pos = 1; pos < name.length(); ++pos) {
if (!Character.isJavaIdentifierPart(name.charAt(pos))) {
return false;
}
}
return true;
}

private URL findDataResource() {
// first, check method specific resource
Class<?> clazz = testMethod.getDeclaringClass();
String methodName = testMethod.getName();

if (isJavaIdentifier(alterName)) {
methodName = alterName;
}

for( String middle : new String[]{ '/'+testMethod.getName(), "" }) {
for( String middle : new String[]{ '/'+methodName, "" }) {
for( String suffix : SUFFIXES ) {
URL res = clazz.getResource(clazz.getSimpleName() + middle+suffix);
if(res!=null) return res;
@@ -63,6 +63,11 @@
* In <tt>org/acme/FooTest.zip</tt> as a zip file.
* </ol>
*
* You can specify a value to use instead of the method name
* with the parameter of annotation. Should be a valid java identifier.
* E.g. <tt>@LocalData("commonData")</tt> results using
* <tt>org/acme/FooTest/commonData(.zip)</tt>.
*
* <p>
* Search is performed in this specific order. The fall back mechanism allows you to write
* one test class that interacts with different aspects of the same data set, by associating
@@ -79,15 +84,17 @@
@Target(METHOD)
@Retention(RUNTIME)
public @interface LocalData {
String value() default "";

class RunnerImpl extends Recipe.Runner<LocalData> {
public void setup(HudsonTestCase testCase, LocalData recipe) throws Exception {
testCase.with(new Local(testCase.getClass().getMethod(testCase.getName())));
testCase.with(new Local(testCase.getClass().getMethod(testCase.getName()), recipe.value()));
}
}
class RuleRunnerImpl extends JenkinsRecipe.Runner<LocalData> {
public void setup(JenkinsRule jenkinsRule, LocalData recipe) throws Exception {
Description desc = jenkinsRule.getTestDescription();
jenkinsRule.with(new Local(desc.getTestClass().getMethod(desc.getMethodName())));
jenkinsRule.with(new Local(desc.getTestClass().getMethod(desc.getMethodName()), recipe.value()));
}
}
}
@@ -43,4 +43,16 @@ public void works() throws Exception {
assertNotNull(r.jenkins.getItem("somejob"));
}

@LocalData
@Test
public void methodData() throws Exception {
assertEquals("This is Jenkins in LocalDataTest#methodData", r.jenkins.getSystemMessage());
}

@LocalData("methodData")
@Test
public void otherData() throws Exception {
assertEquals("This is Jenkins in LocalDataTest#methodData", r.jenkins.getSystemMessage());
}

}
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<hudson>
<version>1.580.1</version>
<systemMessage>This is Jenkins in LocalDataTest#methodData</systemMessage>
</hudson>

0 comments on commit 6cd9513

Please sign in to comment.
You can’t perform that action at this time.