Advertisement
newgro

Submit RefreshingView by Stateless From

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