001: package simpleorm.examples;
002:
003: import simpleorm.core.*;
004:
005: /** This test class uses all data types including Dates and Times.
006: What makes it hard is the inconsistent mapping by different
007: database vendors to the SQL-92 data types.*/
008: public class DataTypesTest implements SConstants {
009:
010: public static class DataTypes extends SRecordInstance {
011:
012: public static final SRecordMeta meta = new SRecordMeta(
013: DataTypes.class, "XX_DATA_TYPES");
014:
015: public static final SFieldString DATA_ID = new SFieldString(
016: meta, "DATA_ID", 20, SFD_PRIMARY_KEY, SFD_STRING_CHAR);
017: // CHAR types typically have COBOL style trailing spaces.
018:
019: public static final SFieldString NAME = new SFieldString(meta,
020: "NAME", 20);
021:
022: public static final SFieldString EMPTYSTR = new SFieldString(
023: meta, "EMPTYSTR", 20);
024:
025: public static final SFieldInteger ANINT = new SFieldInteger(
026: meta, "ANINT");
027:
028: public static final SFieldLong ALONG = new SFieldLong(meta,
029: "ALONG");
030:
031: public static final SFieldTimestamp TSTAMP = new SFieldTimestamp(
032: meta, "TSTAMP");
033:
034: public static final SFieldDate ADATE = new SFieldDate(meta,
035: "ADATE");
036:
037: public static final SFieldTime ATIME = new SFieldTime(meta,
038: "ATIME");
039:
040: public static final SFieldObject TSTAMP_OBJ = new SFieldObject(
041: meta, "TSTAMP_OBJ", SDATA_TYPE.pvalue("TIMESTAMP"));
042: // for Create Table
043:
044: public static final SFieldBigDecimal ADECIMAL = new SFieldBigDecimal(
045: meta, "ADECIMAL", 22, 4);
046: // ie. 123456789012345678.3333, more precission than double.
047:
048: public static final SFieldBytes ABYTES = new SFieldBytes(meta,
049: "ABYTES", 100);
050:
051: public static final SFieldBooleanCharYN YNBOOL = new SFieldBooleanCharYN(
052: meta, "YNBOOL");
053:
054: public SRecordMeta getMeta() {
055: return meta;
056: }; // specializes abstract method
057: }
058:
059: public static void main(String[] argv) throws Exception {
060: TestUte.initializeTest(DataTypesTest.class); // Look at this code
061: try {
062: testInit();
063: dataTest();
064: } finally {
065: SConnection.detachAndClose();
066: }
067: }
068:
069: /** Prepare for tests, Delete old data. */
070: static void testInit() throws Exception {
071: SConnection.begin();
072: /// Delete any old data from a previous run.
073: try {
074: SConnection.rawUpdateDB("DROP TABLE XX_DATA_TYPES");
075: } catch (SException.JDBC ex) {
076: }
077: ; // Tables may not exist.
078: SConnection.rawUpdateDB(DataTypes.meta.createTableSQL());
079: SConnection.commit();
080: }
081:
082: static void dataTest() throws Exception {
083: SConnection.begin();
084:
085: SDataLoader dataDL = new SDataLoader(DataTypes.meta);
086:
087: long now = System.currentTimeMillis();
088: dataDL.insertRecords(new Object[][] { { "100", "Trailing ",
089: "", "2000000000", "123456789012345678",
090: new java.sql.Timestamp(now), new java.util.Date(now),
091: new java.sql.Time(now), new java.sql.Timestamp(now),
092: "123456789012345678.3333", new byte[] { 1, 3, 5, 7 },
093: Boolean.TRUE } });
094:
095: SConnection.commit();
096: SConnection.begin();
097:
098: DataTypes dt = (DataTypes) DataTypes.meta
099: .findOrCreate("100 ");
100: // ## Apparantly need trailing spaces for Oracle ?? Needs more investigation.
101:
102: java.sql.Timestamp nowTS = dt.getTimestamp(DataTypes.TSTAMP);
103: // The following is necessary because nowTS.getTime() is rounded
104: // to 1 second.
105: long now2 = nowTS.getTime() / 1000 * 1000 // Sometimes (HSql) getTime is in milliseconds, not seconds.
106: + nowTS.getNanos() / 1000000;
107:
108: dt.setDate(dt.ADATE, new java.util.Date()); // ie. not java.sql.Date()
109:
110: SLog.slog.message("Retrieved Name '"
111: + dt.getObject(DataTypes.NAME)
112: + "' TStamp "
113: + dt
114: + nowTS
115: + "("
116: + nowTS.getTime()
117: + ", "
118: + nowTS.getNanos()
119: + "<-"
120: + now
121: + ") "
122: + new java.util.Date(now)
123: + " "
124: + now
125: + " "
126: + new java.util.Date(now2)
127: + " "
128: + now2
129: + ", Date "
130: + dt.getDate(DataTypes.ADATE)
131: + ", Time "
132: + dt.getTime(DataTypes.ATIME)
133: + ", Object "
134: + dt.getObject(DataTypes.TSTAMP_OBJ).getClass()
135: .getName() + ", BDec "
136: + dt.getBigDecimal(DataTypes.ADECIMAL) + ", byte "
137: + dt.getBytes(DataTypes.ABYTES)[2] + ", YNBool "
138: + dt.getBoolean(DataTypes.YNBOOL));
139:
140: TestUte.assertTrue(dt.getObject(DataTypes.NAME).toString()
141: .equals("Trailing "));
142: TestUte.assertTrue(dt.getString(DataTypes.NAME).equals(
143: "Trailing")); // trimed
144: //TestUte.assertTrue(dt.getString(DataTypes.EMPTYSTR).equals(""));
145: TestUte.assertTrue(dt.isNull(DataTypes.EMPTYSTR) // Oracle returns null!, '' === NULL!
146: || dt.getString(DataTypes.EMPTYSTR).equals(""));
147:
148: TestUte.assertEqual("" + (now2 + 50) / 100, "" + (now + 50)
149: / 100);
150: // ## Postgresql seems to loose the last few digits precission!
151: // HSQL works exactly. Other dbs unknown (JDBC/DB bug).
152: TestUte.assertTrue(dt.getInt(DataTypes.ANINT) == 2000000000);
153: TestUte
154: .assertTrue(dt.getLong(DataTypes.ALONG) == 123456789012345678L);
155: TestUte.assertTrue(dt.getBigDecimal(DataTypes.ADECIMAL).equals(
156: new java.math.BigDecimal("123456789012345678.3333"))); // Precisely
157: TestUte.assertEqual(dt.getBytes(DataTypes.ABYTES)[2] + "", "5");
158:
159: TestUte.assertTrue(dt.getBoolean(DataTypes.YNBOOL));
160: SConnection.commit();
161: }
162: }
|