Source Code Cross Referenced for InputGroupTest.java in  » Development » jReform » test » org » jreform » 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 » Development » jReform » test.org.jreform 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package test.org.jreform;
002:
003:        import static org.jreform.criteria.Criteria.emailAddress;
004:        import static org.jreform.types.StringType.stringType;
005:
006:        import org.jreform.DuplicateNameException;
007:        import org.jreform.Group;
008:        import org.jreform.HtmlForm;
009:        import org.jreform.Input;
010:
011:        public class InputGroupTest extends BaseTestCase {
012:            private static final String CONTACT = "contact";
013:            private static final String METRIC = "metric";
014:            private static final String IMPERIAL = "imperial";
015:            private static final String MISSING_HEIGHT_WEIGHT = "missingHeightWeight";
016:
017:            private static final String SURNAME = "surname";
018:            private static final String PHONE = "phone";
019:            private static final String EMAIL = "email";
020:
021:            private static final String HEIGHT_M = "heightMetres";
022:            private static final String WEIGHT_KG = "weightKg";
023:
024:            private static final String HEIGHT_FT = "heightFt";
025:            private static final String HEIGHT_IN = "heightInches";
026:            private static final String WEIGHT_LB = "weightLbs";
027:
028:            private TestForm form;
029:
030:            protected void init() {
031:                form = new TestForm();
032:            }
033:
034:            protected HtmlForm getForm() {
035:                return form;
036:            }
037:
038:            /** test add duplicate input throws exception */
039:            public void testAddDuplicateInputThrowsException() {
040:                try {
041:                    // add to the form
042:                    form.input(stringType(), SURNAME);
043:                    fail("cannot add inputs with identical names");
044:                } catch (DuplicateNameException ex) { /* empty */
045:                }
046:
047:                try {
048:                    // add to another group
049:                    form.getGroup(METRIC).input(stringType(), SURNAME);
050:                    fail("cannot add inputs with identical names");
051:                } catch (DuplicateNameException ex) { /* empty */
052:                }
053:
054:            }
055:
056:            /** test input instance is the same from form and group */
057:            public void testInputInstanceIsTheSameFromFormAndGroup() {
058:                setContactGroupWithValidData();
059:                setMetric("1.9", "95");
060:
061:                assertTrue(validateForm());
062:                assertSame(form.getInput(EMAIL), form.getGroup(CONTACT)
063:                        .getInput(EMAIL));
064:            }
065:
066:            /** validation fails if contact group is empty */
067:            public void testValidationFailsIfContactGroupIsEmpty() {
068:                setMetric("1.9", "95");
069:
070:                assertFalse(validateForm());
071:
072:                assertTrue(form.getGroup(CONTACT).isEmpty());
073:                assertFalse(form.getGroup(CONTACT).isValid());
074:
075:                assertFalse(form.getGroup(METRIC).isEmpty());
076:                assertTrue(form.getGroup(METRIC).isValid());
077:
078:                assertTrue(form.getGroup(IMPERIAL).isEmpty());
079:                assertTrue(form.getGroup(IMPERIAL).isValid());
080:
081:                assertFalse(form.getErrors().contains(MISSING_HEIGHT_WEIGHT));
082:            }
083:
084:            /** test additonal validation fails if both optional groups are empty */
085:            public void testAdditionalValidationFailsIfBothOptionalGroupsAreEmpty() {
086:                setContactGroupWithValidData();
087:
088:                assertFalse(validateForm());
089:                assertTrue(form.getGroup(METRIC).isEmpty());
090:                assertTrue(form.getGroup(IMPERIAL).isEmpty());
091:                assertTrue(form.getErrors().contains(MISSING_HEIGHT_WEIGHT));
092:            }
093:
094:            /** validation passes if metric group has valid data */
095:            public void testValidationPassesIfMetricGroupHasValidData() {
096:                final double heightM = 1.75;
097:                final int weightKg = 65;
098:
099:                setContactGroupWithValidData();
100:
101:                setMetric(String.valueOf(heightM), String.valueOf(weightKg));
102:
103:                assertTrue(validateForm());
104:                assertTrue(form.getGroup(METRIC).isValid());
105:                assertTrue(form.getGroup(IMPERIAL).isValid());
106:
107:                assertTrue(form.heightM().getValue() == heightM);
108:                assertTrue(form.weightKg().getValue() == weightKg);
109:            }
110:
111:            /** validation passes if imperial group has valid data */
112:            public void testValidationPassesIfImperialGroupHasValidData() {
113:                final int heightFt = 5;
114:                final int heightIn = 7;
115:                final int weightLb = 135;
116:
117:                setContactGroupWithValidData();
118:
119:                setImperial(String.valueOf(heightFt), String.valueOf(heightIn),
120:                        String.valueOf(weightLb));
121:
122:                assertTrue(validateForm());
123:                assertTrue(form.getGroup(METRIC).isValid());
124:                assertTrue(form.getGroup(IMPERIAL).isValid());
125:
126:                assertTrue(form.heightFt().getValue() == heightFt);
127:                assertTrue(form.heightIn().getValue() == heightIn);
128:                assertTrue(form.weightLb().getValue() == weightLb);
129:            }
130:
131:            /** validation fails if metric group has invalid data */
132:            public void testValidationFailsIfMetricGroupHasInvalidData() {
133:                final double heightM = 1.75;
134:
135:                setContactGroupWithValidData();
136:
137:                setMetric(String.valueOf(heightM), "this should be a number");
138:
139:                assertFalse(validateForm());
140:
141:                assertTrue(form.getGroup(CONTACT).isValid());
142:                assertFalse(form.getGroup(CONTACT).isEmpty());
143:
144:                assertFalse(form.getGroup(METRIC).isValid());
145:                assertFalse(form.getGroup(METRIC).isEmpty());
146:
147:                assertTrue(form.getGroup(IMPERIAL).isValid());
148:                assertTrue(form.getGroup(IMPERIAL).isEmpty());
149:
150:                assertTrue(form.heightM().getValue() == heightM);
151:            }
152:
153:            /** validation fails if contact group has invalid data */
154:            public void testValidationFailsIfContactGroupHasInvalidData() {
155:                setContact("Adam", "Smith",
156:                        "this should be a valid email address");
157:
158:                setMetric("1.55", "65");
159:
160:                assertFalse(validateForm());
161:
162:                assertFalse(form.getGroup(CONTACT).isValid());
163:                assertFalse(form.getGroup(CONTACT).isEmpty());
164:                assertFalse(form.getInput(EMAIL).isValid());
165:
166:                assertTrue(form.getGroup(METRIC).isValid());
167:                assertFalse(form.getGroup(METRIC).isEmpty());
168:
169:                assertTrue(form.getGroup(IMPERIAL).isValid());
170:                assertTrue(form.getGroup(IMPERIAL).isEmpty());
171:            }
172:
173:            private void setContactGroupWithValidData() {
174:                setContact("Adam", "Smith", "adam@smith.com");
175:            }
176:
177:            private void setContact(String surname, String phone, String email) {
178:                setParameter(SURNAME, surname);
179:                setParameter(PHONE, phone);
180:                setParameter(EMAIL, email);
181:            }
182:
183:            private void setMetric(String m, String kg) {
184:                setParameter(HEIGHT_M, m);
185:                setParameter(WEIGHT_KG, kg);
186:            }
187:
188:            private void setImperial(String ft, String in, String lb) {
189:                setParameter(HEIGHT_FT, ft);
190:                setParameter(HEIGHT_IN, in);
191:                setParameter(WEIGHT_LB, lb);
192:            }
193:
194:            private static class TestForm extends HtmlForm {
195:                @SuppressWarnings("unchecked")
196:                public TestForm() {
197:                    Group contact = requiredGroup(CONTACT);
198:                    contact.input(stringType(), SURNAME);
199:                    contact.input(stringType(), PHONE);
200:                    contact.input(stringType(), EMAIL, emailAddress())
201:                            .optional();
202:
203:                    Group metric = optionalGroup(METRIC);
204:                    metric.input(doubleType(), HEIGHT_M);
205:                    metric.input(intType(), WEIGHT_KG);
206:
207:                    Group imperial = optionalGroup(IMPERIAL);
208:                    imperial.input(intType(), HEIGHT_FT);
209:                    imperial.input(intType(), HEIGHT_IN);
210:                    imperial.input(intType(), WEIGHT_LB);
211:
212:                    try {
213:                        Group duplicate = optionalGroup(IMPERIAL);
214:                        fail("Error - duplicate group name must throw an exception");
215:                    } catch (DuplicateNameException ex) {
216:                    }
217:                }
218:
219:                protected void additionalValidate() {
220:                    Group metric = getGroup(METRIC);
221:                    Group imperial = getGroup(IMPERIAL);
222:
223:                    if (metric.isEmpty() && imperial.isEmpty()) {
224:                        addError(MISSING_HEIGHT_WEIGHT);
225:                    }
226:                }
227:
228:                public Input<Double> heightM() {
229:                    return getInput(HEIGHT_M);
230:                }
231:
232:                public Input<Integer> weightKg() {
233:                    return getInput(WEIGHT_KG);
234:                }
235:
236:                public Input<Integer> heightFt() {
237:                    return getInput(HEIGHT_FT);
238:                }
239:
240:                public Input<Integer> heightIn() {
241:                    return getInput(HEIGHT_IN);
242:                }
243:
244:                public Input<Integer> weightLb() {
245:                    return getInput(WEIGHT_LB);
246:                }
247:
248:            }
249:
250:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.