Package javango.forms.fields

Source Code of javango.forms.fields.NumericChoiceFieldTests

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 NumericChoiceFieldTests extends InjectedTestCaseBase {
 
  protected class TestForm extends AbstractForm {
    @FieldProperties(allowNull=true)
    public IntegerSelectField f = new IntegerSelectField(injector.getInstance(WidgetFactory.class));

    public TestForm() {
      super(injector.getInstance(FieldFactory.class));
     
      Map<Integer, String> choices = new LinkedHashMap<Integer, 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 IntegerSelectField 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 = new TestForm();
    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 testInitialValue() throws Exception {
    TestForm form = new TestForm();
    BoundField bf = new BoundField(form.getFields().get("f"), form, "f");
   
    Map<String, Object> initial = new HashMap<String, Object>();
    initial.put("f", 1);
    form.setInitial(initial);
   
    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());
  }
//  TODO
//  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"));   
  }
}
TOP

Related Classes of javango.forms.fields.NumericChoiceFieldTests

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.