001: package jimm.datavision.test;
002:
003: import jimm.datavision.Report;
004: import jimm.datavision.layout.CharSepLE;
005: import jimm.datavision.source.Column;
006: import jimm.datavision.source.sql.Database;
007: import jimm.datavision.source.sql.SQLQuery;
008: import java.io.File;
009: import java.io.PrintWriter;
010: import java.io.FileWriter;
011: import java.sql.*;
012: import junit.framework.TestCase;
013: import junit.framework.TestSuite;
014: import junit.framework.Test;
015:
016: /**
017: * Tests the {@link Database} class and the ability to give a connection to a
018: * report and the state of a connection's query after reconnecting.
019: *
020: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
021: */
022: public class ConnectionTest extends TestCase {
023:
024: protected static final File EXAMPLE_REPORT = new File(AllTests
025: .testDataFile("test.xml"));
026: protected static final String PARAMETER_XML_FILE_NAME = AllTests
027: .testDataFile("test_parameters.xml");
028: protected static final File OUT_FILE = new File(System
029: .getProperty("java.io.tmpdir"),
030: "datavision_connection_test_out.txt");
031:
032: protected static final String DRIVER_CLASS_NAME = "org.postgresql.Driver";
033: protected static final String CONNECTION_INFO = "jdbc:postgresql://127.0.0.1/dv_example";
034: protected static final String DB_NAME = "dv_example";
035: protected static final String DB_USER = "jimm";
036: protected static final String DB_PASSWORD = "";
037:
038: public static Test suite() {
039: return new TestSuite(ConnectionTest.class);
040: }
041:
042: public ConnectionTest(String name) {
043: super (name);
044: }
045:
046: public void testConnection() {
047: Connection conn = null;
048:
049: try {
050: Driver d = (Driver) Class.forName(DRIVER_CLASS_NAME)
051: .newInstance();
052: DriverManager.registerDriver(d);
053: conn = DriverManager.getConnection(CONNECTION_INFO,
054: DB_USER, DB_PASSWORD);
055:
056: Report report = new Report();
057: report.setDatabaseConnection(conn);
058:
059: OUT_FILE.deleteOnExit();
060: PrintWriter out = new PrintWriter(new FileWriter(OUT_FILE));
061: report.setLayoutEngine(new CharSepLE(out, '\t'));
062:
063: report.runReport();
064: } catch (Exception e) {
065: e.printStackTrace();
066: fail("exception thrown: " + e);
067: } finally {
068: if (conn != null) {
069: try {
070: conn.close();
071: } catch (SQLException sqle) {
072: fail("SQL exception thrown: " + sqle);
073: }
074: }
075: if (OUT_FILE.exists())
076: OUT_FILE.delete();
077: }
078: }
079:
080: public void testQueryAfterReset() {
081: Report report = new Report();
082: try {
083: report.setDatabasePassword(DB_PASSWORD);
084: report.read(EXAMPLE_REPORT);
085:
086: Database db = (Database) report.getDataSource();
087: SQLQuery query = (SQLQuery) db.getQuery();
088:
089: assertEquals("{jobs.ID} < 100", query.getWhereClause());
090: assertNotNull(db.findColumn("ALL_CAPS.COL1"));
091: assertNotNull(db.findColumn("jobs.fk_office_id"));
092: assertNotNull(db.findColumn("office.email"));
093: assertNotNull(db.findColumn("aggregate_test.value"));
094:
095: // We should only have two tables in the query.
096: query.findSelectablesUsed();
097: assertEquals(2, query.getNumTables());
098:
099: db.reset(DRIVER_CLASS_NAME, CONNECTION_INFO, DB_NAME,
100: DB_USER, DB_PASSWORD);
101: // The query doesn't have to be the same object, but it's where
102: // clause (and all other information) should darned well be the same.
103: assertEquals("{jobs.ID} < 100", query.getWhereClause());
104: assertNotNull(db.findColumn("public.ALL_CAPS.COL1"));
105: assertNotNull(db.findColumn("public.jobs.fk_office_id"));
106: assertNotNull(db.findColumn("public.office.email"));
107: assertNotNull(db.findColumn("public.aggregate_test.value"));
108:
109: // Make sure we still have two tables in the query.
110: query.findSelectablesUsed();
111: assertEquals(2, query.getNumTables());
112: } catch (Exception e) {
113: fail(e.toString());
114: }
115: }
116:
117: public void testDatabaseReset() throws Exception {
118: Report report = new Report();
119: report.setDatabasePassword(DB_PASSWORD);
120: report.read(EXAMPLE_REPORT);
121:
122: Database db = (Database) report.getDataSource();
123: SQLQuery origQuery = (SQLQuery) db.getQuery();
124:
125: db.reset(db.getDriverClassName(), db.getConnectionInfo(), db
126: .getName(), db.getUserName(), "");
127: SQLQuery q = (SQLQuery) db.getQuery();
128:
129: // Unfortunately, we can't just compare query strings. That's because
130: // the table and column lists aren't guaranteed to be sorted.
131: //
132: // At least these tests detect the bug we're fixing.
133: assertEquals(origQuery.getNumTables(), q.getNumTables());
134: assertEquals(origQuery.getNumSelectables(), q
135: .getNumSelectables());
136: }
137:
138: public void testSchemaNamesInColumns() throws Exception {
139: Report report = new Report();
140: report.setDatabasePassword(DB_PASSWORD);
141: report.read(EXAMPLE_REPORT);
142:
143: // Found because we try blank schema
144: Column col = report.findColumn("jobs.ID");
145: assertNotNull(col);
146: assertEquals("public.jobs.ID", col.getId());
147:
148: // Found due to exact match
149: col = report.findColumn("public.jobs.ID");
150: assertNotNull(col);
151: assertEquals("public.jobs.ID", col.getId());
152:
153: // Not found because schema doesn't match table's schema
154: col = report.findColumn("dv_example.jobs.ID");
155: assertNull(col);
156: }
157:
158: public static void main(String[] args) {
159: junit.textui.TestRunner.run(suite());
160: System.exit(0);
161: }
162:
163: }
|