Advertisement
newgro

Submit ListView on Stateless Form

Jul 9th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.76 KB | None | 0 0
  1. package org.apache.wicket;
  2.  
  3. import static org.hamcrest.Matchers.*;
  4.  
  5. import java.util.Arrays;
  6. import java.util.List;
  7.  
  8. import org.apache.wicket.event.Broadcast;
  9. import org.apache.wicket.event.IEvent;
  10. import org.apache.wicket.markup.IMarkupResourceStreamProvider;
  11. import org.apache.wicket.markup.Markup;
  12. import org.apache.wicket.markup.html.WebPage;
  13. import org.apache.wicket.markup.html.basic.Label;
  14. import org.apache.wicket.markup.html.form.Form;
  15. import org.apache.wicket.markup.html.form.StatelessForm;
  16. import org.apache.wicket.markup.html.form.TextField;
  17. import org.apache.wicket.markup.html.list.ListItem;
  18. import org.apache.wicket.markup.html.list.ListView;
  19. import org.apache.wicket.mock.MockApplication;
  20. import org.apache.wicket.model.IModel;
  21. import org.apache.wicket.model.LoadableDetachableModel;
  22. import org.apache.wicket.model.PropertyModel;
  23. import org.apache.wicket.request.mapper.parameter.PageParameters;
  24. import org.apache.wicket.util.resource.IResourceStream;
  25. import org.apache.wicket.util.tester.FormTester;
  26. import org.apache.wicket.util.tester.WicketTester;
  27. import org.junit.Assert;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30.  
  31. public class ListViewSubmitTest {
  32.  
  33.     public static final class LocalPage extends WebPage implements IMarkupResourceStreamProvider {
  34.        
  35.         private static final String MARKUP =
  36.                 "<html>\n" +
  37.                 "  <form wicket:id=\"form\">\n" +
  38.                 "    <ul>\n" +
  39.                 "      <li wicket:id=\"category\">\n" +
  40.                 "        <ul>\n" +
  41.                 "          <span wicket:id=\"categoryId\"></span>\n" +
  42.                 "          <li wicket:id=\"question\">\n" +
  43.                 "            <span wicket:id=\"questionId\"></span>\n" +
  44.                 "            <input wicket:id=\"name\" />\n" +
  45.                 "          </li>\n" +
  46.                 "        </ul>\n" +
  47.                 "      </li>\n" +
  48.                 "    </ul>\n" +
  49.                 "  </form>\n" +
  50.                 "</html>\n";
  51.        
  52.         public LocalPage() {
  53.             this(new PageParameters());
  54.         }
  55.        
  56.         public LocalPage(PageParameters params) {
  57.             super(params);
  58.            
  59.             LDMPageModel model;
  60.             setDefaultModel(model = new LDMPageModel());
  61.            
  62.             LocalForm form;
  63.             add(form = new LocalForm("form", model));
  64.             form.add(new CategoryListView("category", new PropertyModel<List<Category>>(model, PageModel.CATEGORIES)));
  65.         }
  66.        
  67.         @Override
  68.         public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass) {
  69.             return Markup.of(MARKUP).getMarkupResourceStream();
  70.         }
  71.     }
  72.    
  73.     static class LocalForm extends Form<PageModel> {
  74.        
  75.         public LocalForm(String id, IModel<PageModel> model) {
  76.             super(id, model);
  77.         }
  78.        
  79.         @Override
  80.         protected void onSubmit() {
  81.             PageModelEvent.send(this, getModelObject());
  82.             super.onSubmit();
  83.         }
  84.     }
  85.    
  86.     static class CategoryListView extends ListView<Category> {
  87.  
  88.         public CategoryListView(String id, IModel<List<Category>> model) {
  89.             super(id, model);
  90.             setReuseItems(true);
  91.         }
  92.        
  93.         @Override
  94.         protected void populateItem(ListItem<Category> item) {
  95.             item.add(
  96.                     new Label(
  97.                             "categoryId",
  98.                             new PropertyModel<Integer>(
  99.                                     item.getModel(),
  100.                                     Category.ID
  101.                             )
  102.                     )
  103.             );
  104.             item.add(
  105.                     new QuestionListView(
  106.                             "question",
  107.                             new PropertyModel<List<Question>>(
  108.                                     item.getModel(),
  109.                                     Category.QUESTIONS
  110.                             )
  111.                     )
  112.             );
  113.         }
  114.     }
  115.    
  116.     static class QuestionListView extends ListView<Question> {
  117.  
  118.         public QuestionListView(String id, IModel<List<Question>> model) {
  119.             super(id, model);
  120.             setReuseItems(true);
  121.         }
  122.        
  123.         @Override
  124.         protected void populateItem(ListItem<Question> item) {
  125.             item.add(
  126.                     new Label(
  127.                             "questionId",
  128.                             new PropertyModel<Integer>(
  129.                                     item.getModel(),
  130.                                     Question.ID
  131.                             )
  132.                     )
  133.             );
  134.             item.add(
  135.                     new TextField<String>(
  136.                             "name",
  137.                             new PropertyModel<String>(
  138.                                     item.getModel(),
  139.                                     Question.NAME
  140.                             )
  141.                     )
  142.             );
  143.         }
  144.     }
  145.    
  146.     static class LDMPageModel extends LoadableDetachableModel<PageModel> {
  147.        
  148.         @Override
  149.         protected PageModel load() {
  150.             return PageModel.create();
  151.         }
  152.     }
  153.    
  154.     public static class PageModel {
  155.         public static final String CATEGORIES = "categories";
  156.        
  157.         private final List<Category> categories;
  158.        
  159.         public PageModel(List<Category> categories) {
  160.             this.categories = categories;
  161.         }
  162.        
  163.         public List<Category> getCategories() {
  164.             return categories;
  165.         }
  166.        
  167.         public static PageModel create() {
  168.             return new PageModel(Arrays.asList(Category.create(1), Category.create(2)));
  169.         }
  170.     }
  171.    
  172.     public static class Category {
  173.        
  174.         public static final String ID = "id";
  175.         public static final String QUESTIONS = "questions";
  176.  
  177.         private final int id;
  178.         private final List<Question> questions;
  179.        
  180.         public Category(int id, List<Question> questions) {
  181.             this.id = id;
  182.             this.questions = questions;
  183.         }
  184.        
  185.         public int getId() {
  186.             return id;
  187.         }
  188.        
  189.         public List<Question> getQuestions() {
  190.             return questions;
  191.         }
  192.        
  193.         public static Category create(int id) {
  194.             return new Category(id, Arrays.asList(new Question(1), new Question(2)));
  195.         }
  196.     }
  197.    
  198.     public static class Question {
  199.        
  200.         public static final String ID = "id";
  201.         public static final String NAME = "name";
  202.        
  203.         private final int id;
  204.         private String name = null;
  205.        
  206.         public Question(int id) {
  207.             this.id = id;
  208.         }
  209.        
  210.         public int getId() {
  211.             return id;
  212.         }
  213.        
  214.         public String getName() {
  215.             return name;
  216.         }
  217.        
  218.         public Question setName(String name) {
  219.             this.name = name;
  220.             return this;
  221.         }
  222.     }
  223.    
  224.     static class Holder<T> {
  225.         private T content = null;
  226.        
  227.         public T getContent() {
  228.             return content;
  229.         }
  230.        
  231.         public Holder<T> setContent(T content) {
  232.             this.content = content;
  233.             return this;
  234.         }
  235.     }
  236.    
  237.     private static class PageModelEvent {
  238.  
  239.         private final PageModel model;
  240.        
  241.         public PageModelEvent(PageModel model) {
  242.             this.model = model;
  243.         }
  244.        
  245.         public static void send(Component sink, PageModel model) {
  246.             sink.send(sink, Broadcast.BUBBLE, new PageModelEvent(model));
  247.         }
  248.  
  249.         public static void onEvent(IEvent<?> event, Holder<PageModel> holder) {
  250.             if (event.getPayload() != null && event.getPayload() instanceof PageModelEvent) {
  251.                 ((PageModelEvent) event.getPayload()).assignModelTo(holder);
  252.             }
  253.         }
  254.  
  255.         public void assignModelTo(Holder<PageModel> holder) {
  256.             holder.setContent(model);
  257.         }
  258.        
  259.     }
  260.    
  261.     private WicketTester tester;
  262.    
  263.     private Holder<PageModel> holder;
  264.    
  265.     @Before
  266.     public void setup() {
  267.         holder = new Holder<PageModel>();
  268.         tester = new WicketTester(new MockApplication() {
  269.             @Override
  270.             public void onEvent(IEvent<?> event) {
  271.                 PageModelEvent.onEvent(event, holder);
  272.                 super.onEvent(event);
  273.             }
  274.         });
  275.     }
  276.    
  277.     @Test
  278.     public void can_submit_data_in_listview() {
  279.         tester.startPage(new LocalPage());
  280.         tester.assertNoErrorMessage();
  281.         FormTester ft = tester.newFormTester("form");
  282.         ft.setValue("category:0:question:0:name", "value1");
  283.         ft.setValue("category:0:question:1:name", "value2");
  284.         ft.setValue("category:1:question:0:name", "value3");
  285.         ft.setValue("category:1:question:1:name", "value4");
  286.         ft.submit();
  287.         Assert.assertThat(
  288.                 holder.getContent(),
  289.                 hasProperty(
  290.                         PageModel.CATEGORIES,
  291.                         contains(
  292.                                 hasProperty(
  293.                                         Category.QUESTIONS,
  294.                                         contains(
  295.                                                 hasProperty(
  296.                                                         Question.NAME,
  297.                                                         is(equalTo("value1"))
  298.                                                 ),
  299.                                                 hasProperty(
  300.                                                         Question.NAME,
  301.                                                         is(equalTo("value2"))
  302.                                                 )
  303.                                         )
  304.                                 ),
  305.                                 hasProperty(
  306.                                         Category.QUESTIONS,
  307.                                         contains(
  308.                                                 hasProperty(
  309.                                                         Question.NAME,
  310.                                                         is(equalTo("value3"))
  311.                                                 ),
  312.                                                 hasProperty(
  313.                                                         Question.NAME,
  314.                                                         is(equalTo("value4"))
  315.                                                 )
  316.                                         )
  317.                                 )
  318.                         )
  319.                 )
  320.         );
  321.     }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement