From 1b71a6160ca026b4900e6ffb947944f1901f5bbf Mon Sep 17 00:00:00 2001 From: sanjanarampurkottur01 <149339511+sanjanarampurkottur01@users.noreply.github.com> Date: Thu, 23 Nov 2023 09:08:32 -0400 Subject: [PATCH] Encapsulate test fields (#222) * Removing Deficient Encapsulation using Encapsulate Field technique * Fixed more design issues * Refactored visibility modifier of BaseTestCase --- .../commons/dbutils/AsyncQueryRunnerTest.java | 16 ++++----- .../apache/commons/dbutils/BaseTestCase.java | 16 +++++++-- .../dbutils/BasicRowProcessorTest.java | 36 +++++++++---------- .../commons/dbutils/BeanProcessorTest.java | 27 +++++++------- .../dbutils/GenerousBeanProcessorTest.java | 6 ++-- .../commons/dbutils/OutParameterTest.java | 4 +-- .../commons/dbutils/QueryRunnerTest.java | 22 ++++++------ .../dbutils/ResultSetIteratorTest.java | 2 +- .../dbutils/handlers/ArrayHandlerTest.java | 4 +-- .../handlers/ArrayListHandlerTest.java | 4 +-- .../dbutils/handlers/BeanHandlerTest.java | 8 ++--- .../dbutils/handlers/BeanListHandlerTest.java | 8 ++--- .../handlers/ColumnListHandlerTest.java | 10 +++--- .../dbutils/handlers/KeyedHandlerTest.java | 12 ++++--- .../dbutils/handlers/MapHandlerTest.java | 4 +-- .../dbutils/handlers/MapListHandlerTest.java | 4 +-- .../dbutils/handlers/ScalarHandlerTest.java | 8 ++--- .../columns/AbstractTestColumnHandler.java | 18 ++++++++-- .../columns/BooleanColumnHandlerTest.java | 5 ++- .../columns/ByteColumnHandlerTest.java | 5 ++- .../columns/DoubleColumnHandlerTest.java | 5 ++- .../columns/FloatColumnHandlerTest.java | 5 ++- .../columns/IntegerColumnHandlerTest.java | 6 ++-- .../columns/LongColumnHandlerTest.java | 4 +-- .../columns/SQLXMLColumnHandlerTest.java | 4 +-- .../columns/ShortColumnHandlerTest.java | 4 +-- .../columns/StringColumnHandlerTest.java | 4 +-- .../columns/TimestampColumnHandlerTest.java | 4 +-- .../wrappers/SqlNullCheckedResultSetTest.java | 4 ++- .../wrappers/StringTrimmedResultSetTest.java | 10 +++--- 30 files changed, 156 insertions(+), 113 deletions(-) diff --git a/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java b/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java index 827e91c1..acc317e2 100644 --- a/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java +++ b/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java @@ -47,21 +47,21 @@ @SuppressWarnings("boxing") // test code @RunWith(MockitoJUnitRunner.class) public class AsyncQueryRunnerTest { - AsyncQueryRunner runner; - ArrayHandler handler; + private AsyncQueryRunner runner; + private ArrayHandler handler; @Mock - DataSource dataSource; + private DataSource dataSource; @Mock - Connection conn; + private Connection conn; @Mock - PreparedStatement prepStmt; + private PreparedStatement prepStmt; @Mock - Statement stmt; + private Statement stmt; @Mock - ParameterMetaData meta; + private ParameterMetaData meta; @Mock - ResultSet results; + private ResultSet results; // helper method for calling batch when an exception is expected private void callBatchWithException(final String sql, final Object[][] params) throws Exception { diff --git a/src/test/java/org/apache/commons/dbutils/BaseTestCase.java b/src/test/java/org/apache/commons/dbutils/BaseTestCase.java index 0157cc1d..e1ce7edf 100644 --- a/src/test/java/org/apache/commons/dbutils/BaseTestCase.java +++ b/src/test/java/org/apache/commons/dbutils/BaseTestCase.java @@ -65,12 +65,12 @@ public class BaseTestCase extends TestCase { /** * The ResultSet all test methods will use. */ - protected ResultSet rs; + private ResultSet rs; /** * A ResultSet with 0 rows. */ - protected ResultSet emptyResultSet; + private ResultSet emptyResultSet; /** * Creates a freshly initialized ResultSet. @@ -92,6 +92,18 @@ protected void setUp() throws Exception { emptyResultSet = MockResultSet.create(metaData, null); } + public ResultSet getResultSet() { + return this.rs; + } + + public ResultSet getEmptyResultSet() { + return this.emptyResultSet; + } + + public void setResultSet(ResultSet resultSet) { + this.rs = resultSet; + } + // Test which allows Eclipse to be run on full project (avoids no tests found) // check that the rows are valid for the column definition public void testCheckDataSizes() { diff --git a/src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java b/src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java index d95924e5..f62b9c82 100644 --- a/src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java +++ b/src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java @@ -57,35 +57,35 @@ public void testPutAllContainsKeyAndRemove() throws Exception { public void testToArray() throws SQLException { Object[] a; - assertTrue(this.rs.next()); - a = processor.toArray(this.rs); + assertTrue(this.getResultSet().next()); + a = processor.toArray(this.getResultSet()); assertEquals(COLS, a.length); assertEquals("1", a[0]); assertEquals("2", a[1]); assertEquals("THREE", a[2]); - assertTrue(this.rs.next()); - a = processor.toArray(this.rs); + assertTrue(this.getResultSet().next()); + a = processor.toArray(this.getResultSet()); assertEquals(COLS, a.length); assertEquals("4", a[0]); assertEquals("5", a[1]); assertEquals("SIX", a[2]); - assertFalse(this.rs.next()); + assertFalse(this.getResultSet().next()); } public void testToBean() throws SQLException, ParseException { - assertTrue(this.rs.next()); - TestBean row = processor.toBean(this.rs, TestBean.class); + assertTrue(this.getResultSet().next()); + TestBean row = processor.toBean(this.getResultSet(), TestBean.class); assertEquals("1", row.getOne()); assertEquals("2", row.getTwo()); assertEquals(TestBean.Ordinal.THREE, row.getThree()); assertEquals("not set", row.getDoNotSet()); - assertTrue(this.rs.next()); - row = processor.toBean(this.rs, TestBean.class); + assertTrue(this.getResultSet().next()); + row = processor.toBean(this.getResultSet(), TestBean.class); assertEquals("4", row.getOne()); assertEquals("5", row.getTwo()); @@ -100,13 +100,13 @@ public void testToBean() throws SQLException, ParseException { assertTrue(!"not a date".equals(row.getNotDate())); assertTrue(row.getNotDate().endsWith("789456123")); - assertFalse(this.rs.next()); + assertFalse(this.getResultSet().next()); } public void testToBeanList() throws SQLException, ParseException { - final List list = processor.toBeanList(this.rs, TestBean.class); + final List list = processor.toBeanList(this.getResultSet(), TestBean.class); assertNotNull(list); assertEquals(ROWS, list.size()); @@ -134,28 +134,28 @@ public void testToBeanList() throws SQLException, ParseException { public void testToMap() throws SQLException { - assertTrue(this.rs.next()); - Map m = processor.toMap(this.rs); + assertTrue(this.getResultSet().next()); + Map m = processor.toMap(this.getResultSet()); assertEquals(COLS, m.size()); assertEquals("1", m.get("one")); assertEquals("2", m.get("TWO")); assertEquals("THREE", m.get("Three")); - assertTrue(this.rs.next()); - m = processor.toMap(this.rs); + assertTrue(this.getResultSet().next()); + m = processor.toMap(this.getResultSet()); assertEquals(COLS, m.size()); assertEquals("4", m.get("One")); // case shouldn't matter assertEquals("5", m.get("two")); assertEquals("SIX", m.get("THREE")); - assertFalse(this.rs.next()); + assertFalse(this.getResultSet().next()); } public void testToMapOrdering() throws SQLException { - assertTrue(this.rs.next()); - final Map m = processor.toMap(this.rs); + assertTrue(this.getResultSet().next()); + final Map m = processor.toMap(this.getResultSet()); final Iterator itr = m.keySet().iterator(); assertEquals("one", itr.next()); diff --git a/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java b/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java index 67364c22..7da73d50 100644 --- a/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java +++ b/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java @@ -156,7 +156,7 @@ public void setTwo(final String two) { } private static final class TestNoGetter { - public String testField; + private String testField; /** * Add setter to trigger JavaBeans to populate a PropertyDescriptor @@ -169,7 +169,7 @@ public void setTestField(final String testField) { } private static final class TestWrongSetter { - public Integer testField; + private Integer testField; public Integer getTestField() { return testField; @@ -268,32 +268,33 @@ public void testMapColumnToPropertiesWithOverrides() throws Exception { public void testProcessWithPopulateBean() throws SQLException { TestBean b = new TestBean(); - - assertTrue(this.rs.next()); - b = beanProc.populateBean(this.rs, b); + ResultSet rs = this.getResultSet(); + assertTrue(rs.next()); + b = beanProc.populateBean(rs, b); assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); assertEquals(b.getThree(), TestBean.Ordinal.THREE); - assertTrue(this.rs.next()); - b = beanProc.populateBean(this.rs, b); + assertTrue(rs.next()); + b = beanProc.populateBean(rs, b); assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); assertEquals(b.getThree(), TestBean.Ordinal.SIX); - assertFalse(this.rs.next()); + assertFalse(rs.next()); } public void testProcessWithToBean() throws SQLException { - assertTrue(this.rs.next()); - TestBean b = beanProc.toBean(this.rs, TestBean.class); + ResultSet rs = this.getResultSet(); + assertTrue(rs.next()); + TestBean b = beanProc.toBean(rs, TestBean.class); assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); assertEquals(b.getThree(), TestBean.Ordinal.THREE); - assertTrue(this.rs.next()); - b = beanProc.toBean(this.rs, TestBean.class); + assertTrue(rs.next()); + b = beanProc.toBean(rs, TestBean.class); assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); assertEquals(b.getThree(), TestBean.Ordinal.SIX); - assertFalse(this.rs.next()); + assertFalse(rs.next()); } public void testWrongSetterParamCount() throws Exception { diff --git a/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java b/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java index 1e0552b0..212de673 100644 --- a/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java +++ b/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java @@ -62,11 +62,11 @@ public void setTwo(final int two) { } } - GenerousBeanProcessor processor = new GenerousBeanProcessor(); + private GenerousBeanProcessor processor = new GenerousBeanProcessor(); @Mock - ResultSetMetaData metaData; + private ResultSetMetaData metaData; - PropertyDescriptor[] propDescriptors; + private PropertyDescriptor[] propDescriptors; @Before public void setUp() throws Exception { diff --git a/src/test/java/org/apache/commons/dbutils/OutParameterTest.java b/src/test/java/org/apache/commons/dbutils/OutParameterTest.java index cb5481e2..de0b0bdc 100644 --- a/src/test/java/org/apache/commons/dbutils/OutParameterTest.java +++ b/src/test/java/org/apache/commons/dbutils/OutParameterTest.java @@ -39,9 +39,9 @@ public class OutParameterTest { private static final int VALUE = 42; @Mock - CallableStatement stmt; + private CallableStatement stmt; - OutParameter parameter; + private OutParameter parameter; @Before public void setUp() throws Exception { diff --git a/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java b/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java index a33d0387..424d166a 100644 --- a/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java +++ b/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java @@ -87,33 +87,33 @@ public void setC(final String c) { } } - QueryRunner runner; + private QueryRunner runner; - ArrayHandler handler; + private ArrayHandler handler; @Mock - DataSource dataSource; + private DataSource dataSource; @Mock - Connection conn; + private Connection conn; @Mock - PreparedStatement prepStmt; + private PreparedStatement prepStmt; @Mock - Statement stmt; + private Statement stmt; @Mock - CallableStatement call; + private CallableStatement call; @Mock - ParameterMetaData meta; + private ParameterMetaData meta; @Mock - ResultSet results; + private ResultSet results; @Mock - ResultSetMetaData resultsMeta; + private ResultSetMetaData resultsMeta; // // Batch test cases @@ -685,7 +685,7 @@ public void testExecuteUpdateException() throws Exception { public void testExecuteWithMultipleResultSets() throws Exception { when(call.execute()).thenReturn(true); when(call.getMoreResults()).thenAnswer(new Answer() { - int count = 1; + private int count = 1; @Override public Boolean answer(final InvocationOnMock invocation) { return ++count <= 3; diff --git a/src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java b/src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java index ad18c684..33572452 100644 --- a/src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java +++ b/src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java @@ -40,7 +40,7 @@ public void testCreatesResultSetIteratorTakingThreeArgumentsAndCallsRemove() { public void testNext() { - final Iterator iter = new ResultSetIterator(this.rs); + final Iterator iter = new ResultSetIterator(this.getResultSet()); assertTrue(iter.hasNext()); Object[] row = iter.next(); diff --git a/src/test/java/org/apache/commons/dbutils/handlers/ArrayHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/ArrayHandlerTest.java index 9fbd715b..5e0b9859 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/ArrayHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/ArrayHandlerTest.java @@ -32,14 +32,14 @@ public class ArrayHandlerTest extends BaseTestCase { public void testEmptyResultSetHandle() throws SQLException { final ResultSetHandler h = new ArrayHandler(); - final Object[] results = h.handle(this.emptyResultSet); + final Object[] results = h.handle(this.getEmptyResultSet()); assertThat(results, is(emptyArray())); } public void testHandle() throws SQLException { final ResultSetHandler h = new ArrayHandler(); - final Object[] results = h.handle(this.rs); + final Object[] results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(COLS, results.length); diff --git a/src/test/java/org/apache/commons/dbutils/handlers/ArrayListHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/ArrayListHandlerTest.java index 4c36f766..f893ffdd 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/ArrayListHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/ArrayListHandlerTest.java @@ -30,7 +30,7 @@ public class ArrayListHandlerTest extends BaseTestCase { public void testEmptyResultSetHandle() throws SQLException { final ResultSetHandler> h = new ArrayListHandler(); - final List results = h.handle(this.emptyResultSet); + final List results = h.handle(this.getEmptyResultSet()); assertNotNull(results); assertTrue(results.isEmpty()); @@ -38,7 +38,7 @@ public void testEmptyResultSetHandle() throws SQLException { public void testHandle() throws SQLException { final ResultSetHandler> h = new ArrayListHandler(); - final List results = h.handle(this.rs); + final List results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); diff --git a/src/test/java/org/apache/commons/dbutils/handlers/BeanHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/BeanHandlerTest.java index 63913f05..0b80dab1 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/BeanHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/BeanHandlerTest.java @@ -42,14 +42,14 @@ public interface SubTestBeanInterface { public void testEmptyResultSetHandle() throws SQLException { final ResultSetHandler h = new BeanHandler<>(TestBean.class); - final TestBean results = h.handle(this.emptyResultSet); + final TestBean results = h.handle(this.getEmptyResultSet()); assertNull(results); } public void testHandle() throws SQLException { final ResultSetHandler h = new BeanHandler<>(TestBean.class); - final TestBean results = h.handle(this.rs); + final TestBean results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals("1", results.getOne()); @@ -60,7 +60,7 @@ public void testHandle() throws SQLException { public void testHandleToInterface() throws SQLException { final ResultSetHandler h = new BeanHandler<>(SubTestBean.class); - final SubTestBeanInterface results = h.handle(this.rs); + final SubTestBeanInterface results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals("1", results.getOne()); @@ -71,7 +71,7 @@ public void testHandleToInterface() throws SQLException { public void testHandleToSuperClass() throws SQLException { final ResultSetHandler h = new BeanHandler<>(SubTestBean.class); - final TestBean results = h.handle(this.rs); + final TestBean results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals("1", results.getOne()); diff --git a/src/test/java/org/apache/commons/dbutils/handlers/BeanListHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/BeanListHandlerTest.java index 1d60cc4d..6b851f25 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/BeanListHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/BeanListHandlerTest.java @@ -44,7 +44,7 @@ public interface SubTestBeanInterface { public void testEmptyResultSetHandle() throws SQLException { final ResultSetHandler> h = new BeanListHandler<>(TestBean.class); - final List results = h.handle(this.emptyResultSet); + final List results = h.handle(this.getEmptyResultSet()); assertNotNull(results); assertTrue(results.isEmpty()); @@ -52,7 +52,7 @@ public void testEmptyResultSetHandle() throws SQLException { public void testHandle() throws SQLException { final ResultSetHandler> h = new BeanListHandler<>(TestBean.class); - final List results = h.handle(this.rs); + final List results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); @@ -79,7 +79,7 @@ public void testHandle() throws SQLException { public void testHandleToInterface() throws SQLException { final ResultSetHandler> h = new BeanListHandler<>(SubTestBean.class); - final List results = h.handle(this.rs); + final List results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); @@ -109,7 +109,7 @@ public void testHandleToInterface() throws SQLException { public void testHandleToSuperClass() throws SQLException { final ResultSetHandler> h = new BeanListHandler<>(SubTestBean.class); - final List results = h.handle(this.rs); + final List results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); diff --git a/src/test/java/org/apache/commons/dbutils/handlers/ColumnListHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/ColumnListHandlerTest.java index cd55a704..2f4d88e5 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/ColumnListHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/ColumnListHandlerTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.dbutils.handlers; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @@ -27,9 +28,10 @@ */ public class ColumnListHandlerTest extends BaseTestCase { + public void testColumnIndexHandle() throws SQLException { final ResultSetHandler> h = new ColumnListHandler<>(2); - final List results = h.handle(this.rs); + final List results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); @@ -40,7 +42,7 @@ public void testColumnIndexHandle() throws SQLException { public void testColumnNameHandle() throws SQLException { final ResultSetHandler> h = new ColumnListHandler<>("intTest"); - final List results = h.handle(this.rs); + final List results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); @@ -51,7 +53,7 @@ public void testColumnNameHandle() throws SQLException { public void testEmptyResultSetHandle() throws SQLException { final ResultSetHandler> h = new ColumnListHandler<>(); - final List results = h.handle(this.emptyResultSet); + final List results = h.handle(this.getEmptyResultSet()); assertNotNull(results); assertTrue(results.isEmpty()); @@ -59,7 +61,7 @@ public void testEmptyResultSetHandle() throws SQLException { public void testHandle() throws SQLException { final ResultSetHandler> h = new ColumnListHandler<>(); - final List results = h.handle(this.rs); + final List results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); diff --git a/src/test/java/org/apache/commons/dbutils/handlers/KeyedHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/KeyedHandlerTest.java index 6202f433..b5e0b268 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/KeyedHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/KeyedHandlerTest.java @@ -18,6 +18,7 @@ import static org.mockito.Mockito.mock; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; import java.util.Map.Entry; @@ -28,9 +29,10 @@ public class KeyedHandlerTest extends BaseTestCase { + public void testColumnIndexHandle() throws SQLException { final ResultSetHandler>> h = new KeyedHandler<>(2); - final Map> results = h.handle(this.rs); + final Map> results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); @@ -51,7 +53,7 @@ public void testColumnIndexHandle() throws SQLException { public void testColumnNameHandle() throws SQLException { final ResultSetHandler>> h = new KeyedHandler<>("intTest"); - final Map> results = h.handle(this.rs); + final Map> results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); @@ -72,7 +74,7 @@ public void testColumnNameHandle() throws SQLException { public void testEmptyResultSetHandle() throws SQLException { final ResultSetHandler>> h = new KeyedHandler<>(); - final Map> results = h.handle(this.emptyResultSet); + final Map> results = h.handle(this.getEmptyResultSet()); assertNotNull(results); assertTrue(results.isEmpty()); } @@ -80,7 +82,7 @@ public void testEmptyResultSetHandle() throws SQLException { public void testHandle() throws SQLException { final ResultSetHandler>> h = new KeyedHandler<>(); - final Map> results = h.handle(this.rs); + final Map> results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); @@ -102,7 +104,7 @@ public void testHandle() throws SQLException { public void testInjectedRowProcess() throws Exception { final RowProcessor mockProc = mock(RowProcessor.class); final ResultSetHandler>> h = new KeyedHandler<>(mockProc); - final Map> results = h.handle(this.rs); + final Map> results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); diff --git a/src/test/java/org/apache/commons/dbutils/handlers/MapHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/MapHandlerTest.java index 4026266b..775afd86 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/MapHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/MapHandlerTest.java @@ -29,14 +29,14 @@ public class MapHandlerTest extends BaseTestCase { public void testEmptyResultSetHandle() throws SQLException { final ResultSetHandler> h = new MapHandler(); - final Map results = h.handle(this.emptyResultSet); + final Map results = h.handle(this.getEmptyResultSet()); assertNull(results); } public void testHandle() throws SQLException { final ResultSetHandler> h = new MapHandler(); - final Map results = h.handle(this.rs); + final Map results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(COLS, results.size()); diff --git a/src/test/java/org/apache/commons/dbutils/handlers/MapListHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/MapListHandlerTest.java index 12efe2c7..7c0e7902 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/MapListHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/MapListHandlerTest.java @@ -31,7 +31,7 @@ public class MapListHandlerTest extends BaseTestCase { public void testEmptyResultSetHandle() throws SQLException { final ResultSetHandler>> h = new MapListHandler(); - final List> results = h.handle(this.emptyResultSet); + final List> results = h.handle(this.getEmptyResultSet()); assertNotNull(results); assertTrue(results.isEmpty()); @@ -39,7 +39,7 @@ public void testEmptyResultSetHandle() throws SQLException { public void testHandle() throws SQLException { final ResultSetHandler>> h = new MapListHandler(); - final List> results = h.handle(this.rs); + final List> results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(ROWS, results.size()); diff --git a/src/test/java/org/apache/commons/dbutils/handlers/ScalarHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/ScalarHandlerTest.java index 0dbc022c..aac5a92f 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/ScalarHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/ScalarHandlerTest.java @@ -25,27 +25,27 @@ public class ScalarHandlerTest extends BaseTestCase { public void testColumnIndexHandle() throws SQLException { final ResultSetHandler h = new ScalarHandler<>(2); - final Object results = h.handle(this.rs); + final Object results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals("2", results); } public void testColumnNameHandle() throws SQLException { final ResultSetHandler h = new ScalarHandler<>("intTest"); - final Object results = h.handle(this.rs); + final Object results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals(Integer.valueOf(1), results); } public void testEmptyResultSetHandle() throws SQLException { final ResultSetHandler h = new ScalarHandler<>(); - final Object results = h.handle(this.emptyResultSet); + final Object results = h.handle(this.getEmptyResultSet()); assertNull(results); } public void testHandle() throws SQLException { final ResultSetHandler h = new ScalarHandler<>(); - final Object results = h.handle(this.rs); + final Object results = h.handle(this.getResultSet()); assertNotNull(results); assertEquals("1", results); } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/AbstractTestColumnHandler.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/AbstractTestColumnHandler.java index 1b0878da..caf16f5f 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/AbstractTestColumnHandler.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/AbstractTestColumnHandler.java @@ -28,15 +28,27 @@ public abstract class AbstractTestColumnHandler { @Mock - protected ResultSet rs; - protected final ColumnHandler handler; - protected final Class matchingType; + private ResultSet rs; + private final ColumnHandler handler; + private final Class matchingType; public AbstractTestColumnHandler(final ColumnHandler handler, final Class matchingType) { this.handler = handler; this.matchingType = matchingType; } + public ResultSet getResultSet() { + return this.rs; + } + + public ColumnHandler getColumnHandler() { + return this.handler; + } + + public Class getMatchingType() { + return this.matchingType; + } + @Test public abstract void testApplyType() throws Exception; diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/BooleanColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/BooleanColumnHandlerTest.java index 35820ee4..9a500418 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/BooleanColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/BooleanColumnHandlerTest.java @@ -23,6 +23,8 @@ import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; +import java.sql.ResultSet; + @RunWith(MockitoJUnitRunner.class) public class BooleanColumnHandlerTest extends AbstractTestColumnHandler { @@ -33,7 +35,8 @@ public BooleanColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { + ResultSet rs = getResultSet(); when(rs.getBoolean(1)).thenReturn(Boolean.TRUE); - assertEquals(Boolean.class, handler.apply(rs, 1).getClass()); + assertEquals(Boolean.class, getColumnHandler().apply(rs, 1).getClass()); } } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/ByteColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/ByteColumnHandlerTest.java index 3dd36c4f..efe1b152 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/ByteColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/ByteColumnHandlerTest.java @@ -23,6 +23,8 @@ import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; +import java.sql.ResultSet; + @RunWith(MockitoJUnitRunner.class) public class ByteColumnHandlerTest extends AbstractTestColumnHandler { @@ -33,7 +35,8 @@ public ByteColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { + ResultSet rs = getResultSet(); when(rs.getByte(1)).thenReturn(Byte.MIN_VALUE); - assertEquals(Byte.class, handler.apply(rs, 1).getClass()); + assertEquals(Byte.class, getColumnHandler().apply(rs, 1).getClass()); } } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/DoubleColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/DoubleColumnHandlerTest.java index 8436b375..92eed1f4 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/DoubleColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/DoubleColumnHandlerTest.java @@ -23,6 +23,8 @@ import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; +import java.sql.ResultSet; + @RunWith(MockitoJUnitRunner.class) public class DoubleColumnHandlerTest extends AbstractTestColumnHandler { @@ -33,7 +35,8 @@ public DoubleColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { + ResultSet rs = getResultSet(); when(rs.getDouble(1)).thenReturn(Double.MIN_VALUE); - assertEquals(Double.class, handler.apply(rs, 1).getClass()); + assertEquals(Double.class, getColumnHandler().apply(rs, 1).getClass()); } } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/FloatColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/FloatColumnHandlerTest.java index f44488b7..61c35d15 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/FloatColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/FloatColumnHandlerTest.java @@ -23,6 +23,8 @@ import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; +import java.sql.ResultSet; + @RunWith(MockitoJUnitRunner.class) public class FloatColumnHandlerTest extends AbstractTestColumnHandler { @@ -33,7 +35,8 @@ public FloatColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { + ResultSet rs = getResultSet(); when(rs.getFloat(1)).thenReturn(Float.MIN_VALUE); - assertEquals(Float.class, handler.apply(rs, 1).getClass()); + assertEquals(Float.class, getColumnHandler().apply(rs, 1).getClass()); } } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/IntegerColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/IntegerColumnHandlerTest.java index bd00b933..48ce6ecf 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/IntegerColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/IntegerColumnHandlerTest.java @@ -34,13 +34,13 @@ public IntegerColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { - when(rs.getInt(1)).thenReturn(Integer.MIN_VALUE); - assertEquals(Integer.class, handler.apply(rs, 1).getClass()); + when(getResultSet().getInt(1)).thenReturn(Integer.MIN_VALUE); + assertEquals(Integer.class, getColumnHandler().apply(getResultSet(), 1).getClass()); } @Override @Test public void testMatchNegative() { - assertFalse(handler.match(Float.class)); + assertFalse(getColumnHandler().match(Float.class)); } } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/LongColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/LongColumnHandlerTest.java index d58c6ef9..bb69a50a 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/LongColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/LongColumnHandlerTest.java @@ -33,7 +33,7 @@ public LongColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { - when(rs.getLong(1)).thenReturn(Long.MIN_VALUE); - assertEquals(Long.class, handler.apply(rs, 1).getClass()); + when(getResultSet().getLong(1)).thenReturn(Long.MIN_VALUE); + assertEquals(Long.class, getColumnHandler().apply(getResultSet(), 1).getClass()); } } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/SQLXMLColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/SQLXMLColumnHandlerTest.java index a476f0eb..6f72cad9 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/SQLXMLColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/SQLXMLColumnHandlerTest.java @@ -39,7 +39,7 @@ public SQLXMLColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { - when(rs.getSQLXML(1)).thenReturn(sqlxml); - assertTrue(SQLXML.class.isAssignableFrom(handler.apply(rs, 1).getClass())); + when(getResultSet().getSQLXML(1)).thenReturn(sqlxml); + assertTrue(SQLXML.class.isAssignableFrom(getColumnHandler().apply(getResultSet(), 1).getClass())); } } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/ShortColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/ShortColumnHandlerTest.java index 33d62d06..5a4462bf 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/ShortColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/ShortColumnHandlerTest.java @@ -33,7 +33,7 @@ public ShortColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { - when(rs.getShort(1)).thenReturn(Short.MIN_VALUE); - assertEquals(Short.class, handler.apply(rs, 1).getClass()); + when(getResultSet().getShort(1)).thenReturn(Short.MIN_VALUE); + assertEquals(Short.class, getColumnHandler().apply(getResultSet(), 1).getClass()); } } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/StringColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/StringColumnHandlerTest.java index d9f19882..bf26f170 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/StringColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/StringColumnHandlerTest.java @@ -33,7 +33,7 @@ public StringColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { - when(rs.getString(1)).thenReturn("tester"); - assertEquals(String.class, handler.apply(rs, 1).getClass()); + when(getResultSet().getString(1)).thenReturn("tester"); + assertEquals(String.class, getColumnHandler().apply(getResultSet(), 1).getClass()); } } diff --git a/src/test/java/org/apache/commons/dbutils/handlers/columns/TimestampColumnHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/columns/TimestampColumnHandlerTest.java index 24116a4a..9655b59a 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/columns/TimestampColumnHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/columns/TimestampColumnHandlerTest.java @@ -36,7 +36,7 @@ public TimestampColumnHandlerTest() { @Override @Test public void testApplyType() throws Exception { - when(rs.getTimestamp(1)).thenReturn(new Timestamp(new Date().getTime())); - assertEquals(Timestamp.class, handler.apply(rs, 1).getClass()); + when(getResultSet().getTimestamp(1)).thenReturn(new Timestamp(new Date().getTime())); + assertEquals(Timestamp.class, getColumnHandler().apply(getResultSet(), 1).getClass()); } } diff --git a/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java b/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java index f2614f62..b4c6dccf 100644 --- a/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java +++ b/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java @@ -229,6 +229,7 @@ private static void assertArrayEquals(final byte[] expected, final byte[] actual } private SqlNullCheckedResultSet rs2; + private static ResultSet rs; /** * Sets up instance variables required by this test case. @@ -239,7 +240,8 @@ public void setUp() throws Exception { rs2 = new SqlNullCheckedResultSet(ProxyFactory.instance().createResultSet(new SqlNullUncheckedMockResultSet())); - rs = ProxyFactory.instance().createResultSet(rs2); // Override superclass field + setResultSet(ProxyFactory.instance().createResultSet(rs2)); // Override superclass field + rs = getResultSet(); } /** diff --git a/src/test/java/org/apache/commons/dbutils/wrappers/StringTrimmedResultSetTest.java b/src/test/java/org/apache/commons/dbutils/wrappers/StringTrimmedResultSetTest.java index 25103403..9fca7dee 100644 --- a/src/test/java/org/apache/commons/dbutils/wrappers/StringTrimmedResultSetTest.java +++ b/src/test/java/org/apache/commons/dbutils/wrappers/StringTrimmedResultSetTest.java @@ -31,17 +31,17 @@ public class StringTrimmedResultSetTest extends BaseTestCase { @Override public void setUp() throws Exception { super.setUp(); - this.rs = StringTrimmedResultSet.wrap(this.rs); + this.setResultSet(StringTrimmedResultSet.wrap(this.getResultSet())); } public void testGetObject() throws SQLException { - this.rs.next(); - assertEquals("notInBean", rs.getObject(4)); + this.getResultSet().next(); + assertEquals("notInBean", getResultSet().getObject(4)); } public void testGetString() throws SQLException { - this.rs.next(); - assertEquals("notInBean", rs.getString(4)); + this.getResultSet().next(); + assertEquals("notInBean", getResultSet().getString(4)); } /**