1 16 17 package org.apache.cocoon.forms.formmodel; 18 19 import org.apache.cocoon.core.container.ContainerTestCase; 20 import org.apache.cocoon.environment.mock.MockRequest; 21 import org.apache.cocoon.forms.FormContext; 22 import org.apache.cocoon.forms.event.ValueChangedEvent; 23 import org.apache.cocoon.forms.event.ValueChangedListener; 24 import org.w3c.dom.Document ; 25 26 31 32 public class FieldTestCase extends ContainerTestCase { 33 34 public static final String VALUE_PATH = "fi:fragment/fi:field/fi:value"; 35 public static final String VALIDATION_PATH = "fi:fragment/fi:field/fi:validation-message"; 36 37 38 41 public void testValueDoesParseAndValidate() throws Exception { 42 Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml"); 43 Field field = (Field)form.getChild("intfield"); 44 Action button = (Action)form.getChild("action"); 45 MockRequest request; 46 47 request = new MockRequest(); 48 request.addParameter("intfield", "11"); 49 request.addParameter("action", "pressed"); 50 form.process(new FormContext(request)); 51 52 Document doc = WidgetTestHelper.getWidgetFragment(field, null); 54 WidgetTestHelper.assertXPathEquals("Displayed value", "11", VALUE_PATH, doc); 55 WidgetTestHelper.assertXPathNotExists("Validation error", VALIDATION_PATH, doc); 56 57 assertEquals("Field value", new Integer (11), field.getValue()); 59 doc = WidgetTestHelper.getWidgetFragment(field, null); 61 WidgetTestHelper.assertXPathNotExists("Validation error", VALIDATION_PATH, doc); 62 63 assertTrue("Field does validate", field.validate()); 65 assertNull("getValidationError() null after validation", field.getValidationError()); 66 doc = WidgetTestHelper.getWidgetFragment(field, null); 67 WidgetTestHelper.assertXPathNotExists("Validation error", VALIDATION_PATH, doc); 68 } 69 70 73 public void testValueDoesNotParse() throws Exception { 74 Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml"); 75 Field field = (Field)form.getChild("intfield"); 76 Action button = (Action)form.getChild("action"); 77 MockRequest request; 78 79 request = new MockRequest(); 80 request.addParameter("intfield", "foo"); 81 request.addParameter("action", "pressed"); 82 form.process(new FormContext(request)); 83 84 Document doc = WidgetTestHelper.getWidgetFragment(field, null); 86 WidgetTestHelper.assertXPathEquals("Displayed velue", "foo", VALUE_PATH, doc); 87 WidgetTestHelper.assertXPathNotExists("Validation error before parse", VALIDATION_PATH, doc); 88 89 assertNull("Field value", field.getValue()); 91 doc = WidgetTestHelper.getWidgetFragment(field, null); 93 WidgetTestHelper.assertXPathEquals("Displayed value", "foo", VALUE_PATH, doc); 94 WidgetTestHelper.assertXPathNotExists("Validation error after parse", VALIDATION_PATH, doc); 95 96 assertFalse("Field validation", field.validate()); 98 doc = WidgetTestHelper.getWidgetFragment(field, null); 99 WidgetTestHelper.assertXPathEquals("Displayed velue", "foo", VALUE_PATH, doc); 100 WidgetTestHelper.assertXPathExists("Validation not null after parse", VALIDATION_PATH, doc); 101 assertNotNull("getValidationError() not null after validation", field.getValidationError()); 102 } 103 104 107 public void testValueDoesNotValidate() throws Exception { 108 Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml"); 109 Field field = (Field)form.getChild("intfield"); 110 Action button = (Action)form.getChild("action"); 111 MockRequest request; 112 113 request = new MockRequest(); 114 request.addParameter("intfield", "1"); 115 request.addParameter("action", "pressed"); 116 form.process(new FormContext(request)); 117 118 Document doc = WidgetTestHelper.getWidgetFragment(field, null); 120 WidgetTestHelper.assertXPathEquals("Displayed value", "1", VALUE_PATH, doc); 121 WidgetTestHelper.assertXPathNotExists("Validation error before parse", VALIDATION_PATH, doc); 122 123 assertNull("Field value", field.getValue()); 125 doc = WidgetTestHelper.getWidgetFragment(field, null); 127 WidgetTestHelper.assertXPathNotExists("Validation error after parse", VALIDATION_PATH, doc); 128 129 assertFalse("Field validation", field.validate()); 131 doc = WidgetTestHelper.getWidgetFragment(field, null); 132 WidgetTestHelper.assertXPathExists("Validation error after validation", VALIDATION_PATH, doc); 133 assertNotNull("getValidationError() not null after validation", field.getValidationError()); 134 } 135 136 141 public void testSetEmptyValueWhenValueChangedOnRequest() throws Exception { 142 Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml"); 143 Field field = (Field)form.getChild("stringfield"); 144 Action button = (Action)form.getChild("action"); 145 MockRequest request; 146 147 request = new MockRequest(); 150 request.addParameter("stringfield", "bar"); 151 request.addParameter("action", "pressed"); 152 form.process(new FormContext(request)); 153 154 assertEquals("Form submit widget", button, form.getSubmitWidget()); 156 157 field.setValue(""); 160 161 Document doc = WidgetTestHelper.getWidgetFragment(field, null); 163 WidgetTestHelper.assertXPathEquals("Displayed value", "", VALUE_PATH, doc); 164 assertEquals("Datatype string conversion", "", field.getDatatype().convertToString(field.value, null)); 165 assertEquals("Field value", "", (String )field.getValue()); 166 } 167 168 172 public void testOldValuePresentInEventEvenIfNotParsed() throws Exception { 173 Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml"); 174 Field field = (Field)form.getChild("stringfield"); 175 Action button = (Action)form.getChild("action"); 176 MockRequest request; 177 178 request = new MockRequest(); 181 request.addParameter("stringfield", "foo"); 182 request.addParameter("action", "pressed"); 183 form.process(new FormContext(request)); 184 185 field.addValueChangedListener(new ValueChangedListener (){ 187 public void valueChanged(ValueChangedEvent event) { 188 assertEquals("Old value", "foo", (String )event.getOldValue()); 189 assertEquals("New value", "bar", (String )event.getNewValue()); 190 } 191 }); 192 193 request = new MockRequest(); 196 request.addParameter("stringfield", "bar"); 197 request.addParameter("button", "pressed"); 198 form.process(new FormContext(request)); 199 } 200 201 204 public void testParameterNotReadWhenDisabled() throws Exception { 205 Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml"); 206 Field field = (Field)form.getChild("stringfield"); 207 MockRequest request; 208 209 form.setState(WidgetState.DISABLED); 211 field.setValue("foo"); 212 213 request = new MockRequest(); 214 request.addParameter("stringfield", "bar"); 215 form.process(new FormContext(request)); 216 217 assertEquals("foo", field.getValue()); 219 220 form.setState(WidgetState.ACTIVE); 222 form.process(new FormContext(request)); 223 224 assertEquals("bar", field.getValue()); 226 } 227 } 228 | Popular Tags |