MockResultSet rs = new MockResultSet("myMock"); rs.addColumn("columnA", new Integer[]{1}); rs.addColumn("columnB", new String[]{"Column B Value"}); rs.addColumn("columnC", new Double[]{2}); // make sure to move the cursor to the first row try { rs.next(); } catch (SQLException sqle) { fail("unable to move resultSet"); } // process the result set MyObject obj = processor.processResultSet(rs); // run your tests using the ResultSet like you normally would assertEquals(1, obj.getColumnAValue()); assertEquals("Column B Value", obj.getColumnBValue()); assertEquals(2.0d, obj.getColumnCValue());
/** * Build a mock implementation of Result using Mockito */ private ResultSet buildMockResultSet() { MockResultSet mrs = new MockResultSet(StringUtils.EMPTY); for (MockJdbcColumn column : COLUMNS) { mrs.addColumn(column.getColumnName(), ImmutableList.of(column.getValue())); } return mrs; } }
private void configureMetadata() throws SQLException { ((MockDatabaseMetaData) cx.getMetaData()).setSearchStringEscape(""); MockResultSet tableTypes = new MockResultSet("TABLE_TYPES"); tableTypes.addColumn("TABLE_TYPE"); tableTypes.addRow(new Object[] {"TABLE"}); ((MockDatabaseMetaData) cx.getMetaData()).setTableTypes(tableTypes); MockResultSet tables = new MockResultSet("TABLES"); tables.addColumn("TABLE_SCHEM"); tables.addColumn("TABLE_NAME"); tables.addRow(new Object[] {"", SAMPLE_FEATURE_NAME}); ((MockDatabaseMetaData) cx.getMetaData()).setTables(tables); MockResultSet key = new MockResultSet("KEY"); key.addColumn("COLUMN_NAME"); key.addColumn("DATA_TYPE"); key.addRow(new Object[] {"ID", 1}); ((MockDatabaseMetaData) cx.getMetaData()) .setPrimaryKeys(null, null, SAMPLE_FEATURE_NAME, key); MockResultSet columns = new MockResultSet("COLUMNS"); columns.addColumn("COLUMN_NAME"); columns.addColumn("DATA_TYPE"); columns.addRow(new Object[] {"ID", 1}); ((MockDatabaseMetaData) cx.getMetaData()) .setColumns(null, null, SAMPLE_FEATURE_NAME, "ID", columns); }
public boolean absolute(int row) throws SQLException { if(isCursorInInsertRow) throw new SQLException("cursor is in insert row"); checkResultSetType(); if(getRowCount() == 0) return false; if(row > 0) cursor = row - 1; if(row < 0) cursor = getRowCount() + row; adjustCursor(); return isCurrentRowValid(); }
public ResultSet getResultSet(long index, int count) throws SQLException { if(wasFreeCalled) { throw new SQLException("free() was called"); } Integer[] firstColumn = new Integer[count]; for(int ii = 0; ii < count; ii++) { firstColumn[ii] = ii + 1; } Object[] secondColumn = ArrayUtil.convertToObjectArray(array); secondColumn = (Object[])ArrayUtil.truncateArray(secondColumn, (int)(index - 1), count); MockResultSet resultSet = new MockResultSet(String.valueOf(hashCode())); resultSet.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE); resultSet.setResultSetConcurrency(ResultSet.CONCUR_READ_ONLY); resultSet.addColumn(firstColumn); resultSet.addColumn(secondColumn); return resultSet; }
/** * Adds a column to the simulated database table. * The column will get the name <i>ColumnX</i> where * <i>X</i> is the column index. The first added column * will have the name <i>Column1</i>. No data will be stored * in the column. */ public void addColumn() { addColumn(determineValidColumnName()); }
for(int yy = 0; yy < missingColumns; yy++) addColumn(); adjustColumns(); for(int ii = 0; ii < values.size(); ii++) nextColumnList.add(nextValue); adjustColumns(); copyColumnMap(); adjustFlags();
store.setDataSource(jdbcMock.getMockDataSource()); MockResultSet tableTypes = new MockResultSet("tableTypes"); tableTypes.addColumn("TABLE_TYPE", Arrays.asList("TABLE")); MockResultSet tables = new MockResultSet("tables"); tables.addColumn("TABLE_NAME", Arrays.asList("foo")); tables.addColumn("TABLE_SCHEM", Arrays.asList("")); when(source.getDataStore()).thenReturn(store); MockResultSet rowData = new MockResultSet("foo"); rowData.addColumn("name", Arrays.asList("foo", "bar", "baz")); rowData.setStatement(new MockStatement(cx));
/** * Adds a column to the simulated database table. * The column will get the specified name. * No data will be stored in the column. * @param columnName the column name */ public void addColumn(String columnName) { addColumn(columnName, new ArrayList()); }
/** * Adds a column to the simulated database table. * The column will get the specified name. * The specified data will be stored in the new column. If there * are other columns with not enough rows, the other * columns will be extended and filled with <code>null</code> * values. * @param columnName the column name * @param values the column data as <code>List</code>, the index * in the <code>List</code> corresponds to the row * index, i.e. values.get(0) will be stored in the first * row and so on */ public void addColumn(String columnName, List<Object> values) { List<Object> column = new ArrayList<Object>(values); columnMap.put(columnName, column); columnNameList.add(columnName); adjustColumns(); adjustInsertRow(); copyColumnMap(); adjustFlags(); }
public ResultSet getGeneratedKeys() throws SQLException { if(null == lastGeneratedKeys) { MockResultSet resultSet = new MockResultSet("Last statement did not generate any keys"); resultSet.setStatement(this); return resultSet; } return cloneResultSet(lastGeneratedKeys); }
@Override public ResultSet executeQuery() throws SQLException { // check if the condition ID = ? is present in the query if (sql.matches("^.*\"ID\"\\s+=\\s+\\?.*$")) { filteredOnIds = true; } return new MockResultSet(SAMPLE_FEATURE_NAME); } };
public boolean next() throws SQLException { if(isCursorInInsertRow) throw new SQLException("cursor is in insert row"); if(getRowCount() == 0) return false; cursor++; adjustCursor(); return isCurrentRowValid(); }
private void init() { columnMap = createCaseAwareMap(); columnNameList = new ArrayList<String>(); updatedRows = new ArrayList<Boolean>(); deletedRows = new ArrayList<Boolean>(); insertedRows = new ArrayList<Boolean>(); cursor = -1; wasNull = false; closed = false; isCursorInInsertRow = false; isDatabaseView = false; resultSetMetaData = null; copyColumnMap(); adjustInsertRow(); }
protected MockResultSet cloneResultSet(MockResultSet resultSet) { if(null == resultSet) return null; MockResultSet clone = (MockResultSet)resultSet.clone(); clone.setStatement(this); return clone; }
protected MockResultSet cloneResultSet(MockResultSet resultSet) { if (resultSetConcurrency == ResultSet.CONCUR_READ_ONLY) { // no need to clone return resultSet.shallowCopy(); } if(null == resultSet) return null; MockResultSet clone = (MockResultSet)resultSet.clone(); clone.setStatement(this); return clone; }
public MockResultSet create(String id) { MockResultSet resultSet = new MockResultSet(id); if (columnsCaseSensitive) { resultSet.setColumnsCaseSensitive(true); } return resultSet; } }
String selectSQL = "someselectStmt"; StatementResultSetHandler stmtHandler = conn.GetStatementResultSetHandler(); MockResultSet result = stmtHandler.createResultSet(); result.addRow(new Integer[]{new Integer(1)}); stmtHandler.prepareResultSet(selectSQL, result);
@Test public void testEmpty() throws Exception { MockResultSet resultSet = new MockResultSet("data"); StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = new JsonWriter(stringWriter); jsonWriter.beginObject(); MetricDataRowCallbackHandler handler = new MetricDataRowCallbackHandler( jsonWriter, new MetricDataQueryParams(0, 3, 1), Collections.emptySet() ); while (resultSet.next()) { handler.processRow(resultSet); } handler.finish(); jsonWriter.endObject(); Assert.assertEquals("{}", stringWriter.toString()); }
public void moveToInsertRow() throws SQLException { adjustCursorForInsert(); isCursorInInsertRow = true; }