001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.fw.sql;
020:
021: import static org.easymock.EasyMock.createMock;
022: import static org.easymock.EasyMock.expect;
023: import static org.easymock.EasyMock.replay;
024: import static org.easymock.EasyMock.verify;
025: import static org.junit.Assert.assertEquals;
026: import static org.junit.Assert.fail;
027:
028: import java.sql.ResultSet;
029: import java.sql.ResultSetMetaData;
030: import java.sql.SQLException;
031: import java.sql.Types;
032:
033: import net.sourceforge.squirrel_sql.BaseSQuirreLJUnit4TestCase;
034:
035: import org.junit.After;
036: import org.junit.Before;
037: import org.junit.Ignore;
038: import org.junit.Test;
039:
040: public class ResultSetColumnReaderTest extends
041: BaseSQuirreLJUnit4TestCase {
042:
043: /* Class under test */
044: ResultSetColumnReader readerUnderTest = null;
045:
046: /* Mock objects */
047: ResultSet mockResultSet = null;
048: ResultSetMetaData mockResultSetMetaData = null;
049:
050: @Before
051: public void setUp() throws Exception {
052: /* construct mock objects */
053: mockResultSet = createMock(ResultSet.class);
054: mockResultSetMetaData = createMock(ResultSetMetaData.class);
055: }
056:
057: @After
058: public void tearDown() throws Exception {
059: }
060:
061: private void replayAll() {
062: replay(mockResultSet);
063: replay(mockResultSetMetaData);
064: }
065:
066: private void verifyAll() {
067: verify(mockResultSet);
068: verify(mockResultSetMetaData);
069: }
070:
071: @Test(expected=IllegalArgumentException.class)
072: public final void testNullArg() throws SQLException {
073: readerUnderTest = new ResultSetColumnReader(null);
074: }
075:
076: @Test
077: @Ignore
078: public final void testNext() {
079: fail("Not yet implemented"); // TODO
080: }
081:
082: @Test
083: public final void testGetBoolean() throws SQLException {
084: int index = 1;
085:
086: /* local expectations */
087: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
088: Types.BIT);
089: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
090: Types.BIT);
091: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
092: Types.BOOLEAN);
093: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
094: Types.BOOLEAN);
095: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
096: Types.BOOLEAN);
097: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
098: Types.VARCHAR);
099:
100: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
101: Types.VARCHAR);
102:
103: index = 1;
104: expect(mockResultSet.getMetaData()).andReturn(
105: mockResultSetMetaData).anyTimes();
106: expect(mockResultSet.getObject(index++)).andReturn(
107: Boolean.valueOf(true)).anyTimes();
108: expect(mockResultSet.getObject(index++)).andReturn(
109: Boolean.valueOf(false)).anyTimes();
110: expect(mockResultSet.getObject(index++)).andReturn(
111: Long.valueOf(1)).anyTimes();
112: expect(mockResultSet.getObject(index++)).andReturn(
113: Short.valueOf((short) 0)).anyTimes();
114: expect(mockResultSet.getObject(index++)).andReturn("true")
115: .anyTimes();
116: expect(mockResultSet.getObject(index++)).andReturn("false")
117: .anyTimes();
118: expect(mockResultSet.getObject(index++)).andReturn("true")
119: .anyTimes();
120:
121: /* execute test */
122: index = 1;
123: replayAll();
124: readerUnderTest = new ResultSetColumnReader(mockResultSet);
125: Boolean value = readerUnderTest.getBoolean(index++);
126: assertEquals(true, value);
127: value = readerUnderTest.getBoolean(index++);
128: assertEquals(false, value);
129: value = readerUnderTest.getBoolean(index++);
130: assertEquals(true, value);
131: value = readerUnderTest.getBoolean(index++);
132: assertEquals(false, value);
133: value = readerUnderTest.getBoolean(index++);
134: assertEquals(true, value);
135: value = readerUnderTest.getBoolean(index++);
136: assertEquals(false, value);
137: value = readerUnderTest.getBoolean(index++);
138: assertEquals(true, value);
139:
140: verifyAll();
141: }
142:
143: @Test
144: @Ignore
145: public final void testGetDate() {
146: fail("Not yet implemented"); // TODO
147: }
148:
149: @Test
150: public final void testGetDoubleFromNumber() throws SQLException {
151: int index = 1;
152:
153: /* local expectations */
154: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
155: Types.REAL);
156: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
157: Types.REAL);
158: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
159: Types.REAL);
160: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
161: Types.REAL);
162: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
163: Types.REAL);
164:
165: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
166: Types.VARCHAR);
167:
168: index = 1;
169: expect(mockResultSet.getMetaData()).andReturn(
170: mockResultSetMetaData).anyTimes();
171: expect(mockResultSet.getObject(index++)).andReturn(
172: Long.valueOf(123)).anyTimes();
173: expect(mockResultSet.getObject(index++)).andReturn(
174: Integer.valueOf(345)).anyTimes();
175: expect(mockResultSet.getObject(index++)).andReturn(
176: Float.valueOf((float) 123.5)).anyTimes();
177: expect(mockResultSet.getObject(index++)).andReturn(
178: Short.valueOf((short) 127)).anyTimes();
179: expect(mockResultSet.getObject(index++)).andReturn("567")
180: .anyTimes();
181: expect(mockResultSet.getObject(index++)).andReturn("789")
182: .anyTimes();
183:
184: /* execute test */
185: index = 1;
186: replayAll();
187: readerUnderTest = new ResultSetColumnReader(mockResultSet);
188: Double value = readerUnderTest.getDouble(index++);
189: assertEquals(123, value);
190: value = readerUnderTest.getDouble(index++);
191: assertEquals(345, value);
192: value = readerUnderTest.getDouble(index++);
193: assertEquals(123.5, value);
194: value = readerUnderTest.getDouble(index++);
195: assertEquals(127, value);
196: value = readerUnderTest.getDouble(index++);
197: assertEquals(567, value);
198: value = readerUnderTest.getDouble(index++);
199: assertEquals(789, value);
200:
201: verifyAll();
202: }
203:
204: @Test
205: public final void testGetLong() throws SQLException {
206: int index = 1;
207:
208: /* local expectations */
209: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
210: Types.SMALLINT);
211: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
212: Types.TINYINT);
213: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
214: Types.INTEGER);
215: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
216: Types.BIGINT);
217: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
218: Types.REAL);
219: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
220: Types.VARCHAR);
221: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
222: Types.BIT);
223: expect(mockResultSetMetaData.getColumnType(index++)).andReturn(
224: Types.BIT);
225:
226: index = 1;
227: expect(mockResultSet.getMetaData()).andReturn(
228: mockResultSetMetaData).anyTimes();
229: expect(mockResultSet.getObject(index++)).andReturn(
230: Long.valueOf(123)).anyTimes();
231: expect(mockResultSet.getObject(index++)).andReturn(
232: Integer.valueOf(345)).anyTimes();
233: expect(mockResultSet.getObject(index++)).andReturn("123")
234: .anyTimes();
235: expect(mockResultSet.getObject(index++)).andReturn(
236: Short.valueOf((short) 127)).anyTimes();
237: expect(mockResultSet.getObject(index++)).andReturn("567")
238: .anyTimes();
239: expect(mockResultSet.getObject(index++)).andReturn("789")
240: .anyTimes();
241: expect(mockResultSet.getObject(index++)).andReturn("true")
242: .anyTimes();
243: expect(mockResultSet.getObject(index++)).andReturn("false")
244: .anyTimes();
245:
246: /* execute test */
247: index = 1;
248: replayAll();
249: readerUnderTest = new ResultSetColumnReader(mockResultSet);
250: Long value = readerUnderTest.getLong(index++);
251: assertEquals(123, value);
252: value = readerUnderTest.getLong(index++);
253: assertEquals(345, value);
254: value = readerUnderTest.getLong(index++);
255: assertEquals(123.5, value);
256: value = readerUnderTest.getLong(index++);
257: assertEquals(127, value);
258: value = readerUnderTest.getLong(index++);
259: assertEquals(567, value);
260: value = readerUnderTest.getLong(index++);
261: assertEquals(789, value);
262: value = readerUnderTest.getLong(index++);
263: assertEquals(1, value);
264: value = readerUnderTest.getLong(index++);
265: assertEquals(0, value);
266:
267: verifyAll();
268: }
269:
270: @Test
271: @Ignore
272: public final void testGetObject() {
273: fail("Not yet implemented"); // TODO
274: }
275:
276: @Test
277: @Ignore
278: public final void testGetString() {
279: fail("Not yet implemented"); // TODO
280: }
281:
282: @Test
283: @Ignore
284: public final void testGetTime() {
285: fail("Not yet implemented"); // TODO
286: }
287:
288: @Test
289: @Ignore
290: public final void testGetTimeStamp() {
291: fail("Not yet implemented"); // TODO
292: }
293:
294: @Test
295: @Ignore
296: public final void testWasNull() {
297: fail("Not yet implemented"); // TODO
298: }
299:
300: }
|