From 9e9182aa639fb2358c950a19cb87d1ad3ddf67ab Mon Sep 17 00:00:00 2001 From: smarthi Date: Sat, 16 Jan 2016 01:48:06 -0500 Subject: [PATCH] - Unit test creates resources in HOME directory This closes #174 Signed-off-by: Matt Gilman --- .../standard/util/TestJdbcTypesH2.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcTypesH2.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcTypesH2.java index e3041b6e9d..7d31c21247 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcTypesH2.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcTypesH2.java @@ -20,7 +20,6 @@ import static org.junit.Assert.assertNotNull; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; @@ -34,11 +33,14 @@ import org.apache.avro.generic.GenericDatumReader; import org.apache.avro.generic.GenericRecord; import org.apache.avro.io.DatumReader; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; public class TestJdbcTypesH2 { - final static String DB_LOCATION = "~/var/test/h2"; + @Rule + public TemporaryFolder folder = new TemporaryFolder(); @BeforeClass public static void setup() { @@ -75,11 +77,7 @@ public class TestJdbcTypesH2 { @Test public void testSQLTypesMapping() throws ClassNotFoundException, SQLException, IOException { - // remove previous test database, if any - final File dbLocation = new File(DB_LOCATION); - dbLocation.delete(); - - final Connection con = createConnection(); + final Connection con = createConnection(folder.getRoot().getAbsolutePath()); final Statement st = con.createStatement(); try { @@ -114,8 +112,8 @@ public class TestJdbcTypesH2 { final InputStream instream = new ByteArrayInputStream(serializedBytes); - final DatumReader datumReader = new GenericDatumReader(); - try (final DataFileStream dataFileReader = new DataFileStream(instream, datumReader)) { + final DatumReader datumReader = new GenericDatumReader<>(); + try (final DataFileStream dataFileReader = new DataFileStream<>(instream, datumReader)) { GenericRecord record = null; while (dataFileReader.hasNext()) { // Reuse record object by passing it to next(). This saves us from @@ -132,18 +130,17 @@ public class TestJdbcTypesH2 { public void testDriverLoad() throws ClassNotFoundException, SQLException { // final Class clazz = Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); - Connection con = createConnection(); + Connection con = createConnection(folder.getRoot().getAbsolutePath()); assertNotNull(con); con.close(); } - private Connection createConnection() throws ClassNotFoundException, SQLException { + private Connection createConnection(String location) throws ClassNotFoundException, SQLException { // Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); - String connectionString = "jdbc:h2:file:" + DB_LOCATION + "/testdb7"; - final Connection con = DriverManager.getConnection(connectionString, "SA", ""); - return con; + String connectionString = "jdbc:h2:file:" + location + "/testdb7"; + return DriverManager.getConnection(connectionString, "SA", ""); } }