File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\lib\form\RepeatablePropertyTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (C) 2010-2011 by Anthony Robinson
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
25 package lib.form;
26
27 import com.gargoylesoftware.htmlunit.html.HtmlElement;
28 import com.gargoylesoftware.htmlunit.html.HtmlForm;
29 import com.gargoylesoftware.htmlunit.html.HtmlPage;
30 import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
31 import hudson.Extension;
32 import hudson.model.Describable;
33 import hudson.model.Descriptor;
34 import jenkins.model.Jenkins;
35 import org.jvnet.hudson.test.HudsonTestCase;
36 import org.kohsuke.stapler.DataBoundConstructor;
37
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41
42 public class RepeatablePropertyTest extends HudsonTestCase implements Describable<RepeatablePropertyTest> {
43
44 private static final String VIEW_WITHOUT_DEFAULT = "noDefault";
45 private static final String VIEW_WITH_DEFAULT = "withDefault";
46
47 public ArrayList<ExcitingObject> testRepeatable;
48 public ArrayList<ExcitingObject> defaults;
49
50 public void testSimple() throws Exception {
51 testRepeatable = createRepeatable();
52 assertFormContents(VIEW_WITHOUT_DEFAULT, testRepeatable);
53 }
54
55 public void testNullFieldNoDefault() throws Exception {
56 assertFormContents(VIEW_WITHOUT_DEFAULT, new ArrayList<ExcitingObject>());
57 }
58
59 public void testNullFieldWithDefault() throws Exception {
60 defaults = createRepeatable();
61 assertFormContents(VIEW_WITH_DEFAULT, defaults);
62 }
63
64 public void testFieldNotNullWithDefaultIgnoresDefaults() throws Exception {
65 testRepeatable = createRepeatable();
66 defaults = new ArrayList<ExcitingObject>(Arrays.asList(
67 new ExcitingObject("This default should be ignored"),
68 new ExcitingObject("Ignore me too")
69 ));
70 assertFormContents(VIEW_WITH_DEFAULT, testRepeatable);
71 }
72
73 private void assertFormContents(final String viewName, final ArrayList<ExcitingObject> expected) throws Exception {
74 final HtmlForm form = getForm(viewName);
75 final List<HtmlTextInput> inputs = toTextInputList(form.getElementsByAttribute("input", "type", "text"));
76 assertEquals("size", expected.size(), inputs.size());
77 for (int i = 0; i < expected.size(); i++)
78 assertEquals(expected.get(i).greatProperty, inputs.get(i).getValueAttribute());
79 }
80
81 private List<HtmlTextInput> toTextInputList(final List<HtmlElement> inputs) {
82 assertNotNull(inputs);
83 final List<HtmlTextInput> textInputList = new ArrayList<HtmlTextInput>();
84 for (HtmlElement input : inputs) {
85 assertTrue(input instanceof HtmlTextInput);
86 textInputList.add((HtmlTextInput) input);
87 }
88 return textInputList;
89 }
90
91 private ArrayList<ExcitingObject> createRepeatable() {
92 return new ArrayList<ExcitingObject>(Arrays.asList(
93 new ExcitingObject("A nice thing"),
94 new ExcitingObject("I'm even better"),
95 new ExcitingObject("Don't bother, I'm not exciting at all")
96 ));
97 }
98
99 private HtmlForm getForm(final String viewName) throws Exception {
100 final HtmlPage page = createWebClient().goTo("self/" + viewName);
101 final HtmlForm form = page.getFormByName("config");
102 return form;
103 }
104
105 public DescriptorImpl getDescriptor() {
106 return jenkins.getDescriptorByType(DescriptorImpl.class);
107 }
108
109 @Extension
110 public static final class DescriptorImpl extends Descriptor<RepeatablePropertyTest> {
111 @Override
112 public String getDisplayName() {
113 return "RepeatablePropertyTest";
114 }
115 }
116
117 public static final class ExcitingObject implements Describable<ExcitingObject> {
118 private final String greatProperty;
119 @DataBoundConstructor
120 public ExcitingObject(final String greatProperty) {
121 this.greatProperty = greatProperty;
122 }
123 public String getGreatProperty() {
124 return greatProperty;
125 }
126 public Descriptor<ExcitingObject> getDescriptor() {
127 return Jenkins.getInstance().getDescriptor(ExcitingObject.class);
128 }
129 @Override
130 public boolean equals(Object o) {
131 if (this == o) return true;
132 if (o == null || getClass() != o.getClass()) return false;
133 ExcitingObject that = (ExcitingObject) o;
134 if (greatProperty != null ? !greatProperty.equals(that.greatProperty) : that.greatProperty != null)
135 return false;
136 return true;
137 }
138 @Override
139 public int hashCode() {
140 return greatProperty != null ? greatProperty.hashCode() : 0;
141 }
142 @Override
143 public String toString() {
144 return "ExcitingObject[" + greatProperty + ']';
145 }
146
147 @Extension
148 public static final class ExcitingDescriptor extends Descriptor<ExcitingObject> {
149 public ExcitingDescriptor() {
150 super(ExcitingObject.class);
151 }
152 @Override
153 public String getDisplayName() {
154 return "This is an awesome thing!";
155 }
156 }
157 }
158
159 }
160