Permalink
Browse files

[FIXED JENKINS-19990] Tests for non-ascii properties.

  • Loading branch information...
ikedam committed Dec 5, 2013
1 parent 221df5e commit e535cd745733d91b012692bdca4b5101aa0da17d
@@ -148,5 +148,10 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>

@@ -47,6 +47,7 @@
import hudson.plugins.parameterizedtrigger.FileBuildParameterFactory;
import hudson.plugins.parameterizedtrigger.FileBuildParameterFactory.NoFilesFoundEnum;
import hudson.plugins.parameterizedtrigger.TriggerBuilder;
import hudson.util.FormValidation;

import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.HudsonTestCase;
@@ -61,12 +62,16 @@
public class FileBuildParameterFactoryTest extends HudsonTestCase {

private TriggerBuilder createTriggerBuilder(AbstractProject project, NoFilesFoundEnum action){
return createTriggerBuilder(project, action, null);
}

private TriggerBuilder createTriggerBuilder(AbstractProject project, NoFilesFoundEnum action, String encoding){

TriggerBuilder tBuilder = new TriggerBuilder(
new BlockableBuildTriggerConfig(project.getName(),
new BlockingBehaviour(Result.FAILURE, Result.UNSTABLE, Result.FAILURE),
ImmutableList.<AbstractBuildParameterFactory>of(
new FileBuildParameterFactory("*.txt", action)),
new FileBuildParameterFactory("*.txt", encoding, action)),
Collections.<AbstractBuildParameters>emptyList()));
return tBuilder;
}
@@ -215,4 +220,155 @@ public void testNoFilesFail() throws Exception {
assertEquals(0, builds.size());
}

public void testUtf8File() throws Exception {

//create triggered build, with capture env builder
Project projectB = createFreeStyleProject();
CaptureAllEnvironmentBuilder builder = new CaptureAllEnvironmentBuilder();
projectB.getBuildersList().add(builder);

//create triggering build
FreeStyleProject projectA = createFreeStyleProject();
projectA.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("abc.txt").write(
"TEST=こんにちは\n" // "hello" in Japanese.
+ "TEST=hello_abc", // TEST in multibytes.
"UTF-8"
);
return true;
}
});

// add Trigger builder, with file paramter factory
projectA.getBuildersList().add(createTriggerBuilder(projectB, NoFilesFoundEnum.SKIP, "UTF-8"));

projectA.scheduleBuild2(0).get();

// check triggered builds are correct.
waitUntilNoActivity();
List<FreeStyleBuild> builds = projectB.getBuilds();
assertEquals(1, builds.size());

for (FreeStyleBuild build : builds) {
EnvVars buildEnvVar = builder.getEnvVars().get(build.getId());
System.out.println(String.format("'%s'", "こんにちは"));
System.out.println(String.format("'%s'", buildEnvVar.get("TEST")));
assertEquals("こんにちは", buildEnvVar.get("TEST"));
assertEquals("hello_abc", buildEnvVar.get("TEST"));
}

}

public void testShiftJISFile() throws Exception {
// ShiftJIS is an encoding of Japanese texts.
// I test here that a non-UTF-8 encoding also works.

//create triggered build, with capture env builder
Project projectB = createFreeStyleProject();
CaptureAllEnvironmentBuilder builder = new CaptureAllEnvironmentBuilder();
projectB.getBuildersList().add(builder);

//create triggering build
FreeStyleProject projectA = createFreeStyleProject();
projectA.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("abc.txt").write(
"TEST=こんにちは\n" // "hello" in Japanese.
+ "TEST=hello_abc", // TEST in multibytes.
"Shift_JIS"
);
return true;
}
});

// add Trigger builder, with file paramter factory
projectA.getBuildersList().add(createTriggerBuilder(projectB, NoFilesFoundEnum.SKIP, "Shift_JIS"));

projectA.scheduleBuild2(0).get();

// check triggered builds are correct.
waitUntilNoActivity();
List<FreeStyleBuild> builds = projectB.getBuilds();
assertEquals(1, builds.size());

for (FreeStyleBuild build : builds) {
EnvVars buildEnvVar = builder.getEnvVars().get(build.getId());
assertEquals("こんにちは", buildEnvVar.get("TEST"));
assertEquals("hello_abc", buildEnvVar.get("TEST"));
}

}

public void testPlatformDefaultEncodedFile() throws Exception {

//create triggered build, with capture env builder
Project projectB = createFreeStyleProject();
CaptureAllEnvironmentBuilder builder = new CaptureAllEnvironmentBuilder();
projectB.getBuildersList().add(builder);

//create triggering build
FreeStyleProject projectA = createFreeStyleProject();
projectA.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("abc.txt").write(
"TEST=hello_abc", // TEST=hello_abc in multibytes.
null
);
return true;
}
});

// add Trigger builder, with file paramter factory
projectA.getBuildersList().add(createTriggerBuilder(projectB, NoFilesFoundEnum.SKIP, ""));

projectA.scheduleBuild2(0).get();

// check triggered builds are correct.
waitUntilNoActivity();
List<FreeStyleBuild> builds = projectB.getBuilds();
assertEquals(1, builds.size());

for (FreeStyleBuild build : builds) {
EnvVars buildEnvVar = builder.getEnvVars().get(build.getId());
assertEquals("hello_abc", buildEnvVar.get("TEST"));
}

}

public void testDoCheckEncoding() throws Exception {
FileBuildParameterFactory.DescriptorImpl d
= (FileBuildParameterFactory.DescriptorImpl)jenkins.getDescriptorOrDie(FileBuildParameterFactory.class);

assertEquals(FormValidation.Kind.OK, d.doCheckEncoding(null).kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding("").kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding(" ").kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding("UTF-8").kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding("Shift_JIS").kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding(" UTF-8 ").kind);
assertEquals(FormValidation.Kind.ERROR, d.doCheckEncoding("NoSuchEncoding").kind);
}

public void testNullifyEncoding() throws Exception {
// to use default encoding, encoding must be null.
{
FileBuildParameterFactory target
= new FileBuildParameterFactory("*.properties", null, NoFilesFoundEnum.SKIP);
assertNull(target.getEncoding());
}
{
FileBuildParameterFactory target
= new FileBuildParameterFactory("*.properties", "", NoFilesFoundEnum.SKIP);
assertNull(target.getEncoding());
}
{
FileBuildParameterFactory target
= new FileBuildParameterFactory("*.properties", " ", NoFilesFoundEnum.SKIP);
assertNull(target.getEncoding());
}
}

}
@@ -23,11 +23,13 @@
*/
package hudson.plugins.parameterizedtrigger.test;

import hudson.model.FreeStyleProject;
import hudson.model.Project;
import hudson.plugins.parameterizedtrigger.BuildTrigger;
import hudson.plugins.parameterizedtrigger.BuildTriggerConfig;
import hudson.plugins.parameterizedtrigger.FileBuildParameters;
import hudson.plugins.parameterizedtrigger.ResultCondition;
import hudson.util.FormValidation;

import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.HudsonTestCase;
@@ -109,4 +111,115 @@ public void test_failOnMissingFile() throws Exception {
// There should be no builds of projectB as not triggered.
assertEquals(0, projectB.getBuilds().size());
}

public void testUtf8File() throws Exception {

FreeStyleProject projectA = createFreeStyleProject("projectA");
String properties = "KEY=こんにちは\n" // "hello" in Japanese.
+ "KEY=value"; // "KEY" in multibytes.
projectA.setScm(new SingleFileSCM("properties.txt", properties.getBytes("UTF-8")));
projectA.getPublishersList().add(
new BuildTrigger(
new BuildTriggerConfig("projectB", ResultCondition.SUCCESS,
new FileBuildParameters("properties.txt", "UTF-8", true))));

CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
FreeStyleProject projectB = createFreeStyleProject("projectB");
projectB.getBuildersList().add(builder);
projectB.setQuietPeriod(1);
hudson.rebuildDependencyGraph();

projectA.scheduleBuild2(0).get();
hudson.getQueue().getItem(projectB).getFuture().get();

assertNotNull("builder should record environment", builder.getEnvVars());
assertEquals("こんにちは", builder.getEnvVars().get("KEY"));
assertEquals("value", builder.getEnvVars().get("KEY"));
}

public void testShiftJISFile() throws Exception {
// ShiftJIS is an encoding of Japanese texts.
// I test here that a non-UTF-8 encoding also works.

FreeStyleProject projectA = createFreeStyleProject("projectA");
String properties = "KEY=こんにちは\n" // "hello" in Japanese.
+ "KEY=value"; // "KEY" in multibytes.
projectA.setScm(new SingleFileSCM("properties.txt", properties.getBytes("Shift_JIS")));
projectA.getPublishersList().add(
new BuildTrigger(
new BuildTriggerConfig("projectB", ResultCondition.SUCCESS,
new FileBuildParameters("properties.txt", "Shift_JIS", true))));

CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
FreeStyleProject projectB = createFreeStyleProject("projectB");
projectB.getBuildersList().add(builder);
projectB.setQuietPeriod(1);
hudson.rebuildDependencyGraph();

projectA.scheduleBuild2(0).get();
hudson.getQueue().getItem(projectB).getFuture().get();

assertNotNull("builder should record environment", builder.getEnvVars());
assertEquals("こんにちは", builder.getEnvVars().get("KEY"));
assertEquals("value", builder.getEnvVars().get("KEY"));
}

public void testPlatformDefaultEncodedFile() throws Exception {
// ShiftJIS is an encoding of Japanese texts.
// I test here that a non-UTF-8 encoding also works.

FreeStyleProject projectA = createFreeStyleProject("projectA");
String properties = "KEY=こんにちは\n" // "hello" in Japanese.
+ "KEY=value"; // "KEY" in multibytes.
projectA.setScm(new SingleFileSCM("properties.txt", properties.getBytes()));
projectA.getPublishersList().add(
new BuildTrigger(
new BuildTriggerConfig("projectB", ResultCondition.SUCCESS,
new FileBuildParameters("properties.txt"))));

CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
FreeStyleProject projectB = createFreeStyleProject("projectB");
projectB.getBuildersList().add(builder);
projectB.setQuietPeriod(1);
hudson.rebuildDependencyGraph();

projectA.scheduleBuild2(0).get();
hudson.getQueue().getItem(projectB).getFuture().get();

assertNotNull("builder should record environment", builder.getEnvVars());
assertEquals("こんにちは", builder.getEnvVars().get("KEY"));
assertEquals("value", builder.getEnvVars().get("KEY"));
}

public void testDoCheckEncoding() throws Exception {
FileBuildParameters.DescriptorImpl d
= (FileBuildParameters.DescriptorImpl)jenkins.getDescriptorOrDie(FileBuildParameters.class);

assertEquals(FormValidation.Kind.OK, d.doCheckEncoding(null).kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding("").kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding(" ").kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding("UTF-8").kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding("Shift_JIS").kind);
assertEquals(FormValidation.Kind.OK, d.doCheckEncoding(" UTF-8 ").kind);
assertEquals(FormValidation.Kind.ERROR, d.doCheckEncoding("NoSuchEncoding").kind);
}

public void testNullifyEncoding() throws Exception {
// to use default encoding, encoding must be null.
{
FileBuildParameters target
= new FileBuildParameters("*.properties", null, false);
assertNull(target.getEncoding());
}
{
FileBuildParameters target
= new FileBuildParameters("*.properties", "", false);
assertNull(target.getEncoding());
}
{
FileBuildParameters target
= new FileBuildParameters("*.properties", " ", false);
assertNull(target.getEncoding());
}
}
}
@@ -54,4 +54,27 @@ public void test() throws Exception {
assertNotNull("builder should record environment", builder.getEnvVars());
assertEquals("value", builder.getEnvVars().get("KEY"));
}

public void testNonAscii() throws Exception {

Project projectA = createFreeStyleProject("projectA");
String properties = "KEY=123\n" // 123 in multibytes
+ "KEY=value\n"; // "KEY" in multibytes
projectA.getPublishersList().add(
new BuildTrigger(new BuildTriggerConfig("projectB", ResultCondition.SUCCESS,
new PredefinedBuildParameters(properties))));

CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
Project projectB = createFreeStyleProject("projectB");
projectB.getBuildersList().add(builder);
projectB.setQuietPeriod(1);
hudson.rebuildDependencyGraph();

projectA.scheduleBuild2(0).get();
hudson.getQueue().getItem(projectB).getFuture().get();

assertNotNull("builder should record environment", builder.getEnvVars());
assertEquals("123", builder.getEnvVars().get("KEY"));
assertEquals("value", builder.getEnvVars().get("KEY"));
}
}

0 comments on commit e535cd7

Please sign in to comment.