File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\model\ParametersTest.java.html

1 package hudson.model;
2
3 import org.jvnet.hudson.test.HudsonTestCase;
4 import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
5 import com.gargoylesoftware.htmlunit.html.HtmlPage;
6 import com.gargoylesoftware.htmlunit.html.HtmlForm;
7 import com.gargoylesoftware.htmlunit.html.HtmlElement;
8 import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
9 import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
10 import com.gargoylesoftware.htmlunit.html.HtmlOption;
11 import java.util.Set;
12
13 /**
14 * @author huybrechts
15 */
16 public class ParametersTest extends HudsonTestCase {
17
18 public void testParameterTypes() throws Exception {
19 FreeStyleProject otherProject = createFreeStyleProject();
20 otherProject.scheduleBuild2(0).get();
21
22 FreeStyleProject project = createFreeStyleProject();
23 ParametersDefinitionProperty pdp = new ParametersDefinitionProperty(
24 new StringParameterDefinition("string", "defaultValue", "string description"),
25 new BooleanParameterDefinition("boolean", true, "boolean description"),
26 new ChoiceParameterDefinition("choice", "Choice 1\nChoice 2", "choice description"),
27 new RunParameterDefinition("run", otherProject.getName(), "run description"));
28 project.addProperty(pdp);
29 CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
30 project.getBuildersList().add(builder);
31
32 WebClient wc = new WebClient();
33 wc.setThrowExceptionOnFailingStatusCode(false);
34 HtmlPage page = wc.goTo("/job/" + project.getName() + "/build?delay=0sec");
35
36 HtmlForm form = page.getFormByName("parameters");
37
38 HtmlElement element = (HtmlElement) form.selectSingleNode("//tr[td/div/input/@value='string']");
39 assertNotNull(element);
40 assertEquals("string description", ((HtmlElement) element.selectSingleNode("td/div")).getAttribute("description"));
41 HtmlTextInput stringParameterInput = (HtmlTextInput) element.selectSingleNode(".//input[@name='value']");
42 assertEquals("defaultValue", stringParameterInput.getAttribute("value"));
43 assertEquals("string", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());
44 stringParameterInput.setAttribute("value", "newValue");
45
46 element = (HtmlElement) form.selectSingleNode("//tr[td/div/input/@value='boolean']");
47 assertNotNull(element);
48 assertEquals("boolean description", ((HtmlElement) element.selectSingleNode("td/div")).getAttribute("description"));
49 Object o = element.selectSingleNode(".//input[@name='value']");
50 System.out.println(o);
51 HtmlCheckBoxInput booleanParameterInput = (HtmlCheckBoxInput) o;
52 assertEquals(true, booleanParameterInput.isChecked());
53 assertEquals("boolean", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());
54
55 element = (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='choice']");
56 assertNotNull(element);
57 assertEquals("choice description", ((HtmlElement) element.selectSingleNode("td/div")).getAttribute("description"));
58 assertEquals("choice", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());
59
60 element = (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='run']");
61 assertNotNull(element);
62 assertEquals("run description", ((HtmlElement) element.selectSingleNode("td/div")).getAttribute("description"));
63 assertEquals("run", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());
64
65 submit(form);
66 Queue.Item q = jenkins.getQueue().getItem(project);
67 if (q != null) q.getFuture().get();
68 else Thread.sleep(1000);
69
70 assertEquals("newValue", builder.getEnvVars().get("STRING"));
71 assertEquals("true", builder.getEnvVars().get("BOOLEAN"));
72 assertEquals("Choice 1", builder.getEnvVars().get("CHOICE"));
73 assertEquals(jenkins.getRootUrl() + otherProject.getLastBuild().getUrl(), builder.getEnvVars().get("RUN"));
74 }
75
76 public void testChoiceWithLTGT() throws Exception {
77 FreeStyleProject project = createFreeStyleProject();
78 ParametersDefinitionProperty pdp = new ParametersDefinitionProperty(
79 new ChoiceParameterDefinition("choice", "Choice 1\nChoice <2>", "choice description"));
80 project.addProperty(pdp);
81 CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
82 project.getBuildersList().add(builder);
83
84 WebClient wc = new WebClient();
85 wc.setThrowExceptionOnFailingStatusCode(false);
86 HtmlPage page = wc.goTo("/job/" + project.getName() + "/build?delay=0sec");
87 HtmlForm form = page.getFormByName("parameters");
88
89 HtmlElement element = (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='choice']");
90 assertNotNull(element);
91 assertEquals("choice description", ((HtmlElement) element.selectSingleNode("td/div")).getAttribute("description"));
92 assertEquals("choice", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());
93 HtmlOption opt = (HtmlOption)element.selectSingleNode("td/div/select/option[@value='Choice <2>']");
94 assertNotNull(opt);
95 assertEquals("Choice <2>", opt.asText());
96 opt.setSelected(true);
97
98 submit(form);
99 Queue.Item q = jenkins.getQueue().getItem(project);
100 if (q != null) q.getFuture().get();
101 else Thread.sleep(1000);
102
103 assertNotNull(builder.getEnvVars());
104 assertEquals("Choice <2>", builder.getEnvVars().get("CHOICE"));
105 }
106
107 public void testSensitiveParameters() throws Exception {
108 FreeStyleProject project = createFreeStyleProject();
109 ParametersDefinitionProperty pdb = new ParametersDefinitionProperty(
110 new PasswordParameterDefinition("password", "12345", "password description"));
111 project.addProperty(pdb);
112
113 CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
114 project.getBuildersList().add(builder);
115
116 FreeStyleBuild build = project.scheduleBuild2(0).get();
117 Set<String> sensitiveVars = build.getSensitiveBuildVariables();
118
119 assertNotNull(sensitiveVars);
120 assertTrue(sensitiveVars.contains("password"));
121 }
122
123 public void testNonSensitiveParameters() throws Exception {
124 FreeStyleProject project = createFreeStyleProject();
125 ParametersDefinitionProperty pdb = new ParametersDefinitionProperty(
126 new StringParameterDefinition("string", "defaultValue", "string description"));
127 project.addProperty(pdb);
128
129 CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
130 project.getBuildersList().add(builder);
131
132 FreeStyleBuild build = project.scheduleBuild2(0).get();
133 Set<String> sensitiveVars = build.getSensitiveBuildVariables();
134
135 assertNotNull(sensitiveVars);
136 assertFalse(sensitiveVars.contains("string"));
137 }
138
139 public void testMixedSensitivity() throws Exception {
140 FreeStyleProject project = createFreeStyleProject();
141 ParametersDefinitionProperty pdb = new ParametersDefinitionProperty(
142 new StringParameterDefinition("string", "defaultValue", "string description"),
143 new PasswordParameterDefinition("password", "12345", "password description"),
144 new StringParameterDefinition("string2", "Value2", "string description")
145 );
146 project.addProperty(pdb);
147
148 CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
149 project.getBuildersList().add(builder);
150
151 FreeStyleBuild build = project.scheduleBuild2(0).get();
152 Set<String> sensitiveVars = build.getSensitiveBuildVariables();
153
154 assertNotNull(sensitiveVars);
155 assertFalse(sensitiveVars.contains("string"));
156 assertTrue(sensitiveVars.contains("password"));
157 assertFalse(sensitiveVars.contains("string2"));
158 }
159 }
160