Skip to content

Commit

Permalink
Encapsulate test fields (#222)
Browse files Browse the repository at this point in the history
* Removing Deficient Encapsulation using Encapsulate Field technique

* Fixed more design issues

* Refactored visibility modifier of BaseTestCase
  • Loading branch information
sanjanarampurkottur01 authored Nov 23, 2023
1 parent be5d30a commit 1b71a61
Show file tree
Hide file tree
Showing 30 changed files with 156 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/org/apache/commons/dbutils/BaseTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() {
Expand Down
36 changes: 18 additions & 18 deletions src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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<TestBean> list = processor.toBeanList(this.rs, TestBean.class);
final List<TestBean> list = processor.toBeanList(this.getResultSet(), TestBean.class);
assertNotNull(list);
assertEquals(ROWS, list.size());

Expand Down Expand Up @@ -134,28 +134,28 @@ public void testToBeanList() throws SQLException, ParseException {

public void testToMap() throws SQLException {

assertTrue(this.rs.next());
Map<String, Object> m = processor.toMap(this.rs);
assertTrue(this.getResultSet().next());
Map<String, Object> 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<String, Object> m = processor.toMap(this.rs);
assertTrue(this.getResultSet().next());
final Map<String, Object> m = processor.toMap(this.getResultSet());

final Iterator<String> itr = m.keySet().iterator();
assertEquals("one", itr.next());
Expand Down
27 changes: 14 additions & 13 deletions src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public class OutParameterTest {
private static final int VALUE = 42;

@Mock
CallableStatement stmt;
private CallableStatement stmt;

OutParameter<Number> parameter;
private OutParameter<Number> parameter;

@Before
public void setUp() throws Exception {
Expand Down
22 changes: 11 additions & 11 deletions src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Boolean>() {
int count = 1;
private int count = 1;
@Override
public Boolean answer(final InvocationOnMock invocation) {
return ++count <= 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testCreatesResultSetIteratorTakingThreeArgumentsAndCallsRemove() {

public void testNext() {

final Iterator<Object[]> iter = new ResultSetIterator(this.rs);
final Iterator<Object[]> iter = new ResultSetIterator(this.getResultSet());

assertTrue(iter.hasNext());
Object[] row = iter.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public class ArrayHandlerTest extends BaseTestCase {

public void testEmptyResultSetHandle() throws SQLException {
final ResultSetHandler<Object[]> 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<Object[]> h = new ArrayHandler();
final Object[] results = h.handle(this.rs);
final Object[] results = h.handle(this.getResultSet());

assertNotNull(results);
assertEquals(COLS, results.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public class ArrayListHandlerTest extends BaseTestCase {

public void testEmptyResultSetHandle() throws SQLException {
final ResultSetHandler<List<Object[]>> h = new ArrayListHandler();
final List<Object[]> results = h.handle(this.emptyResultSet);
final List<Object[]> results = h.handle(this.getEmptyResultSet());

assertNotNull(results);
assertTrue(results.isEmpty());
}

public void testHandle() throws SQLException {
final ResultSetHandler<List<Object[]>> h = new ArrayListHandler();
final List<Object[]> results = h.handle(this.rs);
final List<Object[]> results = h.handle(this.getResultSet());

assertNotNull(results);
assertEquals(ROWS, results.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public interface SubTestBeanInterface {

public void testEmptyResultSetHandle() throws SQLException {
final ResultSetHandler<TestBean> 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<TestBean> 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());
Expand All @@ -60,7 +60,7 @@ public void testHandle() throws SQLException {

public void testHandleToInterface() throws SQLException {
final ResultSetHandler<SubTestBeanInterface> 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());
Expand All @@ -71,7 +71,7 @@ public void testHandleToInterface() throws SQLException {

public void testHandleToSuperClass() throws SQLException {
final ResultSetHandler<TestBean> 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());
Expand Down
Loading

0 comments on commit 1b71a61

Please sign in to comment.