Java 类javax.sql.rowset.spi.SyncResolver 实例源码

项目:cn1    文件:SyncResolverTest.java   
public void testNextPreviousConflict() throws Exception {

        RowSetMetaData metadata = new RowSetMetaDataImpl();
        metadata.setColumnCount(DEFAULT_COLUMN_COUNT);

        SyncResolverImpl resolver = new SyncResolverImpl(metadata);
        resolver.addConflictRow(
                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 1,
                SyncResolver.INSERT_ROW_CONFLICT);

        resolver.addConflictRow(
                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 2,
                SyncResolver.INSERT_ROW_CONFLICT);

        assertTrue(resolver.nextConflict());
        assertTrue(resolver.nextConflict());
        assertFalse(resolver.nextConflict());
        assertFalse(resolver.nextConflict());

        assertTrue(resolver.previousConflict());
        assertTrue(resolver.previousConflict());
        assertFalse(resolver.previousConflict());
        assertFalse(resolver.previousConflict());
    }
项目:freeVM    文件:SyncResolverTest.java   
public void testNextPreviousConflict() throws Exception {

        RowSetMetaData metadata = new RowSetMetaDataImpl();
        metadata.setColumnCount(DEFAULT_COLUMN_COUNT);

        SyncResolverImpl resolver = new SyncResolverImpl(metadata);
        resolver.addConflictRow(
                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 1,
                SyncResolver.INSERT_ROW_CONFLICT);

        resolver.addConflictRow(
                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 2,
                SyncResolver.INSERT_ROW_CONFLICT);

        assertTrue(resolver.nextConflict());
        assertTrue(resolver.nextConflict());
        assertFalse(resolver.nextConflict());
        assertFalse(resolver.nextConflict());

        assertTrue(resolver.previousConflict());
        assertTrue(resolver.previousConflict());
        assertFalse(resolver.previousConflict());
        assertFalse(resolver.previousConflict());
    }
项目:cn1    文件:SyncProviderExceptionTest.java   
/**
 * @add tests
 *      {@link javax.sql.rowset.spi.SyncProviderException#SyncProviderException(javax.sql.rowset.spi.SyncResolver)}
 */
public void test_constructor_Ljavax_sql_rowset_spi_SyncResolver() {
    try {
        new SyncProviderException((SyncResolver) null);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // expected
    }
}
项目:cn1    文件:SyncResolverTest.java   
public void testAcceptChanges_InsertException_Two() throws Exception {
    /*
     * Insert a new row. The new row's primary key has existed. Therefore,
     * it should throw SyncProviderException.
     */
    crset = newNoInitialInstance();
    crset.populate(st.executeQuery("select * from USER_INFO"));
    crset.setTableName("USER_INFO");
    crset.moveToInsertRow();
    crset.updateInt(1, 4); // The ID valued 4 has existed in db.
    crset.updateString(2, "test5");
    crset.updateBigDecimal(4, new BigDecimal(12));
    crset.updateTimestamp(12, new Timestamp(874532105));
    crset.insertRow();
    crset.moveToCurrentRow();
    try {
        crset.acceptChanges(conn);
        fail("should throw SyncProviderException");
    } catch (SyncProviderException e) {
        SyncResolver resolver = e.getSyncResolver();
        assertEquals(0, resolver.getRow());
        assertTrue(resolver.nextConflict());
        assertEquals(1, resolver.getRow());
        assertEquals(SyncResolver.INSERT_ROW_CONFLICT, resolver.getStatus());
        for (int i = 1; i <= crset.getMetaData().getColumnCount(); i++) {
            assertNull(resolver.getConflictValue(i));
            try {
                resolver.setResolvedValue(i, null);
                fail("should throw SQLException");
            } catch (SQLException ex) {
                // expected
            }
        }
        assertFalse(resolver.nextConflict());
    }
}
项目:cn1    文件:SyncResolverTest.java   
public void testAcceptChanges_UpdateException_Two() throws Exception {
    /*
     * Update a row which has been updated in database
     */
    crset = newNoInitialInstance();
    crset.populate(st.executeQuery("select * from USER_INFO"));
    int result = st
            .executeUpdate("update USER_INFO set NAME = 'update44' where ID = 4");
    assertEquals(1, result);

    // move to the updated row
    assertTrue(crset.absolute(4));
    assertEquals(4, crset.getInt(1));
    assertEquals("test4", crset.getString(2));
    crset.updateString(2, "change4");
    crset.updateRow();
    try {
        crset.acceptChanges(conn);
        fail("should throw SyncProviderException");
    } catch (SyncProviderException e) {
        SyncResolver resolver = e.getSyncResolver();
        assertEquals(0, resolver.getRow());
        assertTrue(resolver.nextConflict());
        assertEquals(4, resolver.getRow());
        assertEquals(SyncResolver.UPDATE_ROW_CONFLICT, resolver.getStatus());

        if ("true".equals(System.getProperty("Testing Harmony"))) {
            resolver.getConflictValue(1);
        } else {
            try {
                resolver.getConflictValue(1);
                fail("should throw SQLException");
            } catch (SQLException ex) {
                // TODO RI throw SQLException here, maybe RI's bug
            }
        }
        assertFalse(resolver.nextConflict());
    }
}
项目:cn1    文件:SyncResolverTest.java   
public void testAcceptChanges_UpdateException_Three() throws Exception {
    /*
     * Update a row in which one column's value is out of range
     */
    crset = newNoInitialInstance();
    crset.populate(st.executeQuery("select * from USER_INFO"));
    assertEquals(4, crset.size());
    assertTrue(crset.absolute(4));
    assertEquals(4, crset.getInt(1));
    crset.updateString(2, "update4");
    crset.updateLong(3, 555555L);
    crset.updateInt(4, 200000); // 200000 exceeds the NUMERIC's range
    crset.updateBigDecimal(5, new BigDecimal(23));
    crset.updateFloat(8, 4.888F);
    crset.updateRow();
    try {
        crset.acceptChanges(conn);
        fail("should throw SyncProviderException");
    } catch (SyncProviderException e) {
        SyncResolver resolver = e.getSyncResolver();
        assertEquals(0, resolver.getRow());
        assertTrue(resolver.nextConflict());
        assertEquals(4, resolver.getRow());
        assertEquals(SyncResolver.UPDATE_ROW_CONFLICT, resolver.getStatus());

        for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
            assertNull(resolver.getConflictValue(i));
            try {
                resolver.setResolvedValue(i, null);
                fail("should throw SQLException");
            } catch (SQLException ex) {
                // expected
            }
        }
        assertFalse(resolver.nextConflict());
    }
}
项目:cn1    文件:SyncResolverTest.java   
public void testSetResolvedValue_LStringLObject() throws Exception {
    /*
     * Insert a new row. The new row's primary key has existed. Therefore,
     * it should throw SyncProviderException.
     */
    crset = newNoInitialInstance();
    crset.populate(st.executeQuery("select * from USER_INFO"));
    crset.setTableName("USER_INFO");
    crset.moveToInsertRow();
    crset.updateInt(1, 4); // The ID valued 4 has existed in db.
    crset.updateString(2, "test5");
    crset.updateBigDecimal(4, new BigDecimal(12));
    crset.updateTimestamp(12, new Timestamp(874532105));
    crset.insertRow();
    crset.moveToCurrentRow();
    try {
        crset.acceptChanges(conn);
        fail("should throw SyncProviderException");
    } catch (SyncProviderException e) {
        SyncResolver resolver = e.getSyncResolver();

        // before call nextConflict
        resolver.setResolvedValue(null, 1);
        resolver.setResolvedValue("ID", 1);
        resolver.setResolvedValue("not exist", 1);

        assertTrue(resolver.nextConflict());

        resolver.setResolvedValue(null, 1);
        resolver.setResolvedValue("ID", 1);
        assertNull(resolver.getConflictValue("ID"));
        resolver.setResolvedValue("NAME", "hello");
        assertNull(resolver.getConflictValue("NAME"));
        resolver.setResolvedValue("not exist", 1);
    }
}
项目:freeVM    文件:SyncProviderExceptionTest.java   
/**
 * @add tests
 *      {@link javax.sql.rowset.spi.SyncProviderException#SyncProviderException(javax.sql.rowset.spi.SyncResolver)}
 */
public void test_constructor_Ljavax_sql_rowset_spi_SyncResolver() {
    try {
        new SyncProviderException((SyncResolver) null);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // expected
    }
}
项目:cn1    文件:SyncResolverTest.java   
public void testAcceptChanges_InsertException_Three() throws Exception {
    /*
     * Insert a new row. Before inserting the new row, another new row which
     * has the same data is inserted into the DB. However, the current
     * CachedRowSet doesn't know it.
     */
    crset = newNoInitialInstance();
    crset.populate(st.executeQuery("select * from USER_INFO"));
    String insertSQL = "INSERT INTO USER_INFO(ID, NAME, BIGINT_T, NUMERIC_T,DECIMAL_T, SMALLINT_T, "
            + "FLOAT_T, REAL_T, DOUBLE_T, DATE_T, TIME_T, TIMESTAMP_T) VALUES(?, ?, ?, ?, ?, ?,"
            + "?, ?, ?, ?, ?, ? )";
    PreparedStatement preStmt = conn.prepareStatement(insertSQL);
    preStmt.setInt(1, 80);
    preStmt.setString(2, "test" + 80);
    preStmt.setLong(3, 444423L);
    preStmt.setBigDecimal(4, new BigDecimal(12));
    preStmt.setBigDecimal(5, new BigDecimal(23));
    preStmt.setInt(6, 41);
    preStmt.setFloat(7, 4.8F);
    preStmt.setFloat(8, 4.888F);
    preStmt.setDouble(9, 4.9999);
    preStmt.setDate(10, new Date(965324512));
    preStmt.setTime(11, new Time(452368512));
    preStmt.setTimestamp(12, new Timestamp(874532105));
    preStmt.executeUpdate();
    if (preStmt != null) {
        preStmt.close();
    }
    // check the new row in DB
    rs = st.executeQuery("select COUNT(*) from USER_INFO where ID = 80");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));

    // now call CachedRowSet.insertRow()
    crset.setTableName("USER_INFO");
    assertTrue(crset.absolute(3));
    crset.moveToInsertRow();
    crset.updateInt(1, 80);
    crset.updateString(2, "test" + 80);
    crset.updateLong(3, 444423L);
    crset.updateBigDecimal(4, new BigDecimal(12));
    crset.updateBigDecimal(5, new BigDecimal(23));
    crset.updateInt(6, 41);
    crset.updateFloat(7, 4.8F);
    crset.updateFloat(8, 4.888F);
    crset.updateDouble(9, 4.9999);
    crset.updateDate(10, new Date(965324512));
    crset.updateTime(11, new Time(452368512));
    crset.updateTimestamp(12, new Timestamp(874532105));
    crset.insertRow();
    crset.moveToCurrentRow();
    try {
        crset.acceptChanges(conn);
        fail("should throw SyncProviderException");
    } catch (SyncProviderException e) {
        SyncResolver resolver = e.getSyncResolver();
        assertEquals(0, resolver.getRow());
        assertTrue(resolver.nextConflict());
        assertEquals(4, resolver.getRow());
        assertEquals(SyncResolver.INSERT_ROW_CONFLICT, resolver.getStatus());
        for (int i = 1; i <= crset.getMetaData().getColumnCount(); i++) {
            assertNull(resolver.getConflictValue(i));
            try {
                resolver.setResolvedValue(i, null);
                fail("should throw SQLException");
            } catch (SQLException ex) {
                // expected
            }
        }
        assertFalse(resolver.nextConflict());
    }
}
项目:cn1    文件:SyncResolverTest.java   
public void testAcceptChanges_DeleteException_Two() throws Exception {
    /*
     * Delete a row which has been updated in database
     */
    crset = newNoInitialInstance();
    crset.setCommand("SELECT * FROM USER_INFO");
    crset.setUrl(DERBY_URL);
    crset.execute();

    int result = st
            .executeUpdate("update USER_INFO set NAME = 'update44' where ID = 4");
    assertEquals(1, result);
    // move to the updated row
    crset.absolute(4);
    assertEquals(4, crset.getInt(1));
    assertEquals("test4", crset.getString(2));
    crset.deleteRow();
    assertFalse(crset.getShowDeleted());
    try {
        crset.acceptChanges(conn);
        fail("should throw SyncProviderException");
    } catch (SyncProviderException e) {
        SyncResolver resolver = e.getSyncResolver();
        assertEquals(0, resolver.getRow());
        assertTrue(resolver.nextConflict());
        assertEquals(SyncResolver.DELETE_ROW_CONFLICT, resolver.getStatus());
        if ("true".equals(System.getProperty("Testing Harmony"))) {
            assertEquals(4, resolver.getRow());
            assertNull(resolver.getConflictValue(1));
        } else {
            assertEquals(0, resolver.getRow());
            try {
                resolver.getConflictValue(1);
                fail("should throw SQLException");
            } catch (SQLException ex) {
                // expected
            }
        }
        assertFalse(resolver.nextConflict());
    }

    // check database
    rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 4");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));

    // check CachedRowSet
    assertFalse(crset.getShowDeleted());
    crset.beforeFirst();
    assertFalse(crset.absolute(4));

    crset.setShowDeleted(true);
    assertTrue(crset.absolute(4));
    assertEquals(4, crset.getInt(1));
    assertTrue(crset.rowDeleted());
}
项目:cn1    文件:SyncResolverTest.java   
public void testAcceptChanges_MultiConflicts() throws Exception {
    /*
     * Update a row in which one column's value is out of range
     */
    assertTrue(crset.absolute(3));
    assertEquals(3, crset.getInt(1));
    crset.updateString(2, "update4");
    crset.updateLong(3, 555555L);
    crset.updateInt(4, 200000); // 200000 exceeds the NUMERIC's range
    crset.updateBigDecimal(5, new BigDecimal(23));
    crset.updateFloat(8, 4.888F);
    crset.updateRow();

    /*
     * Delete a row which has been deleted from database
     */
    int result = st.executeUpdate("delete from USER_INFO where ID = 1");
    assertEquals(1, result);
    // move to the first row which doesn't exist in database
    assertTrue(crset.absolute(1));
    assertEquals(1, crset.getInt(1));
    crset.deleteRow();

    /*
     * Insert a new row. One given column's value exceeds the max range.
     */
    assertTrue(crset.last());
    crset.setTableName("USER_INFO");
    crset.moveToInsertRow();
    crset.updateInt(1, 5);
    crset.updateString(2, "test5");
    crset.updateLong(3, 555555L);
    crset.updateInt(4, 200000); // 200000 exceeds the NUMERIC's range
    crset.updateBigDecimal(5, new BigDecimal(23));
    crset.updateFloat(8, 4.888F);
    crset.insertRow();
    crset.moveToCurrentRow();

    try {
        crset.acceptChanges(conn);
        fail("should throw SyncProviderException");
    } catch (SyncProviderException e) {
        SyncResolver resolver = e.getSyncResolver();
        assertEquals(0, resolver.getRow());

        try {
            resolver.getConflictValue(1);
            fail("should throw SQLException");
        } catch (SQLException ex) {
            // expected
        }

        assertTrue(resolver.nextConflict());
        assertEquals(SyncResolver.DELETE_ROW_CONFLICT, resolver.getStatus());
        if ("true".equals(System.getProperty("Testing Harmony"))) {
            assertEquals(1, resolver.getRow());
        } else {
            assertEquals(0, resolver.getRow());
        }
        for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
            assertNull(resolver.getConflictValue(i));
        }

        assertTrue(resolver.nextConflict());
        assertEquals(SyncResolver.UPDATE_ROW_CONFLICT, resolver.getStatus());
        assertEquals(3, resolver.getRow());
        for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
            assertNull(resolver.getConflictValue(i));
        }

        assertTrue(resolver.nextConflict());
        assertEquals(SyncResolver.INSERT_ROW_CONFLICT, resolver.getStatus());
        assertEquals(5, resolver.getRow());
        for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
            assertNull(resolver.getConflictValue(i));
        }

        assertFalse(resolver.nextConflict());
    }
}
项目:cn1    文件:CachedRowSetImplTest.java   
public void testCreateCopy2() throws Exception {

        CachedRowSet copy = crset.createCopy();

        copy.absolute(3);
        crset.absolute(3);

        copy.updateString(2, "updated");
        assertEquals("updated", copy.getString(2));
        assertEquals("test3", crset.getString(2));
        copy.updateRow();
        copy.acceptChanges();

        assertEquals("updated", copy.getString(2));
        assertEquals("test3", crset.getString(2));

        crset.updateString(2, "again");

        assertEquals("updated", copy.getString(2));
        assertEquals("again", crset.getString(2));

        crset.updateRow();
        try {
            /*
             * seems ri doesn't release lock when exception throw from
             * acceptChanges(), which will cause test case block at insertData()
             * when next test case setUp, so we must pass current connection to
             * it, and all resource would be released after connection closed.
             */
            crset.acceptChanges(conn);
            fail("Should throw SyncProviderException");
        } catch (SyncProviderException e) {
            SyncResolver resolver = e.getSyncResolver();
            assertEquals(0, resolver.getRow());

            try {
                resolver.getConflictValue(1);
                fail("Should throw SQLException");
            } catch (SQLException ex) {
                // expected, Invalid cursor position
            }

            assertTrue(resolver.nextConflict());
            assertEquals(3, resolver.getRow());

            assertEquals(SyncResolver.UPDATE_ROW_CONFLICT, resolver.getStatus());

            for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
                // all values are null
                assertNull(resolver.getConflictValue(i));
            }

            assertFalse(resolver.nextConflict());
        }

        assertEquals("updated", copy.getString(2));

        crset.absolute(3);
        // data doesn't change
        assertEquals("again", crset.getString(2));
    }
项目:freeVM    文件:CachedRowSetImplTest.java   
public void testCreateCopy2() throws Exception {

        CachedRowSet copy = crset.createCopy();

        copy.absolute(3);
        crset.absolute(3);

        copy.updateString(2, "updated");
        assertEquals("updated", copy.getString(2));
        assertEquals("test3", crset.getString(2));
        copy.updateRow();
        copy.acceptChanges();

        assertEquals("updated", copy.getString(2));
        assertEquals("test3", crset.getString(2));

        crset.updateString(2, "again");

        assertEquals("updated", copy.getString(2));
        assertEquals("again", crset.getString(2));

        crset.updateRow();
        try {
            /*
             * seems ri doesn't release lock when exception throw from
             * acceptChanges(), which will cause test case block at insertData()
             * when next test case setUp, so we must pass current connection to
             * it, and all resource would be released after connection closed.
             */
            crset.acceptChanges(conn);
            fail("Should throw SyncProviderException");
        } catch (SyncProviderException e) {
            SyncResolver resolver = e.getSyncResolver();
            assertEquals(0, resolver.getRow());

            try {
                resolver.getConflictValue(1);
                fail("Should throw SQLException");
            } catch (SQLException ex) {
                // expected, Invalid cursor position
            }

            assertTrue(resolver.nextConflict());
            assertEquals(3, resolver.getRow());

            assertEquals(SyncResolver.UPDATE_ROW_CONFLICT, resolver.getStatus());

            for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
                // all values are null
                assertNull(resolver.getConflictValue(i));
            }

            assertFalse(resolver.nextConflict());
        }

        assertEquals("updated", copy.getString(2));

        crset.absolute(3);
        // data doesn't change
        assertEquals("again", crset.getString(2));
    }