-
Notifications
You must be signed in to change notification settings - Fork 99
JENKINS-31843 - Merge build parameter values when "Build is parameterized" #34
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
base: master
Are you sure you want to change the base?
Changes from all commits
eb08675
cc9b1da
af88dd9
87f1de3
ce52b5d
753ef9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,10 @@ | |
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static com.sonyericsson.rebuild.matchers.StringParameterValuesMatcher.hasStringParamValues; | ||
import static org.hamcrest.core.IsNot.not; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* For testing the extension point. | ||
* | ||
|
@@ -393,6 +397,110 @@ public void testRebuildSupportedUnknownParameterValue() throws Exception { | |
page.asText().contains("This is a mark for test")); | ||
} | ||
|
||
public void testNewParametersShouldOverrideExistingParametersIfHaveSameName() | ||
throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to rename this test, I think there's something wrong with the sentence itself and it is not really clear what's the goal of the test, probably something like "newParametersShouldOverrideExistingPatametersIfHaveSameName" or something like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed |
||
FreeStyleProject project = createFreeStyleProject(); | ||
project.addProperty(new ParametersDefinitionProperty( | ||
new StringParameterDefinition("existing", "defaultValue"))); | ||
|
||
// Build (#1) | ||
project.scheduleBuild2(0, new Cause.UserIdCause(), | ||
new ParametersAction(new StringParameterValue("existing", "overridingValue"))) | ||
.get(); | ||
HtmlPage rebuildConfigPage = createWebClient().getPage(project, | ||
"1/rebuild"); | ||
// Rebuild (#2) | ||
submit(rebuildConfigPage.getFormByName("config")); | ||
|
||
List<ParameterValue> parameterValues = project.getBuildByNumber(2).getAction(ParametersAction.class).getParameters(); | ||
|
||
assertThat(parameterValues, hasStringParamValues(new StringParameterValue("existing", "overridingValue"))); | ||
assertThat(parameterValues, not(hasStringParamValues(new StringParameterValue("existing", "defaultValue")))); | ||
} | ||
|
||
public void testRebuildingLastBuildShouldMaintainExistingParameters() | ||
throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, something like "rebuildingLastBuildShouldMaintainExistingParameters" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed |
||
FreeStyleProject project = createFreeStyleProject(); | ||
project.addProperty(new ParametersDefinitionProperty( | ||
new StringParameterDefinition("existing", "defaultValue"))); | ||
|
||
// Build (#1) | ||
project.scheduleBuild2(0, new Cause.UserIdCause(), | ||
new ParametersAction(new StringParameterValue("existing", "overridingValue"))) | ||
.get(); | ||
HtmlPage rebuildConfigPage = createWebClient().getPage(project, | ||
"1/rebuild"); | ||
// Rebuild (#2) | ||
submit(rebuildConfigPage.getFormByName("config")); | ||
|
||
HtmlPage projectPage = createWebClient().getPage(project); | ||
WebAssert.assertLinkPresentWithText(projectPage, "Rebuild Last"); | ||
|
||
HtmlAnchor rebuildHref = projectPage.getAnchorByText("Rebuild Last"); | ||
assertEquals("Rebuild Last should point to the second build", "/" | ||
+ project.getUrl() + "lastCompletedBuild/rebuild", | ||
rebuildHref.getHrefAttribute()); | ||
|
||
List<ParameterValue> parameterValues = project.getLastCompletedBuild().getAction(ParametersAction.class).getParameters(); | ||
|
||
assertThat(parameterValues, hasStringParamValues(new StringParameterValue("existing", "overridingValue"))); | ||
assertThat(parameterValues, not(hasStringParamValues(new StringParameterValue("existing", "defaultValue")))); | ||
} | ||
|
||
public void testInjectedParametersShouldOverrideExistingParametersIfHaveSameName() | ||
throws Exception { | ||
FreeStyleProject project = createFreeStyleProject(); | ||
project.addProperty(new ParametersDefinitionProperty( | ||
new StringParameterDefinition("oldName", "oldValue"))); | ||
|
||
// Build (#1) | ||
project.scheduleBuild2(0, new Cause.UserIdCause(), | ||
new ParametersAction(new StringParameterValue("injectedName", "injectedValue"))) | ||
.get(); | ||
HtmlPage rebuildConfigPage = createWebClient().getPage(project, | ||
"1/rebuild"); | ||
// Rebuild (#2) | ||
submit(rebuildConfigPage.getFormByName("config")); | ||
|
||
List<ParameterValue> parameterValues = project.getBuildByNumber(2).getAction(ParametersAction.class).getParameters(); | ||
|
||
assertThat(parameterValues, hasStringParamValues( | ||
new StringParameterValue("oldName", "oldValue"), | ||
new StringParameterValue("injectedName", "injectedValue") | ||
)); | ||
} | ||
|
||
public void testRebuildingLastBuildShouldMaintainInjectedParameters() | ||
throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed |
||
FreeStyleProject project = createFreeStyleProject(); | ||
project.addProperty(new ParametersDefinitionProperty( | ||
new StringParameterDefinition("oldName", "oldValue"))); | ||
|
||
// Build (#1) | ||
project.scheduleBuild2(0, new Cause.UserIdCause(), | ||
new ParametersAction(new StringParameterValue("injectedName", "injectedValue"))) | ||
.get(); | ||
HtmlPage rebuildConfigPage = createWebClient().getPage(project, | ||
"1/rebuild"); | ||
// Rebuild (#2) | ||
submit(rebuildConfigPage.getFormByName("config")); | ||
|
||
HtmlPage projectPage = createWebClient().getPage(project); | ||
WebAssert.assertLinkPresentWithText(projectPage, "Rebuild Last"); | ||
|
||
HtmlAnchor rebuildHref = projectPage.getAnchorByText("Rebuild Last"); | ||
assertEquals("Rebuild Last should point to the second build", "/" | ||
+ project.getUrl() + "lastCompletedBuild/rebuild", | ||
rebuildHref.getHrefAttribute()); | ||
|
||
List<ParameterValue> parameterValues = project.getLastCompletedBuild().getAction(ParametersAction.class).getParameters(); | ||
|
||
assertThat(parameterValues, hasStringParamValues( | ||
new StringParameterValue("oldName", "oldValue"), | ||
new StringParameterValue("injectedName", "injectedValue") | ||
)); | ||
} | ||
|
||
/** | ||
* A parameter value rebuild plugin does not know. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.sonyericsson.rebuild.matchers; | ||
|
||
import hudson.model.ParameterValue; | ||
import hudson.model.StringParameterValue; | ||
import org.hamcrest.Description; | ||
import org.junit.internal.matchers.TypeSafeMatcher; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class StringParameterValuesMatcher extends TypeSafeMatcher<Collection<ParameterValue>> { | ||
|
||
private final List<StringParameterValue> thisParameters; | ||
|
||
public static StringParameterValuesMatcher hasStringParamValues(StringParameterValue... parameters) { | ||
return new StringParameterValuesMatcher(parameters); | ||
} | ||
|
||
StringParameterValuesMatcher(StringParameterValue... parameters) { | ||
this.thisParameters = new ArrayList<StringParameterValue>(parameters.length); | ||
for (StringParameterValue parameter : parameters) { | ||
thisParameters.add(parameter); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean matchesSafely(Collection<ParameterValue> parameters) { | ||
Map<StringParameterValue, Boolean> resultMap = new HashMap<StringParameterValue, Boolean>(); | ||
for (StringParameterValue stringParameterValue : thisParameters) { | ||
resultMap.put(stringParameterValue, false); | ||
for (ParameterValue parameter : parameters) { | ||
if (matchesInternal(stringParameterValue, parameter)) { | ||
resultMap.put(stringParameterValue, true); | ||
break; | ||
} | ||
} | ||
} | ||
return !resultMap.values().contains(false); | ||
} | ||
|
||
private boolean matchesInternal(StringParameterValue thisParameter, ParameterValue parameter) { | ||
return parameter instanceof StringParameterValue | ||
&& thisParameter.getName().equals((parameter).getName()) | ||
&& thisParameter.value.equals(((StringParameterValue)parameter).value); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendValueList("<[", ", ", "]>", thisParameters); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you have some formatting changes