Skip to content

Commit

Permalink
[JENKINS-27955] Add ability to define fitnesse port as enironment var…
Browse files Browse the repository at this point in the history
…iable
  • Loading branch information
antoine-aumjaud committed Jun 22, 2015
1 parent 4ad7c6f commit a0bedbc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
15 changes: 10 additions & 5 deletions src/main/java/hudson/plugins/fitnesse/FitnesseBuilder.java
Expand Up @@ -168,9 +168,13 @@ public String getFitnesseJavaWorkingDirectory() {
/**
* referenced in config.jelly
*/
public int getFitnessePort() {
public String getFitnessePort() {
return getOption(FITNESSE_PORT_REMOTE, getOption(FITNESSE_PORT_LOCAL, getOption(FITNESSE_PORT, "-1")));
}

public int getFitnessePort(EnvVars environment) {
return Integer.parseInt(getOption(FITNESSE_PORT_REMOTE,
getOption(FITNESSE_PORT_LOCAL, getOption(FITNESSE_PORT, "-1"))));
getOption(FITNESSE_PORT_LOCAL, getOption(FITNESSE_PORT, "-1", environment), environment), environment));
}

/**
Expand Down Expand Up @@ -236,8 +240,8 @@ public String getFitnessePathToXmlResultsOut(EnvVars environment) {
/**
* referenced in config.jelly
*/
public int getFitnesseHttpTimeout() {
return Integer.parseInt(getOption(HTTP_TIMEOUT, String.valueOf(_URL_READ_TIMEOUT_MILLIS)));
public String getFitnesseHttpTimeout() {
return getOption(HTTP_TIMEOUT, String.valueOf(_URL_READ_TIMEOUT_MILLIS));
}

public int getFitnesseHttpTimeout(EnvVars environment) {
Expand Down Expand Up @@ -296,7 +300,8 @@ public FormValidation doCheckFitnessePort(@QueryParameter String value) throws I
if (intValue < 1)
return FormValidation.error("Port must be a positive integer.");
} catch (NumberFormatException e) {
return FormValidation.error("Port must be a number.");
if (!value.startsWith("$"))
return FormValidation.error("Port must be a number.");
}
return FormValidation.ok();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/fitnesse/FitnesseExecutor.java
Expand Up @@ -75,7 +75,7 @@ public boolean execute(Launcher launcher, AbstractBuild<?, ?> build) throws Inte

private FitnesseBuildAction getFitnesseBuildAction(AbstractBuild<?, ?> build) throws IOException {
return new FitnesseBuildAction(builder.getFitnesseStart(), builder.getFitnesseHost(build, envVars),
builder.getFitnessePort());
builder.getFitnessePort(envVars));
}

private Proc startFitnesse(FilePath workingDirectory, Launcher launcher) throws IOException, InterruptedException {
Expand Down Expand Up @@ -116,7 +116,7 @@ public ArrayList<String> getJavaCmd(FilePath workingDirectory) throws IOExceptio
String[] fitnesse_opts = { // --
"-d", absolutePathToFitNesseRoot.getParent().getRemote(), // --
"-r", absolutePathToFitNesseRoot.getName(), // --
"-p", Integer.toString(builder.getFitnessePort()) };
"-p", Integer.toString(builder.getFitnessePort(envVars)) };

// split additional fitness options and add them to those explicitly configured ones
String[] addOps = splitOptions(builder.getAdditionalFitnesseOptions());
Expand Down Expand Up @@ -280,7 +280,7 @@ public byte[] getHttpBytes(URL pageCmdTarget, Resettable timeout, int httpTimeou
/* package for test */URL getFitnessePage(AbstractBuild<?, ?> build, boolean withCommand) throws IOException {
return new URL("http", //
builder.getFitnesseHost(build, envVars), //
builder.getFitnessePort(), //
builder.getFitnessePort(envVars), //
withCommand ? getFitnessePageCmd() : getFitnessePageBase());
}

Expand Down
48 changes: 35 additions & 13 deletions src/test/java/hudson/plugins/fitnesse/FitnesseBuilderTest.java
Expand Up @@ -21,26 +21,28 @@ public class FitnesseBuilderTest {
@Test
public void getJdkShouldReturnSpecificJavaHomeIfSpecified() {
HashMap<String, String> options = new HashMap<String, String>();
String expectedJavaHome = "jdk1.6.0_18";
options.put(FitnesseBuilder.FITNESSE_JDK, expectedJavaHome);
FitnesseBuilder builder = new FitnesseBuilder(options);

final String expectedJavaHome = "jdk1.6.0_18";
options.put(FitnesseBuilder.FITNESSE_JDK, expectedJavaHome);
Assert.assertEquals(expectedJavaHome, builder.getFitnesseJdk());
}

@Test
public void getJdkShouldReturnNothingIfNotSpecifiedSoThatTheDefaultJDKIsUsed() {
HashMap<String, String> options = new HashMap<String, String>();
String expectedJavaHome = "";
FitnesseBuilder builder = new FitnesseBuilder(options);

String expectedJavaHome = "";
Assert.assertEquals(expectedJavaHome, builder.getFitnesseJdk());
}

@Test
public void getPortShouldReturnLocalPortIfSpecified() {
HashMap<String, String> options = new HashMap<String, String>();
options.put(FitnesseBuilder.FITNESSE_PORT_LOCAL, "99");
FitnesseBuilder builder = new FitnesseBuilder(options);

options.put(FitnesseBuilder.FITNESSE_PORT_LOCAL, "99");
Assert.assertEquals(99, builder.getFitnessePort());

options.put(FitnesseBuilder.FITNESSE_PORT_REMOTE, null);
Expand All @@ -50,11 +52,13 @@ public void getPortShouldReturnLocalPortIfSpecified() {
Assert.assertEquals(99, builder.getFitnessePort());
}


@Test
public void getPortShouldReturnRemotePortIfSpecified() {
HashMap<String, String> options = new HashMap<String, String>();
options.put(FitnesseBuilder.FITNESSE_PORT_REMOTE, "999");
FitnesseBuilder builder = new FitnesseBuilder(options);

options.put(FitnesseBuilder.FITNESSE_PORT_REMOTE, "999");
Assert.assertEquals(999, builder.getFitnessePort());

options.put(FitnesseBuilder.FITNESSE_PORT_LOCAL, null);
Expand All @@ -64,12 +68,25 @@ public void getPortShouldReturnRemotePortIfSpecified() {
Assert.assertEquals(999, builder.getFitnessePort());
}


@Test
public void getPortShouldReturnEnvValueIfMacroIsSpecified() {
HashMap<String, String> options = new HashMap<String, String>();
FitnesseBuilder builder = new FitnesseBuilder(options);

EnvVars envVars = new EnvVars();
envVars.put("PORT", "99");
options.put(FitnesseBuilder.FITNESSE_PORT_REMOTE, "$PORT");

Assert.assertEquals(99, builder.getFitnessePort(envVars));
}

@Test
public void getHostShouldReturnLocalHostIfStartBuildIsTrue() {
HashMap<String, String> options = new HashMap<String, String>();
options.put(FitnesseBuilder.START_FITNESSE, "True");
FitnesseBuilder builder = new FitnesseBuilder(options);

options.put(FitnesseBuilder.START_FITNESSE, "True");
Assert.assertTrue(builder.getFitnesseStart());
Assert.assertEquals("localhost", builder.getFitnesseHost());

Expand All @@ -80,10 +97,10 @@ public void getHostShouldReturnLocalHostIfStartBuildIsTrue() {
@Test
public void getHostShouldReturnSpecifiedHostIfStartBuildIsFalse() {
HashMap<String, String> options = new HashMap<String, String>();
options.put(FitnesseBuilder.START_FITNESSE, "False");
options.put(FitnesseBuilder.FITNESSE_HOST, "hudson.local");
FitnesseBuilder builder = new FitnesseBuilder(options);

options.put(FitnesseBuilder.START_FITNESSE, "False");
options.put(FitnesseBuilder.FITNESSE_HOST, "hudson.local");
Assert.assertFalse(builder.getFitnesseStart());
Assert.assertEquals("hudson.local", builder.getFitnesseHost());

Expand All @@ -95,6 +112,7 @@ public void getHostShouldReturnSpecifiedHostIfStartBuildIsFalse() {
public void getHttpTimeoutShouldReturn60000UnlessValueIsExplicit() {
HashMap<String, String> options = new HashMap<String, String>();
FitnesseBuilder builder = new FitnesseBuilder(options);

Assert.assertEquals(60000, builder.getFitnesseHttpTimeout());
options.put(FitnesseBuilder.HTTP_TIMEOUT, "1000");
Assert.assertEquals(1000, builder.getFitnesseHttpTimeout());
Expand All @@ -104,6 +122,7 @@ public void getHttpTimeoutShouldReturn60000UnlessValueIsExplicit() {
public void getTestTimeoutShouldReturn60000UnlessValueIsExplicit() {
HashMap<String, String> options = new HashMap<String, String>();
FitnesseBuilder builder = new FitnesseBuilder(options);

Assert.assertEquals(60000, builder.getFitnesseTestTimeout());
options.put(FitnesseBuilder.TEST_TIMEOUT, "1000");
Assert.assertEquals(1000, builder.getFitnesseTestTimeout());
Expand All @@ -112,10 +131,11 @@ public void getTestTimeoutShouldReturn60000UnlessValueIsExplicit() {
@Test
public void getJavaWorkingDirShouldReturnParentOfFitnessseJarUnlessValueIsExplicit() throws Exception {
HashMap<String, String> options = new HashMap<String, String>();
FitnesseBuilder builder = new FitnesseBuilder(options);

File tmpFile = File.createTempFile("fitnesse", ".jar");
options.put(FitnesseBuilder.PATH_TO_JAR, tmpFile.getAbsolutePath());

FitnesseBuilder builder = new FitnesseBuilder(options);
Assert.assertEquals(tmpFile.getParentFile().getAbsolutePath(), builder.getFitnesseJavaWorkingDirectory());

options.put(FitnesseBuilder.JAVA_WORKING_DIRECTORY, "/some/explicit/path");
Expand All @@ -125,29 +145,31 @@ public void getJavaWorkingDirShouldReturnParentOfFitnessseJarUnlessValueIsExplic
@Test
public void getJavaWorkingDirShouldReturnParentOfFitnessseJarEvenIfRelativeToBuildDir() throws Exception {
HashMap<String, String> options = new HashMap<String, String>();
FitnesseBuilder builder = new FitnesseBuilder(options);

File tmpFile = new File("relativePath", "fitnesse.jar");
options.put(FitnesseBuilder.PATH_TO_JAR, tmpFile.getPath());

FitnesseBuilder builder = new FitnesseBuilder(options);
Assert.assertEquals("relativePath", builder.getFitnesseJavaWorkingDirectory());
}

@Test
public void getJavaWorkingDirShouldBeEmptyIfFitnessseJarUnspecified() throws Exception {
HashMap<String, String> options = new HashMap<String, String>();
FitnesseBuilder builder = new FitnesseBuilder(options);

Assert.assertEquals("", builder.getFitnesseJavaWorkingDirectory());
}

@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void getFitnesseHostShouldNotThrowANullPointerWhenNodePropertyIsNull() throws InterruptedException,
IOException {
@SuppressWarnings("rawtypes")

AbstractBuild build = Mockito.mock(AbstractBuild.class);
Node node = Mockito.mock(Node.class);
when(build.getBuiltOn()).thenReturn(node);
@SuppressWarnings("rawtypes")

DescribableList describableList = Mockito.mock(DescribableList.class);
when(node.getNodeProperties()).thenReturn(describableList);
when(describableList.get(EnvironmentVariablesNodeProperty.class)).thenReturn(null);
Expand Down

0 comments on commit a0bedbc

Please sign in to comment.