Skip to content

Commit 88f8e31

Browse files
authored
Merge pull request #20 from res0nance/JENKINS-23729
[JENKINS-23729] Cleaner fix implementation
2 parents 4b5759a + 8fd8e2f commit 88f8e31

3 files changed

Lines changed: 85 additions & 107 deletions

File tree

src/main/java/hudson/plugins/validating_string_parameter/ValidatingStringParameterDefinition.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ public class ValidatingStringParameterDefinition extends ParameterDefinition {
6060
private String regex;
6161
private String failedValidationMessage;
6262

63-
private static Map<String, String> replacements;
64-
65-
static {
66-
replacements = new HashMap<>();
67-
replacements.put("\\", "\\\\");
68-
replacements.put("\"", "\\\"");
69-
}
70-
7163
@DataBoundConstructor
7264
public ValidatingStringParameterDefinition(String name, String defaultValue, String regex, String failedValidationMessage, String description) {
7365
super(name, description);
@@ -88,26 +80,10 @@ public String getRegex() {
8880
return regex;
8981
}
9082

91-
public String getJsEncodedRegex() {
92-
return jsEscape(regex);
93-
}
94-
9583
public String getFailedValidationMessage() {
9684
return failedValidationMessage;
9785
}
9886

99-
public String getJsEncodedFailedValidationMessage() {
100-
return jsEscape(failedValidationMessage);
101-
}
102-
103-
private String jsEscape(String input) {
104-
String res = input;
105-
for(Map.Entry<String,String> entry : replacements.entrySet()) {
106-
res = res.replace(entry.getKey(), entry.getValue());
107-
}
108-
return res;
109-
}
110-
11187
public String getRootUrl() {
11288
return Jenkins.getInstance().getRootUrl();
11389
}
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
<!--
2-
The MIT License
3-
4-
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
5-
6-
Permission is hereby granted, free of charge, to any person obtaining a copy
7-
of this software and associated documentation files (the "Software"), to deal
8-
in the Software without restriction, including without limitation the rights
9-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
copies of the Software, and to permit persons to whom the Software is
11-
furnished to do so, subject to the following conditions:
12-
13-
The above copyright notice and this permission notice shall be included in
14-
all copies or substantial portions of the Software.
15-
16-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22-
THE SOFTWARE.
23-
-->
24-
<?jelly escape-by-default='true'?>
25-
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
26-
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"
27-
xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
28-
29-
<j:set var="escapeEntryTitleAndDescription" value="false"/>
30-
31-
<f:entry title="${h.xmlEscape(it.name)}" description="${it.formattedDescription}">
32-
<div name="parameter" description="${it.formattedDescription}">
33-
<input type="hidden" name="name" value="${it.name}" />
34-
<f:textbox name="value" value="${it.defaultValue}"
35-
checkUrl="'${rootURL}/descriptorByName/hudson.plugins.validating_string_parameter.ValidatingStringParameterDefinition/validate?regex='+encodeURIComponent(&quot;${it.jsEncodedRegex}&quot;)+'&amp;failedValidationMessage='+encodeURIComponent(&quot;${it.jsEncodedFailedValidationMessage}&quot;)+'&amp;value='+encodeURIComponent(this.value)"/>
36-
</div>
37-
</f:entry>
38-
</j:jelly>
1+
<!--
2+
The MIT License
3+
4+
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
-->
24+
<?jelly escape-by-default='true'?>
25+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
26+
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"
27+
xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
28+
29+
<j:set var="escapeEntryTitleAndDescription" value="false"/>
30+
31+
<f:entry title="${h.xmlEscape(it.name)}" description="${it.formattedDescription}">
32+
<div name="parameter" description="${it.formattedDescription}">
33+
<input type="hidden" name="name" value="${it.name}" />
34+
<f:textbox name="value" value="${it.defaultValue}"
35+
checkUrl="'${rootURL}/descriptorByName/hudson.plugins.validating_string_parameter.ValidatingStringParameterDefinition/validate?regex='+encodeURIComponent('${h.jsStringEscape(it.regex)}')+'&amp;failedValidationMessage='+encodeURIComponent('${h.jsStringEscape(it.failedValidationMessage)}')+'&amp;value='+encodeURIComponent(this.value)"/>
36+
</div>
37+
</f:entry>
38+
</j:jelly>
Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
1-
package hudson.plugins.validating_string_parameter;
2-
3-
import hudson.cli.CLICommand;
4-
import hudson.cli.ConsoleCommand;
5-
import org.junit.Rule;
6-
import org.junit.Test;
7-
import org.jvnet.hudson.test.JenkinsRule;
8-
9-
import java.io.IOException;
10-
11-
import static org.junit.Assert.assertEquals;
12-
13-
public class ValidatingStringParameterDefinitionTest {
14-
15-
@Rule
16-
public JenkinsRule r = new JenkinsRule();
17-
18-
@Test
19-
public void testSimpleConfiguration() throws Exception {
20-
ValidatingStringParameterDefinition d = new ValidatingStringParameterDefinition("DUMMY", "foo", ".+", "Your parameter does not match the regular expression!", "Some parameter");
21-
assertEquals("DUMMY", d.getName());
22-
assertEquals("foo", d.getDefaultValue());
23-
assertEquals(".+", d.getRegex());
24-
assertEquals("Your parameter does not match the regular expression!", d.getFailedValidationMessage());
25-
assertEquals("Some parameter", d.getDescription());
26-
}
27-
28-
@Test
29-
public void testJavaScriptEncode() throws Exception {
30-
ValidatingStringParameterDefinition d = new ValidatingStringParameterDefinition("DUMMY", "foo", "\".+", "Your parameter does not match the regular expression!", "Some parameter");
31-
assertEquals("DUMMY", d.getName());
32-
assertEquals("foo", d.getDefaultValue());
33-
assertEquals("\".+", d.getRegex());
34-
assertEquals("\\\\\".+", d.getJsEncodedRegex());
35-
assertEquals("Your parameter does not match the regular expression!", d.getFailedValidationMessage());
36-
assertEquals("Some parameter", d.getDescription());
37-
}
38-
39-
@Test
40-
public void testCLICommand() throws IOException, InterruptedException {
41-
ValidatingStringParameterDefinition d = new ValidatingStringParameterDefinition("DUMMY", "foo", "\".+", "Your parameter does not match the regular expression!", "Some parameter");
42-
CLICommand cliCommand = new ConsoleCommand();
43-
assertEquals(d.getDefaultParameterValue(),d.createValue(cliCommand, null));
44-
assertEquals(new ValidatingStringParameterValue("DUMMY", "\"hello"), d.createValue(cliCommand, "\"hello"));
45-
}
1+
package hudson.plugins.validating_string_parameter;
2+
3+
import hudson.Functions;
4+
import hudson.cli.CLICommand;
5+
import hudson.cli.ConsoleCommand;
6+
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.jvnet.hudson.test.JenkinsRule;
10+
11+
import java.io.IOException;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class ValidatingStringParameterDefinitionTest {
16+
17+
@Rule
18+
public JenkinsRule r = new JenkinsRule();
19+
20+
@Test
21+
public void testSimpleConfiguration() throws Exception {
22+
ValidatingStringParameterDefinition d = new ValidatingStringParameterDefinition("DUMMY", "foo", ".+", "Your parameter does not match the regular expression!", "Some parameter");
23+
assertEquals("DUMMY", d.getName());
24+
assertEquals("foo", d.getDefaultValue());
25+
assertEquals(".+", d.getRegex());
26+
assertEquals("Your parameter does not match the regular expression!", d.getFailedValidationMessage());
27+
assertEquals("Some parameter", d.getDescription());
28+
}
29+
30+
@Test
31+
public void testJavaScriptEncode() throws Exception {
32+
ValidatingStringParameterDefinition d = new ValidatingStringParameterDefinition("DUMMY", "foo", "\".+", "Your parameter does not match the regular expression!", "Some parameter");
33+
assertEquals("DUMMY", d.getName());
34+
assertEquals("foo", d.getDefaultValue());
35+
assertEquals("\".+", d.getRegex());
36+
assertEquals("\\\".+", Functions.jsStringEscape(d.getRegex()));
37+
assertEquals("Your parameter does not match the regular expression!", d.getFailedValidationMessage());
38+
assertEquals("Some parameter", d.getDescription());
39+
}
40+
41+
@Test
42+
public void testCLICommand() throws IOException, InterruptedException {
43+
ValidatingStringParameterDefinition d = new ValidatingStringParameterDefinition("DUMMY", "foo", "\".+", "Your parameter does not match the regular expression!", "Some parameter");
44+
CLICommand cliCommand = new ConsoleCommand();
45+
assertEquals(d.getDefaultParameterValue(),d.createValue(cliCommand, null));
46+
assertEquals(new ValidatingStringParameterValue("DUMMY", "\"hello"), d.createValue(cliCommand, "\"hello"));
47+
}
4648
}

0 commit comments

Comments
 (0)