Package javango.forms

Source Code of javango.forms.ModelFormTest$IncludeTestContactForm

package javango.forms;

import java.util.HashMap;
import java.util.Map;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;

import javango.forms.Form;
import javango.forms.Forms;
import javango.forms.ModelForm;
import javango.forms.fields.CharField;
import javango.forms.fields.EmailField;
import javango.forms.fields.Field;
import javango.forms.fields.FieldFactory;
import javango.forms.fields.LongField;
import junit.framework.TestCase;

public class ModelFormTest extends TestCase implements Module {

  protected Injector injector;
  protected Forms formFactory;
  protected FieldFactory fieldFactory;
 
  public void configure(Binder arg0) {
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    injector = Guice.createInjector(this);
    formFactory = injector.getInstance(Forms.class);
    fieldFactory = injector.getInstance(FieldFactory.class);
  }

  public static class SampleModel {
    private String field;

    public String getField() {
      return field;
    }

    public void setField(String field) {
      this.field = field;
    }   
  }
 
  public static class ContactBean {
    private String subject;
    private String message;
    private String sender;
    private Long value;
    private Boolean ccMyself;
   
    public String getSubject() {
      return subject;
    }
    public void setSubject(String subject) {
      this.subject = subject;
    }
    public String getMessage() {
      return message;
    }
    public void setMessage(String message) {
      this.message = message;
    }
    public String getSender() {
      return sender;
    }
    public void setSender(String sender) {
      this.sender = sender;
    }
    public Long getValue() {
      return value;
    }
    public void setValue(Long value) {
      this.value = value;
    }
    public Boolean getCcMyself() {
      return ccMyself;
    }
    public void setCcMyself(Boolean ccMyself) {
      this.ccMyself = ccMyself;
    }     
  }
 
  public static class ContactForm extends ModelForm {

    public EmailField sender;
    public LongField value;
   
    @Inject
    public ContactForm(FieldFactory fieldFactory) {
      super(fieldFactory);
      setModel(ContactBean.class);
    }   
       
  }

  public void testCreateForm() throws Exception {
    Form form = formFactory.forModel(SampleModel.class);
    Map<String, Field<?>> fields = form.getFields();
    assertTrue(fields.containsKey("field"));
    Field<?> field = fields.get("field");
    assertEquals(CharField.class, field.getClass());
    assertEquals("field", field.getName());
  }
   
  public void testUnboundErrors() throws Exception {
    Form f = formFactory.forModel(ContactBean.class);
    assertFalse(f.isValid());
    Map l = f.getErrors();
    assertNotNull(l);
    assertEquals(l.size(), 0);
  }
 
  public void testBound() throws Exception {
    Map<String, String[]> m = new HashMap<String, String[]>();
    m.put("subject", new String[] {"hello"});
    m.put("message", new String[] {"Hi there"});
    m.put("sender", new String[] {"foo@example.com"});
    m.put("cc_myself", new String[] {"true"});
    m.put("value", new String[] {"1234"});
    Form f = formFactory.forModel(ContactBean.class);
    assertFalse(f.isBound());
    f = formFactory.forModel(ContactBean.class).bind(m);
    assertTrue(f.isBound());
  }
 
  public void testHtml() throws Exception {
    Map<String, String[]> m = new HashMap<String, String[]>();
    m.put("subject", new String[] {"hello"});
    m.put("message", new String[] {"Hi there"});
    m.put("sender", new String[] {"foo@example.com"});
    m.put("ccMyself", new String[] {"true"});
    m.put("value", new String[] {"1234"});
    Form f = formFactory.forModel(ContactBean.class).bind(m).setId(null);
    assertTrue(f.isValid());
    String expected = "<tr><th>Subject</th><td><input type=\"text\" name=\"subject\" value=\"hello\" /></td></tr>\n" +
        "<tr><th>Message</th><td><input type=\"text\" name=\"message\" value=\"Hi there\" /></td></tr>\n" +
        "<tr><th>Sender</th><td><input type=\"text\" name=\"sender\" value=\"foo@example.com\" /></td></tr>\n" +
        "<tr><th>Value</th><td><input type=\"text\" name=\"value\" value=\"1234\" /></td></tr>\n" +
        "<tr><th>Cc Myself</th><td><input type=\"checkbox\" name=\"ccMyself\" checked=\"checked\" /></td></tr>\n";
    assertEquals(expected, f.asTable());

  }
  public void testValidateData() throws Exception {
    Map<String, String[]> m = new HashMap<String, String[]>();
    m.put("subject", new String[] {"hello"});
    m.put("message", new String[] {"Hi there"});
    m.put("sender", new String[] {"foo@example.com"});
    m.put("ccMyself", new String[] {"true"});
    m.put("value", new String[] {"1234"});
    Form f = injector.getInstance(ContactForm.class).bind(m);
    assertTrue(f.isValid());
  }
 
  public void testValidateEmail() throws Exception {
    Map<String, String[]> m = new HashMap<String, String[]>();
    m.put("subject", new String[] {"hello"});
    m.put("message", new String[] {"Hi there"});
    m.put("sender", new String[] {"fooample.com"});
    m.put("cc_myself", new String[] {"true"});
    m.put("value", new String[] {"12asdf34"});
    Form f = injector.getInstance(ContactForm.class).bind(m);
    assertFalse(f.isValid());
    Map errors = f.getErrors();
    assertTrue(errors.containsKey("sender"));
    assertTrue(errors.containsKey("value"));
  }
 
  public void testCleanData() throws Exception {
    Map<String, String[]> m = new HashMap<String, String[]>();
    m.put("subject", new String[] {"hello"});
    m.put("message", new String[] {"Hi there"});
    m.put("sender", new String[] {"foo@example.com"});
    m.put("ccMyself", new String[] {"true"});
    m.put("value", new String[] {"1234"});
    Form f = formFactory.forModel(ContactBean.class).bind(m);
    assertTrue(f.isValid());
    ContactBean bean = (ContactBean)f.clean(ContactBean.class);
    assertEquals(bean.getSubject(), "hello");
    assertEquals(bean.getMessage(), "Hi there");
    assertEquals(bean.getSender(), "foo@example.com");
    assertEquals(bean.getCcMyself(), Boolean.TRUE);
    assertEquals(bean.getValue(), new Long(1234));
  }
 
  public void testRequireByDefault() throws Exception {
    Map<String, String[]> m = new HashMap<String, String[]>();
    m.put("message", new String[] {"Hi there"});       
    Form f = formFactory.forModel(ContactBean.class);
    f.bind(m);   
    assertFalse(f.isValid());
    Map errors = f.getErrors();
    assertTrue(errors.containsKey("subject"));
    assertTrue(errors.containsKey("sender"));
    assertTrue(errors.containsKey("value"));
  }

  // This class adds a field that is not in the ContactBean class.
  public static class EnhancedContactForm extends ModelForm {
    public CharField newField;
    public LongField ccMyself;
    @Inject
    public EnhancedContactForm(FieldFactory fieldFactory) {
      super(fieldFactory);
      setModel(ContactBean.class);
    }
   
  }
  public void testModelFormAddField() throws Exception {
    Form form = injector.getInstance(EnhancedContactForm.class);
//    Form form = new EnhancedContactForm();
    assertTrue(form.getFields().containsKey("newField"));
    assertTrue(form.getFields().containsKey("ccMyself"));
    Field ccField = form.getFields().get("ccMyself");
    assertEquals(LongField.class, ccField.getClass());
  }
 

  // This class adds a field that is not in the ContactBean class.
  public static class NewContactForm extends ModelForm {
    public CharField newField;
    public LongField ccMyself;

    @Inject
    public NewContactForm(FieldFactory fieldFactory) {
      super(fieldFactory);
      setModel(ContactBean.class);
      setExclude("subject");
      init();
    }    
  }
 
  public void testNewContextForm() throws Exception {
    Form form = injector.getInstance(NewContactForm.class);
//    Form form = new EnhancedContactForm();
    assertTrue(form.getFields().containsKey("newField"));
    assertTrue(form.getFields().containsKey("ccMyself"));
    assertTrue(!form.getFields().containsKey("subject"));
    Field ccField = form.getFields().get("ccMyself");
    assertEquals(LongField.class, ccField.getClass());
  }
 
  public static class IncludeTestContactForm extends ModelForm {
    public CharField newField;
    public LongField ccMyself;

    @Inject
    public IncludeTestContactForm(FieldFactory fieldFactory) {
      super(fieldFactory);
      setModel(ContactBean.class);
      setInclude("subject");
      init();
    }    
  }
 
  public void testIncludeField() throws Exception {
    Form form = injector.getInstance(IncludeTestContactForm.class);
    assertEquals(3, form.getFields().size());
    assertTrue(form.getFields().containsKey("newField"));
    assertTrue(form.getFields().containsKey("ccMyself"));
    assertTrue(form.getFields().containsKey("subject"));
    assertTrue(!form.getFields().containsKey("sender"));

  }

  public void testBlankInclude() throws Exception {
    // to set the form to include no fields use setInclude(""),  should return an empty list
    ModelForm form = injector.getInstance(IncludeTestContactForm.class);
    form.setInclude("");
    assertEquals("", form.getInclude()[0]);

    assertEquals(0, form.include.size());

    form.setInclude(null);
    assertNull(form.getInclude());
    assertNull(form.include);
  }
   
}
TOP

Related Classes of javango.forms.ModelFormTest$IncludeTestContactForm

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.