Skip to content

Commit

Permalink
JUnit 5 migration (p6spy#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjo67 committed Apr 18, 2021
1 parent e3929df commit debfb39
Show file tree
Hide file tree
Showing 22 changed files with 361 additions and 263 deletions.
13 changes: 12 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ dependencies {
compileOnly 'ch.qos.logback:logback-classic:1.2.3'
compileOnly 'ch.qos.logback:logback-core:1.2.3'

testCompile 'junit:junit:4.12'
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.7.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.1")

// junit4 support
testImplementation("junit:junit:4.13")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.1")

testCompile 'org.eclipse.jetty:jetty-plus:9.4.12.v20180830' // datasource testing through JNDI
testCompile 'commons-dbcp:commons-dbcp:1.4' // datasource testing
testCompile 'org.codehaus.btm:btm:2.1.4' // xa datasource testing
Expand All @@ -92,6 +99,7 @@ dependencies {
testCompile 'org.apache.commons:commons-lang3:3.8.1'
testCompile 'commons-beanutils:commons-beanutils:1.9.3'
testCompile 'org.mockito:mockito-core:2.22.0'
testCompile 'org.mockito:mockito-junit-jupiter:2.23.0'
testCompile 'org.apache.tomcat:tomcat-jdbc:9.0.12' // tomcat pooled datasource testing

// all the JDBC drivers tested
Expand Down Expand Up @@ -128,6 +136,9 @@ license {
}

test {

useJUnitPlatform()

systemProperty 'user.language', 'en'
systemProperty 'user.country', 'US'
systemProperty 'derby.stream.error.file', 'target/derby.log'
Expand Down
48 changes: 26 additions & 22 deletions src/test/java/com/p6spy/engine/common/CustomHashedHashSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
*/
package com.p6spy.engine.common;

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.assertTrue;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.p6spy.engine.logging.P6LogFactory;
import com.p6spy.engine.spy.P6Factory;
Expand All @@ -40,57 +44,57 @@ public class CustomHashedHashSetTest {
private P6SpyFactory fs1 = new P6SpyFactory();
private P6SpyFactory fs2 = new P6SpyFactory();

@Before
@BeforeEach
public void before() {
hasher = new ClassHasher();
set = new CustomHashedHashSet<P6Factory>(hasher);

fl1 = new P6LogFactory();
fl2 = new P6LogFactory();

Assert.assertNotEquals(fl1.hashCode(), fl2.hashCode());
Assert.assertEquals(hasher.getHashCode(fl1), hasher.getHashCode(fl2));
assertNotEquals(fl1.hashCode(), fl2.hashCode());
assertEquals(hasher.getHashCode(fl1), hasher.getHashCode(fl2));

fs1 = new P6SpyFactory();
fs2 = new P6SpyFactory();

Assert.assertNotEquals(fs1.hashCode(), fs2.hashCode());
Assert.assertEquals(hasher.getHashCode(fs1), hasher.getHashCode(fs2));
assertNotEquals(fs1.hashCode(), fs2.hashCode());
assertEquals(hasher.getHashCode(fs1), hasher.getHashCode(fs2));

Assert.assertEquals(0, set.size());
assertEquals(0, set.size());
}

@Test
public void testAdd() {
set.add(fl1);
Assert.assertEquals(1, set.size());
assertEquals(1, set.size());
set.add(fl2);
Assert.assertEquals(1, set.size());
assertEquals(1, set.size());

set.addAll(Arrays.asList(fs1, fs2));
Assert.assertEquals(2, set.size());
assertEquals(2, set.size());
}

@Test
public void testRemove() {
set.addAll(Arrays.asList(fl1, fl2, fs1, fs2));
Assert.assertEquals(2, set.size());
assertEquals(2, set.size());

set.remove(fl1);
Assert.assertEquals(1, set.size());
assertEquals(1, set.size());
set.removeAll(Arrays.asList(fs1, fs2));
Assert.assertEquals(0, set.size());
assertEquals(0, set.size());
}

@Test
public void testContains() {
Assert.assertFalse(set.contains(fl1));
assertFalse(set.contains(fl1));

set.addAll(Arrays.asList(fl1, fs1));
Assert.assertTrue(set.contains(fl1));
Assert.assertTrue(set.contains(fl2));
Assert.assertTrue(set.contains(fs1));
Assert.assertTrue(set.contains(fs2));
assertTrue(set.contains(fl1));
assertTrue(set.contains(fl2));
assertTrue(set.contains(fs1));
assertTrue(set.contains(fs2));
}

@Test
Expand All @@ -101,8 +105,8 @@ public void testIterator() {

for (Iterator<P6Factory> it = set.iterator(); it.hasNext();) {
Object elem = it.next();
Assert.assertTrue(list.contains(elem));
Assert.assertFalse(classHashEqualList.contains(elem));
assertTrue(list.contains(elem));
assertFalse(classHashEqualList.contains(elem));
}
}
}
44 changes: 24 additions & 20 deletions src/test/java/com/p6spy/engine/common/P6UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,38 @@
*/
package com.p6spy.engine.common;

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

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

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

public class P6UtilTest {
@Test
public void testSingleLine() {
Assert.assertEquals("abc efg", P6Util.singleLine("abc\nefg"));
Assert.assertEquals("abc efg", P6Util.singleLine("abc\n\nefg"));
Assert.assertEquals("abc efg", P6Util.singleLine("abc\r\n\nefg"));
assertEquals("abc efg", P6Util.singleLine("abc\nefg"));
assertEquals("abc efg", P6Util.singleLine("abc\n\nefg"));
assertEquals("abc efg", P6Util.singleLine("abc\r\n\nefg"));
}
@Test
public void testJoinNullSafe() {
Assert.assertEquals("", P6Util.joinNullSafe(null, null));
Assert.assertEquals("", P6Util.joinNullSafe(null, ""));
Assert.assertEquals("", P6Util.joinNullSafe(null, ","));
Assert.assertEquals("", P6Util.joinNullSafe(Collections.<String>emptyList(), null));
Assert.assertEquals("", P6Util.joinNullSafe(Collections.<String>emptyList(), ","));
Assert.assertEquals("foo", P6Util.joinNullSafe(Arrays.asList("foo"), ","));
Assert.assertEquals("foobar", P6Util.joinNullSafe(Arrays.asList("foo", "bar"), null));
Assert.assertEquals("foo,bar", P6Util.joinNullSafe(Arrays.asList("foo", "bar"), ","));
Assert.assertEquals("foo|bar|aaa", P6Util.joinNullSafe(Arrays.asList("foo", "bar", "aaa"), "|"));
assertEquals("", P6Util.joinNullSafe(null, null));
assertEquals("", P6Util.joinNullSafe(null, ""));
assertEquals("", P6Util.joinNullSafe(null, ","));
assertEquals("", P6Util.joinNullSafe(Collections.<String>emptyList(), null));
assertEquals("", P6Util.joinNullSafe(Collections.<String>emptyList(), ","));
assertEquals("foo", P6Util.joinNullSafe(Arrays.asList("foo"), ","));
assertEquals("foobar", P6Util.joinNullSafe(Arrays.asList("foo", "bar"), null));
assertEquals("foo,bar", P6Util.joinNullSafe(Arrays.asList("foo", "bar"), ","));
assertEquals("foo|bar|aaa", P6Util.joinNullSafe(Arrays.asList("foo", "bar", "aaa"), "|"));
}

@Test
Expand All @@ -54,11 +58,11 @@ public void testGetPropertiesMap() throws IOException {
properties.load(new ByteArrayInputStream(string.getBytes()));
final Map<String, String> map = P6Util.getPropertiesMap(properties);

Assert.assertTrue(map.containsKey("key1"));
Assert.assertEquals("", map.get("key1"));
Assert.assertTrue(map.containsKey("key2"));
Assert.assertEquals("val2", map.get("key2"));
Assert.assertFalse(map.containsKey("key3"));
Assert.assertNull(map.get("key3"));
assertTrue(map.containsKey("key1"));
assertEquals("", map.get("key1"));
assertTrue(map.containsKey("key2"));
assertEquals("val2", map.get("key2"));
assertFalse(map.containsKey("key3"));
assertNull(map.get("key3"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@
*/
package com.p6spy.engine.common;

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

import com.p6spy.engine.test.AbstractTestConnection;
import com.p6spy.engine.test.BaseTestCase;
import com.p6spy.engine.test.TestConnection;
import com.p6spy.engine.test.TestConnectionImpl;
import com.p6spy.engine.wrapper.ConnectionWrapper;

import org.apache.commons.dbcp.DelegatingConnection;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Wrapper;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class P6WrapperIsWrapperDelegateTest extends BaseTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*/
package com.p6spy.engine.common;

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

import java.sql.Connection;
import java.sql.ResultSet;
Expand All @@ -33,7 +33,7 @@
import com.p6spy.engine.wrapper.AbstractWrapper;
import com.p6spy.engine.wrapper.ConnectionWrapper;
import org.apache.commons.dbcp.DelegatingConnection;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class P6WrapperUnwrapDelegateTest extends BaseTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@
*/
package com.p6spy.engine.event;

import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doThrow;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -59,6 +61,15 @@
import javax.sql.DataSource;
import javax.sql.PooledConnection;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.p6spy.engine.common.CallableStatementInformation;
import com.p6spy.engine.common.ConnectionInformation;
import com.p6spy.engine.common.PreparedStatementInformation;
Expand All @@ -76,17 +87,8 @@
import com.p6spy.engine.wrapper.PreparedStatementWrapper;
import com.p6spy.engine.wrapper.ResultSetWrapper;
import com.p6spy.engine.wrapper.StatementWrapper;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class CompoundJdbcEventListenerTest {

@Mock
Expand All @@ -113,7 +115,7 @@ public class CompoundJdbcEventListenerTest {

private static final String SQL = "SELECT * FROM DUAL";

@Before
@BeforeEach
public void before() throws SQLException, IOException {
P6TestFactory.setJdbcEventListener(mockedJdbcListener);
new P6TestFramework("H2") {
Expand All @@ -138,9 +140,9 @@ public JdbcEventListener createJdbcEventListener() {
return mockedJdbcListener;
}
});
when(mockedDataSource.getConnection()).thenReturn(mockedConnection);
when(mockedDataSource.getConnection(anyString(), anyString())).thenReturn(mockedConnection);
when(mockedPooledConnection.getConnection()).thenReturn(mockedConnection);
lenient().when(mockedDataSource.getConnection()).thenReturn(mockedConnection);
lenient().when(mockedDataSource.getConnection(anyString(), anyString())).thenReturn(mockedConnection);
lenient().when(mockedPooledConnection.getConnection()).thenReturn(mockedConnection);

connectionInformation = ConnectionInformation.fromTestConnection(mockedConnection);
statementInformation = new StatementInformation(connectionInformation);
Expand All @@ -167,7 +169,7 @@ public JdbcEventListener createJdbcEventListener() {
});
}

@After
@AfterEach
public void after() throws Exception {
P6TestFactory.setJdbcEventListener(null);
P6SpyDriver.setJdbcEventListenerFactory(null);
Expand All @@ -189,12 +191,12 @@ public void testConnectionOnAfterGetConnectionAfterGettingFromDataSourceWithThro

try {
wrappedDataSource.getConnection();
Assert.fail("exception should be thrown");
fail("exception should be thrown");
} catch (SQLException expected) {
}
try {
wrappedDataSource.getConnection("test", "test");
Assert.fail("exception should be thrown");
fail("exception should be thrown");
} catch (SQLException expected) {
}
verify(mockedJdbcListener, times(2)).onAfterGetConnection(connectionInformationWithoutConnection(), eq(sqle));
Expand All @@ -211,7 +213,7 @@ public void testConnectionOnBeforeAfterGetConnectionFromDriver() throws SQLExcep
public void testConnectionOnAfterGetConnectionAfterGettingFromDriverWithThrowingSQLException() throws SQLException {
try {
DriverManager.getConnection("jdbc:p6spy:h2:tcp://dev/null/", "sa", null);
Assert.fail("exception should be thrown");
fail("exception should be thrown");
} catch (SQLException expected) {
}
verify(mockedJdbcListener).onAfterGetConnection(connectionInformationWithoutConnection(), any(SQLException.class));
Expand All @@ -231,7 +233,7 @@ public void testConnectionOnAfterGetConnectionAfterGettingFromPooledConnectionWi

try {
wrappedPooledConnection.getConnection();
Assert.fail("exception should be thrown");
fail("exception should be thrown");
} catch (SQLException expected) {
}
verify(mockedJdbcListener).onAfterGetConnection(connectionInformationWithoutConnection(), eq(sqle));
Expand Down Expand Up @@ -1106,7 +1108,7 @@ public void testConnectionOnAfterConnectionSetAutoCommitWithException() throws S
boolean currentAutoCommit = connectionWrapper.getAutoCommit();
try {
connectionWrapper.setAutoCommit(false);
Assert.fail("exception should be thrown");
fail("exception should be thrown");
} catch (SQLException e){
}
verify(mockedJdbcListener).onAfterSetAutoCommit(eq(connectionInformation), eq(false), eq(currentAutoCommit),
Expand Down
Loading

0 comments on commit debfb39

Please sign in to comment.