提交 cc41d34a 编写于 作者: M mindless

[HUDSON-5028] expand radiobutton support in repeatable content

to work in nested repeatables.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@29170 71c3de6d-444a-0410-be80-ed276b4c234a
上级 8f8ffe76
......@@ -27,8 +27,10 @@ import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import org.jvnet.hudson.test.HudsonTestCase;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
/**
......@@ -36,11 +38,15 @@ import org.kohsuke.stapler.StaplerRequest;
*/
public class RepeatableTest extends HudsonTestCase {
private JSONObject formData;
public ArrayList<Object> list = new ArrayList<Object>();
private Class<?> bindClass;
private List<?> bindResult;
public List<Object> list = new ArrayList<Object>();
public Integer minimum = null;
public void doSubmitTest(StaplerRequest req) throws Exception {
formData = req.getSubmittedForm();
if (bindClass != null)
bindResult = req.bindJSONToList(bindClass, formData.get("items"));
}
private void doTestSimple() throws Exception {
......@@ -66,7 +72,9 @@ public class RepeatableTest extends HudsonTestCase {
public static class Foo {
public String txt;
public boolean bool;
public Foo(String s, boolean b) { txt = s; bool = b; }
@DataBoundConstructor
public Foo(String txt, boolean bool) { this.txt = txt; this.bool = bool; }
@Override public String toString() { return "foo:" + txt + ':' + bool; }
}
private void addData() {
......@@ -148,4 +156,75 @@ public class RepeatableTest extends HudsonTestCase {
+ "{\"radio\":{\"a\":\"avalue two\",\"value\":\"one\"},\"txt\":\"txt two\"}]",
formData.get("foos").toString());
}
public static class FooList {
public String title;
public Foo[] list = new Foo[0];
@DataBoundConstructor public FooList(String title, Foo[] foo) {
this.title = title;
this.list = foo;
}
@Override public String toString() {
StringBuilder buf = new StringBuilder("FooList:" + title + ":[");
for (int i = 0; i < list.length; i++) {
if (i > 0) buf.append(',');
buf.append(list[i].toString());
}
buf.append(']');
return buf.toString();
}
}
/** Tests nested repeatable and use of @DataBoundContructor to process formData */
public void testNested() throws Exception {
HtmlPage p = createWebClient().goTo("self/testNested");
HtmlForm f = p.getFormByName("config");
try {
f.getButtonByCaption("Add").click();
f.getInputByValue("").setValueAttribute("title one");
Thread.sleep(1000); // Saw intermittent failures w/o this
f.getButtonByCaption("Add Foo").click();
f.getInputByValue("").setValueAttribute("txt one");
f.getButtonByCaption("Add Foo").click();
f.getInputByValue("").setValueAttribute("txt two");
f.getInputsByName("bool").get(1).click();
f.getButtonByCaption("Add").click();
f.getInputByValue("").setValueAttribute("title two");
f.getElementsByTagName("button").get(1).click(); // 2nd "Add Foo" button
f.getInputByValue("").setValueAttribute("txt 2.1");
} catch (Exception e) {
System.err.println("HTML at time of failure:\n" + p.getBody().asXml());
throw e;
}
bindClass = FooList.class;
submit(f);
assertEquals("[FooList:title one:[foo:txt one:false,foo:txt two:true], "
+ "FooList:title two:[foo:txt 2.1:false]]", bindResult.toString());
}
public void testNestedRadio() throws Exception {
HtmlPage p = createWebClient().goTo("self/testNestedRadio");
HtmlForm f = p.getFormByName("config");
try {
f.getButtonByCaption("Add").click();
f.getElementsByAttribute("input", "type", "radio").get(1).click(); // outer=two
Thread.sleep(500); // Saw intermittent failures w/o this
f.getButtonByCaption("Add Moo").click();
f.getElementsByAttribute("input", "type", "radio").get(2).click(); // inner=inone
f.getButtonByCaption("Add").click();
f.getElementsByAttribute("input", "type", "radio").get(4).click(); // outer=one
f.getElementsByTagName("button").get(1).click(); // 2nd "Add Moo" button
f.getElementsByAttribute("input", "type", "radio").get(7).click(); // inner=intwo
Thread.sleep(500);
f.getElementsByTagName("button").get(1).click();
f.getElementsByAttribute("input", "type", "radio").get(8).click(); // inner=inone
} catch (Exception e) {
System.err.println("HTML at time of failure:\n" + p.getBody().asXml());
throw e;
}
submit(f);
assertEquals("[{\"moo\":{\"inner\":\"inone\"},\"outer\":\"two\"},"
+ "{\"moo\":[{\"inner\":\"intwo\"},{\"inner\":\"inone\"}],\"outer\":\"one\"}]",
formData.get("items").toString());
}
}
\ No newline at end of file
<!--
The MIT License
Copyright (c) 2004-2010, Sun Microsystems, Inc., Alan Harder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<l:layout title="Testing radio block in repeatable">
<l:main-panel>
<f:form method="post" name="config" action="submitTest">
<f:repeatable var="item" items="${it.list}" name="items">
<table>
<f:entry>
<f:textbox name="title" value="${item.title}"/>
</f:entry>
<f:nested>
<f:repeatable var="foo" items="${item.list}" add="Add Foo">
<table>
<f:entry>
<f:textbox name="txt" value="${foo.txt}"/>
</f:entry>
<f:entry>
<f:checkbox name="bool" checked="${foo.bool}"/>
</f:entry>
</table>
</f:repeatable>
</f:nested>
</table>
</f:repeatable>
<f:entry>
<f:submit value="submit"/>
</f:entry>
</f:form>
</l:main-panel>
</l:layout>
</j:jelly>
<!--
The MIT License
Copyright (c) 2004-2010, Sun Microsystems, Inc., Alan Harder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<l:layout title="Testing radio block in repeatable">
<l:main-panel>
<f:form method="post" name="config" action="submitTest">
<f:repeatable var="item" items="${it.list}" name="items">
<table>
<f:entry>
<f:radio name="outer" value="one"/> One
<f:radio name="outer" value="two"/> Two
</f:entry>
<f:nested>
<f:repeatable var="moo" items="${item.list}" add="Add Moo">
<table>
<f:entry>
<f:radio name="inner" value="inone"/>
<f:radio name="inner" value="intwo"/>
</f:entry>
</table>
</f:repeatable>
</f:nested>
</table>
</f:repeatable>
<f:entry>
<f:submit value="submit"/>
</f:entry>
</f:form>
</l:main-panel>
</l:layout>
</j:jelly>
......@@ -1560,7 +1560,7 @@ function buildFormTree(form) {
break;
case "radio":
if(!e.checked) break;
if (e.name.substring(0,8)=='removeme')
while (e.name.substring(0,8)=='removeme')
e.name = e.name.substring(e.name.indexOf('_',8)+1);
if(e.groupingNode) {
p = findParent(e);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册