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

Improved rebuild parameter page #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 29 additions & 9 deletions src/main/java/com/sonyericsson/rebuild/RebuildAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,38 @@ public RebuildParameterPage getRebuildParameterPage(ParameterValue value) {
}
}

// Check if we have a branched Jelly in the plugin.
if (getClass().getResource(String.format("/%s/%s.jelly", getClass().getCanonicalName().replace('.', '/'), value.getClass().getSimpleName())) != null) {
// No provider available, use an existing view provided by rebuild plugin.
return findRebuildParameterPage(value.getClass());
}

/**
* Recursively searches for a corresponding Jelly resource file (e.g., *.jelly) in the current
* plugin for the specified class or its superclasses.
*
* @param valueClass the class of the parameter value for which to search a corresponding Jelly file.
* This can be the class itself or any of its superclasses.
* @return {@code null} if the Jelly resource for the parameter value class,
* including all its superclasses, cannot be found, it will return null, which may trigger a Jelly fallback.
* <pre>
* fallback: {@link ParameterDefinition#copyWithDefaultValue(ParameterValue)}
* </pre>
*/
private RebuildParameterPage findRebuildParameterPage(Class<?> valueClass) {
if (valueClass == null) {
return null;
}

String resourcePath = String.format("/%s/%s.jelly",
getClass().getCanonicalName().replace('.', '/'),
valueClass.getSimpleName());

if (getClass().getResource(resourcePath) != null) {
return new RebuildParameterPage(
getClass(),
String.format("%s.jelly", value.getClass().getSimpleName())
);

String.format("%s.jelly", valueClass.getSimpleName())
);
}
// Else we return that we haven't found anything.
// So Jelly fallback could occur.
return null;

return findRebuildParameterPage(valueClass.getSuperclass());
}

@Override
Expand Down
85 changes: 85 additions & 0 deletions src/test/java/com/sonyericsson/rebuild/RebuildActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.sonyericsson.rebuild;

import hudson.model.BooleanParameterValue;
import hudson.model.Build;
import hudson.model.Cause;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.Project;
import hudson.model.StringParameterValue;
import hudson.model.TextParameterValue;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

import static org.junit.Assert.assertEquals;

@RunWith(Enclosed.class)
public class RebuildActionTest {

@RunWith(Parameterized.class)
public static class RebuildActionRebuildParameterPageTest {
@Rule
public JenkinsRule j = new JenkinsRule();
private Project projectA;
private Build buildA;
private RebuildAction rebuildAction;

private ParameterValue parameterValue;
private String expectedJellyResourceName;

public RebuildActionRebuildParameterPageTest(ParameterValue parameterValue, String expectedJellyResourceName) {
this.parameterValue = parameterValue;
this.expectedJellyResourceName = expectedJellyResourceName;
}

@Before
public void setUp() throws IOException, ExecutionException, InterruptedException {
projectA = j.createFreeStyleProject("testFreeStyleA");
buildA = (Build) projectA.scheduleBuild2(
0,
new Cause.UserIdCause(),
new ParametersAction()).get();
rebuildAction = buildA.getAction(RebuildAction.class);
}

@Test
public void getRebuildParameterPage() {
RebuildParameterPage parameterPage = rebuildAction.getRebuildParameterPage(parameterValue);
assertEquals(expectedJellyResourceName, Optional.ofNullable(parameterPage).map(RebuildParameterPage::getPage).orElse(null));
}

@Parameterized.Parameters
public static Collection<Object[]> getTestParameters() {
return Arrays.asList(new Object[][]{
{new StringParameterValue("stringParam", "stringParam"), "StringParameterValue.jelly"},
{new BooleanParameterValue("booleanParam", true), "BooleanParameterValue.jelly"},
{new TextParameterValue("textParam", "textParam"), "TextParameterValue.jelly"},
{new NotExistsJellyParameterValue("notExistsJellyParam"), null},
{new ExistsJellySubParameterValue("existsJellySubParam", "existsJellySubParam"), "StringParameterValue.jelly"}
});
}

private static class NotExistsJellyParameterValue extends ParameterValue {
public NotExistsJellyParameterValue(String name) {
super(name);
}
}

private static class ExistsJellySubParameterValue extends StringParameterValue {
public ExistsJellySubParameterValue(String name, String value) {
super(name, value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package com.sonyericsson.rebuild;

import hudson.model.SimpleParameterDefinition;
import net.sf.json.JSONObject;
import org.htmlunit.HttpMethod;
import org.htmlunit.WebAssert;
import org.htmlunit.WebRequest;
Expand Down Expand Up @@ -61,6 +63,7 @@
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import java.io.IOException;
import java.util.HashSet;
Expand All @@ -77,7 +80,7 @@ public class RebuildValidatorTest {
* Sleep delay value.
*/
public static final int DELAY = 100;

@Rule
public JenkinsRule j = new JenkinsRule();

Expand Down Expand Up @@ -582,16 +585,22 @@ public void testRebuildDispatcherExtensionActionIsNotCopied() throws Exception {
* A parameter value rebuild plugin does not know.
*/
public static class UnsupportedUnknownParameterValue extends
StringParameterValue {
ParameterValue {
private static final long serialVersionUID = 3182218854913929L;
private String value;

public UnsupportedUnknownParameterValue(String name, String value) {
super(name, value);
}

@Override
public String getValue() {
return value;
}
}

public static class UnsupportedUnknownParameterDefinition extends
StringParameterDefinition {
SimpleParameterDefinition {
private static final long serialVersionUID = 1014662680565914672L;

@DataBoundConstructor
Expand All @@ -606,9 +615,9 @@ public ParameterValue createValue(String value) {
}

@Override
public StringParameterValue getDefaultParameterValue() {
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
return new UnsupportedUnknownParameterValue(this.getName(),
this.getDefaultValue());
jo.getString("value"));
}

@Extension
Expand Down