The JDBC Appender should use keys and values from a Log4j
MapMessage. Exception clean up. Use try-with-resources.
*/
package org.apache.logging.log4j.core.appender.db.jdbc;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.mock;
+
import java.sql.Connection;
import java.sql.SQLException;
-import javax.naming.NamingException;
+
import javax.sql.DataSource;
import org.apache.logging.log4j.junit.JndiRule;
@@ -29,10 +36,6 @@ import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
-import static org.junit.Assert.*;
-import static org.mockito.BDDMockito.given;
-import static org.mockito.Mockito.mock;
-
@RunWith(Parameterized.class)
public class DataSourceConnectionSourceTest {
@@ -79,17 +82,17 @@ public class DataSourceConnectionSourceTest {
}
@Test
- public void testDataSource() throws NamingException, SQLException {
- final Connection connection1 = mock(Connection.class);
- final Connection connection2 = mock(Connection.class);
+ public void testDataSource() throws SQLException {
+ try (final Connection connection1 = mock(Connection.class);
+ final Connection connection2 = mock(Connection.class)) {
given(dataSource.getConnection()).willReturn(connection1, connection2);
DataSourceConnectionSource source = DataSourceConnectionSource.createConnectionSource(jndiURL);
assertNotNull("The connection source should not be null.", source);
- assertEquals("The toString value is not correct.", "dataSource{ name=" + jndiURL + ", value="
- + dataSource + " }", source.toString());
+ assertEquals("The toString value is not correct.",
+ "dataSource{ name=" + jndiURL + ", value=" + dataSource + " }", source.toString());
assertSame("The connection is not correct (1).", connection1, source.getConnection());
assertSame("The connection is not correct (2).", connection2, source.getConnection());
@@ -97,5 +100,6 @@ public class DataSourceConnectionSourceTest {
assertNull("The connection source should be null now.", source);
}
+ }
}
