Java Code Examples for com.mockrunner.mock.jdbc.MockResultSet#addRow()

The following examples show how to use com.mockrunner.mock.jdbc.MockResultSet#addRow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MockJdbcUtils.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Constructs a mock Column metadata {@link ResultSet} describing fake columns of the tables.
 */
private static ResultSet createColumnMetadataResultSet(String... tableNames) {
  MockResultSet mockResultSet = initResultSet(COLUMN_METADATA_LABELS);

  for (int i = 0; i < tableNames.length; i++) {
    Object[] row = new Object[COLUMN_METADATA_LABELS.length];
    Arrays.fill(row, "");
    row[2] = tableNames[i];
    row[3] = "column";
    row[4] = Types.VARCHAR;
    row[5] = "string(255)";
    row[6] = 255;
    row[7] = 0;
    row[8] = 0;
    mockResultSet.addRow(row);
  }

  return mockResultSet;
}
 
Example 2
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void testPatchRetrievesSetWithPatchesApplied () throws SQLException, MigrationException {
    
    handler = conn.getPreparedStatementResultSetHandler();
    
	MockDatabaseMetaData mdmd = (MockDatabaseMetaData) conn.getMetaData();
    MockResultSet mrs = handler.createResultSet();
    mrs.addRow(new String[]{"", "", "", "system_name"});
    mrs.addRow(new String[]{"", "", "", "patch_level"});
    mdmd.setPrimaryKeys(mrs);
    
    MockResultSet rs = handler.createResultSet();
    rs.addColumn("patch_level", new Object[]{1, 2});
    handler.prepareGlobalResultSet(rs);

    Set<Integer> expected = new HashSet<Integer>();
    expected.add(1);
    expected.add(2);
    assertEquals(expected, table.getPatchesApplied());
    commonVerifications();
    verifyPreparedStatementPresent(table.getSql("patches.all"));
}
 
Example 3
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validates that the system recognizes an existing patches table.
 * 
 * @throws Exception if an unexpected error occurs
 */
public void testVerifyPatchesTable() throws Exception
{
    // Test-specific setup
    handler = conn.getPreparedStatementResultSetHandler();
    MockResultSet rs = handler.createResultSet();
    
    MockDatabaseMetaData mdmd = (MockDatabaseMetaData) conn.getMetaData();
    MockResultSet mrs = handler.createResultSet();
    mrs.addRow(new String[]{"", "", "", "system_name"});
    mrs.addRow(new String[]{"", "", "", "patch_level"});
    mdmd.setPrimaryKeys(mrs);
    handler.prepareGlobalResultSet(rs);
    rs.addRow(new Integer[] {new Integer(13)});
    
    table.createPatchStoreIfNeeded();
    
    commonVerifications();
    verifyNotCommitted();
    verifyPreparedStatementParameter(0, 1, "milestone");
    verifyPreparedStatementNotPresent(table.getSql("patches.upgrade"));
    verifyPreparedStatementNotPresent(table.getSql("patches.create"));
}
 
Example 4
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validates that the system recognizes an existing patches table.
 * 
 * @throws Exception if an unexpected error occurs
 */
public void testVerifyAndUpgradePatchesTable() throws Exception
{
    // Test-specific setup
    handler = conn.getPreparedStatementResultSetHandler();
    MockResultSet rs = handler.createResultSet();
    
    MockDatabaseMetaData mdmd = (MockDatabaseMetaData) conn.getMetaData();
    MockResultSet mrs = handler.createResultSet();
    mrs.addRow(new String[]{"", "", "", "system_name"});
    mdmd.setPrimaryKeys(mrs);
    handler.prepareGlobalResultSet(rs);
    rs.addRow(new Integer[] {new Integer(13)});
    
    table.createPatchStoreIfNeeded();
    
    commonVerifications();
    verifyCommitted();
    verifyPreparedStatementParameter(0, 1, "milestone");
    verifyPreparedStatementPresent(table.getSql("patches.upgrade"));
    verifyPreparedStatementNotPresent(table.getSql("patches.create"));
}
 
Example 5
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validates that <code>getPatchLevel</code> works on an existing system.
 * 
 * @throws Exception if an unexpected error occurs
 */
public void testGetPatchLevel() throws Exception
{
    // Test-specific setup
    handler = conn.getPreparedStatementResultSetHandler();
    
    MockDatabaseMetaData mdmd = (MockDatabaseMetaData) conn.getMetaData();
    MockResultSet mrs = handler.createResultSet();
    mrs.addRow(new String[]{"", "", "", "system_name"});
    mrs.addRow(new String[]{"", "", "", "patch_level"});
    mdmd.setPrimaryKeys(mrs);
    
    MockResultSet rs = handler.createResultSet();
    rs.addRow(new Integer[]{new Integer(13)});
    handler.prepareGlobalResultSet(rs);

    int i = table.getPatchLevel();

    assertEquals(13, i);
    commonVerifications();
    verifyNotCommitted();
    verifyPreparedStatementParameter(1, 1, "milestone");
    verifyPreparedStatementNotPresent(table.getSql("patches.upgrade"));
    verifyPreparedStatementNotPresent(table.getSql("level.create"));
}
 
Example 6
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validates that the patch level can be updated.
 *  
 * @throws Exception if an unexpected error occurs
 */
public void testUpdatePatchLevel() throws Exception
{
    handler = conn.getPreparedStatementResultSetHandler();
    
    MockDatabaseMetaData mdmd = (MockDatabaseMetaData) conn.getMetaData();
    MockResultSet mrs = handler.createResultSet();
    mrs.addRow(new String[]{"", "", "", "system_name"});
    mrs.addRow(new String[]{"", "", "", "patch_level"});
    mdmd.setPrimaryKeys(mrs);
    
    MockResultSet rs = handler.createResultSet();
    rs.addRow(new Integer[]{new Integer(12)});
    handler.prepareResultSet(table.getSql("level.read"), rs, new String[]{"milestone"});

    table.updatePatchLevel(13);
    
    verifyPreparedStatementParameter(table.getSql("level.update"), 1, new Integer(13));
    verifyPreparedStatementParameter(table.getSql("level.update"), 2, "milestone");
    commonVerifications();
    verifyCommitted();
}
 
Example 7
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validates that <code>isPatchTableLocked</code> works when no lock exists.
 * 
 * @throws Exception if an unexpected error occurs
 */
public void testIsPatchTableNotLocked() throws Exception
{
    // Test-specific setup
    // Return a non-empty set in response to the patch lock query
    handler = conn.getPreparedStatementResultSetHandler();
    
    MockDatabaseMetaData mdmd = (MockDatabaseMetaData) conn.getMetaData();
    MockResultSet mrs = handler.createResultSet();
    mrs.addRow(new String[]{"", "", "", "system_name"});
    mrs.addRow(new String[]{"", "", "", "patch_level"});
    mdmd.setPrimaryKeys(mrs);
    
    MockResultSet rs = handler.createResultSet();
    rs.addRow(new String[]{"F"});
    handler.prepareResultSet(table.getSql("lock.read"), rs, new String[]{"milestone", "milestone"});
    
    assertFalse(table.isPatchStoreLocked());
    commonVerifications();
    verifyNotCommitted();
}
 
Example 8
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validates that <code>isPatchTableLocked</code> works when a lock already exists.
 * 
 * @throws Exception if an unexpected error occurs
 */
public void testIsPatchTableLocked() throws Exception
{
    // Test-specific setup
    // Return a non-empty set in response to the patch lock query
    handler = conn.getPreparedStatementResultSetHandler();
    
    MockDatabaseMetaData mdmd = (MockDatabaseMetaData) conn.getMetaData();
    MockResultSet mrs = handler.createResultSet();
    mrs.addRow(new String[]{"", "", "", "system_name"});
    mrs.addRow(new String[]{"", "", "", "patch_level"});
    mdmd.setPrimaryKeys(mrs);
    
    MockResultSet rs = handler.createResultSet();
    rs.addRow(new String[]{"T"});
    handler.prepareResultSet(table.getSql("lock.read"), rs, new String[]{"milestone", "milestone"});
    
    assertTrue(table.isPatchStoreLocked());
    commonVerifications();
    verifyNotCommitted();
}
 
Example 9
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validates that the patches table can be locked as long as no other lock
 * is in place.
 * 
 * @throws Exception if an unexpected error occurs
 */
public void testLockPatchTableWhenNotAlreadyLocked() throws Exception
{
    // Test-specific setup
    // Return an empty set in response to the patch lock query
    handler = conn.getPreparedStatementResultSetHandler();
    
    MockDatabaseMetaData mdmd = (MockDatabaseMetaData) conn.getMetaData();
    MockResultSet mrs = handler.createResultSet();
    mrs.addRow(new String[]{"", "", "", "system_name"});
    mrs.addRow(new String[]{"", "", "", "patch_level"});
    mdmd.setPrimaryKeys(mrs);
    
    MockResultSet rs = handler.createResultSet();
    handler.prepareUpdateCount(table.getSql("lock.obtain"), 1, new String[] {"milestone", "milestone"});
    
    table.lockPatchStore();
    verifyPreparedStatementParameter(table.getSql("lock.obtain"), 1, "milestone");
    verifyPreparedStatementParameter(table.getSql("lock.obtain"), 2, "milestone");
    commonVerifications();
    verifyCommitted();
}
 
Example 10
Source File: JdbcBenchmark.java    From spring-data-dev-tools with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void convertWithSpringData(Blackhole sink) throws Exception {

	MockResultSet resultSet = new MockResultSet("book");
	resultSet.addColumns(columns);
	resultSet.addRow(values);
	resultSet.next();

	sink.consume(this.bookEntityMapper.mapRow(resultSet, 1));
}
 
Example 11
Source File: ResultSetTester.java    From fastods with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param head the header
 * @param rows the rows of the RS
 * @return a RS object
 */
public MockResultSet createResultSet(final Iterable<String> head,
                                     final Iterable<List<Object>> rows) {
    final MockResultSet rs = this.resultSetHandler.createResultSet();
    for (final String name : head) {
        rs.addColumn(name);
    }

    this.resultSetHandler.prepareGlobalResultSet(rs);
    for (final List<Object> row : rows) {
        rs.addRow(row);
    }
    return rs;
}
 
Example 12
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void testIsPatchApplied() throws MigrationException
{
    handler = conn.getPreparedStatementResultSetHandler();
    MockResultSet rs = handler.createResultSet();
    rs.addRow(new Integer[]{new Integer(3)});
    handler.prepareGlobalResultSet(rs);

    assertEquals(true, table.isPatchApplied(3));
    commonVerifications();
    verifyPreparedStatementPresent(table.getSql("level.exists"));

}
 
Example 13
Source File: PatchTableTest.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Validates that an <code>IllegalStateException</code> is thrown when trying
 * to lock an already locked patches table.
 * 
 * @throws Exception if an unexpected error occurs
 */
public void testLockPatchTableWhenAlreadyLocked() throws Exception
{
    // Test-specific setup
    // Return a non-empty set in response to the patch lock query
    handler = conn.getPreparedStatementResultSetHandler();
    
    MockDatabaseMetaData mdmd = (MockDatabaseMetaData) conn.getMetaData();
    MockResultSet mrs = handler.createResultSet();
    mrs.addRow(new String[]{"", "", "", "system_name"});
    mrs.addRow(new String[]{"", "", "", "patch_level"});
    mdmd.setPrimaryKeys(mrs);
    
    handler.prepareUpdateCount(table.getSql("lock.obtain"), 0, new String[] {"milestone", "milestone"});
    
    try
    {
        table.lockPatchStore();
        fail("Expected an IllegalStateException since a lock already exists.");
    }
    catch (IllegalStateException e)
    {
        // Expected
    }
    
    verifyPreparedStatementParameter(table.getSql("lock.obtain"), 1, "milestone");
    verifyPreparedStatementParameter(table.getSql("lock.obtain"), 2, "milestone");
    commonVerifications();
    verifyCommitted();
}
 
Example 14
Source File: MockJdbcUtils.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Constructs a {@link MockResultSet} containing database metadata about indices for testing.
 */
private static ResultSet createIndexMetadataResultSet(String... indexNames) {
  MockResultSet mockResultSet = initResultSet("INDEX_NAME");

  for (int i = 0; i < indexNames.length; i++) {
    String[] row = new String[]{indexNames[i]};
    mockResultSet.addRow(row);
  }

  return mockResultSet;

}
 
Example 15
Source File: MockJdbcUtils.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Constructs a mock Table metadata {@link ResultSet} describing table names.
 */
private static ResultSet createTableMetadataResultSet(String... tableNames) {
  MockResultSet mockResultSet = initResultSet(TABLE_METADATA_COLUMNS);

  for (int i = 0; i < tableNames.length; i++) {
    String[] row = new String[TABLE_METADATA_COLUMNS.length];
    Arrays.fill(row, "");
    row[2] = tableNames[i];
    row[3] = "TABLE";
    mockResultSet.addRow(row);
  }

  return mockResultSet;
}
 
Example 16
Source File: JdbcMigrationLauncherTest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test doing migrations with a lock override
 *
 * @throws Exception if there is a problem
 */
public void testLockOverride() throws Exception {
    // Setup enough for the first
    PreparedStatementResultSetHandler h = conn.getPreparedStatementResultSetHandler();
    MockResultSet rs = h.createResultSet();
    rs.addRow(new Integer[]{new Integer(0)});
    h.prepareGlobalResultSet(rs);


    MockControl mockControl = MockControl.createStrictControl(PatchInfoStore.class);
    PatchInfoStore patchStore = (PatchInfoStore) mockControl.getMock();

    // First they see if it is locked three times, and it is, so they spin
    patchStore.isPatchStoreLocked();
    mockControl.setReturnValue(true);
    patchStore.isPatchStoreLocked();
    mockControl.setReturnValue(true);
    patchStore.isPatchStoreLocked();
    mockControl.setReturnValue(true);
    patchStore.isPatchStoreLocked();
    mockControl.setReturnValue(true);

    // after the third time, they unlock it
    patchStore.unlockPatchStore();

    // now the lock succeeds
    patchStore.isPatchStoreLocked();
    mockControl.setReturnValue(false);
    patchStore.getPatchLevel();
    mockControl.setReturnValue(2);
    patchStore.lockPatchStore();

    IMocksControl migrationRunnerStrategyControl = createStrictControl();
    MigrationRunnerStrategy migrationStrategyMock = migrationRunnerStrategyControl.createMock(MigrationRunnerStrategy.class);
    expect(migrationStrategyMock.shouldMigrationRun(anyInt(), eq(patchStore))).andReturn(true).anyTimes();

    patchStore.updatePatchLevel(4);
    patchStore.updatePatchLevel(5);
    patchStore.updatePatchLevel(6);
    patchStore.updatePatchLevel(7);
    patchStore.unlockPatchStore();

    TestJdbcMigrationLauncher testLauncher = new TestJdbcMigrationLauncher(context);
    testLauncher.getMigrationProcess().setMigrationRunnerStrategy(migrationStrategyMock);
    testLauncher.setLockPollMillis(0);
    testLauncher.setLockPollRetries(3);
    testLauncher.setIgnoreMigrationSuccessfulEvents(false);
    testLauncher.setPatchStore(patchStore);
    testLauncher.setPatchPath("com.tacitknowledge.util.migration.tasks.normal");

    mockControl.replay();
    migrationRunnerStrategyControl.replay();
    testLauncher.doMigrations();
    mockControl.verify();
}
 
Example 17
Source File: JdbcMigrationLauncherTest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test doing migrations with a lock race from a quick cluster
 *
 * @throws Exception if there is a problem
 */
public void testDoMigrationsWithLockRace() throws Exception {
    // Setup enough for the first
    PreparedStatementResultSetHandler h = conn.getPreparedStatementResultSetHandler();
    MockResultSet rs = h.createResultSet();
    rs.addRow(new Integer[]{new Integer(0)});
    h.prepareGlobalResultSet(rs);


    MockControl mockControl = MockControl.createStrictControl(PatchInfoStore.class);
    PatchInfoStore patchStore = (PatchInfoStore) mockControl.getMock();

    // First they see if it is locked, and it is, so they spin
    patchStore.isPatchStoreLocked();
    mockControl.setReturnValue(true);

    // Second they see if it is locked again, and it isn't, so they try and fail and spin
    patchStore.isPatchStoreLocked();
    mockControl.setReturnValue(false);
    patchStore.getPatchLevel();
    mockControl.setReturnValue(0);
    patchStore.lockPatchStore();
    mockControl.setThrowable(new IllegalStateException("The table is already locked"));

    // Finally they see if it is locked again, and it isn't, and it works
    patchStore.isPatchStoreLocked();
    mockControl.setReturnValue(false);


    patchStore.getPatchLevel();
    mockControl.setReturnValue(2, MockControl.ONE_OR_MORE);
    patchStore.lockPatchStore();

    IMocksControl migrationRunnerStrategyControl = createStrictControl();
    MigrationRunnerStrategy migrationStrategyMock = migrationRunnerStrategyControl.createMock(MigrationRunnerStrategy.class);
    expect(migrationStrategyMock.shouldMigrationRun(anyInt(), eq(patchStore))).andReturn(true).anyTimes();

    patchStore.updatePatchLevel(4);
    patchStore.updatePatchLevel(5);
    patchStore.updatePatchLevel(6);
    patchStore.updatePatchLevel(7);
    patchStore.unlockPatchStore();

    mockControl.replay();
    migrationRunnerStrategyControl.replay();

    TestJdbcMigrationLauncher testLauncher = new TestJdbcMigrationLauncher(context);
    testLauncher.getMigrationProcess().setMigrationRunnerStrategy(migrationStrategyMock);
    testLauncher.setLockPollMillis(0);
    testLauncher.setLockPollRetries(4);
    testLauncher.setIgnoreMigrationSuccessfulEvents(false);
    testLauncher.setPatchStore(patchStore);
    testLauncher.setPatchPath("com.tacitknowledge.util.migration.tasks.normal");
    testLauncher.doMigrations();
    mockControl.verify();
}