Skip to content

Commit

Permalink
NIFI-6742 Use JUnit TemporaryFolder when creating test databases
Browse files Browse the repository at this point in the history
- 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 <phrocker@apache.org>

This closes #4137.
  • Loading branch information
Adam Taft authored and phrocker committed Mar 13, 2020
1 parent 97e250c commit decb5d0
Showing 1 changed file with 25 additions and 63 deletions.
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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));

Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit decb5d0

Please sign in to comment.