Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.apache.wicket;
- import static org.hamcrest.Matchers.*;
- import java.util.Arrays;
- import java.util.List;
- import org.apache.wicket.event.Broadcast;
- import org.apache.wicket.event.IEvent;
- import org.apache.wicket.markup.IMarkupResourceStreamProvider;
- import org.apache.wicket.markup.Markup;
- import org.apache.wicket.markup.html.WebPage;
- import org.apache.wicket.markup.html.basic.Label;
- import org.apache.wicket.markup.html.form.Form;
- import org.apache.wicket.markup.html.form.StatelessForm;
- import org.apache.wicket.markup.html.form.TextField;
- import org.apache.wicket.markup.html.list.ListItem;
- import org.apache.wicket.markup.html.list.ListView;
- import org.apache.wicket.mock.MockApplication;
- import org.apache.wicket.model.IModel;
- import org.apache.wicket.model.LoadableDetachableModel;
- import org.apache.wicket.model.PropertyModel;
- import org.apache.wicket.request.mapper.parameter.PageParameters;
- import org.apache.wicket.util.resource.IResourceStream;
- import org.apache.wicket.util.tester.FormTester;
- import org.apache.wicket.util.tester.WicketTester;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
- public class ListViewSubmitTest {
- public static final class LocalPage extends WebPage implements IMarkupResourceStreamProvider {
- private static final String MARKUP =
- "<html>\n" +
- " <form wicket:id=\"form\">\n" +
- " <ul>\n" +
- " <li wicket:id=\"category\">\n" +
- " <ul>\n" +
- " <span wicket:id=\"categoryId\"></span>\n" +
- " <li wicket:id=\"question\">\n" +
- " <span wicket:id=\"questionId\"></span>\n" +
- " <input wicket:id=\"name\" />\n" +
- " </li>\n" +
- " </ul>\n" +
- " </li>\n" +
- " </ul>\n" +
- " </form>\n" +
- "</html>\n";
- public LocalPage() {
- this(new PageParameters());
- }
- public LocalPage(PageParameters params) {
- super(params);
- LDMPageModel model;
- setDefaultModel(model = new LDMPageModel());
- LocalForm form;
- add(form = new LocalForm("form", model));
- form.add(new CategoryListView("category", new PropertyModel<List<Category>>(model, PageModel.CATEGORIES)));
- }
- @Override
- public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass) {
- return Markup.of(MARKUP).getMarkupResourceStream();
- }
- }
- static class LocalForm extends Form<PageModel> {
- public LocalForm(String id, IModel<PageModel> model) {
- super(id, model);
- }
- @Override
- protected void onSubmit() {
- PageModelEvent.send(this, getModelObject());
- super.onSubmit();
- }
- }
- static class CategoryListView extends ListView<Category> {
- public CategoryListView(String id, IModel<List<Category>> model) {
- super(id, model);
- setReuseItems(true);
- }
- @Override
- protected void populateItem(ListItem<Category> item) {
- item.add(
- new Label(
- "categoryId",
- new PropertyModel<Integer>(
- item.getModel(),
- Category.ID
- )
- )
- );
- item.add(
- new QuestionListView(
- "question",
- new PropertyModel<List<Question>>(
- item.getModel(),
- Category.QUESTIONS
- )
- )
- );
- }
- }
- static class QuestionListView extends ListView<Question> {
- public QuestionListView(String id, IModel<List<Question>> model) {
- super(id, model);
- setReuseItems(true);
- }
- @Override
- protected void populateItem(ListItem<Question> item) {
- item.add(
- new Label(
- "questionId",
- new PropertyModel<Integer>(
- item.getModel(),
- Question.ID
- )
- )
- );
- item.add(
- new TextField<String>(
- "name",
- new PropertyModel<String>(
- item.getModel(),
- Question.NAME
- )
- )
- );
- }
- }
- static class LDMPageModel extends LoadableDetachableModel<PageModel> {
- @Override
- protected PageModel load() {
- return PageModel.create();
- }
- }
- public static class PageModel {
- public static final String CATEGORIES = "categories";
- private final List<Category> categories;
- public PageModel(List<Category> categories) {
- this.categories = categories;
- }
- public List<Category> getCategories() {
- return categories;
- }
- public static PageModel create() {
- return new PageModel(Arrays.asList(Category.create(1), Category.create(2)));
- }
- }
- public static class Category {
- public static final String ID = "id";
- public static final String QUESTIONS = "questions";
- private final int id;
- private final List<Question> questions;
- public Category(int id, List<Question> questions) {
- this.id = id;
- this.questions = questions;
- }
- public int getId() {
- return id;
- }
- public List<Question> getQuestions() {
- return questions;
- }
- public static Category create(int id) {
- return new Category(id, Arrays.asList(new Question(1), new Question(2)));
- }
- }
- public static class Question {
- public static final String ID = "id";
- public static final String NAME = "name";
- private final int id;
- private String name = null;
- public Question(int id) {
- this.id = id;
- }
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public Question setName(String name) {
- this.name = name;
- return this;
- }
- }
- static class Holder<T> {
- private T content = null;
- public T getContent() {
- return content;
- }
- public Holder<T> setContent(T content) {
- this.content = content;
- return this;
- }
- }
- private static class PageModelEvent {
- private final PageModel model;
- public PageModelEvent(PageModel model) {
- this.model = model;
- }
- public static void send(Component sink, PageModel model) {
- sink.send(sink, Broadcast.BUBBLE, new PageModelEvent(model));
- }
- public static void onEvent(IEvent<?> event, Holder<PageModel> holder) {
- if (event.getPayload() != null && event.getPayload() instanceof PageModelEvent) {
- ((PageModelEvent) event.getPayload()).assignModelTo(holder);
- }
- }
- public void assignModelTo(Holder<PageModel> holder) {
- holder.setContent(model);
- }
- }
- private WicketTester tester;
- private Holder<PageModel> holder;
- @Before
- public void setup() {
- holder = new Holder<PageModel>();
- tester = new WicketTester(new MockApplication() {
- @Override
- public void onEvent(IEvent<?> event) {
- PageModelEvent.onEvent(event, holder);
- super.onEvent(event);
- }
- });
- }
- @Test
- public void can_submit_data_in_listview() {
- tester.startPage(new LocalPage());
- tester.assertNoErrorMessage();
- FormTester ft = tester.newFormTester("form");
- ft.setValue("category:0:question:0:name", "value1");
- ft.setValue("category:0:question:1:name", "value2");
- ft.setValue("category:1:question:0:name", "value3");
- ft.setValue("category:1:question:1:name", "value4");
- ft.submit();
- Assert.assertThat(
- holder.getContent(),
- hasProperty(
- PageModel.CATEGORIES,
- contains(
- hasProperty(
- Category.QUESTIONS,
- contains(
- hasProperty(
- Question.NAME,
- is(equalTo("value1"))
- ),
- hasProperty(
- Question.NAME,
- is(equalTo("value2"))
- )
- )
- ),
- hasProperty(
- Category.QUESTIONS,
- contains(
- hasProperty(
- Question.NAME,
- is(equalTo("value3"))
- ),
- hasProperty(
- Question.NAME,
- is(equalTo("value4"))
- )
- )
- )
- )
- )
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement