Source Code Cross Referenced for XMLConfigurationTest.java in  » Development » OVal » net » sf » oval » test » guard » 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.guard 
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.guard;
013:
014:        import java.util.ArrayList;
015:        import java.util.HashSet;
016:        import java.util.Set;
017:        import java.util.regex.Pattern;
018:
019:        import junit.framework.TestCase;
020:        import net.sf.oval.Check;
021:        import net.sf.oval.ConstraintViolation;
022:        import net.sf.oval.configuration.pojo.elements.ClassConfiguration;
023:        import net.sf.oval.configuration.pojo.elements.ConstraintSetConfiguration;
024:        import net.sf.oval.configuration.pojo.elements.ConstructorConfiguration;
025:        import net.sf.oval.configuration.pojo.elements.FieldConfiguration;
026:        import net.sf.oval.configuration.pojo.elements.MethodConfiguration;
027:        import net.sf.oval.configuration.pojo.elements.MethodReturnValueConfiguration;
028:        import net.sf.oval.configuration.pojo.elements.ParameterConfiguration;
029:        import net.sf.oval.configuration.xml.XMLConfigurer;
030:        import net.sf.oval.constraint.AssertConstraintSetCheck;
031:        import net.sf.oval.constraint.Length;
032:        import net.sf.oval.constraint.LengthCheck;
033:        import net.sf.oval.constraint.MatchPatternCheck;
034:        import net.sf.oval.constraint.NotNullCheck;
035:        import net.sf.oval.exception.ConstraintsViolatedException;
036:        import net.sf.oval.exception.ValidationFailedException;
037:        import net.sf.oval.guard.ConstraintsViolatedAdapter;
038:        import net.sf.oval.guard.Guard;
039:        import net.sf.oval.guard.Guarded;
040:
041:        /**
042:         * @author Sebastian Thomschke
043:         */
044:        public class XMLConfigurationTest extends TestCase {
045:            @Guarded
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:                public User() {
058:                    // do nothing
059:                }
060:
061:                public User(String userId, String managerId, int somethingElse) {
062:                    this .userId = userId;
063:                    this .managerId = managerId;
064:                }
065:
066:                /**
067:                 * @return the managerId
068:                 */
069:                public String getManagerId() {
070:                    return managerId;
071:                }
072:
073:                /**
074:                 * @param managerId the managerId to set
075:                 */
076:                public void setManagerId(String managerId) {
077:                    this .managerId = managerId;
078:                }
079:            }
080:
081:            public void testImportedFile() {
082:                try {
083:                    XMLConfigurer x = new XMLConfigurer();
084:                    x.fromXML(XMLConfigurationTest.class
085:                            .getResourceAsStream("XMLConfigurationTest.xml"));
086:
087:                    Guard guard = new Guard(x);
088:                    guard.setInvariantsEnabled(false);
089:                    TestGuardAspect.aspectOf().setGuard(guard);
090:
091:                    validateUser();
092:                } catch (ValidationFailedException ex) {
093:                    ex.getCause().printStackTrace();
094:                    throw ex;
095:                }
096:            }
097:
098:            public void testSerializedObjectConfiguration() {
099:                XMLConfigurer x = new XMLConfigurer();
100:
101:                /*
102:                 * define a configuration
103:                 */
104:                final Set<ConstraintSetConfiguration> constraintSetsConfig = new HashSet<ConstraintSetConfiguration>();
105:                {
106:                    ConstraintSetConfiguration csf = new ConstraintSetConfiguration();
107:                    constraintSetsConfig.add(csf);
108:
109:                    csf.id = "user.userid";
110:                    csf.checks = new ArrayList<Check>();
111:                    NotNullCheck nnc = new NotNullCheck();
112:                    nnc.setMessage("{context} is null");
113:                    csf.checks.add(nnc);
114:                    MatchPatternCheck rec = new MatchPatternCheck();
115:                    rec.setPattern(Pattern.compile("^[a-z0-9]{8}$", 0));
116:                    rec
117:                            .setMessage("{context} does not match the pattern {pattern}");
118:                    csf.checks.add(rec);
119:                }
120:
121:                final Set<ClassConfiguration> classConfigs = new HashSet<ClassConfiguration>();
122:                {
123:                    ClassConfiguration cf = new ClassConfiguration();
124:                    classConfigs.add(cf);
125:                    cf.type = User.class;
126:
127:                    cf.fieldConfigurations = new HashSet<FieldConfiguration>();
128:                    {
129:                        FieldConfiguration fc = new FieldConfiguration();
130:                        cf.fieldConfigurations.add(fc);
131:
132:                        fc.name = "firstName";
133:                        fc.checks = new ArrayList<Check>();
134:                        LengthCheck lc = new LengthCheck();
135:                        lc
136:                                .setMessage("{context} is not between {min} and {max} characters long");
137:                        lc.setMax(3);
138:                        fc.checks.add(lc);
139:                    }
140:                    {
141:                        FieldConfiguration fc = new FieldConfiguration();
142:                        cf.fieldConfigurations.add(fc);
143:
144:                        fc.name = "lastName";
145:                        fc.checks = new ArrayList<Check>();
146:                        LengthCheck lc = new LengthCheck();
147:                        lc
148:                                .setMessage("{context} is not between {min} and {max} characters long");
149:                        lc.setMax(5);
150:                        fc.checks.add(lc);
151:                    }
152:                    {
153:                        FieldConfiguration fc = new FieldConfiguration();
154:                        fc.overwrite = Boolean.TRUE;
155:                        cf.fieldConfigurations.add(fc);
156:
157:                        fc.name = "userId";
158:                        fc.checks = new ArrayList<Check>();
159:                        AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
160:                        acsc.setId("user.userid");
161:                        fc.checks.add(acsc);
162:                    }
163:
164:                    cf.constructorConfigurations = new HashSet<ConstructorConfiguration>();
165:                    {
166:                        ConstructorConfiguration cc = new ConstructorConfiguration();
167:                        cf.constructorConfigurations.add(cc);
168:                        cc.parameterConfigurations = new ArrayList<ParameterConfiguration>();
169:
170:                        AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
171:                        acsc.setId("user.userid");
172:
173:                        ParameterConfiguration pc1 = new ParameterConfiguration();
174:                        pc1.type = String.class;
175:                        pc1.checks = new ArrayList<Check>();
176:                        pc1.checks.add(acsc);
177:                        cc.parameterConfigurations.add(pc1);
178:                        ParameterConfiguration pc2 = new ParameterConfiguration();
179:                        pc2.type = String.class;
180:                        pc2.checks = new ArrayList<Check>();
181:                        pc2.checks.add(acsc);
182:                        cc.parameterConfigurations.add(pc2);
183:                        ParameterConfiguration pc3 = new ParameterConfiguration();
184:                        pc3.type = int.class;
185:                        cc.parameterConfigurations.add(pc3);
186:                    }
187:
188:                    cf.methodConfigurations = new HashSet<MethodConfiguration>();
189:                    {
190:                        AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
191:                        acsc.setId("user.userid");
192:
193:                        MethodConfiguration mc = new MethodConfiguration();
194:                        cf.methodConfigurations.add(mc);
195:                        mc.name = "getManagerId";
196:                        mc.returnValueConfiguration = new MethodReturnValueConfiguration();
197:                        mc.returnValueConfiguration.checks = new ArrayList<Check>();
198:                        mc.returnValueConfiguration.checks.add(acsc);
199:
200:                        mc = new MethodConfiguration();
201:                        cf.methodConfigurations.add(mc);
202:                        mc.name = "setManagerId";
203:                        mc.parameterConfigurations = new ArrayList<ParameterConfiguration>();
204:                        ParameterConfiguration pc1 = new ParameterConfiguration();
205:                        pc1.type = String.class;
206:                        pc1.checks = new ArrayList<Check>();
207:                        pc1.checks.add(acsc);
208:                        mc.parameterConfigurations.add(pc1);
209:                    }
210:                }
211:
212:                x.getPojoConfigurer().setClassConfigurations(classConfigs);
213:                x.getPojoConfigurer().setConstraintSetConfigurations(
214:                        constraintSetsConfig);
215:
216:                /*
217:                 * serialize the configuration to XML
218:                 */
219:                String xmlConfig = x.toXML();
220:
221:                /*
222:                 * deserialize the configuration from XML
223:                 */
224:                x.fromXML(xmlConfig);
225:
226:                Guard guard = new Guard(x);
227:                guard.setInvariantsEnabled(false);
228:                TestGuardAspect.aspectOf().setGuard(guard);
229:
230:                validateUser();
231:            }
232:
233:            private void validateUser() {
234:                ConstraintsViolatedAdapter listener = new ConstraintsViolatedAdapter();
235:                TestGuardAspect.aspectOf().getGuard().addListener(listener,
236:                        User.class);
237:
238:                listener.clear();
239:                try {
240:                    new User(null, null, 1);
241:                    fail("ConstraintViolationException expected");
242:                } catch (ConstraintsViolatedException ex) {
243:                    ConstraintViolation[] violations = ex
244:                            .getConstraintViolations();
245:                    assertEquals(2, violations.length);
246:                    assertEquals(
247:                            User.class.getName()
248:                                    + "(class java.lang.String,class java.lang.String,int) Parameter 0 (userId) is null",
249:                            violations[0].getMessage());
250:                    assertEquals(
251:                            User.class.getName()
252:                                    + "(class java.lang.String,class java.lang.String,int) Parameter 1 (managerId) is null",
253:                            violations[1].getMessage());
254:                }
255:
256:                listener.clear();
257:                try {
258:                    User user = new User("12345678", "12345678", 1);
259:                    user.setManagerId(null);
260:                    fail("ConstraintViolationException expected");
261:                } catch (ConstraintsViolatedException ex) {
262:                    ConstraintViolation[] violations = ex
263:                            .getConstraintViolations();
264:                    assertEquals(1, violations.length);
265:                    assertEquals(
266:                            User.class.getName()
267:                                    + ".setManagerId(class java.lang.String) Parameter 0 (managerId) is null",
268:                            violations[0].getMessage());
269:                }
270:
271:                listener.clear();
272:                try {
273:                    User user = new User();
274:                    user.getManagerId();
275:                    fail("ConstraintViolationException expected");
276:                } catch (ConstraintsViolatedException ex) {
277:                    ConstraintViolation[] violations = ex
278:                            .getConstraintViolations();
279:                    assertEquals(1, violations.length);
280:                    assertEquals(User.class.getName()
281:                            + ".getManagerId() is null", violations[0]
282:                            .getMessage());
283:                }
284:            }
285:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.