001: package simpleorm.examples;
002:
003: import simpleorm.core.*; // .* OK, all classes prefixed with "S".
004:
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.PrintStream;
008: import java.sql.Connection;
009: import java.util.Properties;
010:
011: /** Utilities used by all the test cases.
012: */
013: public class TestUte implements SConstants {
014:
015: /** Create and attach a SimpleORM connection based on a simple JDBC connection.
016: *
017: * @see SDataSource for details.
018: * @see SDataSourceJavaX for details about using a real DataSource object.
019: */
020: static public void initializeTest(String testName,
021: boolean deprecated) throws Exception {
022: System.setErr(System.out); // Tidy up any stack traces.
023: System.err.println("\n==== Test " + testName + " ====");
024: //System.err.println("CP " + System.getProperty("java.class.path"));
025:
026: String dburl = systemProperties.getProperty("database.url",
027: "jdbc:hsqldb:hsqlTempFiles");
028: if (dburl == null)
029: throw new SException.Error(
030: "database.url property not found");
031: dburl = dburl.trim();
032: String dbUserName = systemProperties.getProperty(
033: "database.username", "sa");
034: String dbPassword = systemProperties.getProperty(
035: "database.password", "");
036:
037: String dbDriver = systemProperties.getProperty(
038: "database.driver", "org.hsqldb.jdbcDriver");
039: try {
040: Class.forName(dbDriver);
041: } catch (Exception ex) {
042: throw new SException.JDBC("Loading " + dbDriver, ex);
043: }
044:
045: String traceLevel = systemProperties.getProperty("trace.level");
046: if (traceLevel != null && !traceLevel.equals(""))
047: SLog.slog.level = Integer.parseInt(traceLevel);
048:
049: if (!deprecated) {
050: SDataSource ds = new SDataSource(dburl, dbUserName,
051: dbPassword);
052: // If connection pooling is used then write
053: // DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
054: // SDataSource ds = new SDataSourceJavaX(ds);
055: SConnection.attach(ds, "TestCon");
056: } else {
057: Connection con = java.sql.DriverManager.getConnection(
058: dburl, dbUserName, dbPassword);
059: SConnection.attach(con, "DeprecTest");
060: }
061: }
062:
063: static public void initializeTest(Class test) throws Exception {
064: initializeTest(test.getName(), false);
065: }
066:
067: /** These need to be dropped in the correct order to avoid problems
068: with referential integreity constrains. */
069: static void dropAllTables() {
070: SConnection.dropTableNoError("XX_UGLY_PAY_DTL");
071: SConnection.dropTableNoError("XX_PSLIP_DTL");
072: SConnection.dropTableNoError("XX_PAY_SLIP");
073: SConnection.dropTableNoError("xx_PAY_PERIOD");
074: SConnection.dropTableNoError("XX_EMPLOYEE");
075: SConnection.dropTableNoError("XX_DEPARTMENT");
076: SConnection.dropTableNoError("XX_VALIDATED");
077: }
078:
079: /** Create and populate basic Dept and Emp tables. */
080: static void createDeptEmp() throws Exception {
081: System.out
082: .println("################ CreateDeptEmp #################");
083: int level = SLog.slog.level;
084: SLog.slog.level = 20;
085:
086: SConnection.begin();
087:
088: dropAllTables();
089:
090: SConnection.rawUpdateDB(Department.meta.createTableSQL());
091:
092: SConnection.rawUpdateDB(Employee.meta.createTableSQL());
093:
094: SConnection.commit();
095: SConnection.begin();
096:
097: // Create some test data
098: SDataLoader deptDL = new SDataLoader(Department.meta);
099: Department depts[] = (Department[]) deptDL
100: .insertRecords(new Object[][] {
101: { "100", "D100", "Count Pennies", "10000",
102: "200000" },
103: { "200", "D200", "Be Happy", "20000", "300000" },
104: { "300", "D300", "Enjoy Life", "30000",
105: "150000" },
106: { "400", "D400", "Fill Tables", "40000",
107: "100000" },
108: { "500", "D500", "More Data", "40000", "120000" } });
109: SDataLoader empDL = new SDataLoader(Employee.meta);
110:
111: Employee e100 = (Employee) empDL.insertRecord(new Object[] {
112: "100", "One00", "123 456 7890", "10000", "3", depts[0],
113: null });
114: Employee[] emps1 = (Employee[]) empDL
115: .insertRecords(new Object[][] {
116: { "200", "Two00", "123 456 7890", "20000", "0",
117: depts[1], e100 },
118: { "300", "Three00", "123 456 7890", "30000",
119: "1", null, e100 } });
120: SConnection.commit();
121:
122: SLog.slog.level = level;
123: System.out
124: .println("\n################ CreateDeptEmp END #################\n");
125: }
126:
127: static void assertTrue(boolean cond) {
128: if (!cond)
129: throw new SException.Test(
130: "Assertion Failed, see stack trace.");
131: }
132:
133: static void assertEqual(String s1, String s2) {
134: if (s1 == null && s2 == null)
135: return;
136: if (s1 == null || !s1.equals(s2))
137: throw new SException.Test("Assertion Failed: " + s1
138: + " != " + s2);
139: }
140:
141: static SystemProperties systemProperties = new SystemProperties();
142:
143: /**
144: * Simple property mechanism, reads from the ~/simpleotm.properties file.
145: * Used by Default configuration to set the database.name etc.
146: */
147: static public class SystemProperties {
148:
149: /**
150: * Causes the property cache to be emptied so that properties are re-read from the
151: * property file next time a property is required.
152: */
153: public void refreshProperties() {
154: properties = null;
155: }
156:
157: protected Properties properties;
158:
159: protected void loadProperties() {
160: if (properties == null) {
161: String propsFile = null;
162: try {
163: String home = System.getProperty("user.home");
164: propsFile = home + "/simpleorm.properties";
165: SLog.slog.connections("Loading properties from "
166: + propsFile);
167: properties = new Properties(System.getProperties());
168: FileInputStream in = new FileInputStream(propsFile);
169: properties.load(in);
170: in.close();
171: } catch (Exception ex) {
172: throw new SException.Error(
173: "While obtaining Properties from "
174: + propsFile, ex);
175: }
176: }
177: }
178:
179: public String getProperty(String property) {
180: loadProperties();
181: String prop = properties.getProperty(property);
182: return prop == null ? null : prop.trim();
183: // The .trim() is important!
184: // I once spent a long time tacking down a trailing space in a properties file driver name!
185: }
186:
187: public String getProperty(String property, String defalt) {
188: String val = getProperty(property);
189: return val == null ? defalt : val;
190: }
191:
192: public void setProperty(String property, String value) {
193: loadProperties();
194: properties.setProperty(property, value);
195: }
196: }
197:
198: }
|