Skip to content
Permalink
Browse files
Merge pull request #2 from wolfs/hetero-radio
[JENKINS-22583] Added hetero-radio to Ui-Samples with roundtrip-test
  • Loading branch information
jglick committed Apr 14, 2014
2 parents 29361d4 + 06ecb94 commit edf3a3af0e71d3f9a8ff233671bfdaa7a8a165d0
@@ -65,6 +65,15 @@ THE SOFTWARE.
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
@@ -24,22 +24,24 @@

package jenkins.plugins.ui_samples;

import com.google.common.collect.ImmutableList;
import hudson.Extension;
import hudson.XmlFile;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.util.FormApply;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;

@Extension public final class HeteroList extends UISample {

@@ -147,4 +149,23 @@ public ListBoxModel doFillChoiceItems() {
}
}

public static final class HeteroRadioEntry extends Entry {
private final Entry entry;

@DataBoundConstructor public HeteroRadioEntry(Entry entry) { this.entry = entry; }

public Entry getEntry() {
return entry;
}

@Extension public static class DescriptorImpl extends Descriptor<Entry> {

@Override public String getDisplayName() { return "Hetero-Radio"; }

public List<Descriptor> getEntryDescriptors() {
Jenkins jenkins = Jenkins.getInstance();
return ImmutableList.of(jenkins.getDescriptor(ChoiceEntry.class), jenkins.getDescriptor(SimpleEntry.class));
}
}
}
}
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2013 Jesse Glick.
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:f="/lib/form">
<f:entry title="Hetero-Choice">
<f:hetero-radio field="entry" descriptors="${descriptor.entryDescriptors}" />
</f:entry>
</j:jelly>
@@ -0,0 +1,55 @@
package jenkins.plugins.ui_samples;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import hudson.XmlFile;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class HeteroListTest {

public static final String CONFIGURE_URL = "configure";
public static final String CONFIG_FORM_NAME = "config";
@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void Hetero_Radio_Roundtrip() throws Exception {
HeteroList.Entry firstRadioEntry = new HeteroList.HeteroRadioEntry(new HeteroList.SimpleEntry("first"));
HeteroList.Entry secondRadioEntry = new HeteroList.HeteroRadioEntry(new HeteroList.SimpleEntry("second"));

List<HeteroList.Entry> savedEntries = configRoundtrip(firstRadioEntry, secondRadioEntry);

assertThat(savedEntries).extracting("entry.text").containsExactly("first", "second");
}

private List<HeteroList.Entry> configRoundtrip(HeteroList.Entry... entries) throws Exception {
HeteroList heteroList = new HeteroList();
XmlFile configFile = heteroList.getConfigFile();
heteroList.setConfig(new HeteroList.Config(ImmutableList.copyOf(entries)));
configFile.write(heteroList);
long savedManually = configFile.getFile().lastModified();

configRoundtrip(heteroList);
long savedViaWebInterface = configFile.getFile().lastModified();

assertThat(savedManually).isLessThan(savedViaWebInterface);

HeteroList saved = new HeteroList();

return saved.getConfig().getEntries();
}

private void configRoundtrip(UISample uiSample) throws Exception {
j.submit(j.createWebClient().goTo(getUiSampleConfigureUrl(uiSample)).getFormByName(CONFIG_FORM_NAME));
}

private String getUiSampleConfigureUrl(UISample uiSample) {
return Joiner.on('/').join(new Root().getUrlName(), uiSample.getUrlName(), CONFIGURE_URL);
}
}

0 comments on commit edf3a3a

Please sign in to comment.