Source Code Cross Referenced for FieldTestCase.java in  » Content-Management-System » apache-lenya-2.0 » org » apache » cocoon » forms » formmodel » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » apache lenya 2.0 » org.apache.cocoon.forms.formmodel 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.cocoon.forms.formmodel;
019:
020:        import org.apache.cocoon.core.container.ContainerTestCase;
021:        import org.apache.cocoon.environment.mock.MockRequest;
022:        import org.apache.cocoon.forms.FormContext;
023:        import org.apache.cocoon.forms.event.ValueChangedEvent;
024:        import org.apache.cocoon.forms.event.ValueChangedListener;
025:        import org.w3c.dom.Document;
026:
027:        /**
028:         * Test case for CForm's Field widget
029:         *
030:         * @version $Id: FieldTestCase.java 449149 2006-09-23 03:58:05Z crossley $
031:         */
032:
033:        public class FieldTestCase extends ContainerTestCase {
034:
035:            public static final String VALUE_PATH = "fi:fragment/fi:field/fi:value";
036:            public static final String VALIDATION_PATH = "fi:fragment/fi:field/fi:validation-message";
037:
038:            /**
039:             * Nominal test where the request data is syntactically correct and validates
040:             */
041:            public void testValueDoesParseAndValidate() throws Exception {
042:                Form form = WidgetTestHelper.loadForm(getManager(), this ,
043:                        "FieldTestCase.model.xml");
044:                Field field = (Field) form.getChild("intfield");
045:                Action button = (Action) form.getChild("action");
046:                MockRequest request;
047:
048:                request = new MockRequest();
049:                request.addParameter("intfield", "11");
050:                request.addParameter("action", "pressed");
051:                form.process(new FormContext(request));
052:
053:                // No parsing nor validation where performed
054:                Document doc = WidgetTestHelper.getWidgetFragment(field, null);
055:                WidgetTestHelper.assertXPathEquals("Displayed value", "11",
056:                        VALUE_PATH, doc);
057:                WidgetTestHelper.assertXPathNotExists("Validation error",
058:                        VALIDATION_PATH, doc);
059:
060:                // Now do some parsing.
061:                assertEquals("Field value", new Integer(11), field.getValue());
062:                // And still no validation error (do not call getValidationError() as it does validate)
063:                doc = WidgetTestHelper.getWidgetFragment(field, null);
064:                WidgetTestHelper.assertXPathNotExists("Validation error",
065:                        VALIDATION_PATH, doc);
066:
067:                // Now validate
068:                assertTrue("Field does validate", field.validate());
069:                assertNull("getValidationError() null after validation", field
070:                        .getValidationError());
071:                doc = WidgetTestHelper.getWidgetFragment(field, null);
072:                WidgetTestHelper.assertXPathNotExists("Validation error",
073:                        VALIDATION_PATH, doc);
074:            }
075:
076:            /**
077:             * Request data is not syntactically correct
078:             */
079:            public void testValueDoesNotParse() throws Exception {
080:                Form form = WidgetTestHelper.loadForm(getManager(), this ,
081:                        "FieldTestCase.model.xml");
082:                Field field = (Field) form.getChild("intfield");
083:                Action button = (Action) form.getChild("action");
084:                MockRequest request;
085:
086:                request = new MockRequest();
087:                request.addParameter("intfield", "foo");
088:                request.addParameter("action", "pressed");
089:                form.process(new FormContext(request));
090:
091:                // No parsing nor validation where performed
092:                Document doc = WidgetTestHelper.getWidgetFragment(field, null);
093:                WidgetTestHelper.assertXPathEquals("Displayed velue", "foo",
094:                        VALUE_PATH, doc);
095:                WidgetTestHelper.assertXPathNotExists(
096:                        "Validation error before parse", VALIDATION_PATH, doc);
097:
098:                // Now do some parsing. Will return null as it's not parseable
099:                assertNull("Field value", field.getValue());
100:                // But still no validation error
101:                doc = WidgetTestHelper.getWidgetFragment(field, null);
102:                WidgetTestHelper.assertXPathEquals("Displayed value", "foo",
103:                        VALUE_PATH, doc);
104:                WidgetTestHelper.assertXPathNotExists(
105:                        "Validation error after parse", VALIDATION_PATH, doc);
106:
107:                // Now validate
108:                assertFalse("Field validation", field.validate());
109:                doc = WidgetTestHelper.getWidgetFragment(field, null);
110:                WidgetTestHelper.assertXPathEquals("Displayed velue", "foo",
111:                        VALUE_PATH, doc);
112:                WidgetTestHelper
113:                        .assertXPathExists("Validation not null after parse",
114:                                VALIDATION_PATH, doc);
115:                assertNotNull("getValidationError() not null after validation",
116:                        field.getValidationError());
117:            }
118:
119:            /**
120:             * Request data is syntactically correct but doesn't validate
121:             */
122:            public void testValueDoesNotValidate() throws Exception {
123:                Form form = WidgetTestHelper.loadForm(getManager(), this ,
124:                        "FieldTestCase.model.xml");
125:                Field field = (Field) form.getChild("intfield");
126:                Action button = (Action) form.getChild("action");
127:                MockRequest request;
128:
129:                request = new MockRequest();
130:                request.addParameter("intfield", "1");
131:                request.addParameter("action", "pressed");
132:                form.process(new FormContext(request));
133:
134:                // No parsing nor validation where performed
135:                Document doc = WidgetTestHelper.getWidgetFragment(field, null);
136:                WidgetTestHelper.assertXPathEquals("Displayed value", "1",
137:                        VALUE_PATH, doc);
138:                WidgetTestHelper.assertXPathNotExists(
139:                        "Validation error before parse", VALIDATION_PATH, doc);
140:
141:                // Now do some parsing. Will return null although syntactically correct as it's invalid
142:                assertNull("Field value", field.getValue());
143:                // But still no validation error
144:                doc = WidgetTestHelper.getWidgetFragment(field, null);
145:                WidgetTestHelper.assertXPathNotExists(
146:                        "Validation error after parse", VALIDATION_PATH, doc);
147:
148:                // Now validate
149:                assertFalse("Field validation", field.validate());
150:                doc = WidgetTestHelper.getWidgetFragment(field, null);
151:                WidgetTestHelper.assertXPathExists(
152:                        "Validation error after validation", VALIDATION_PATH,
153:                        doc);
154:                assertNotNull("getValidationError() not null after validation",
155:                        field.getValidationError());
156:            }
157:
158:            /**
159:             * Test that a field's value is properly set by a call to setValue("") with an
160:             * empty string when the field is in unparsed state (there used to be a bug in
161:             * that case)
162:             */
163:            public void testSetEmptyValueWhenValueChangedOnRequest()
164:                    throws Exception {
165:                Form form = WidgetTestHelper.loadForm(getManager(), this ,
166:                        "FieldTestCase.model.xml");
167:                Field field = (Field) form.getChild("stringfield");
168:                Action button = (Action) form.getChild("action");
169:                MockRequest request;
170:
171:                // Set a value in stringfield and submit with an action
172:                // (no validation, thus no call to doParse())
173:                request = new MockRequest();
174:                request.addParameter("stringfield", "bar");
175:                request.addParameter("action", "pressed");
176:                form.process(new FormContext(request));
177:
178:                // Verify submit widget, just to be sure that validation did not occur
179:                assertEquals("Form submit widget", button, form
180:                        .getSubmitWidget());
181:
182:                // Set the value to an empty string. In that case, a faulty test made
183:                // it actually ignore it when state was VALUE_UNPARSED
184:                field.setValue("");
185:
186:                // Check value by various means
187:                Document doc = WidgetTestHelper.getWidgetFragment(field, null);
188:                WidgetTestHelper.assertXPathEquals("Displayed value", "",
189:                        VALUE_PATH, doc);
190:                assertEquals("Datatype string conversion", "", field
191:                        .getDatatype().convertToString(field.value, null));
192:                assertEquals("Field value", "", (String) field.getValue());
193:            }
194:
195:            /**
196:             * Test that the previous field value is correctly passed to event listeners
197:             * even if it was not already parsed.
198:             */
199:            public void testOldValuePresentInEventEvenIfNotParsed()
200:                    throws Exception {
201:                Form form = WidgetTestHelper.loadForm(getManager(), this ,
202:                        "FieldTestCase.model.xml");
203:                Field field = (Field) form.getChild("stringfield");
204:                Action button = (Action) form.getChild("action");
205:                MockRequest request;
206:
207:                // Set a value on "stringfield", and submit using an action so that
208:                // it stays in unparsed state
209:                request = new MockRequest();
210:                request.addParameter("stringfield", "foo");
211:                request.addParameter("action", "pressed");
212:                form.process(new FormContext(request));
213:
214:                // Now add an event listener that will check old an new value
215:                field.addValueChangedListener(new ValueChangedListener() {
216:                    public void valueChanged(ValueChangedEvent event) {
217:                        assertEquals("Old value", "foo", (String) event
218:                                .getOldValue());
219:                        assertEquals("New value", "bar", (String) event
220:                                .getNewValue());
221:                    }
222:                });
223:
224:                // Change value to "bar", still without explicit validation
225:                // That will call the event listener
226:                request = new MockRequest();
227:                request.addParameter("stringfield", "bar");
228:                request.addParameter("button", "pressed");
229:                form.process(new FormContext(request));
230:            }
231:
232:            /**
233:             * Request parameters are not read when a field is not in active state
234:             */
235:            public void testParameterNotReadWhenDisabled() throws Exception {
236:                Form form = WidgetTestHelper.loadForm(getManager(), this ,
237:                        "FieldTestCase.model.xml");
238:                Field field = (Field) form.getChild("stringfield");
239:                MockRequest request;
240:
241:                // Disable the form
242:                form.setState(WidgetState.DISABLED);
243:                field.setValue("foo");
244:
245:                request = new MockRequest();
246:                request.addParameter("stringfield", "bar");
247:                form.process(new FormContext(request));
248:
249:                // Check that "bar" was not read
250:                assertEquals("foo", field.getValue());
251:
252:                // Switch back to active and resumbit the same request
253:                form.setState(WidgetState.ACTIVE);
254:                form.process(new FormContext(request));
255:
256:                // Should have changed now
257:                assertEquals("bar", field.getValue());
258:            }
259:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.