提交 ee8de0f9 编写于 作者: A Andrew Bayer

Merge pull request #122 from bap2000/master

Add a default attribute to repeatableProperty and repeatable jelly tags
......@@ -81,6 +81,9 @@ THE SOFTWARE.
<st:attribute name="items">
The item collection to loop over. Required unless @field is given.
</st:attribute>
<st:attribute name="default">
Use this collection for items if items or @field is null
</st:attribute>
<st:attribute name="noAddButton">
true if the default 'add' button (that adds a new copy) shouldn't be displayed.
When you use this attribute,
......@@ -107,14 +110,14 @@ THE SOFTWARE.
<j:when test="${attrs.field!=null}">
<j:set var="name" value="${field}"/>
<j:set var="var" value="instance"/>
<j:set var="items" value="${instance[name]}"/>
<j:set var="items" value="${instance[name] ?: attrs.default}"/>
<!-- and expose update descriptor to the body of this tag -->
<j:set var="descriptor" value="${descriptor.getPropertyType(instance,field).itemTypeDescriptor}" />
</j:when>
<j:otherwise>
<j:set var="name" value="${attrs.name?:attrs.var}"/>
<j:set var="items" value="${attrs.items}"/>
<j:set var="items" value="${attrs.items ?: attrs.default}"/>
</j:otherwise>
</j:choose>
......
......@@ -31,6 +31,9 @@ THE SOFTWARE.
<st:attribute name="field">
Used for the data binding.
</st:attribute>
<st:attribute name="default">
The default value to use for this collection when 'instance[field]' is null.
</st:attribute>
<st:attribute name="noAddButton">
true if the default 'add' button (that adds a new copy) shouldn't be displayed.
When you use this attribute,
......@@ -49,7 +52,7 @@ THE SOFTWARE.
</st:documentation>
<f:repeatable field="${attrs.field}" noAddButton="${attrs.noAddButton}" header="${attrs.header}">
<f:repeatable field="${attrs.field}" default="${attrs.default}" noAddButton="${attrs.noAddButton}" header="${attrs.header}">
<table style="width:100%">
<st:include page="config.jelly" class="${descriptor.clazz}" />
</table>
......
/*
* The MIT License
*
* Copyright (C) 2010-2011 by Anthony Robinson
*
* 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.
*/
package lib.form;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import hudson.Extension;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import org.jvnet.hudson.test.HudsonTestCase;
import org.kohsuke.stapler.DataBoundConstructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class RepeatablePropertyTest extends HudsonTestCase implements Describable<RepeatablePropertyTest> {
private static final String VIEW_WITHOUT_DEFAULT = "noDefault";
private static final String VIEW_WITH_DEFAULT = "withDefault";
public ArrayList<ExcitingObject> testRepeatable;
public ArrayList<ExcitingObject> defaults;
public void testSimple() throws Exception {
testRepeatable = createRepeatable();
assertFormContents(VIEW_WITHOUT_DEFAULT, testRepeatable);
}
public void testNullFieldNoDefault() throws Exception {
assertFormContents(VIEW_WITHOUT_DEFAULT, new ArrayList<ExcitingObject>());
}
public void testNullFieldWithDefault() throws Exception {
defaults = createRepeatable();
assertFormContents(VIEW_WITH_DEFAULT, defaults);
}
public void testFieldNotNullWithDefaultIgnoresDefaults() throws Exception {
testRepeatable = createRepeatable();
defaults = new ArrayList<ExcitingObject>(Arrays.asList(
new ExcitingObject("This default should be ignored"),
new ExcitingObject("Ignore me too")
));
assertFormContents(VIEW_WITH_DEFAULT, testRepeatable);
}
private void assertFormContents(final String viewName, final ArrayList<ExcitingObject> expected) throws Exception {
final HtmlForm form = getForm(viewName);
final List<HtmlTextInput> inputs = toTextInputList(form.getElementsByAttribute("input", "type", "text"));
assertEquals("size", expected.size(), inputs.size());
for (int i = 0; i < expected.size(); i++)
assertEquals(expected.get(i).greatProperty, inputs.get(i).getValueAttribute());
}
private List<HtmlTextInput> toTextInputList(final List<HtmlElement> inputs) {
assertNotNull(inputs);
final List<HtmlTextInput> textInputList = new ArrayList<HtmlTextInput>();
for (HtmlElement input : inputs) {
assertTrue(input instanceof HtmlTextInput);
textInputList.add((HtmlTextInput) input);
}
return textInputList;
}
private ArrayList<ExcitingObject> createRepeatable() {
return new ArrayList<ExcitingObject>(Arrays.asList(
new ExcitingObject("A nice thing"),
new ExcitingObject("I'm even better"),
new ExcitingObject("Don't bother, I'm not exciting at all")
));
}
private HtmlForm getForm(final String viewName) throws Exception {
final HtmlPage page = createWebClient().goTo("self/" + viewName);
final HtmlForm form = page.getFormByName("config");
return form;
}
public DescriptorImpl getDescriptor() {
return hudson.getDescriptorByType(DescriptorImpl.class);
}
@Extension
public static final class DescriptorImpl extends Descriptor<RepeatablePropertyTest> {
@Override
public String getDisplayName() {
return "RepeatablePropertyTest";
}
}
public static final class ExcitingObject implements Describable<ExcitingObject> {
private final String greatProperty;
@DataBoundConstructor
public ExcitingObject(final String greatProperty) {
this.greatProperty = greatProperty;
}
public String getGreatProperty() {
return greatProperty;
}
public Descriptor<ExcitingObject> getDescriptor() {
return Hudson.getInstance().getDescriptor(ExcitingObject.class);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExcitingObject that = (ExcitingObject) o;
if (greatProperty != null ? !greatProperty.equals(that.greatProperty) : that.greatProperty != null)
return false;
return true;
}
@Override
public int hashCode() {
return greatProperty != null ? greatProperty.hashCode() : 0;
}
@Override
public String toString() {
return "ExcitingObject[" + greatProperty + ']';
}
@Extension
public static final class ExcitingDescriptor extends Descriptor<ExcitingObject> {
public ExcitingDescriptor() {
super(ExcitingObject.class);
}
@Override
public String getDisplayName() {
return "This is an awesome thing!";
}
}
}
}
......@@ -53,6 +53,7 @@ public class RepeatableTest extends HudsonTestCase {
private Class<?> bindClass;
private List<?> bindResult;
public List<Object> list = new ArrayList<Object>();
public List<Object> defaults = null;
public Integer minimum = null;
public void doSubmitTest(StaplerRequest req) throws Exception {
......@@ -135,6 +136,62 @@ public class RepeatableTest extends HudsonTestCase {
+ "{\"bool\":true,\"txt\":\"existing two\"},{\"bool\":false,\"txt\":\"new one\"}]",
formData.get("foos").toString());
}
public void testNoData() throws Exception {
list = null;
defaults = null;
gotoAndSubmitConfig("defaultForField");
assertNull(formData.get("list"));
gotoAndSubmitConfig("defaultForItems");
assertNull(formData.get("list"));
}
public void testItemsWithDefaults() throws Exception {
assertWithDefaults("defaultForItems");
}
public void testItemsDefaultsIgnoredIfFieldHasData() throws Exception {
assertDefaultsIgnoredIfHaveData("defaultForItems");
}
public void testFieldWithDefaults() throws Exception {
assertWithDefaults("defaultForField");
}
public void testFieldDefaultsIgnoredIfFieldHasData() throws Exception {
assertDefaultsIgnoredIfHaveData("defaultForField");
}
private void addDefaults() {
defaults = new ArrayList<Object>();
defaults.add(new Foo("default one", true));
defaults.add(new Foo("default two", false));
}
private void assertWithDefaults(final String viewName) throws Exception {
list = null;
addDefaults();
gotoAndSubmitConfig(viewName);
assertNotNull(formData.get("list"));
assertEquals("[{\"bool\":true,\"txt\":\"default one\"},{\"bool\":false,\"txt\":\"default two\"}]",
formData.get("list").toString());
}
private void assertDefaultsIgnoredIfHaveData(final String viewName) throws Exception {
addData();
addDefaults();
gotoAndSubmitConfig(viewName);
assertNotNull(formData.get("list"));
assertEquals("[{\"bool\":true,\"txt\":\"existing one\"},{\"bool\":false,\"txt\":\"existing two\"}]",
formData.get("list").toString());
}
private void gotoAndSubmitConfig(final String viewName) throws Exception {
HtmlPage p = createWebClient().goTo("self/" + viewName);
HtmlForm f = p.getFormByName("config");
submit(f);
}
// ========================================================================
......
<?jelly escape-by-default='true'?>
<!--
~ The MIT License
~
~ Copyright (C) 2010-2011 by Anthony Robinson
~
~ 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">
<f:entry field="greatProperty">
<f:textbox/>
</f:entry>
</j:jelly>
<?jelly escape-by-default='true'?>
<!--
~ The MIT License
~
~ Copyright (C) 2010-2011 by Anthony Robinson
~
~ 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 repeatableProperty without default attribute">
<l:main-panel>
<f:form method="post" name="config" action="thisFormIsOnlyInspectedInTheTests">
<j:set var="instance" value="${it}"/>
<j:set var="descriptor" value="${it.descriptor}"/>
<f:entry title="Exciting Objects">
<f:repeatableProperty field="testRepeatable"/>
</f:entry>
</f:form>
</l:main-panel>
</l:layout>
</j:jelly>
<?jelly escape-by-default='true'?>
<!--
~ The MIT License
~
~ Copyright (C) 2010-2011 by Anthony Robinson
~
~ 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 repeatableProperty with default attribute">
<l:main-panel>
<f:form method="post" name="config" action="thisFormIsOnlyInspectedInTheTests">
<j:set var="instance" value="${it}"/>
<j:set var="descriptor" value="${it.descriptor}"/>
<f:entry title="Exciting Objects">
<f:repeatableProperty field="testRepeatable" default="${it.defaults}"/>
</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.
-->
<?jelly escape-by-default='true'?>
<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">
<j:set var="instance" value="${it}"/>
<j:set var="descriptor" value="${it.descriptor}"/>
<f:repeatable field="list" default="${it.defaults}">
<table>
<f:entry>
<f:textbox name="txt" value="${instance.txt}"/>
</f:entry>
<f:entry>
<f:checkbox name="bool" checked="${instance.bool}"/>
</f:entry>
</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.
-->
<?jelly escape-by-default='true'?>
<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="foo" items="${it.list}" default="${it.defaults}" name="list" minimum="${it.minimum}">
<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:entry>
<f:submit value="submit"/>
</f:entry>
</f:form>
</l:main-panel>
</l:layout>
</j:jelly>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册