package javango.contrib.hibernate.tests;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import com.google.inject.Guice;
import com.google.inject.Injector;
import javango.contrib.hibernate.HibernateModule;
import javango.contrib.hibernate.HibernateUtil;
import javango.contrib.hibernate.ModelFactory;
import javango.contrib.hibernate.ModelChoiceField;
import javango.contrib.hibernate.annotations.Filter;
import javango.contrib.hibernate.annotations.ModelChoiceFieldProperties;
import javango.db.Manager;
import javango.db.ManagerException;
import javango.db.Managers;
import javango.db.QuerySet;
import javango.forms.AbstractForm;
import javango.forms.Form;
import javango.forms.fields.FieldFactory;
import javango.forms.fields.annotations.FieldProperties;
import junit.framework.TestCase;
public class ModelChoiceFieldTest extends TestCase {
FieldFactory fields;
ModelFactory models;
HibernateUtil hibernateUtil;
Injector injector;
Managers managers;
@Override
protected void setUp() throws Exception {
super.setUp();
injector = Guice.createInjector(new HibernateModule());
AnnotationConfiguration cfg = injector.getInstance(HibernateUtil.class).getConfiguration();
new SchemaExport(cfg).drop(false, true);
new SchemaExport(cfg).create(false, true);
fields = injector.getInstance(FieldFactory.class);
models = injector.getInstance(ModelFactory.class);
managers = injector.getInstance(Managers.class);
hibernateUtil = injector.getInstance(HibernateUtil.class);
}
public void fixture(int ct) throws Exception {
injector.getInstance(HibernateUtil.class).getSession().beginTransaction();
Manager<Poll> polls = managers.forClass(Poll.class);
Manager<Choice> choices = managers.forClass(Choice.class);
for (int i=0; i<ct; i++) {
Poll p = new Poll();
p.setQuestion("AdminTestQuestion : " + i );
p.setPubDate(new Date());
polls.save(p);
Choice c = new Choice();
c.setChoice("Choice A " + i);
c.setPoll(p);
c.setVotes(0L);
choices.save(c);
c = new Choice();
c.setChoice("Choice B " + i);
c.setPoll(p);
c.setVotes(2L);
choices.save(c);
}
injector.getInstance(HibernateUtil.class).getSession().getTransaction().commit();
}
public class TestForm extends AbstractForm {
@FieldProperties(required=false, allowNull=true)
@ModelChoiceFieldProperties(modelClass=Poll.class)
public ModelChoiceField<Poll> mcf;
@ModelChoiceFieldProperties(modelClass=Poll.class)
public ModelChoiceField<Poll> mcfAnnotated;
public TestForm(FieldFactory fieldFactory) {
super(fieldFactory);
}
}
public void testEmptyChoices() throws Exception {
TestForm form = new TestForm(fields);
Map<String, String[]> values = new HashMap<String, String[]>();
values.put("mcf", new String[] {"1"});
values.put("mcfAnnotated", new String[] {"1"});
form.bind(values);
assertFalse(form.isValid());
}
public void testChoices() throws Exception {
TestForm form = new TestForm(fields);
Map<String, String[]> values = new HashMap<String, String[]>();
values.put("mcf", new String[] {"1"});
values.put("mcfAnnotated", new String[] {"1"});
form.bind(values);
fixture(2);
assertTrue(form.isValid());
Poll p = (Poll)form.getCleanedValue(form.mcf);
assertEquals((Long)1L, p.getId());
}
public void testAllowNull() throws Exception {
TestForm form = new TestForm(fields);
Map<String, String[]> values = new HashMap<String, String[]>();
values.put("mcf", new String[] {""});
values.put("mcfAnnotated", new String[] {"1"});
form.bind(values);
fixture(2);
assertTrue(form.isValid());
assertNull(form.getCleanedValue(form.mcf));
}
public void testRequired() throws Exception {
TestForm form = new TestForm(fields);
form.getFields();
form.mcf.setRequired(true).setAllowNull(false);
Map<String, String[]> values = new HashMap<String, String[]>();
values.put("mcf", new String[] {""});
values.put("mcfAnnotated", new String[] {"1"});
form.bind(values);
fixture(2);
assertFalse(form.isValid());
assertTrue(form.getErrors().containsKey("mcf"));
assertEquals("This field is required.", form.getErrors().get("mcf"));
}
public void testInitial() throws Exception {
fixture(2);
TestForm form = new TestForm(fields);
Poll poll = (Poll)injector.getInstance(Managers.class).forClass(Poll.class).get(1L);
assertNotNull(poll);
form.getInitial().put("mcf", poll);
form.getFields().get("mcfAnnotated").setInitial(poll);
String expected = "<tr><th><label for='id_mcf'>Mcf</label></th><td><select id=\"id_mcf\" name=\"mcf\">" +
"<option value=\"\" >--</option>" +
"<option value=\"1\" selected=\"selected\">AdminTestQuestion : 0</option>" +
"<option value=\"2\" >AdminTestQuestion : 1</option></select></td></tr>\n" +
"<tr><th><label for='id_mcfAnnotated'>Mcf Annotated</label></th><td><select id=\"id_mcfAnnotated\" name=\"mcfAnnotated\">" +
"<option value=\"\" >--</option>" +
"<option value=\"1\" selected=\"selected\">AdminTestQuestion : 0</option>" +
"<option value=\"2\" >AdminTestQuestion : 1</option></select></td></tr>\n";
assertEquals(expected, form.asTable());
}
public void testReadonly() throws Exception {
fixture(2);
TestForm form = new TestForm(fields);
form.setReadOnly(true);
Poll poll = (Poll)injector.getInstance(Managers.class).forClass(Poll.class).get(1L);
assertNotNull(poll);
form.getInitial().put("mcf", poll);
form.getInitial().put("mcfAnnotated", poll);
String expected = "<tr><th><label for='id_mcf'>Mcf</label></th><td>AdminTestQuestion : 0</td></tr>\n" +
"<tr><th><label for='id_mcfAnnotated'>Mcf Annotated</label></th><td>AdminTestQuestion : 0</td></tr>\n";
assertEquals(expected, form.asTable());
}
public void testRedrawSelected() throws Exception {
fixture(2);
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("mcf", new String[]{"1"});
params.put("mcfAnnotated", new String[]{"1"});
TestForm form = new TestForm(fields);
form.bind(params);
assertTrue(form.isValid());
String expected = "<tr><th><label for='id_mcf'>Mcf</label></th><td><select id=\"id_mcf\" name=\"mcf\">" +
"<option value=\"\" >--</option>" +
"<option value=\"1\" selected=\"selected\">AdminTestQuestion : 0</option>" +
"<option value=\"2\" >AdminTestQuestion : 1</option></select></td></tr>\n" +
"<tr><th><label for='id_mcfAnnotated'>Mcf Annotated</label></th><td><select id=\"id_mcfAnnotated\" name=\"mcfAnnotated\">" +
"<option value=\"\" >--</option>" +
"<option value=\"1\" selected=\"selected\">AdminTestQuestion : 0</option>" +
"<option value=\"2\" >AdminTestQuestion : 1</option></select></td></tr>\n";
assertEquals(expected, form.asTable());
}
public void testForInstance() throws Exception {
fixture(2);
Choice c = (Choice)managers.forClass(Choice.class).get(1L);
assertNotNull(c);
Form form = models.forInstance(c);
String expected = "<tr><th><label for='id_choice'>Choice</label></th><td><input maxlength=\"255\" id=\"id_choice\" type=\"text\" name=\"choice\" value=\"Choice A 0\" /></td></tr>\n" +
"<tr><th><label for='id_poll'>Poll</label></th><td><select id=\"id_poll\" name=\"poll\"><option value=\"\" >--</option>" +
"<option value=\"1\" selected=\"selected\">AdminTestQuestion : 0</option>" +
"<option value=\"2\" >AdminTestQuestion : 1</option>" +
"</select></td></tr>\n" +
"<tr><th><label for='id_votes'>Votes</label></th><td><input id=\"id_votes\" type=\"text\" name=\"votes\" value=\"0\" /></td></tr>\n";
assertEquals(expected, form.asTable());
}
public class TestFormQuerySet extends AbstractForm {
public ModelChoiceField<Choice> mcf = fields.newField(ModelChoiceField.class).setModel(Choice.class);
public TestFormQuerySet(FieldFactory fieldFactory) {
super(fieldFactory);
try {
Manager<Choice> polls = managers.forClass(Choice.class);
QuerySet<Choice> qs = polls.filter("choice__like", "Choice B%");
mcf.setQuerySet(qs);
} catch (ManagerException e) {
throw new RuntimeException(e);
}
}
}
public void testQuerySet() throws Exception {
fixture(2);
TestFormQuerySet form = new TestFormQuerySet(fields);
String expected = "<tr><th><label for='id_mcf'>Mcf</label></th>" +
"<td>" +
"<select id=\"id_mcf\" name=\"mcf\">" +
"<option value=\"\" >--</option>" +
"<option value=\"2\" >Choice B 0</option>" +
"<option value=\"4\" >Choice B 1</option>" +
"</select>" +
"</td></tr>\n";
assertEquals(expected, form.asTable());
}
public void testQuerySetGoodChoice() throws Exception {
fixture(2);
TestFormQuerySet form = new TestFormQuerySet(fields);
Map<String, String[]> values = new HashMap<String, String[]>();
values.put("mcf", new String[] {"2"});
form.bind(values);
assertTrue(form.isValid());
}
public void testQuerySetBadChoice() throws Exception {
fixture(2);
TestFormQuerySet form = new TestFormQuerySet(fields);
Map<String, String[]> values = new HashMap<String, String[]>();
values.put("mcf", new String[] {"1"});
form.bind(values);
// TODO
// assertFalse(form.isValid());
// assertEquals("Select a valid choice. That choice is not one of the available choices.", form.getErrors().get("mcf"));
}
public class TestFormQuerySetAnnotated extends AbstractForm {
@ModelChoiceFieldProperties(modelClass=Choice.class,
filters = {@Filter(property="choice__like", value="Choice B%")},
orderby = "-choice"
)
public ModelChoiceField mcf;
@ModelChoiceFieldProperties(modelClass=Choice.class,
filters = {@Filter(property="choice__in", value={"Choice B 1", "Choice A 1"})},
orderby = "-choice"
)
public ModelChoiceField mcf_in;
@ModelChoiceFieldProperties(modelClass=Choice.class,
filters = {@Filter(property="id", otherProperty="votes")},
orderby = "-choice"
)
public ModelChoiceField mcf_property;
public TestFormQuerySetAnnotated(FieldFactory fieldFactory) {
super(fieldFactory);
}
}
public void testQuerySetAnnotated() throws Exception {
fixture(2);
TestFormQuerySetAnnotated form = new TestFormQuerySetAnnotated(fields);
String expected = "<tr><th><label for='id_mcf'>Mcf</label></th>" +
"<td>" +
"<select id=\"id_mcf\" name=\"mcf\">" +
"<option value=\"\" >--</option>" +
"<option value=\"4\" >Choice B 1</option>" +
"<option value=\"2\" >Choice B 0</option>" +
"</select>" +
"</td></tr>\n" +
"<tr><th><label for='id_mcf_in'>Mcf _in</label></th>" +
"<td>" +
"<select id=\"id_mcf_in\" name=\"mcf_in\">" +
"<option value=\"\" >--</option>" +
"<option value=\"4\" >Choice B 1</option>" +
"<option value=\"3\" >Choice A 1</option>" +
"</select>" +
"</td></tr>\n" +
"<tr><th><label for='id_mcf_property'>Mcf _property</label></th>" +
"<td>" +
"<select id=\"id_mcf_property\" name=\"mcf_property\">" +
"<option value=\"\" >--</option>" +
"<option value=\"2\" >Choice B 0</option>" +
"</select>" +
"</td></tr>\n";
assertEquals(expected, form.asTable());
}
public void testQuerySetGoodChoiceAnnotated() throws Exception {
fixture(2);
TestFormQuerySetAnnotated form = new TestFormQuerySetAnnotated(fields);
Map<String, String[]> values = new HashMap<String, String[]>();
values.put("mcf", new String[] {"2"});
values.put("mcf_in", new String[] {"3"});
values.put("mcf_property", new String[] {"2"});
form.bind(values);
assertTrue(form.isValid());
}
public void testQuerySetBadChoiceAnnotated() throws Exception {
fixture(2);
TestFormQuerySetAnnotated form = new TestFormQuerySetAnnotated(fields);
Map<String, String[]> values = new HashMap<String, String[]>();
values.put("mcf", new String[] {"1"});
form.bind(values);
// TODO
// assertFalse(form.isValid());
// assertEquals("Select a valid choice. That choice is not one of the available choices.", form.getErrors().get("mcf"));
}
public class TestFormQuerySetAnnotatedNulls extends AbstractForm {
@ModelChoiceFieldProperties(modelClass=Choice.class,
filters = {@Filter(property="choice", nullCheck=true)},
orderby = "-choice"
)
public ModelChoiceField mcf_null;
@ModelChoiceFieldProperties(modelClass=Choice.class,
filters = {@Filter(property="choice__ne", nullCheck=true)},
orderby = "-choice"
)
public ModelChoiceField mcf_notnull;
public TestFormQuerySetAnnotatedNulls(FieldFactory fieldFactory) {
super(fieldFactory);
}
}
public void testQuerySetAnnotatedNulls() throws Exception {
fixture(2);
TestFormQuerySetAnnotatedNulls form = new TestFormQuerySetAnnotatedNulls(fields);
String expected = "<tr><th><label for='id_mcf_null'>Mcf _null</label></th>" +
"<td>" +
"<select id=\"id_mcf_null\" name=\"mcf_null\">" +
"<option value=\"\" >--</option>" +
"</select>" +
"</td></tr>\n" +
"<tr><th><label for='id_mcf_notnull'>Mcf _notnull</label></th>" +
"<td>" +
"<select id=\"id_mcf_notnull\" name=\"mcf_notnull\">" +
"<option value=\"\" >--</option>" +
"<option value=\"4\" >Choice B 1</option>" +
"<option value=\"2\" >Choice B 0</option>" +
"<option value=\"3\" >Choice A 1</option>" +
"<option value=\"1\" >Choice A 0</option>" +
"</select>" +
"</td></tr>\n";
assertEquals(expected, form.asTable());
}
}