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: }
|