Package javango.forms.fields

Source Code of javango.forms.fields.ChoiceFieldTests$TestForm

package javango.forms.fields;

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

import com.google.inject.Inject;

import javango.forms.AbstractForm;

import javango.forms.fields.BoundField;
import javango.forms.fields.Field;
import javango.forms.fields.FieldFactory;
import javango.forms.fields.NumberSelectField;
import javango.forms.fields.SelectField;
import javango.forms.fields.annotations.Choice;
import javango.forms.fields.annotations.ChoiceFieldProperties;
import javango.forms.fields.annotations.FieldProperties;
import javango.forms.tests.InjectedTestCaseBase;
import javango.forms.widgets.SelectWidget;
import javango.forms.widgets.WidgetFactory;


public class ChoiceFieldTests extends InjectedTestCaseBase {
 
  protected static class TestForm extends AbstractForm {
    @FieldProperties(allowNull=true)
    public SelectField f;

    @Inject
    public TestForm(FieldFactory fieldFactory) {
      super(fieldFactory);
      getFields();
      Map<String, String> choices = new LinkedHashMap<String, String>();
      choices.put("1", "1");
      choices.put("2", "2");
      choices.put("3", "3");
      choices.put("4", "4");
      this.f.setChoices(choices);
    }
  }
 
  public static class AnnotatedTestForm extends AbstractForm {

    @ChoiceFieldProperties(
        choices = {
            @Choice(key="1", value="1"),
            @Choice(key="2", value="2"),
            @Choice(key="3", value="3"),
            @Choice(key="4", value="4")
          }
    )
    public SelectField f;
   
    @Inject
    public AnnotatedTestForm(FieldFactory fieldFactory) {
      super(fieldFactory);
    }
   
  }
 
  public void testDefaultWidget() throws Exception {
    Field f = new SelectField(injector.getInstance(WidgetFactory.class));
    assertEquals(SelectWidget.class, f.getWidget().getClass());
  }

  public void testSelectedValue() throws Exception {
    TestForm form = injector.getInstance(TestForm.class);
    BoundField bf = new BoundField(form.getFields().get("f"), form, "f");
   
    Map<String, String[]> params = new HashMap<String, String[]>();
    params.put("f", new String[] {"1"});
    form.bind(params);
   
    String expected = "<select id=\"id_f\" name=\"f\">" +
        "<option value=\"\" >--</option>" +
        "<option value=\"1\" selected=\"selected\">1</option>" +
        "<option value=\"2\" >2</option>" +
        "<option value=\"3\" >3</option>" +
        "<option value=\"4\" >4</option>" +
        "</select>";
   
    assertEquals(expected, form.get("f").toString());
  }
 
  public void testRequired() throws Exception {
    Map<String, String> choices = new HashMap<String, String>();
    choices.put("1", "1");
    choices.put("2", "2");
    Field<String> f = new SelectField(injector.getInstance(WidgetFactory.class)).setChoices(choices).setName("choice_field");
   
    Map<String, String> errors = new HashMap<String, String>();
   
    f.clean(new String[]{""}, errors);
    assertEquals("This field is required.", errors.get("choice_field"));
   
    errors = new HashMap<String, String>();
    f.clean((String)null, errors);
    assertEquals("This field is required.", errors.get("choice_field"));
   
    errors = new HashMap<String, String>();
    assertEquals("1", f.clean(new String[]{"1"}, errors));
    assertTrue(errors.isEmpty());
   
    errors = new HashMap<String, String>();
    f.clean(new String[]{"3"}, errors);
    assertEquals("Select a valid choice. That choice is not one of the available choices.", errors.get("choice_field"));
  }
 
  public void testNotRequired() throws Exception {
    Map<String, String> choices = new HashMap<String, String>();
    choices.put("1", "1");
    choices.put("2", "2");
    Field<String> f = new SelectField(injector.getInstance(WidgetFactory.class)).setChoices(choices).setName("choice_field").setRequired(false);
   
    Map<String, String> errors = new HashMap<String, String>();
   
    assertEquals(null, f.clean(new String[]{""}, errors));
    assertTrue(errors.isEmpty());
   
    errors = new HashMap<String, String>();
    assertEquals(null, f.clean((String)null, errors));
    assertTrue(errors.isEmpty());
   
    errors = new HashMap<String, String>();
    assertEquals("1", f.clean(new String[]{"1"}, errors));
    assertTrue(errors.isEmpty());
   
    errors = new HashMap<String, String>();
    f.clean(new String[]{"3"}, errors);
    assertEquals("Select a valid choice. That choice is not one of the available choices.", errors.get("choice_field"));
  }
 
  public void testAllowNull() throws Exception {
    Map<String, String> choices = new HashMap<String, String>();
    choices.put("1", "1");
    choices.put("2", "2");
    Field<String> f = new SelectField(injector.getInstance(WidgetFactory.class)).setChoices(choices).setName("choice_field").setRequired(false).setAllowNull(true);
   
    Map<String, String> errors = new HashMap<String, String>();
   
    assertEquals(null, f.clean(new String[]{""}, errors));
    assertTrue(errors.isEmpty());
   
    errors = new HashMap<String, String>();
    assertEquals(null, f.clean((String)null, errors));
    assertTrue(errors.isEmpty());
   
    errors = new HashMap<String, String>();
    assertEquals("1", f.clean(new String[]{"1"}, errors));
    assertTrue(errors.isEmpty());
   
    errors = new HashMap<String, String>();
    f.clean(new String[]{"3"}, errors);
    assertEquals("Select a valid choice. That choice is not one of the available choices.", errors.get("choice_field"));   
  }
 
  public void testGetByValue() throws Exception {
    Map<String, String> choices = new HashMap<String, String>();
    choices.put("J", "John");
    choices.put("P", "Paul");
    Field<String> f = new SelectField(injector.getInstance(WidgetFactory.class)).setChoices(choices).setName("choice_field");
   
    Map<String, String> errors = new HashMap<String, String>();
   
    assertEquals("J", f.clean(new String[]{"J"}, errors));
    assertTrue(errors.isEmpty());
   
    errors = new HashMap<String, String>();
    f.clean(new String[]{"John"}, errors);
    assertEquals("Select a valid choice. That choice is not one of the available choices.", errors.get("choice_field"));   
  }
 
  public void testMultipleGetByValue() throws Exception {
    Map<String, String> choices = new HashMap<String, String>();
    choices.put("J", "John");
    choices.put("P", "Paul");
    Field<String[]> f = new MultipleSelectField(injector.getInstance(WidgetFactory.class)).setChoices(choices).setName("choice_field");
   
    Map<String, String> errors = new HashMap<String, String>();
    String[] values = (String[])f.clean(new String[]{"J"}, errors);
    assertEquals(1, values.length);
    assertTrue(errors.isEmpty());

    errors = new HashMap<String, String>();
    values = (String[])f.clean(new String[]{"J","P"}, errors);
    assertEquals(2, values.length);
    assertTrue(errors.isEmpty());


    errors = new HashMap<String, String>();
    f.clean(new String[]{"John"}, errors);
    assertEquals("Select a valid choice. That choice is not one of the available choices.", errors.get("choice_field"));   
  }
 
  public void testSelectedValueAnnotated() throws Exception {
    AnnotatedTestForm form = injector.getInstance(AnnotatedTestForm.class);
    BoundField bf = new BoundField(form.getFields().get("f"), form, "f");
   
    Map<String, String[]> params = new HashMap<String, String[]>();
    params.put("f", new String[] {"1"});
    form.bind(params);
   
    String expected = "<select id=\"id_f\" name=\"f\">" +
        "<option value=\"\" >--</option>" +
        "<option value=\"1\" selected=\"selected\">1</option>" +
        "<option value=\"2\" >2</option>" +
        "<option value=\"3\" >3</option>" +
        "<option value=\"4\" >4</option>" +
        "</select>";
   
    assertEquals(expected, form.get("f").toString());
   
  }
 
  public void testLongGetByValue() throws Exception {
    Map<Long, String> choices = new HashMap<Long, String>();
    choices.put(1L, "John");
    choices.put(2L, "Paul");
    Field f = (injector.getInstance(LongSelectField.class)).setChoices(choices).setName("choice_field");
   
    Map<String, String> errors = new HashMap<String, String>();
   
    assertEquals(1L, f.clean(new String[]{"1"}, errors));
    assertTrue(errors.isEmpty());

   
    errors = new HashMap<String, String>();
    f.clean(new String[]{"John"}, errors);
    assertEquals("Select a valid choice. That choice is not one of the available choices.", errors.get("choice_field"));   
  }
 
  public void testBlankIsAnOption() throws Exception {
    Map<String, String> choices = new HashMap<String, String>();
    choices.put(" ", "Blank");
    choices.put("P", "Paul");
    Field<String> f = injector.getInstance(SelectField.class).setChoices(choices).setName("choice_field");
   
    Map<String, String> errors = new HashMap<String, String>();
   
    assertEquals(" ", f.clean(new String[]{" "}, errors));
    assertTrue(errors.isEmpty());
   
    errors = new HashMap<String, String>();
    f.clean(new String[]{""}, errors);
    assertEquals(AbstractField.REQUIRED_ERROR, errors.get("choice_field"));         
  }
}
TOP

Related Classes of javango.forms.fields.ChoiceFieldTests$TestForm

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.