From decb5d062ea20714b666906e1414e0c9a8e4064c Mon Sep 17 00:00:00 2001 From: Adam Taft Date: Wed, 23 Oct 2019 21:03:38 -0600 Subject: [PATCH] NIFI-6742 Use JUnit TemporaryFolder when creating test databases - Add @Rule for TemporaryFolder - Replace use of previous target/db with TemporaryFolder/db - Remove use of ~/test db (in home directory) - Remove System.out lines Signed-off-by: Marc Parisi This closes #4137. --- .../org/apache/nifi/dbcp/DBCPServiceTest.java | 88 ++++++------------- 1 file changed, 25 insertions(+), 63 deletions(-) diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-dbcp-service/src/test/java/org/apache/nifi/dbcp/DBCPServiceTest.java b/nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-dbcp-service/src/test/java/org/apache/nifi/dbcp/DBCPServiceTest.java index 7522125acc8a..fdad692253e9 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-dbcp-service/src/test/java/org/apache/nifi/dbcp/DBCPServiceTest.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-dbcp-service/src/test/java/org/apache/nifi/dbcp/DBCPServiceTest.java @@ -23,13 +23,16 @@ import org.apache.nifi.util.TestRunners; import org.h2.tools.Server; import org.junit.Assert; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; import java.io.File; +import java.io.IOException; import java.io.PrintWriter; import java.net.InetAddress; import java.net.MalformedURLException; @@ -51,13 +54,21 @@ public class DBCPServiceTest { - final static String DB_LOCATION = "target/db"; + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(new File("target")); + + private String dbLocation; @BeforeClass public static void setup() { System.setProperty("derby.stream.error.file", "target/derby.log"); } + @Before + public void before() throws IOException { + this.dbLocation = new File(tempFolder.getRoot(), "db").getPath(); + } + /** * Missing property values. */ @@ -79,12 +90,8 @@ public void testMaxWait() throws InitializationException { final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-good1", service); - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - // set embedded Derby database connection url - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + DB_LOCATION + ";create=true"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + dbLocation + ";create=true"); runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester"); runner.setProperty(service, DBCPConnectionPool.DB_PASSWORD, "testerp"); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver"); @@ -103,12 +110,8 @@ public void testIdleConnectionsSettings() throws InitializationException { final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-good1", service); - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - // set embedded Derby database connection url - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + DB_LOCATION + ";create=true"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + dbLocation + ";create=true"); runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester"); runner.setProperty(service, DBCPConnectionPool.DB_PASSWORD, "testerp"); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver"); @@ -129,12 +132,8 @@ public void testMinIdleCannotBeNegative() throws InitializationException { final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-good1", service); - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - // set embedded Derby database connection url - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + DB_LOCATION + ";create=true"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + dbLocation + ";create=true"); runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester"); runner.setProperty(service, DBCPConnectionPool.DB_PASSWORD, "testerp"); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver"); @@ -153,12 +152,8 @@ public void testIdleSettingsAreSet() throws InitializationException, SQLExceptio final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-good1", service); - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - // set embedded Derby database connection url - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + DB_LOCATION + ";create=true"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + dbLocation + ";create=true"); runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester"); runner.setProperty(service, DBCPConnectionPool.DB_PASSWORD, "testerp"); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver"); @@ -191,12 +186,8 @@ public void testIdle() throws InitializationException, SQLException, Interrupted final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-good1", service); - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - // set embedded Derby database connection url - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + DB_LOCATION + ";create=true"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + dbLocation + ";create=true"); runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester"); runner.setProperty(service, DBCPConnectionPool.DB_PASSWORD, "testerp"); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver"); @@ -260,12 +251,8 @@ public void testCreateInsertSelect() throws InitializationException, SQLExceptio final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-good1", service); - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - // set embedded Derby database connection url - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + DB_LOCATION + ";create=true"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + dbLocation + ";create=true"); runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester"); runner.setProperty(service, DBCPConnectionPool.DB_PASSWORD, "testerp"); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver"); @@ -329,12 +316,8 @@ public void testExhaustPool() throws InitializationException, SQLException { final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-exhaust", service); - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - // set embedded Derby database connection url - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + DB_LOCATION + ";create=true"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + dbLocation + ";create=true"); runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester"); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver"); @@ -367,7 +350,8 @@ public void testDropInvalidConnectionsH2_Default() throws Exception { final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-dropcreate", service); - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:h2:tcp://localhost:" + server.getPort() + "/~/test"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, + "jdbc:h2:tcp://localhost:" + server.getPort() + "/./" + dbLocation); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.h2.Driver"); runner.enableControllerService(service); @@ -378,7 +362,6 @@ public void testDropInvalidConnectionsH2_Default() throws Exception { // get and verify connections for (int i = 0; i < 10; i++) { final Connection connection = dbcpService.getConnection(); - System.out.println(connection); Assert.assertNotNull(connection); assertValidConnectionH2(connection, i); connection.close(); @@ -391,7 +374,6 @@ public void testDropInvalidConnectionsH2_Default() throws Exception { for (int i = 0; i < 10; i++) { final Connection connection = dbcpService.getConnection(); - System.out.println(connection); Assert.assertNotNull(connection); assertValidConnectionH2(connection, i); connection.close(); @@ -415,7 +397,8 @@ public void testDropInvalidConnectionsH2_Better() throws Exception { final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-dropcreate", service); - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:h2:tcp://localhost:" + server.getPort() + "/~/test"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, + "jdbc:h2:tcp://localhost:" + server.getPort() + "/./" + dbLocation); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.h2.Driver"); runner.setProperty(service, DBCPConnectionPool.VALIDATION_QUERY, "SELECT 5"); runner.enableControllerService(service); @@ -427,7 +410,6 @@ public void testDropInvalidConnectionsH2_Better() throws Exception { // get and verify connections for (int i = 0; i < 10; i++) { final Connection connection = dbcpService.getConnection(); - System.out.println(connection); Assert.assertNotNull(connection); assertValidConnectionH2(connection, i); connection.close(); @@ -443,7 +425,6 @@ public void testDropInvalidConnectionsH2_Better() throws Exception { // Pool should remove invalid connections and create new valid connections. for (int i = 0; i < 10; i++) { final Connection connection = dbcpService.getConnection(); - System.out.println(connection); Assert.assertNotNull(connection); assertValidConnectionH2(connection, i); connection.close(); @@ -472,15 +453,9 @@ private void assertValidConnectionH2(Connection connection, int num) throws SQLE @Test public void testDropInvalidConnectionsDerby() throws Exception { - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - if (dbLocation.exists()) - throw new RuntimeException("Still exists " + dbLocation.getAbsolutePath()); - // Start Derby server. System.setProperty("derby.drda.startNetworkServer", "true"); - System.setProperty("derby.system.home", DB_LOCATION); + System.setProperty("derby.system.home", dbLocation); NetworkServerControl serverControl = new NetworkServerControl(InetAddress.getLocalHost(),1527); serverControl.start(new PrintWriter(System.out, true)); @@ -506,26 +481,17 @@ public void testDropInvalidConnectionsDerby() throws Exception { for (int i = 0; i < 10; i++) { final Connection connection = dbcpService.getConnection(); - System.out.println(connection); Assert.assertNotNull(connection); assertValidConnectionDerby(connection, i); connection.close(); } serverControl.shutdown(); - dbLocation.delete(); - if (dbLocation.exists()) - throw new RuntimeException("Still exists " + dbLocation.getAbsolutePath()); - try { - serverControl.ping(); - } catch (Exception e) { - } Thread.sleep(2000); for (int i = 0; i < 10; i++) { final Connection connection = dbcpService.getConnection(); - System.out.println(connection); Assert.assertNotNull(connection); assertValidConnectionDerby(connection, i); connection.close(); @@ -553,12 +519,8 @@ public void testGetManyNormal() throws InitializationException, SQLException { final DBCPConnectionPool service = new DBCPConnectionPool(); runner.addControllerService("test-exhaust", service); - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - // set embedded Derby database connection url - runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + DB_LOCATION + ";create=true"); + runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + dbLocation + ";create=true"); runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester"); runner.setProperty(service, DBCPConnectionPool.DB_PASSWORD, "testerp"); runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver");