Skip to content

Commit

Permalink
JENKINS-25226] - A direct unit test for the issue
Browse files Browse the repository at this point in the history
The test checks that the variables substitution works properly for build parameters.
The change also refactors the provisioning of PerforceSCM stubs.

Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information
oleg-nenashev committed Oct 30, 2014
1 parent 816a741 commit 7fdf8d0
Showing 1 changed file with 63 additions and 13 deletions.
76 changes: 63 additions & 13 deletions src/test/java/hudson/plugins/perforce/PerforceSCMTest.java
@@ -1,19 +1,32 @@
package hudson.plugins.perforce;

import hudson.model.FreeStyleBuild;
import hudson.plugins.perforce.config.DepotType;
import hudson.model.FreeStyleProject;
import hudson.model.Hudson;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.plugins.perforce.PerforceToolInstallation.DescriptorImpl;
import hudson.plugins.perforce.browsers.P4Web;
import hudson.plugins.perforce.config.CleanTypeConfig;
import hudson.plugins.perforce.config.MaskViewConfig;
import hudson.plugins.perforce.config.WorkspaceCleanupConfig;
import hudson.plugins.perforce.utils.MacroStringHelper;
import hudson.tools.ToolProperty;
import java.net.MalformedURLException;

import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Future;
import javax.annotation.Nonnull;
import static junit.framework.Assert.assertNotNull;
import org.jvnet.hudson.test.Bug;

import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.recipes.LocalData;
Expand All @@ -35,11 +48,7 @@ public class PerforceSCMTest extends HudsonTestCase {
*/
public void testConfigRoundtrip() throws Exception {
FreeStyleProject project = createFreeStyleProject();
P4Web browser = new P4Web(new URL("http://localhost/"));
PerforceSCM scm = new PerforceSCM(
"user", "pass", "client", "port", "", "exe", "sysRoot",
"sysDrive", "label", "counter", "upstreamProject", "shared", "charset", "charset2", "user", false, true, true, true, true, true, false,
false, true, false, false, false, "${basename}", 0, -1, browser, "exclude_user", "exclude_file", true, TEST_DEPOT, TEST_WORKSPACE_CLEANUP, TEST_MASKVIEW);
PerforceSCM scm = createPerforceSCMStub();
scm.setProjectPath("path");
project.setScm(scm);

Expand All @@ -58,9 +67,9 @@ public void testConfigRoundtripWithNoSystemRoot() throws Exception {
FreeStyleProject project = createFreeStyleProject();
P4Web browser = new P4Web(new URL("http://localhost/"));
PerforceSCM scm = new PerforceSCM(
"user", "pass", "client", "port", "", "exe", "",
"", "label", "counter", "upstreamProject", "shared", "charset", "charset2", "user", false, true, true, true, true, true, false,
false, true, false, false, false, "${basename}", 0, -1, browser, "exclude_user", "exclude_file", true, EMPTY_DEPOT, EMPTY_WORKSPACE_CLEANUP, EMPTY_MASKVIEW);
"user", "pass", "client", "port", "", "exe", "",
"", "label", "counter", "upstreamProject", "shared", "charset", "charset2", "user", false, true, true, true, true, true, false,
false, true, false, false, false, "${basename}", 0, -1, browser, "exclude_user", "exclude_file", true, EMPTY_DEPOT, EMPTY_WORKSPACE_CLEANUP, EMPTY_MASKVIEW);
assertEquals("", scm.getP4SysDrive());
assertEquals("", scm.getP4SysRoot());
scm.setProjectPath("path");
Expand All @@ -76,11 +85,7 @@ public void testConfigRoundtripWithNoSystemRoot() throws Exception {

public void testConfigRoundtripWithStream() throws Exception {
FreeStyleProject project = createFreeStyleProject();
P4Web browser = new P4Web(new URL("http://localhost/"));
PerforceSCM scm = new PerforceSCM(
"user", "pass", "client", "port", "", "exe", "sysRoot",
"sysDrive", "label", "counter", "upstreamProject", "shared", "charset", "charset2", "user", false, true, true, true, true, true, false,
false, true, false, false, false, "${basename}", 0, -1, browser, "exclude_user", "exclude_file", true, EMPTY_DEPOT, EMPTY_WORKSPACE_CLEANUP, EMPTY_MASKVIEW);
PerforceSCM scm = createPerforceSCMStub();
scm.setP4Stream("stream");
scm.setUseStreamDepot(true);
project.setScm(scm);
Expand Down Expand Up @@ -400,4 +405,49 @@ public void testP4UpstreamProjectRenaming() throws Exception {
scm = (PerforceSCM) downstreamProject.getScm();
assertEquals(scm.p4UpstreamProject, newName);
}

/**
* Checks that the variables substitution works properly for build parameters.
* Actually, it's a test for {@link MacroStringHelper}, but it requires a {@link HudsonTestCase} environment.
*/
@Bug(25226)
public void testCheckParamSubstitutionOrder() throws Exception {
final String projectPath_format = "//depot1/%s/... //client/path1/...";

final FreeStyleProject prj = createFreeStyleProject();
prj.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("PARAM1", "defaultValue")));

PerforceToolInstallation stubInstallation = new PerforceToolInstallation("p4_stub", "echo", new LinkedList<ToolProperty<?>>());
PerforceToolInstallation.DescriptorImpl descriptor = (PerforceToolInstallation.DescriptorImpl) Hudson.getInstance().getDescriptor(PerforceToolInstallation.class);
descriptor.setInstallations(new PerforceToolInstallation[] { stubInstallation });
descriptor.save();

final PerforceSCM scm = PerforceSCMTest.createPerforceSCMStub();
scm.setProjectPath(String.format(projectPath_format, "${PARAM1}"));
scm.setP4Tool("p4_stub");
prj.setScm(scm);

// Run without params
Future<FreeStyleBuild> fBuild = prj.scheduleBuild2(0);
assertNotNull(fBuild);
FreeStyleBuild build = fBuild.get();
assertLogContains(String.format(projectPath_format, "defaultValue"), build);

// Run with params
fBuild = prj.scheduleBuild2(0, null, new ParametersAction(new StringParameterValue("PARAM1", "value")));
assertNotNull(fBuild);
build = fBuild.get();
assertLogContains(String.format(projectPath_format, "value"), build);
}

/**
* Creates {@link PerforceSCM} with default fields.
*/
public static @Nonnull PerforceSCM createPerforceSCMStub() throws MalformedURLException {
P4Web browser = new P4Web(new URL("http://localhost/"));
return new PerforceSCM(
"user", "pass", "client", "port", "", "exe", "sysRoot",
"sysDrive", "label", "counter", "upstreamProject", "shared", "charset", "charset2", "user", false, true, true, true, true, true, false,
false, true, false, false, false, "${basename}", 0, -1, browser, "exclude_user", "exclude_file", true, TEST_DEPOT, TEST_WORKSPACE_CLEANUP, TEST_MASKVIEW);
}
}

0 comments on commit 7fdf8d0

Please sign in to comment.