Package javango.forms.fields

Source Code of javango.forms.fields.PercentFieldTest$Form

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.PercentField;
import javango.forms.tests.InjectedTestCaseBase;
import javango.forms.widgets.PercentInputWidget;
import javango.forms.widgets.Widget;

public class PercentFieldTest extends InjectedTestCaseBase {

  public void testRequired() throws Exception {
    PercentField field = injector.getInstance(PercentField.class);
    field.setName("fieldName");

    Map<String, String> errors = new HashMap<String, String>();
   
    Double value = field.clean(new String[]{""}, errors);
    assertFalse(errors.isEmpty());
    assertTrue(errors.containsKey(field.getName()));
    assertEquals(AbstractField.REQUIRED_ERROR, errors.get(field.getName()));
  }
 
  public void testNotRequired() throws Exception {
    PercentField field = injector.getInstance(PercentField.class);
    field.setName("fieldName");
    field.setRequired(false);
   
    Map<String, String> errors = new HashMap<String, String>();
   
    Double value = field.clean(new String[]{""}, errors);
    assertTrue(errors.isEmpty());
  }

  public void testInvalid() throws Exception {
    PercentField field = injector.getInstance(PercentField.class);
    field.setName("fieldName");
   
    Map<String, String> errors = new HashMap<String, String>();
   
    Double value = field.clean(new String[]{"qwert"}, errors);
    assertFalse(errors.isEmpty());
    assertTrue(errors.containsKey("fieldName"));
    assertEquals(PercentField.ERROR_NUMBER_NOT_VALID, errors.get("fieldName"));
  }

  public void testValid() throws Exception {
    PercentField field = injector.getInstance(PercentField.class);
    field.setName("fieldName");
   
    Map<String, String> errors = new HashMap<String, String>();
   
    Double value = field.clean(new String[]{"12"}, errors);
    assertTrue(errors.isEmpty());
    assertEquals(new Double(.12), value);
  }

  public void testHtmlNull() throws Exception {
    Widget w = injector.getInstance(PercentInputWidget.class);
    Double value = null;
    String output = w.render("field", value, new LinkedHashMap<String, Object>());
    String html = "<input type=\"text\" name=\"field\" />";
    assertEquals(html, output);
   
    assertEquals("", w.asText(value));   
  }
 
  public void testHtml0() throws Exception {
    PercentInputWidget w = injector.getInstance(PercentInputWidget.class);
    w.setDecimalPlaces(0);
    Double value = 0.0;
    String output = w.render("field", value, new LinkedHashMap<String, Object>());
    String html = "<input type=\"text\" name=\"field\" value=\"0\" />";
    assertEquals(html, output);
   
    assertEquals("0", w.asText(value));   
  }

  public void testHtmlValue() throws Exception {
    PercentInputWidget w = injector.getInstance(PercentInputWidget.class);
    w.setDecimalPlaces(0);
    Double value = .45;
    String output = w.render("field", value, new LinkedHashMap<String, Object>());
    String html = "<input type=\"text\" name=\"field\" value=\"45\" />";
    assertEquals(html, output);
   
    assertEquals("45", w.asText(value));   
  }

  public static class Form extends AbstractForm {

    public PercentField percent;
   
    @Inject
    public Form(FieldFactory fieldFactory) {
      super(fieldFactory);
    }
  }
 
  public void testForm() throws Exception {
    Form form = injector.getInstance(Form.class);
    Map<String, String[]> params = new HashMap<String, String[]>();
    params.put("percent", new String[] {"66"});
    form.bind(params);
   
    assertTrue(form.isValid());
    assertEquals(new Double(.66), form.getCleanedValue(form.percent));
   
    String html = "<tr><th><label for='id_percent'>Percent</label></th><td><input id=\"id_percent\" type=\"text\" name=\"percent\" value=\"66\" /></td></tr>\n";
    assertEquals(html, form.asTable());
  }
 
  public void testFormInitial() throws Exception {
    Form form = injector.getInstance(Form.class);
    Map<String, Object> initial = new HashMap<String, Object>();
    initial.put("percent", new Double(.66));
    form.setInitial(initial);
   
    String html = "<tr><th><label for='id_percent'>Percent</label></th><td><input id=\"id_percent\" type=\"text\" name=\"percent\" value=\"66\" /></td></tr>\n";
    assertEquals(html, form.asTable());
  }
}
TOP

Related Classes of javango.forms.fields.PercentFieldTest$Form

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.