From c7b3782a695a687307b3f612c509b0eb528d356b Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 10 May 2013 17:59:23 +0200 Subject: [PATCH] [#1846] Added more test cases --- .../test/_/testcases/KeepResultSetTests.java | 91 +++++++++++++++++++ .../src/org/jooq/test/jOOQAbstractTest.java | 15 +++ .../java/org/jooq/impl/AbstractRecord.java | 42 +++++---- 3 files changed, 131 insertions(+), 17 deletions(-) diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/KeepResultSetTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/KeepResultSetTests.java index 91afafa311..db6d3cadf1 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/KeepResultSetTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/KeepResultSetTests.java @@ -40,13 +40,16 @@ import static org.jooq.KeepResultSetMode.CLOSE_AFTER_FETCH; import static org.jooq.KeepResultSetMode.KEEP_AFTER_FETCH; import static org.jooq.KeepResultSetMode.UPDATE_ON_CHANGE; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.sql.Date; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Collections; import org.jooq.Cursor; import org.jooq.Record1; @@ -56,6 +59,7 @@ import org.jooq.Record6; import org.jooq.Result; import org.jooq.TableRecord; import org.jooq.UpdatableRecord; +import org.jooq.exception.DataAccessException; import org.jooq.test.BaseTest; import org.jooq.test.jOOQAbstractTest; @@ -105,8 +109,10 @@ extends BaseTest c1 = create().selectFrom(TBook()).keepResultSet(CLOSE_AFTER_FETCH).fetchLazy(); + assertTrue(c1.closesAfterFetch()); while (c1.hasNext()) { Result result = c1.fetch(1); + assertNotNull(result.get(0).resultSet()); assertNull(result.resultSet()); assertNotNull(c1.resultSet()); } @@ -123,8 +129,10 @@ extends BaseTest c1 = create().selectFrom(TBook()).keepResultSet(KEEP_AFTER_FETCH).fetchLazy(); + assertFalse(c1.closesAfterFetch()); while (c1.hasNext()) { Result result = c1.fetch(1); + assertNotNull(result.get(0).resultSet()); assertNotNull(result.resultSet()); assertNotNull(c1.resultSet()); } @@ -146,6 +154,7 @@ extends BaseTest books = + create().selectFrom(TBook()) + .orderBy(TBook_ID()) + .keepResultSet(UPDATE_ON_CHANGE) + .fetchLazy(); + + assertNotNull(books.resultSet()); + assertFalse(books.closesAfterFetch()); + while (books.hasNext()) { + B book = books.fetchOne(); + assertNotNull(book.resultSet()); + book.setValue(TBook_TITLE(), "Title X"); + } + + Result booksTest = getBooks(); + assertEquals( + Collections.nCopies(4, "Title X"), + booksTest.getValues(TBook_TITLE())); + + // After closing, setting values to records should no longer have any + // effect + assertNotNull(books.resultSet()); + assertFalse(books.isClosed()); + books.close(); + assertNull(books.resultSet()); + assertTrue(books.isClosed()); + } + + @Test + public void testKeepRSWithUpdateOnChangeUnsuccessful() throws Exception { + jOOQAbstractTest.reset = false; + + B book = + create().selectFrom(TBook()) + .where(TBook_ID().eq(1)) + .keepResultSet(UPDATE_ON_CHANGE) + .fetchOne(); + + assertNotNull(book.resultSet()); + book.setValue(TBook_AUTHOR_ID(), 2); + assertEquals(2, (int) book.getValue(TBook_AUTHOR_ID())); + book.refresh(); + assertEquals(2, (int) book.getValue(TBook_AUTHOR_ID())); + + try { + book.setValue(TBook_AUTHOR_ID(), -1); + fail(); + } + catch (DataAccessException expected) {} + + assertEquals(2, (int) book.getValue(TBook_AUTHOR_ID())); + book.close(); + assertNull(book.resultSet()); + } + + @Test + public void testKeepRSWithUpdateOnChangeRemove() throws Exception { + jOOQAbstractTest.reset = false; + + } + + /* + * TODO: More tests: + * ----------------- + * + * [#2265] Pull up store(), delete(), refresh() from UpdatableRecord + * - store() will perform a scan and update if UPDATE_ON_STORE is set. Otherwise: no-op + * - delete() will remove the record + * - refresh() will pefrom a scan from the ResultSet + * + * [#1846] Add ResultQuery.keepResultSet() with UPDATE_ON_CHANGE + * - The successful setting of a value should modify the original() value. + * - Implement all data types from ResultSet.updateXXX() (e.g. updateInt(), etc) + * - Implement UPDATE_ON_STORE + * - Check if KEEP_AFTER_FETCH doesn't perform any operations on the ResultSet + */ } diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index 5256ed3bb4..cb22d8de19 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -2230,6 +2230,21 @@ public abstract class jOOQAbstractTest< new KeepResultSetTests(this).testKeepRSWithUpdateOnChange(); } + @Test + public void testKeepRSWithUpdateOnChangeLazy() throws Exception { + new KeepResultSetTests(this).testKeepRSWithUpdateOnChangeLazy(); + } + + @Test + public void testKeepRSWithUpdateOnChangeUnsuccessful() throws Exception { + new KeepResultSetTests(this).testKeepRSWithUpdateOnChangeUnsuccessful(); + } + + @Test + public void testKeepRSWithUpdateOnChangeRemove() throws Exception { + new KeepResultSetTests(this).testKeepRSWithUpdateOnChangeRemove(); + } + @Test public void testKeepStatement() throws Exception { new StatementTests(this).testKeepStatement(); diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java b/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java index a82cb33ae4..2a578e4ac0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java @@ -272,6 +272,31 @@ abstract class AbstractRecord extends AbstractStore implements Record { @Override public final void setValue(Field field, T value) { + + // [#1846] Execute this first to fail early, when UPDATE_ON_CHANGE fails + if (rs != null) { + int columnIndex = fieldsRow().indexOf(field) + 1; + + if (log.isDebugEnabled()) { + log.debug("Updating Result", "Updating Result position " + rsIndex + ":" + columnIndex + " with value " + value); + } + + try { + if (rs.getRow() != rsIndex) { + rs.absolute(rsIndex); + } + + // [#1846] TODO: Add more typesafety here + rs.updateObject(columnIndex, value); + + // [#1846] TODO: Update only in case of KeepResultSetMode.UPDATE_ON_CHANGE + rs.updateRow(); + } + catch (SQLException e) { + throw translate("Error when updating ResultSet", e); + } + } + UniqueKey key = getPrimaryKey(); Value val = getValue0(field); @@ -295,23 +320,6 @@ abstract class AbstractRecord extends AbstractStore implements Record { changed(true); } } - - if (rs != null) { - try { - if (rs.getRow() != rsIndex) { - rs.absolute(rsIndex); - } - - // [#1846] TODO: Add more typesafety here - rs.updateObject(fieldsRow().indexOf(field) + 1, value); - - // [#1846] TODO: Update only in case of KeepResultSetMode.UPDATE_ON_CHANGE - rs.updateRow(); - } - catch (SQLException e) { - throw translate("Error when updating ResultSet", e); - } - } } @Override