01: package net.sourceforge.squirrel_sql.fw.sql;
02:
03: import static org.easymock.EasyMock.expect;
04: import static org.easymock.EasyMock.replay;
05: import static org.easymock.EasyMock.verify;
06:
07: import java.sql.Date;
08: import java.sql.ResultSet;
09: import java.sql.ResultSetMetaData;
10: import java.sql.SQLException;
11: import java.sql.Timestamp;
12:
13: import net.sourceforge.squirrel_sql.BaseSQuirreLTestCase;
14: import net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.DTProperties;
15:
16: import org.easymock.EasyMock;
17:
18: public class ResultSetReaderTest extends BaseSQuirreLTestCase {
19:
20: private static final String dateClassName = "net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.DataTypeDate";
21:
22: ResultSet mockResultSet = null;
23: ResultSetMetaData mockResultSetMetaData = null;
24:
25: ResultSetReader readerUnderTest = null;
26:
27: protected void setUp() throws Exception {
28: super .setUp();
29: mockResultSet = EasyMock.createMock(ResultSet.class);
30: mockResultSetMetaData = EasyMock
31: .createMock(ResultSetMetaData.class);
32: expect(mockResultSet.getMetaData()).andReturn(
33: mockResultSetMetaData);
34: expect(mockResultSetMetaData.getColumnCount()).andReturn(1)
35: .atLeastOnce();
36: expect(mockResultSet.next()).andReturn(true).once();
37: expect(mockResultSetMetaData.getColumnType(1)).andReturn(91)
38: .anyTimes();
39: expect(mockResultSetMetaData.getColumnTypeName(1)).andReturn(
40: "DATE").anyTimes();
41: expect(mockResultSet.wasNull()).andReturn(false).anyTimes();
42: }
43:
44: private void verifyAll() {
45: verify(mockResultSet);
46: verify(mockResultSetMetaData);
47:
48: }
49:
50: private void replayAll() {
51: replay(mockResultSet);
52: replay(mockResultSetMetaData);
53: }
54:
55: protected void tearDown() throws Exception {
56: super .tearDown();
57: }
58:
59: public void testReadDateAsTimestamp() throws SQLException {
60: testReadType(Timestamp.class.getName(), "true");
61: }
62:
63: public void testReadDateAsDate() throws SQLException {
64: testReadType(Date.class.getName(), "false");
65: }
66:
67: public void testReadDateAsDefault() throws SQLException {
68: testReadType(Date.class.getName(), null);
69: }
70:
71: private void testReadType(String type, String readDatePropVal)
72: throws SQLException {
73: DTProperties.put(dateClassName, "readDateAsTimestamp",
74: readDatePropVal);
75:
76: long now = System.currentTimeMillis();
77: if (type.equals(Date.class.getName())) {
78: expect(mockResultSet.getDate(1)).andReturn(
79: new java.sql.Date(now));
80: }
81:
82: if (type.equals(Timestamp.class.getName())) {
83: expect(mockResultSet.getTimestamp(1)).andReturn(
84: new java.sql.Timestamp(now));
85: }
86:
87: replayAll();
88: readerUnderTest = new ResultSetReader(mockResultSet);
89: Object[] result = readerUnderTest.readRow();
90: if (result[0].getClass().getName().equals(type)) {
91: //
92: } else {
93: fail("result[0] not a Date: "
94: + result[0].getClass().getName());
95: }
96: verifyAll();
97: }
98:
99: }
|