Source Code Cross Referenced for XMLConfigurationTest.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.io.ByteArrayInputStream;
015:        import java.io.ByteArrayOutputStream;
016:        import java.io.ObjectInputStream;
017:        import java.io.ObjectOutputStream;
018:        import java.util.ArrayList;
019:        import java.util.HashSet;
020:        import java.util.List;
021:        import java.util.Set;
022:        import java.util.regex.Pattern;
023:
024:        import junit.framework.TestCase;
025:        import net.sf.oval.Check;
026:        import net.sf.oval.ConstraintViolation;
027:        import net.sf.oval.Validator;
028:        import net.sf.oval.configuration.pojo.POJOConfigurer;
029:        import net.sf.oval.configuration.pojo.elements.ClassConfiguration;
030:        import net.sf.oval.configuration.pojo.elements.ConstraintSetConfiguration;
031:        import net.sf.oval.configuration.pojo.elements.FieldConfiguration;
032:        import net.sf.oval.configuration.pojo.elements.MethodConfiguration;
033:        import net.sf.oval.configuration.pojo.elements.MethodReturnValueConfiguration;
034:        import net.sf.oval.configuration.xml.XMLConfigurer;
035:        import net.sf.oval.constraint.AssertCheck;
036:        import net.sf.oval.constraint.AssertConstraintSetCheck;
037:        import net.sf.oval.constraint.Length;
038:        import net.sf.oval.constraint.LengthCheck;
039:        import net.sf.oval.constraint.MatchPatternCheck;
040:        import net.sf.oval.constraint.NotNullCheck;
041:
042:        /**
043:         * @author Sebastian Thomschke
044:         */
045:        public class XMLConfigurationTest extends TestCase {
046:            public static class User {
047:                // added @Length to test if overwrite=true works
048:                @Length(min=10,max=10)
049:                protected String userId;
050:
051:                protected String managerId;
052:
053:                protected String firstName;
054:
055:                protected String lastName;
056:
057:                /**
058:                 * @return the managerId
059:                 */
060:                public String getManagerId() {
061:                    return managerId;
062:                }
063:            }
064:
065:            public void testImportedFile() {
066:                final XMLConfigurer x = new XMLConfigurer();
067:                x.fromXML(XMLConfigurationTest.class
068:                        .getResourceAsStream("XMLConfigurationTest.xml"));
069:                validateUser(new Validator(x));
070:            }
071:
072:            public void testSerializedObjectConfiguration() throws Exception {
073:                final XMLConfigurer x = new XMLConfigurer();
074:
075:                /*
076:                 * define a configuration
077:                 */
078:                final Set<ConstraintSetConfiguration> constraintSetsConfig = new HashSet<ConstraintSetConfiguration>();
079:                {
080:                    final ConstraintSetConfiguration csf = new ConstraintSetConfiguration();
081:                    constraintSetsConfig.add(csf);
082:
083:                    csf.id = "user.userid";
084:                    csf.checks = new ArrayList<Check>();
085:                    final NotNullCheck nnc = new NotNullCheck();
086:                    nnc.setMessage("{context} is null");
087:                    csf.checks.add(nnc);
088:                    final MatchPatternCheck rec = new MatchPatternCheck();
089:                    rec.setPattern(Pattern.compile("^[a-z0-9]{8}$", 0));
090:                    rec
091:                            .setMessage("{context} does not match the pattern {pattern}");
092:                    rec.setProfiles("a", "b");
093:                    csf.checks.add(rec);
094:                }
095:
096:                final Set<ClassConfiguration> classConfigs = new HashSet<ClassConfiguration>();
097:                {
098:                    final ClassConfiguration cf = new ClassConfiguration();
099:                    classConfigs.add(cf);
100:                    cf.type = User.class;
101:
102:                    cf.fieldConfigurations = new HashSet<FieldConfiguration>();
103:                    {
104:                        final FieldConfiguration fc = new FieldConfiguration();
105:                        cf.fieldConfigurations.add(fc);
106:
107:                        fc.name = "firstName";
108:                        fc.checks = new ArrayList<Check>();
109:                        final AssertCheck ac = new AssertCheck();
110:                        ac
111:                                .setExpression("_value != null && _value.length() < 3");
112:                        ac
113:                                .setMessage("{context} cannot be longer than 3 characters");
114:                        ac.setLang("groovy");
115:                        fc.checks.add(ac);
116:                    }
117:                    {
118:                        final FieldConfiguration fc = new FieldConfiguration();
119:                        cf.fieldConfigurations.add(fc);
120:
121:                        fc.name = "lastName";
122:                        fc.checks = new ArrayList<Check>();
123:                        final LengthCheck lc = new LengthCheck();
124:                        lc
125:                                .setMessage("{context} is not between {min} and {max} characters long");
126:                        lc.setMin(1);
127:                        lc.setMax(5);
128:                        fc.checks.add(lc);
129:                    }
130:                    {
131:                        final FieldConfiguration fc = new FieldConfiguration();
132:                        fc.overwrite = Boolean.TRUE;
133:                        cf.fieldConfigurations.add(fc);
134:
135:                        fc.name = "userId";
136:                        fc.checks = new ArrayList<Check>();
137:                        final AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
138:                        acsc.setId("user.userid");
139:                        fc.checks.add(acsc);
140:                    }
141:
142:                    cf.methodConfigurations = new HashSet<MethodConfiguration>();
143:                    {
144:                        final MethodConfiguration mc = new MethodConfiguration();
145:                        cf.methodConfigurations.add(mc);
146:                        mc.name = "getManagerId";
147:                        mc.isInvariant = true;
148:                        mc.returnValueConfiguration = new MethodReturnValueConfiguration();
149:                        mc.returnValueConfiguration.checks = new ArrayList<Check>();
150:                        final AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
151:                        acsc.setId("user.userid");
152:                        mc.returnValueConfiguration.checks.add(acsc);
153:                    }
154:                }
155:
156:                x.getPojoConfigurer().setClassConfigurations(classConfigs);
157:                x.getPojoConfigurer().setConstraintSetConfigurations(
158:                        constraintSetsConfig);
159:
160:                /*
161:                 * test POJO Configurer object serialization
162:                 */
163:                final ByteArrayOutputStream bos = new ByteArrayOutputStream();
164:                final ObjectOutputStream oos = new ObjectOutputStream(bos);
165:                oos.writeObject(x.getPojoConfigurer());
166:                oos.flush();
167:                oos.close();
168:
169:                final ByteArrayInputStream bin = new ByteArrayInputStream(bos
170:                        .toByteArray());
171:                final ObjectInputStream ois = new ObjectInputStream(bin);
172:                x.setPojoConfigurer((POJOConfigurer) ois.readObject());
173:                ois.close();
174:
175:                /*
176:                 * test XML de/serialization
177:                 */
178:                final String xmlConfig = x.toXML();
179:                // System.out.println(xmlConfig);
180:                x.fromXML(xmlConfig);
181:                validateUser(new Validator(x));
182:            }
183:
184:            private void validateUser(final Validator validator) {
185:                final User usr = new User();
186:
187:                usr.lastName = "1";
188:                usr.userId = "12345678";
189:                usr.managerId = "12345678";
190:
191:                /*
192:                 * check constraints for firstName
193:                 */
194:                usr.firstName = "123456";
195:                List<ConstraintViolation> violations = validator.validate(usr);
196:                assertEquals(1, violations.size());
197:                assertEquals(User.class.getName()
198:                        + ".firstName cannot be longer than 3 characters",
199:                        violations.get(0).getMessage());
200:
201:                usr.firstName = "";
202:
203:                /*
204:                 * check constraints for lastName
205:                 */
206:                usr.lastName = "123456";
207:                violations = validator.validate(usr);
208:                assertEquals(1, violations.size());
209:                assertEquals(User.class.getName()
210:                        + ".lastName is not between 1 and 5 characters long",
211:                        violations.get(0).getMessage());
212:
213:                usr.lastName = "1";
214:
215:                /*
216:                 * check constraints for userId
217:                 */
218:                usr.userId = null;
219:                violations = validator.validate(usr);
220:                assertEquals(1, violations.size());
221:                assertEquals(User.class.getName() + ".userId is null",
222:                        violations.get(0).getMessage());
223:
224:                usr.userId = "%$$e3";
225:                violations = validator.validate(usr);
226:                assertEquals(1, violations.size());
227:                assertEquals(User.class.getName()
228:                        + ".userId does not match the pattern ^[a-z0-9]{8}$",
229:                        violations.get(0).getMessage());
230:                usr.userId = "12345678";
231:
232:                /*
233:                 * check constraints for managerId
234:                 */
235:                usr.managerId = null;
236:                violations = validator.validate(usr);
237:                assertEquals(1, violations.size());
238:                assertEquals(User.class.getName() + ".getManagerId() is null",
239:                        violations.get(0).getMessage());
240:
241:                usr.managerId = "%$$e3";
242:                violations = validator.validate(usr);
243:                assertEquals(1, violations.size());
244:                assertEquals(
245:                        User.class.getName()
246:                                + ".getManagerId() does not match the pattern ^[a-z0-9]{8}$",
247:                        violations.get(0).getMessage());
248:            }
249:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.