Skip to content

Commit

Permalink
[FIXED JENKINS-38306] Add a parameter to LocalData and use that inste…
Browse files Browse the repository at this point in the history
…ad of method name
  • Loading branch information
ikedam committed Sep 17, 2016
1 parent 2407c77 commit 6cd9513
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
29 changes: 27 additions & 2 deletions src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java
Expand Up @@ -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>.
*
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/jvnet/hudson/test/recipes/LocalData.java
Expand Up @@ -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
Expand All @@ -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()));
}
}
}
12 changes: 12 additions & 0 deletions src/test/java/org/jvnet/hudson/test/recipes/LocalDataTest.java
Expand Up @@ -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.