package javango.forms.tests;
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 com.google.inject.name.Names;
import javango.forms.AbstractForm;
import javango.forms.Form;
import javango.forms.fields.BooleanField;
import javango.forms.fields.CharField;
import javango.forms.fields.DoubleField;
import javango.forms.fields.EmailField;
import javango.forms.fields.Field;
import javango.forms.fields.FieldFactory;
import javango.forms.fields.HiddenField;
import javango.forms.fields.annotations.FieldProperties;
import junit.framework.TestCase;
public class TestHtmlOutput extends TestCase implements Module {
public void configure(Binder binder) {
}
Injector injector;
@Override
protected void setUp() throws Exception {
injector = Guice.createInjector(this);
super.setUp();
}
public static class ContactForm extends AbstractForm {
public CharField subject;
public CharField message;
public EmailField sender;
public DoubleField cost;
@FieldProperties(verboseName="Carbon Copy Myself")
public BooleanField ccMyself;
public Field value;
public Field hiddenField;
@Inject
public ContactForm(FieldFactory factory) {
super(factory);
value = factory.newField(CharField.class).setLabel("The Value");
hiddenField = factory.newField(HiddenField.class).setRequired(false);
}
}
public static class PhoneContactForm extends ContactForm {
public CharField phoneNumber;
@Inject
public PhoneContactForm(FieldFactory factory) {
super(factory);
}
}
public void testUnboundHtml() throws Exception {
Form f = injector.getInstance(ContactForm.class);
String expected = "<tr><th><label for='id_subject'>Subject</label></th><td><input id=\"id_subject\" type=\"text\" name=\"subject\" /></td></tr>\n" +
"<tr><th><label for='id_message'>Message</label></th><td><input id=\"id_message\" type=\"text\" name=\"message\" /></td></tr>\n" +
"<tr><th><label for='id_sender'>Sender</label></th><td><input id=\"id_sender\" type=\"text\" name=\"sender\" /></td></tr>\n" +
"<tr><th><label for='id_cost'>Cost</label></th><td><input id=\"id_cost\" type=\"text\" name=\"cost\" /></td></tr>\n" +
"<tr><th><label for='id_ccMyself'>Carbon Copy Myself</label></th><td><input id=\"id_ccMyself\" type=\"checkbox\" name=\"ccMyself\" /></td></tr>\n" +
"<tr><th><label for='id_value'>The Value</label></th><td><input id=\"id_value\" type=\"text\" name=\"value\" />" +
"<input id=\"id_hiddenField\" type=\"hidden\" name=\"hiddenField\" />\n</td></tr>\n";
assertEquals(expected, f.asTable());
}
public void testBasicHtml() 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("cost", new String[] {"1.23"});
m.put("ccMyself", new String[] {"true"});
m.put("value", new String[] {"1234"});
Form f = injector.getInstance(ContactForm.class).bind(m).setId(null);
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>Cost</th><td><input type=\"text\" name=\"cost\" value=\"1.23\" /></td></tr>\n" +
"<tr><th>Carbon Copy Myself</th><td><input type=\"checkbox\" name=\"ccMyself\" checked=\"checked\" /></td></tr>\n" +
"<tr><th>The Value</th><td><input type=\"text\" name=\"value\" value=\"1234\" />" +
"<input type=\"hidden\" name=\"hiddenField\" />\n</td></tr>\n";
assertEquals(expected, f.asTable());
}
public void testBasicError() throws Exception {
Map<String, String[]> m = new HashMap<String, String[]>();
m.put("hiddenField", new String[] {"hidden value"});
m.put("subject", new String[] {""});
m.put("message", new String[] {"Hi there"});
m.put("sender", new String[] {"foo@example.com"});
m.put("cost", new String[] {"1.23"});
m.put("ccMyself", new String[] {"true"});
m.put("value", new String[] {"1234"});
Form f = injector.getInstance(ContactForm.class).bind(m).setId(null);
f.isValid();
String expected = "<tr><th>Subject</th><td><ul class=\"errorlist\"><li>This field is required.</li></ul><input type=\"text\" name=\"subject\" /></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>Cost</th><td><input type=\"text\" name=\"cost\" value=\"1.23\" /></td></tr>\n" +
"<tr><th>Carbon Copy Myself</th><td><input type=\"checkbox\" name=\"ccMyself\" checked=\"checked\" /></td></tr>\n" +
"<tr><th>The Value</th><td><input type=\"text\" name=\"value\" value=\"1234\" />" +
"<input type=\"hidden\" name=\"hiddenField\" value=\"hidden value\" />\n</td></tr>\n";
assertEquals(expected, f.asTable());
}
public void testReadonly() throws Exception {
Form f = injector.getInstance(ContactForm.class);
f.getFields().get("sender").setEditable(false);
f.getFields().get("cost").setEditable(false);
String expected = "<tr><th><label for='id_subject'>Subject</label></th><td><input id=\"id_subject\" type=\"text\" name=\"subject\" /></td></tr>\n" +
"<tr><th><label for='id_message'>Message</label></th><td><input id=\"id_message\" type=\"text\" name=\"message\" /></td></tr>\n" +
"<tr><th><label for='id_sender'>Sender</label></th><td><input id=\"id_sender\" readonly=\"readonly\" type=\"text\" name=\"sender\" /></td></tr>\n" +
"<tr><th><label for='id_cost'>Cost</label></th><td><input id=\"id_cost\" readonly=\"readonly\" type=\"text\" name=\"cost\" /></td></tr>\n" +
"<tr><th><label for='id_ccMyself'>Carbon Copy Myself</label></th><td><input id=\"id_ccMyself\" type=\"checkbox\" name=\"ccMyself\" /></td></tr>\n" +
"<tr><th><label for='id_value'>The Value</label></th><td><input id=\"id_value\" type=\"text\" name=\"value\" />" +
"<input id=\"id_hiddenField\" type=\"hidden\" name=\"hiddenField\" />\n</td></tr>\n";
assertEquals(expected, f.asTable());
}
public void testMaxlength() throws Exception {
Form f = injector.getInstance(ContactForm.class);
((CharField)f.getFields().get("message")).setMaxLength(20);
String expected = "<tr><th><label for='id_subject'>Subject</label></th><td><input id=\"id_subject\" type=\"text\" name=\"subject\" /></td></tr>\n" +
"<tr><th><label for='id_message'>Message</label></th><td><input maxlength=\"20\" id=\"id_message\" type=\"text\" name=\"message\" /></td></tr>\n" +
"<tr><th><label for='id_sender'>Sender</label></th><td><input id=\"id_sender\" type=\"text\" name=\"sender\" /></td></tr>\n" +
"<tr><th><label for='id_cost'>Cost</label></th><td><input id=\"id_cost\" type=\"text\" name=\"cost\" /></td></tr>\n" +
"<tr><th><label for='id_ccMyself'>Carbon Copy Myself</label></th><td><input id=\"id_ccMyself\" type=\"checkbox\" name=\"ccMyself\" /></td></tr>\n" +
"<tr><th><label for='id_value'>The Value</label></th><td><input id=\"id_value\" type=\"text\" name=\"value\" />" +
"<input id=\"id_hiddenField\" type=\"hidden\" name=\"hiddenField\" />\n</td></tr>\n";
assertEquals(expected, f.asTable());
}
public void testTemplateAttrs() throws Exception {
Form f = injector.getInstance(ContactForm.class);
((CharField)f.getFields().get("message")).setMaxLength(20);
String expected = "<input maxlength=\"20\" size=\"20\" something=\"else\" id=\"id_message\" type=\"text\" name=\"message\" />";
assertEquals(expected, f.get("message").withAttrs("size=20, something=else"));
}
public void testInitial() throws Exception {
Form f = injector.getInstance(ContactForm.class);
f.getInitial().put("message", "myvalue");
String expected = "<input id=\"id_message\" type=\"text\" name=\"message\" value=\"myvalue\" />";
assertEquals(expected, f.get("message").toString());
}
public void testInitialQuoted() throws Exception {
Form f = injector.getInstance(ContactForm.class);
f.getInitial().put("message", "\"myvalue\"");
String expected = "<input id=\"id_message\" type=\"text\" name=\"message\" value=\""myvalue"\" />";
assertEquals(expected, f.get("message").toString());
}
public void testSuperClassHtml() 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("cost", new String[] {"1.23"});
m.put("ccMyself", new String[] {"true"});
m.put("phoneNumber", new String[] {"111-222-3333"});
m.put("value", new String[] {"1234"});
Form f = injector.getInstance(PhoneContactForm.class).bind(m).setId(null);
f.isValid();
String expected = "<tr><th>Phone Number</th><td><input type=\"text\" name=\"phoneNumber\" value=\"111-222-3333\" /></td></tr>\n" +
"<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>Cost</th><td><input type=\"text\" name=\"cost\" value=\"1.23\" /></td></tr>\n" +
"<tr><th>Carbon Copy Myself</th><td><input type=\"checkbox\" name=\"ccMyself\" checked=\"checked\" /></td></tr>\n" +
"<tr><th>The Value</th><td><input type=\"text\" name=\"value\" value=\"1234\" />" +
"<input type=\"hidden\" name=\"hiddenField\" />\n</td></tr>\n";
assertEquals(expected, f.asTable());
}
public static class PhoneContactFormWithCss extends ContactForm {
public CharField phoneNumber;
@Inject
public PhoneContactFormWithCss(FieldFactory factory) {
super(factory);
}
@Override
public String getErrorCssClass() {
return "error";
}
@Override
public String getRequiredCssClass() {
return "required";
}
}
public void testRequiredCss() 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("cost", new String[] {"1.23"});
m.put("ccMyself", new String[] {"true"});
m.put("phoneNumber", new String[] {"111-222-3333"});
m.put("value", new String[] {"1234"});
Form f = injector.getInstance(PhoneContactFormWithCss.class).bind(m).setId(null);
f.isValid();
String expected = "<tr class=\"required\"><th>Phone Number</th><td><input class=\"required\" type=\"text\" name=\"phoneNumber\" value=\"111-222-3333\" /></td></tr>\n" +
"<tr class=\"error required\"><th>Subject</th><td><ul class=\"errorlist\"><li>This field is required.</li></ul><input class=\"error required\" type=\"text\" name=\"subject\" /></td></tr>\n" +
"<tr class=\"required\"><th>Message</th><td><input class=\"required\" type=\"text\" name=\"message\" value=\"Hi there\" /></td></tr>\n" +
"<tr class=\"required\"><th>Sender</th><td><input class=\"required\" type=\"text\" name=\"sender\" value=\"foo@example.com\" /></td></tr>\n" +
"<tr class=\"required\"><th>Cost</th><td><input class=\"required\" type=\"text\" name=\"cost\" value=\"1.23\" /></td></tr>\n" +
"<tr class=\"required\"><th>Carbon Copy Myself</th><td><input class=\"required\" type=\"checkbox\" name=\"ccMyself\" checked=\"checked\" /></td></tr>\n" +
"<tr class=\"required\"><th>The Value</th><td><input class=\"required\" type=\"text\" name=\"value\" value=\"1234\" />" +
"<input type=\"hidden\" name=\"hiddenField\" />\n</td></tr>\n";
assertEquals(expected, f.asTable());
}
}