/** with limited metadata info. */ private ResultsMessage exampleResultsMsg2a() { ResultsMessage resultsMsg = exampleResultsMsg2(); List[] results = exampleResults2(); resultsMsg.setDataTypes(dataTypes()); resultsMsg.setColumnNames(columnNames()); resultsMsg.setResults(results); resultsMsg.setFinalRow(results.length); resultsMsg.setLastRow(results.length); resultsMsg.setFirstRow(1); return resultsMsg; }
/** without metadata info. */ private ResultsMessage exampleResultsMsg1() { return exampleMessage(exampleResults1(5), new String[] { "IntNum" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER }); //$NON-NLS-1$ }
@Test public void testGetMetaData() throws Exception { ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg2a(), statement); ResultSetMetaData rmetadata = rs.getMetaData(); assertEquals(2, rmetadata.getColumnCount()); String[] columnNames = columnNames(); String[] dataTypes = dataTypes(); for (int i = 0; i < 2; i++) { assertEquals(columnNames[i], rmetadata.getColumnLabel(i + 1)); assertEquals(dataTypes[i], rmetadata.getColumnTypeName(i + 1)); } rs.close(); }
/** without metadata info. */ private ResultsMessage exampleResultsMsg2() { return exampleMessage(exampleResults2(), new String[] { "IntNum", "StringNum" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.STRING }); //$NON-NLS-1$ //$NON-NLS-2$ }
@Test public void testPrevious() throws Exception { List[] results = exampleResults1(5); ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg1(), statement); while (rs.next()) { // just walk to the end; } // walk reversely; int i = results.length - 1; while (rs.previous()) { List expected = new ArrayList(1); expected.add(new Integer(i + 1)); assertEquals(expected, rs.getCurrentRecord()); i--; } rs.close(); }
private ResultSetImpl helpGetResultSetImpl(int type) throws SQLException { ResultsMessage rsMsg = exampleResultsMsg2(); statement = TestResultSet.createMockStatement(type); ResultSetImpl rs = new ResultSetImpl(rsMsg, statement); return rs; }
static List<Object>[] exampleResults1(int length) { return exampleResults1(length, 1); }
@Test public void testGetCurrentRecord() throws Exception { List[] results = exampleResults2(); ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg2(), statement); rs.next(); List actual = rs.getCurrentRecord(); assertEquals(results[0], actual); rs.close(); }
/** test getObject() at columnIndex = 2 of 5th row */ @Test public void testGetObject1() throws Exception { List[] results = exampleResults2(); ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg2a(), statement); String actual = null; String expected = "a3"; //$NON-NLS-1$ // move cursor to the 4th row for (int i = 0; i < results.length - 2; i++) { rs.next(); } // only compare the 4th row's 2nd column if (rs.next()) { actual = (String) rs.getObject(2); } assertEquals(expected, actual); rs.close(); }
/** test next(), get result set and close without walking through */ @Test public void testNext3() throws Exception { ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg1(), statement); assertEquals(new Integer(0), new Integer(rs.getRow())); rs.close(); }
@Test public void testNotCallingNext() throws SQLException { ResultSetImpl cs = new ResultSetImpl(exampleResultsMsg2a(), statement); try { cs.getObject(1); fail("Exception expected"); //$NON-NLS-1$ } catch (SQLException e) { assertEquals("The cursor is not on a valid row.", e.getMessage()); //$NON-NLS-1$ } }
/** no rows. */ private ResultsMessage exampleResultsMsg3() { return exampleMessage(new List[0], new String[] { "IntNum", "StringNum" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.STRING }); //$NON-NLS-1$ //$NON-NLS-2$ }
@Override public ResultsFuture<ResultsMessage> answer( InvocationOnMock invocation) throws Throwable { ResultsFuture<ResultsMessage> nextBatch = new ResultsFuture<ResultsMessage>(); int begin = Math.min(totalLength, (Integer)invocation.getArguments()[1]); if (partial && begin == fetchSize + 1) { begin = begin -5; } int length = Math.min(fetchSize, Math.min(totalLength - begin + 1, batchLength)); nextBatch.getResultsReceiver().receiveResults(exampleResultsMsg4(begin, length, begin + length - 1>= totalLength)); return nextBatch; } });
private ResultSetImpl helpGetNoResults(int type) throws SQLException { ResultsMessage rsMsg = exampleResultsMsg3(); statement = TestResultSet.createMockStatement(type); ResultSetImpl rs = new ResultSetImpl(rsMsg, statement); return rs; }
/** test hasNext(), actual result set should return TRUE. */ @Test public void testHasNext2() throws Exception { List[] results = exampleResults1(5); ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg1(), statement); for (int i = 1; i < results.length; i++) { rs.next(); } boolean actual = rs.hasNext(); boolean expected = true; assertEquals(expected, actual); rs.close(); }
@Test public void testClose() throws Exception { ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg2(), statement); rs.close(); verify(statement, times(0)).close(); }
/** test next() with walking through all the rows and compare records */ @Test public void testNext2() throws SQLException { List<?>[] expected = TestAllResultsImpl.exampleResults1(1000); ResultSetImpl cs = helpExecuteQuery(); int i=0; while(cs.next()) { assertEquals(" Actual doesn't match with expected. ", expected[i], cs.getCurrentRecord()); //$NON-NLS-1$ assertEquals((i < 800?BATCH_SIZE:200) - (i%BATCH_SIZE) - 1, cs.available()); i++; } cs.close(); }
/** * test next(), whether the result set's cursor is positioned on next row or * not */ @Test public void testNext1() throws Exception { ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg1(), statement); // point to first row boolean actual = rs.next(); boolean expected = true; assertEquals(" Actual doesn't match with expected. ", expected, actual); //$NON-NLS-1$ rs.close(); }
@Test public void testFindColumn() throws Exception { ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg2a(), statement); assertEquals(1, rs.findColumn("IntNum")); //$NON-NLS-1$ rs.close(); }
@Test public void testGetters() throws SQLException{ TimeZone.setDefault(TimeZone.getTimeZone("GMT-05:00")); //$NON-NLS-1$ ResultsMessage message = exampleMessage(new List[] { Arrays.asList(1, TimestampUtil.createTime(0, 0, 0), TimestampUtil.createDate(1, 1, 1), TimestampUtil.createTimestamp(1, 1, 1, 1, 1, 1, 1), "<root/>") }, //$NON-NLS-1$ new String[] { "int", "time", "date", "timestamp", "sqlxml" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ new String[] { DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.TIME, DataTypeManager.DefaultDataTypes.DATE, DataTypeManager.DefaultDataTypes.TIMESTAMP, DataTypeManager.DefaultDataTypes.STRING }); TimestampWithTimezone.resetCalendar(TimeZone.getTimeZone("GMT-06:00")); //$NON-NLS-1$ ResultSetImpl rs = new ResultSetImpl(message, statement); assertTrue(rs.next()); assertEquals(Boolean.TRUE.booleanValue(), rs.getBoolean(1)); assertEquals(1, rs.getShort(1)); assertEquals(1, rs.getInt(1)); assertEquals(1l, rs.getLong(1)); assertEquals(1f, rs.getFloat(1), 0); assertEquals(1d, rs.getDouble(1), 0); assertEquals("1", rs.getString(1)); //$NON-NLS-1$ assertEquals(Integer.valueOf(1), rs.getObject(1)); //the mock statement is in GMT-6 the server results are from GMT-5, so we expect them to display the same assertEquals(TimestampUtil.createTime(0, 0, 0), rs.getTime(2)); assertEquals(TimestampUtil.createDate(1, 1, 1), rs.getDate(3)); assertEquals(TimestampUtil.createTimestamp(1, 1, 1, 1, 1, 1, 1), rs.getTimestamp(4)); assertEquals("<root/>", rs.getSQLXML(5).getString()); //$NON-NLS-1$ try { rs.getSQLXML(1); } catch (SQLException e) { assertEquals("Unable to transform the column value 1 to a SQLXML.", e.getMessage()); //$NON-NLS-1$ } assertFalse(rs.next()); TimestampWithTimezone.resetCalendar(null); }