KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > test > ConnectionTest


1 package jimm.datavision.test;
2 import jimm.datavision.Report;
3 import jimm.datavision.layout.CharSepLE;
4 import jimm.datavision.source.Column;
5 import jimm.datavision.source.sql.Database;
6 import jimm.datavision.source.sql.SQLQuery;
7 import java.io.File JavaDoc;
8 import java.io.PrintWriter JavaDoc;
9 import java.io.FileWriter JavaDoc;
10 import java.sql.*;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import junit.framework.Test;
14
15 /**
16  * Tests the {@link Database} class and the ability to give a connection to a
17  * report and the state of a connection's query after reconnecting.
18  *
19  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
20  */

21 public class ConnectionTest extends TestCase {
22
23 protected static final File JavaDoc EXAMPLE_REPORT =
24     new File JavaDoc(AllTests.testDataFile("test.xml"));
25 protected static final String JavaDoc PARAMETER_XML_FILE_NAME =
26     AllTests.testDataFile("test_parameters.xml");
27 protected static final File JavaDoc OUT_FILE =
28     new File JavaDoc(System.getProperty("java.io.tmpdir"),
29          "datavision_connection_test_out.txt");
30
31 protected static final String JavaDoc DRIVER_CLASS_NAME = "org.postgresql.Driver";
32 protected static final String JavaDoc CONNECTION_INFO =
33     "jdbc:postgresql://127.0.0.1/dv_example";
34 protected static final String JavaDoc DB_NAME = "dv_example";
35 protected static final String JavaDoc DB_USER = "jimm";
36 protected static final String JavaDoc DB_PASSWORD = "";
37
38 public static Test suite() {
39     return new TestSuite(ConnectionTest.class);
40 }
41
42 public ConnectionTest(String JavaDoc name) {
43     super(name);
44 }
45
46 public void testConnection() {
47     Connection conn = null;
48
49     try {
50     Driver d = (Driver)Class.forName(DRIVER_CLASS_NAME).newInstance();
51     DriverManager.registerDriver(d);
52     conn = DriverManager.getConnection(CONNECTION_INFO, DB_USER,
53                        DB_PASSWORD);
54
55     Report report = new Report();
56     report.setDatabaseConnection(conn);
57
58     OUT_FILE.deleteOnExit();
59     PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileWriter JavaDoc(OUT_FILE));
60     report.setLayoutEngine(new CharSepLE(out, '\t'));
61
62     report.runReport();
63     }
64     catch (Exception JavaDoc e) {
65     e.printStackTrace();
66     fail("exception thrown: " + e);
67     }
68     finally {
69     if (conn != null) {
70         try {
71         conn.close();
72         }
73         catch (SQLException sqle) {
74         fail("SQL exception thrown: " + sqle);
75         }
76     }
77     if (OUT_FILE.exists())
78         OUT_FILE.delete();
79     }
80 }
81
82 public void testQueryAfterReset() {
83     Report report = new Report();
84     try {
85     report.setDatabasePassword(DB_PASSWORD);
86     report.read(EXAMPLE_REPORT);
87
88     Database db = (Database)report.getDataSource();
89     SQLQuery query = (SQLQuery)db.getQuery();
90
91     assertEquals("{jobs.ID} < 100", query.getWhereClause());
92     assertNotNull(db.findColumn("ALL_CAPS.COL1"));
93     assertNotNull(db.findColumn("jobs.fk_office_id"));
94     assertNotNull(db.findColumn("office.email"));
95     assertNotNull(db.findColumn("aggregate_test.value"));
96
97     // We should only have two tables in the query.
98
query.findSelectablesUsed();
99     assertEquals(2, query.getNumTables());
100
101     db.reset(DRIVER_CLASS_NAME, CONNECTION_INFO, DB_NAME, DB_USER,
102          DB_PASSWORD);
103     // The query doesn't have to be the same object, but it's where
104
// clause (and all other information) should darned well be the same.
105
assertEquals("{jobs.ID} < 100", query.getWhereClause());
106     assertNotNull(db.findColumn("public.ALL_CAPS.COL1"));
107     assertNotNull(db.findColumn("public.jobs.fk_office_id"));
108     assertNotNull(db.findColumn("public.office.email"));
109     assertNotNull(db.findColumn("public.aggregate_test.value"));
110
111     // Make sure we still have two tables in the query.
112
query.findSelectablesUsed();
113     assertEquals(2, query.getNumTables());
114     }
115     catch (Exception JavaDoc e) {
116     fail(e.toString());
117     }
118 }
119
120 public void testDatabaseReset() throws Exception JavaDoc {
121     Report report = new Report();
122     report.setDatabasePassword(DB_PASSWORD);
123     report.read(EXAMPLE_REPORT);
124
125     Database db = (Database)report.getDataSource();
126     SQLQuery origQuery = (SQLQuery)db.getQuery();
127
128     db.reset(db.getDriverClassName(), db.getConnectionInfo(), db.getName(),
129          db.getUserName(), "");
130     SQLQuery q = (SQLQuery)db.getQuery();
131
132     // Unfortunately, we can't just compare query strings. That's because
133
// the table and column lists aren't guaranteed to be sorted.
134
//
135
// At least these tests detect the bug we're fixing.
136
assertEquals(origQuery.getNumTables(), q.getNumTables());
137     assertEquals(origQuery.getNumSelectables(), q.getNumSelectables());
138 }
139
140 public void testSchemaNamesInColumns() throws Exception JavaDoc {
141     Report report = new Report();
142     report.setDatabasePassword(DB_PASSWORD);
143     report.read(EXAMPLE_REPORT);
144
145     // Found because we try blank schema
146
Column col = report.findColumn("jobs.ID");
147     assertNotNull(col);
148     assertEquals("public.jobs.ID", col.getId());
149
150     // Found due to exact match
151
col = report.findColumn("public.jobs.ID");
152     assertNotNull(col);
153     assertEquals("public.jobs.ID", col.getId());
154
155     // Not found because schema doesn't match table's schema
156
col = report.findColumn("dv_example.jobs.ID");
157     assertNull(col);
158 }
159
160 public static void main(String JavaDoc[] args) {
161     junit.textui.TestRunner.run(suite());
162     System.exit(0);
163 }
164
165 }
166
Popular Tags