Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tests to JUnit5 #324

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
</scm>
<dependencies>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.14.1</version>
<scope>test</scope>
</dependency>
Expand Down Expand Up @@ -324,7 +324,7 @@
<email>ggregory at apache.org</email>
<url>https://www.garygregory.com</url>
<organization>The Apache Software Foundation</organization>
<organizationUrl>https://www.apache.org/</organizationUrl>
<organizationUrl>https://www.apache.org/</organizationUrl>
<roles>
<role>PMC Member</role>
</roles>
Expand Down
89 changes: 52 additions & 37 deletions src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
package org.apache.commons.dbutils;

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
Expand All @@ -38,29 +39,29 @@
import javax.sql.DataSource;

import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

@SuppressWarnings("boxing") // test code
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class AsyncQueryRunnerTest {
private AsyncQueryRunner runner;
private ArrayHandler handler;

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
private DataSource dataSource;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
private Connection conn;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
private PreparedStatement prepStmt;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
private Statement stmt;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
private ParameterMetaData meta;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
private ResultSet results;

// helper method for calling batch when an exception is expected
Expand Down Expand Up @@ -255,7 +256,7 @@ private void callUpdateWithException(final Object... params) throws Exception {
}
}

@Before
@BeforeEach
public void setUp() throws Exception {
when(dataSource.getConnection()).thenReturn(conn);

Expand All @@ -282,10 +283,12 @@ public void testAddBatchException() throws Exception {
//
// Random tests
//
@Test(expected = ExecutionException.class)
@Test
public void testBadPrepareConnection() throws Exception {
runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
runner.update("update blah set unit = test").get();
assertThrows(ExecutionException.class, () -> {
runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
runner.update("update blah set unit = test").get();
});
}

@Test
Expand Down Expand Up @@ -397,37 +400,45 @@ public void testNoParamsUpdate() throws Exception {
callGoodUpdate();
}

@Test(expected = ExecutionException.class)
@Test
public void testNullConnectionBatch() throws Exception {
final String[][] params = { { "unit", "unit" }, { "test", "test" } };
assertThrows(ExecutionException.class, () -> {
final String[][] params = {{"unit", "unit"}, {"test", "test"}};

when(dataSource.getConnection()).thenReturn(null);
when(dataSource.getConnection()).thenReturn(null);

runner.batch("select * from blah where ? = ?", params).get();
runner.batch("select * from blah where ? = ?", params).get();
});
}

@Test(expected = ExecutionException.class)
@Test
public void testNullConnectionQuery() throws Exception {
when(dataSource.getConnection()).thenReturn(null);
assertThrows(ExecutionException.class, () -> {
when(dataSource.getConnection()).thenReturn(null);

runner.query("select * from blah where ? = ?", handler, "unit", "test").get();
runner.query("select * from blah where ? = ?", handler, "unit", "test").get();
});
}

@Test(expected = ExecutionException.class)
@Test
public void testNullConnectionUpdate() throws Exception {
when(dataSource.getConnection()).thenReturn(null);
assertThrows(ExecutionException.class, () -> {
when(dataSource.getConnection()).thenReturn(null);

runner.update("select * from blah where ? = ?", "unit", "test").get();
runner.update("select * from blah where ? = ?", "unit", "test").get();
});
}

@Test(expected = ExecutionException.class)
@Test
public void testNullHandlerQuery() throws Exception {
runner.query("select * from blah where ? = ?", null).get();
assertThrows(ExecutionException.class, () ->
runner.query("select * from blah where ? = ?", null).get());
}

@Test(expected = ExecutionException.class)
@Test
public void testNullParamsArgBatch() throws Exception {
runner.batch("select * from blah where ? = ?", null).get();
assertThrows(ExecutionException.class, () ->
runner.batch("select * from blah where ? = ?", null).get());
}

@Test
Expand All @@ -437,21 +448,25 @@ public void testNullParamsBatch() throws Exception {
callGoodBatch(params);
}

@Test(expected = ExecutionException.class)
@Test
public void testNullSqlBatch() throws Exception {
final String[][] params = { { "unit", "unit" }, { "test", "test" } };
assertThrows(ExecutionException.class, () -> {
final String[][] params = {{"unit", "unit"}, {"test", "test"}};

runner.batch(null, params).get();
runner.batch(null, params).get();
});
}

@Test(expected = ExecutionException.class)
@Test
public void testNullSqlQuery() throws Exception {
runner.query(null, handler).get();
assertThrows(ExecutionException.class, () ->
runner.query(null, handler).get());
}

@Test(expected = ExecutionException.class)
@Test
public void testNullSqlUpdate() throws Exception {
runner.update(null).get();
assertThrows(ExecutionException.class, () ->
runner.update(null).get());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
*/
package org.apache.commons.dbutils;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.junit.Test;
import org.junit.jupiter.api.Test;

public final class BaseResultSetHandlerTest extends BaseTestCase {

Expand Down
22 changes: 13 additions & 9 deletions src/test/java/org/apache/commons/dbutils/BaseTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
*/
package org.apache.commons.dbutils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Timestamp;
import java.util.Date;

import junit.framework.TestCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* BaseTestCase is the base class for all test cases as well as the "all tests" runner.
*/
public class BaseTestCase extends TestCase {
public class BaseTestCase {

private static final String[] columnNames = { "one", "two", "three", "notInBean", "intTest", "integerTest", "nullObjectTest", "nullPrimitiveTest",
"notDate", "columnProcessorDoubleTest", null };
Expand Down Expand Up @@ -94,25 +98,25 @@ public void setResultSet(final ResultSet resultSet) {
/**
* This is called before each test method so ResultSet will be fresh each time.
*
* @see junit.framework.TestCase#setUp()
* @see org.junit.jupiter.api.BeforeEach
*/
@Override
@BeforeEach
protected void setUp() throws Exception {
super.setUp();

rs = createMockResultSet();
emptyResultSet = MockResultSet.create(metaData, null);
}

// Test which allows Eclipse to be run on full project (avoids no tests found)
// check that the rows are valid for the column definition
@Test
public void testCheckDataSizes() {
assertEquals("Row 1 must contain correct number of columns", columnNames.length, row1.length);
assertEquals("Row 1 must contain correct number of columns", columnNames.length, row2.length);
assertEquals(columnNames.length, row1.length, "Row 1 must contain correct number of columns");
assertEquals(columnNames.length, row2.length, "Row 1 must contain correct number of columns");
}

@Test
public void testResultSets() throws Exception {
assertFalse("emptyResultSet should be empty", emptyResultSet.next());
assertFalse(emptyResultSet.next(), "emptyResultSet should be empty");
// fails in SqlNullCheckedResultSetTest assertTrue("rs should not be empty", rs.next());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
package org.apache.commons.dbutils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
Expand All @@ -26,6 +33,8 @@
import java.util.Locale;
import java.util.Map;

import org.junit.jupiter.api.Test;

/**
* Test the BasicRowProcessor class.
*/
Expand All @@ -38,6 +47,7 @@ public class BasicRowProcessorTest extends BaseTestCase {
*/
private static final DateFormat datef = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

@Test
public void testPutAllContainsKeyAndRemove() throws Exception {
final Map<String, Object> test = new HashMap<>(3);
test.put("fiRst", "thing");
Expand All @@ -54,6 +64,7 @@ public void testPutAllContainsKeyAndRemove() throws Exception {
assertFalse(brpMap.containsKey("first"));
}

@Test
public void testToArray() throws SQLException {

Object[] a;
Expand All @@ -75,6 +86,7 @@ public void testToArray() throws SQLException {
assertFalse(getResultSet().next());
}

@Test
public void testToBean() throws SQLException, ParseException {

assertTrue(getResultSet().next());
Expand All @@ -97,13 +109,14 @@ public void testToBean() throws SQLException, ParseException {
assertEquals(0, row.getNullPrimitiveTest());
// test date -> string handling
assertNotNull(row.getNotDate());
assertTrue(!"not a date".equals(row.getNotDate()));
assertNotEquals("not a date", row.getNotDate());
assertTrue(row.getNotDate().endsWith("789456123"));

assertFalse(getResultSet().next());

}

@Test
public void testToBeanList() throws SQLException, ParseException {

final List<TestBean> list = processor.toBeanList(getResultSet(), TestBean.class);
Expand All @@ -128,10 +141,11 @@ public void testToBeanList() throws SQLException, ParseException {
assertEquals(0, b.getNullPrimitiveTest());
// test date -> string handling
assertNotNull(b.getNotDate());
assertTrue(!"not a date".equals(b.getNotDate()));
assertNotEquals("not a date", b.getNotDate());
assertTrue(b.getNotDate().endsWith("789456123"));
}

@Test
public void testToMap() throws SQLException {

assertTrue(getResultSet().next());
Expand All @@ -152,6 +166,7 @@ public void testToMap() throws SQLException {
assertFalse(getResultSet().next());
}

@Test
public void testToMapOrdering() throws SQLException {

assertTrue(getResultSet().next());
Expand Down
Loading
Loading