Source Code Cross Referenced for ValidationTest.java in  » Database-ORM » SimpleORM » simpleorm » examples » 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 » Database ORM » SimpleORM » simpleorm.examples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package simpleorm.examples;
002:
003:        import simpleorm.core.*;
004:
005:        // .* OK, all classes prefixed with "S".
006:
007:        /** Tests validation, OnFieldValidate etc. 
008:         *  (Basic validation test is also in ADemo/Employee.)
009:         * */
010:
011:        public class ValidationTest implements  SConstants {
012:
013:            static public class Validated extends SRecordInstance implements 
014:                    java.io.Serializable {
015:
016:                public static final SRecordMeta meta = new SRecordMeta(
017:                        Validated.class, "XX_VALIDATED");
018:
019:                public static final SFieldString VALID_ID = new SFieldString(
020:                        meta, "VALID_ID", 20, SCon.SFD_PRIMARY_KEY);
021:
022:                public static final SFieldString NAME = new SFieldString(meta,
023:                        "NAME", 40, SCon.SFD_DESCRIPTIVE);
024:
025:                public static final SFieldString PHONE_NR = new SFieldString(
026:                        meta, "PHONE_NR", 20);
027:
028:                public static final SFieldDouble SALARY = new SFieldDouble(
029:                        meta, "SALARY");
030:
031:                static final SFieldReference MANAGER = // Recursive Reference
032:                new SFieldReference(meta, Validated.meta, "MANAGER_");
033:
034:                public SRecordMeta getMeta() {
035:                    return meta;
036:                }; // specializes abstract method
037:
038:                Object salary, validId, name; // Only used for tests.
039:
040:                /** Called when the Salary is setDouble()d. */
041:                public void validateField(SFieldMeta field, Object newValue) {
042:                    SLog.slog.fields("Validated.field " + field + " := "
043:                            + newValue);
044:                    if (field == VALID_ID) {
045:                        validId = newValue;
046:                    }
047:                    if (field == NAME) {
048:                        name = newValue;
049:                    }
050:                    if (field == SALARY) {
051:                        salary = newValue;
052:                        double sal = newValue == null ? 0 : ((Number) newValue)
053:                                .doubleValue();
054:                        if (sal < 0)
055:                            throw new SValidationException("Salary {0} < 0.",
056:                                    SJSharp.newDouble(sal));
057:                    }
058:                }
059:
060:                boolean validated = false;
061:
062:                /** Called when the record is flushed, and so both Departent and
063:                 Salary will have been set. */
064:                public void validateRecord() {
065:                    SLog.slog.fields("Validated.record " + this );
066:                    validated = true;
067:                    if (isValid(SALARY)) {
068:                        double sal = getDouble(SALARY);
069:                        //Department dept = (Department)getReference(DEPARTMENT);
070:                        //if (dept != null) {
071:                        //double max = dept.getDouble(Department.MAX_SALARY);
072:                        double max = 100000;
073:                        if (sal > max)
074:                            throw new SValidationException(
075:                                    "Salary {0} is greater than the Departments Maximum {1}.",
076:                                    new Object[] { SJSharp.newDouble(sal),
077:                                            SJSharp.newDouble(max) }, this ); // Always add the record instance for validate record.
078:
079:                    }
080:                }
081:            } // Validated
082:
083:            public static void main(String[] argv) throws Exception {
084:                TestUte.initializeTest(ValidationTest.class); // Look at this code.
085:                try {
086:                    testInit();
087:                    validationTest();
088:                } finally {
089:                    SConnection.detachAndClose();
090:                }
091:            }
092:
093:            /** Prepare for tests, Delete old data. */
094:            static void testInit() throws Exception {
095:                System.out.println("################ Init #################");
096:                SConnection.begin();
097:
098:                /// Delete any old data from a previous run.
099:                TestUte.dropAllTables();
100:
101:                SConnection.rawUpdateDB(Validated.meta.createTableSQL());
102:
103:                SConnection.commit();
104:            }
105:
106:            static void validationTest() { // Foreign Keys
107:                System.out
108:                        .println("################ validationTest #################");
109:                SConnection.begin();
110:
111:                /// Create a validated
112:                SDataLoader vldDL = new SDataLoader(Validated.meta);
113:                Validated[] recs = (Validated[]) vldDL
114:                        .insertRecords(new Object[][] {
115:                                { "100", "One00", "123 456 7890", "10000", null },
116:                                { "200", "Too00", "123 456 7890", "10000", null } });
117:
118:                assertTrue("Too00".equals(recs[1].name));
119:                assertTrue("100".equals(recs[0].validId));
120:
121:                recs[1].validated = false;
122:                SConnection.commit();
123:                assertTrue(recs[1].validated);
124:                SConnection.begin();
125:
126:                /// Create empty v300
127:                Validated v300 = (Validated) Validated.meta.create("300"); //new
128:                assertTrue("300".equals(v300.validId));
129:                SConnection.flush();
130:                assertTrue(v300.validated); // despite no non-key fields set.
131:
132:                /// Non-creation of empty v400.
133:                Validated v400 = (Validated) Validated.meta.findOrCreate("400"); //new
134:                assertTrue("400".equals(v400.validId));
135:                // v400.setDirty()   needed to make record dirty and thus insert.  cf. create(keys).
136:                SConnection.flush();
137:                assertTrue(!v400.validated);
138:
139:                /// Attempt to pay e200 -ve is traped immediately.
140:                Validated e200 = (Validated) Validated.meta.findOrCreate("200");
141:                try {
142:                    e200.setDouble(e200.SALARY, -1);
143:                    throw new SException.Test("Negative Salary not detected.");
144:                } catch (SValidationException ve) {
145:                    SLog.slog.message("Salary Negative message: "
146:                            + ve.getMessage());
147:                }
148:
149:                /// Attempt to pay e200 too much is detected at Commit/Flush time.
150:                e200.setDouble(e200.SALARY, 500000);
151:                try {
152:                    SConnection.commit();
153:                    throw new SException.Test("Big Salary not detected.");
154:                } catch (SValidationException ve) {
155:                    SLog.slog.message("Big Salary message: "
156:                            + ve.getRecordInstance() + ve.getMessage());
157:                    SConnection.rollback();
158:                }
159:
160:            }
161:
162:            static void assertTrue(boolean cond) {
163:                TestUte.assertTrue(cond);
164:            }
165:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.