Skip to content

Commit

Permalink
[JENKINS-22583] Added hetero-radio to Ui-Samples with roundtrip-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Wolf committed Apr 14, 2014
1 parent 29361d4 commit 06ecb94
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 5 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Expand Up @@ -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>
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/jenkins/plugins/ui_samples/HeteroList.java
Expand Up @@ -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 {

Expand Down Expand Up @@ -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>
55 changes: 55 additions & 0 deletions src/test/java/jenkins/plugins/ui_samples/HeteroListTest.java
@@ -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 06ecb94

Please sign in to comment.