Source Code Cross Referenced for AssertValidTest.java in  » Development » OVal » net » sf » oval » test » validator » 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 » OVal » net.sf.oval.test.validator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003:         * Thomschke.
004:         * 
005:         * All Rights Reserved. This program and the accompanying materials
006:         * are made available under the terms of the Eclipse Public License v1.0
007:         * which accompanies this distribution, and is available at
008:         * http://www.eclipse.org/legal/epl-v10.html
009:         * 
010:         * Contributors:
011:         *     Sebastian Thomschke - initial implementation.
012:         *******************************************************************************/package net.sf.oval.test.validator;
013:
014:        import java.util.ArrayList;
015:        import java.util.Arrays;
016:        import java.util.HashMap;
017:        import java.util.HashSet;
018:        import java.util.List;
019:        import java.util.Map;
020:        import java.util.Set;
021:
022:        import junit.framework.TestCase;
023:        import net.sf.oval.ConstraintViolation;
024:        import net.sf.oval.Validator;
025:        import net.sf.oval.constraint.AssertValid;
026:        import net.sf.oval.constraint.Length;
027:        import net.sf.oval.constraint.MatchPattern;
028:        import net.sf.oval.constraint.NotEmpty;
029:        import net.sf.oval.constraint.NotNull;
030:
031:        /**
032:         * @author Sebastian Thomschke
033:         */
034:        public class AssertValidTest extends TestCase {
035:            private static class Address {
036:                @NotNull
037:                public String street;
038:
039:                @NotNull
040:                public String city;
041:
042:                @NotNull
043:                @Length(max=6)
044:                @NotEmpty
045:                @MatchPattern(pattern="^[0-9]*$")
046:                public String zipCode;
047:
048:                @AssertValid(message="ASSERT_VALID")
049:                public Person contact;
050:            }
051:
052:            private static class Person {
053:                @NotNull
054:                public String firstName;
055:
056:                @NotNull
057:                public String lastName;
058:
059:                @AssertValid(message="ASSERT_VALID")
060:                public Address homeAddress;
061:
062:                @AssertValid(message="ASSERT_VALID")
063:                public List<Address> otherAddresses1;
064:
065:                @AssertValid(message="ASSERT_VALID",requireValidElements=false)
066:                public Set<Address> otherAddresses2;
067:
068:                @AssertValid(message="ASSERT_VALID",requireValidElements=true)
069:                public Set<Address> otherAddresses3;
070:
071:            }
072:
073:            private static class Registry {
074:                @AssertValid
075:                public List<Address[]> addressClusters;
076:
077:                @AssertValid
078:                public Map<String, List<Person>> personsByCity;
079:
080:                @AssertValid
081:                public Map<String, Map<String, Address[]>> addressesByCityAndStreet;
082:            }
083:
084:            public void testCollectionValues() {
085:                final Validator validator = new Validator();
086:
087:                final Person p = new Person();
088:                p.firstName = "John";
089:                p.lastName = "Doe";
090:                p.otherAddresses1 = new ArrayList<Address>();
091:                p.otherAddresses2 = new HashSet<Address>();
092:                p.otherAddresses3 = new HashSet<Address>();
093:
094:                final Address a = new Address();
095:                a.street = "The Street";
096:                a.city = "The City";
097:                a.zipCode = null;
098:                assertEquals(1, validator.validate(a).size());
099:
100:                p.otherAddresses1.add(a);
101:                assertEquals(1, validator.validate(p).size());
102:
103:                p.otherAddresses1.remove(a);
104:                p.otherAddresses2.add(a);
105:                assertEquals(0, validator.validate(p).size());
106:
107:                p.otherAddresses3.add(a);
108:                assertEquals(1, validator.validate(p).size());
109:            }
110:
111:            public void testRecursion() {
112:                final Validator validator = new Validator();
113:
114:                final Registry registry = new Registry();
115:
116:                // nulled collections and maps are valid
117:                assertTrue(validator.validate(registry).size() == 0);
118:
119:                registry.addressesByCityAndStreet = new HashMap<String, Map<String, Address[]>>();
120:                registry.addressClusters = new ArrayList<Address[]>();
121:                registry.personsByCity = new HashMap<String, List<Person>>();
122:
123:                // empty collections and maps are valid
124:                assertEquals(0, validator.validate(registry).size());
125:
126:                final Person invalidPerson1 = new Person();
127:                final Person invalidPerson2 = new Person();
128:
129:                // map with an empty list is valid
130:                registry.personsByCity.put("city1", new ArrayList<Person>());
131:                assertEquals(0, validator.validate(registry).size());
132:
133:                registry.personsByCity.put("city1", Arrays
134:                        .asList(new Person[] { invalidPerson1 }));
135:                assertEquals(1, validator.validate(registry).size());
136:
137:                registry.personsByCity.put("city2", Arrays
138:                        .asList(new Person[] { invalidPerson2 }));
139:                assertEquals(2, validator.validate(registry).size());
140:
141:                registry.personsByCity.clear();
142:                registry.personsByCity.put("city1", Arrays.asList(new Person[] {
143:                        invalidPerson1, invalidPerson1, invalidPerson2,
144:                        invalidPerson2 }));
145:                assertEquals(4, validator.validate(registry).size());
146:
147:                registry.personsByCity.clear();
148:
149:                // list with an array with empty elements is valid
150:                registry.addressClusters.add(new Address[10]);
151:                assertEquals(0, validator.validate(registry).size());
152:
153:                final Address invalidAddress1 = new Address();
154:                final Address invalidAddress2 = new Address();
155:
156:                registry.addressClusters.add(new Address[10]);
157:                assertEquals(0, validator.validate(registry).size());
158:
159:                registry.addressClusters.add(new Address[] { invalidAddress1,
160:                        invalidAddress2, invalidAddress1, invalidAddress2 });
161:                assertEquals(4, validator.validate(registry).size());
162:
163:                registry.addressClusters.clear();
164:
165:                // map with an entry with an empty map is valid
166:                registry.addressesByCityAndStreet.put("city1",
167:                        new HashMap<String, Address[]>());
168:                assertEquals(0, validator.validate(registry).size());
169:
170:                // map with an entry with an map with an element with an empty array is valid
171:                registry.addressesByCityAndStreet.get("city1").put("street1",
172:                        new Address[0]);
173:                assertEquals(0, validator.validate(registry).size());
174:
175:                registry.addressesByCityAndStreet.get("city1").put(
176:                        "street1",
177:                        new Address[] { invalidAddress1, invalidAddress1,
178:                                invalidAddress2, invalidAddress2 });
179:                assertEquals(4, validator.validate(registry).size());
180:            }
181:
182:            public void testScalarValues() {
183:                final Validator validator = new Validator();
184:
185:                final Person p = new Person();
186:                p.firstName = "John";
187:                p.lastName = "Doe";
188:                assertEquals(0, validator.validate(p).size());
189:
190:                final Address a = new Address();
191:                a.street = "The Street";
192:                a.city = "The City";
193:                a.zipCode = "12345";
194:                assertEquals(0, validator.validate(a).size());
195:
196:                // make the address invalid
197:                a.zipCode = null;
198:                assertEquals(1, validator.validate(a).size());
199:
200:                // associate the invalid address with the person check the person for validity
201:                p.homeAddress = a;
202:                List<ConstraintViolation> violations = validator.validate(p);
203:                assertEquals(1, violations.size());
204:                assertEquals("ASSERT_VALID", violations.get(0).getMessage());
205:
206:                // test circular dependencies
207:                a.contact = p;
208:                violations = validator.validate(p);
209:                assertEquals(1, violations.size());
210:                assertEquals("ASSERT_VALID", violations.get(0).getMessage());
211:            }
212:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.