Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-38306] Add a parameter to LocalData to use instead of method name #36

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java
Original file line number Diff line number Diff line change
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be replaced with SourceVersion.latestSupported().isIdentifier(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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we care whether it is a Java identifier or not? Should not matter for purposes of resource loading. Anyway, if it is unusable for some reason, we should fail with an error.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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());
}

}
Original file line number Diff line number Diff line change
@@ -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>