001: package simpleorm.examples;
002:
003: import simpleorm.core.*; // .* OK, all classes prefixed with "S".
004: import java.io.*;
005:
006: /** Tests and demonstrates selective column queries, locking and
007: flushing details as well as derived columns and joins. Note
008: that care is taken to trap attempts to access unqueried fields
009: and so prevent errors that could otherwise be the source of
010: nasty bugs as the application scales.*/
011: public class ColumnCacheTest implements SConstants {
012:
013: public static void main(String[] argv) throws Exception {
014: TestUte.initializeTest(ColumnCacheTest.class); // Look at this code
015: try {
016: TestUte.createDeptEmp();
017: findOrCreateTest();
018: selectTest();
019: referenceTest();
020: updateOrderTest();
021: columnQueryTest();
022: } finally {
023: SConnection.detachAndClose();
024: }
025: }
026:
027: static void findOrCreateTest() {
028: SConnection.begin();
029:
030: /// Employee.RESUME not normally retrieved, need to explicitly query.
031: /// Firs create a Resume. Setting unqueried fields is OK.
032: Employee e100a = (Employee) Employee.meta.findOrCreate("100");
033: String res = "My life is but a walking shadow ... signifying nothing.";
034: e100a.setString(e100a.RESUME, res);
035:
036: SConnection.commit();
037: SConnection.begin();
038:
039: /// Trap attempt to access unqueried RESUME
040: Employee e100b = (Employee) Employee.meta.findOrCreate("100");
041: try {
042: e100b.getString(e100b.RESUME); // Unqueried;
043: throw new SException.Test("Unqueried Resume not trapped");
044: } catch (SException.Error re) {
045: SLog.slog.message("Get Failure " + re);
046: }
047:
048: /// Retrieve it properly and check value.
049: Employee e100c = (Employee) Employee.meta.findOrCreate("100",
050: SQY_UNQUERIED);
051: // This actually issues a query to get the extra column.
052: TestUte.assertTrue(res.equals(e100c.getString(e100b.RESUME)));
053: TestUte.assertTrue(e100b == e100c);
054:
055: try {
056: e100b.setString(e100b.EMPEE_ID, "XXX"); // Primary Key
057: throw new SException.Test("Set Primary Key not trapped");
058: } catch (SException.Error ke) {
059: SLog.slog.message("Key Failure " + ke);
060: }
061:
062: SConnection.commit();
063: SConnection.begin();
064:
065: /// Retrieve a record without Lokcing and try to update or delete it.
066: Department d100a = (Department) Department.meta.findOrCreate(
067: "100", SQY_READ_ONLY);
068: try {
069: d100a.setString(d100a.NAME, "OOPS"); // Should fail
070: throw new SException.Test("Set unlocked record " + d100a);
071: } catch (SException.Error se) {
072: SLog.slog.message("Set Failure " + se);
073: }
074: try {
075: d100a.deleteRecord(); // Should fail
076: throw new SException.Test("Deleted unlocked record "
077: + d100a);
078: } catch (SException.Error de) {
079: SLog.slog.message("Delete Failure " + de);
080: }
081:
082: /// Try to update a non-queried for update field
083: Department d100b = (Department) Department.meta.findOrCreate(
084: "100", SQY_DESCRIPTIVE);
085: d100b.setString(d100b.NAME, "One00a"); // OK, queried and locked
086: d100b.getDouble(d100b.BUDGET); // OK, queried but not locked
087: if (d100a != d100b)
088: throw new SException.Test("Diff Depts " + d100a + d100b);
089: try {
090: d100b.setDouble(d100b.BUDGET, 666); // Should fail
091: throw new SException.Test("Update unlocked field " + d100b);
092: } catch (SException.Error e) {
093: }
094:
095: Department d100c = (Department) Department.meta
096: .findOrCreate("100");
097: d100b.setDouble(d100b.BUDGET, 1234); // Should finally be OK
098:
099: /// Create a new Record. Can update fields because new, so we
100: /// reliably know the values of all columns (null, or default
101: /// later).
102: Department d900 = (Department) Department.meta.findOrCreate(
103: "900", SQY_DESCRIPTIVE);
104: d900.assertNewRow();
105:
106: d900.setString(d900.NAME, "Five00");
107:
108: SConnection.flush(); // Inserts DEPT_ID, NAME
109:
110: d900.setDouble(d900.BUDGET, 777); // OK because new record.
111: SConnection.commit(); // Updates BUDGET only.
112:
113: SConnection.begin();
114:
115: Department d900a = (Department) Department.meta
116: .findOrCreate("900");
117: if (d900a.getDouble(d900a.BUDGET) != 777)
118: throw new SException.Test("Bad Budget " + d900a);
119:
120: SConnection.commit();
121: SConnection.begin();
122:
123: /// Use an explicit list of fields
124: Department d200a = (Department) Department.meta.findOrCreate(
125: "200", 0, new SFieldMeta[] { Department.BUDGET });
126: try {
127: d200a.getString(d200a.NAME); // Should fail
128: throw new SException.Test("Get unqueried record " + d200a);
129: } catch (SException.Error g2e) {
130: SLog.slog.message("Set Failure " + g2e);
131: }
132: TestUte.assertTrue(d200a.getDouble(d200a.BUDGET) == 20000);
133:
134: SConnection.commit();
135:
136: }
137:
138: static void selectTest() {
139: SConnection.begin();
140:
141: SResultSet res = Department.meta.newQuery(
142: SQY_DESCRIPTIVE | SQY_READ_ONLY).gt(Department.BUDGET,
143: SJSharp.newInteger(10000)).descending(Department.NAME)
144: .execute();
145: res.hasNext();
146: Department dept = (Department) res.getRecord();
147: dept.getString(dept.NAME); // OK
148: try {
149: dept.getString(dept.BUDGET); // Unqueried;
150: throw new SException.Test("Unqueried Budget not trapped");
151: } catch (SException.Error ge) {
152: SLog.slog.message("Get Failure " + dept + " "
153: + dept.getString(dept.NAME) + " " + ge);
154: }
155: try {
156: dept.setString(dept.NAME, "XXX"); // ReadOnly;
157: throw new SException.Test("ReadONLY not trapped");
158: } catch (SException.Error se) {
159: SLog.slog.message("Set Failure " + dept + " "
160: + dept.getString(dept.NAME) + " " + se);
161: }
162:
163: SConnection.commit();
164: }
165:
166: /** Test partial queries of references. */
167: static void referenceTest() {
168: SConnection.begin();
169: {
170: Employee e100a = (Employee) Employee.meta.findOrCreate(
171: "100", 0, new SFieldMeta[] { Employee.DEPARTMENT });
172:
173: Department d100a = (Department) e100a
174: .getReference(e100a.DEPARTMENT);
175: TestUte.assertTrue("D100".equals(d100a
176: .getString(d100a.NAME)));
177:
178: try {
179: e100a.getString(e100a.SALARY); // Unqueried;
180: throw new SException.Test(
181: "Unqueried Salary not trapped");
182: } catch (SException.Error se) {
183: SLog.slog.message("Get Failure " + se);
184: }
185:
186: Department d200a = (Department) Department.meta
187: .findOrCreate("200");
188: e100a.setReference(e100a.DEPARTMENT, d200a);
189: }
190:
191: SConnection.commit();
192: SConnection.begin();
193:
194: {
195: Employee e100b = (Employee) Employee.meta.findOrCreate(
196: "100", SQY_READ_ONLY,
197: new SFieldMeta[] { Employee.DEPARTMENT });
198:
199: Department d200b = (Department) e100b
200: .getReference(e100b.DEPARTMENT);
201: TestUte.assertTrue("D200".equals(d200b
202: .getString(d200b.NAME)));
203:
204: try {
205: Department d300a = (Department) Department.meta
206: .findOrCreate("300");
207: e100b.setReference(e100b.DEPARTMENT, d300a);
208: throw new SException.Test("Read Only Dept not trapped");
209: } catch (SException.Error sde) {
210: SLog.slog.message("Set Failure " + sde);
211: }
212: }
213:
214: SConnection.commit();
215: SConnection.begin();
216:
217: {
218: Employee e100r = (Employee) Employee.meta
219: .findOrCreate("100");
220: Department dept = (Department) e100r.getReference(
221: e100r.DEPARTMENT, SQY_DESCRIPTIVE);
222: TestUte
223: .assertTrue("D200"
224: .equals(dept.getString(dept.NAME)));
225:
226: try {
227: dept.getString(dept.BUDGET);
228: throw new SException.Test(
229: "Descriptive Dept not trapped");
230: } catch (SException.Error dre) {
231: SLog.slog.message("Get Descr Failure " + dre);
232: }
233: }
234:
235: SConnection.commit();
236:
237: }
238:
239: /** Make sure referenced records are updated first. */
240: static void updateOrderTest() {
241: SConnection.begin();
242:
243: Department d800a = (Department) Department.meta
244: .findOrCreate("800");
245: d800a.assertNewRow();
246: //d800a.setDirty(); // To avoid the exception. Or set the depts name!
247:
248: Employee e300c = (Employee) Employee.meta.findOrCreate("300");
249: e300c.setDouble(e300c.SALARY, 0);
250: try {
251: e300c.setReference(e300c.DEPARTMENT, d800a);
252: throw new SException.Test(
253: "Bad FKey update order not trapped");
254: } catch (SException.Error re) {
255: SLog.slog.message("RSet Failure " + re);
256: }
257:
258: Department d700a = (Department) Department.meta
259: .findOrCreate("700");
260: d700a.assertNewRow();
261: d700a.setDirty(); // Check no errors with just key set.
262:
263: SConnection.commit();
264: SConnection.begin();
265:
266: Department d700b = (Department) Department.meta
267: .findOrCreate("700");
268: d700b.assertNotNewRow();
269: d700b.setDirty(); // Trap empty UPDATE ... SET WHERE...
270:
271: SConnection.commit();
272: }
273:
274: /** Test SCOLUMN_QUERY */
275: static void columnQueryTest() {
276: SConnection.begin();
277:
278: Employee e100 = (Employee) Employee.meta.findOrCreate("100",
279: SQY_UNQUERIED);
280: String dname = e100.getString(e100.DEPT_NAME);
281: TestUte.assertTrue("D200".equals(dname));
282:
283: try {
284: e100.setString(e100.DEPT_NAME, "Changed");
285: throw new SException.Test(
286: "Attempt to set read only Dept_Name not trapped");
287: } catch (SException.Error se) {
288: SLog.slog.message("Set Failure " + se);
289: }
290:
291: SConnection.commit();
292: SConnection.begin();
293:
294: SResultSet res = Employee.meta.newQuery(SQY_UNQUERIED).eq(
295: Employee.EMPEE_ID, "100").execute();
296: res.hasNext(1);
297: Employee e100b = (Employee) res.getRecord();
298: String dname2 = e100b.getString(e100.DEPT_NAME);
299: TestUte.assertTrue("D200".equals(dname2));
300: res.hasNext(1);
301:
302: SConnection.commit();
303: }
304: }
|