diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlExample.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlExample.java index 36362fd8681d3..77862ceb25a91 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlExample.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlExample.java @@ -158,7 +158,7 @@ public class FlightSqlExample implements FlightSqlProducer, AutoCloseable { private static final Logger LOGGER = getLogger(FlightSqlExample.class); protected static final Calendar DEFAULT_CALENDAR = JdbcToArrowUtils.getUtcCalendar(); - public static final String DB_NAME = "derbyDB"; + public static final Path DB_PATH = Paths.get("target", "derbyDB"); private final String databaseUri; // ARROW-15315: Use ExecutorService to simulate an async scenario private final ExecutorService executorService = Executors.newFixedThreadPool(10); @@ -171,21 +171,35 @@ public class FlightSqlExample implements FlightSqlProducer, AutoCloseable { public static void main(String[] args) throws Exception { Location location = Location.forGrpcInsecure("localhost", 55555); - final FlightSqlExample example = new FlightSqlExample(location, DB_NAME); + final FlightSqlExample example = new FlightSqlExample(location, DB_PATH); Location listenLocation = Location.forGrpcInsecure("0.0.0.0", 55555); try (final BufferAllocator allocator = new RootAllocator(); final FlightServer server = FlightServer.builder(allocator, listenLocation, example).build()) { server.start(); server.awaitTermination(); } + removeDerbyDatabaseIfExists(DB_PATH); } - public FlightSqlExample(final Location location, final String dbName) { + private String toUri(Path path) { + if (path.getNameCount() == 0) { + return ""; + } + + String uri = path.getName(0).toString(); + for (int i = 1; i < path.getNameCount(); i++) { + uri = uri.concat("/").concat(path.getName(i).toString()); + } + return uri; + } + + public FlightSqlExample(final Location location, final Path dbName) { // TODO Constructor should not be doing work. + final String derbyDatabaseUri = toUri(dbName); + databaseUri = "jdbc:derby:" + derbyDatabaseUri; checkState( - removeDerbyDatabaseIfExists(dbName) && populateDerbyDatabase(dbName), + populateDerbyDatabase(databaseUri), "Failed to reset Derby database!"); - databaseUri = "jdbc:derby:target/" + dbName; final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(databaseUri, new Properties()); final PoolableConnectionFactory poolableConnectionFactory = @@ -250,9 +264,8 @@ public FlightSqlExample(final Location location, final String dbName) { } - public static boolean removeDerbyDatabaseIfExists(final String dbName) { + public static boolean removeDerbyDatabaseIfExists(final Path path) { boolean wasSuccess; - final Path path = Paths.get("target" + File.separator + dbName); try (final Stream walk = Files.walk(path)) { /* @@ -279,8 +292,8 @@ public static boolean removeDerbyDatabaseIfExists(final String dbName) { return wasSuccess; } - private static boolean populateDerbyDatabase(final String dbName) { - try (final Connection connection = DriverManager.getConnection("jdbc:derby:target/" + dbName + ";create=true"); + private static boolean populateDerbyDatabase(final String databaseUri) { + try (final Connection connection = DriverManager.getConnection(databaseUri + ";create=true"); Statement statement = connection.createStatement()) { dropTable(statement, "intTable"); @@ -779,7 +792,6 @@ public void close() throws Exception { } catch (Throwable t) { LOGGER.error(format("Failed to close resources: <%s>", t.getMessage()), t); } - AutoCloseables.close(dataSource, rootAllocator); } diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlStatelessExample.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlStatelessExample.java index c79c09c0967dc..e7f49ea2ee509 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlStatelessExample.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlStatelessExample.java @@ -31,6 +31,8 @@ import java.io.StreamCorruptedException; import java.nio.ByteBuffer; import java.nio.channels.Channels; +import java.nio.file.Path; +import java.nio.file.Paths; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -66,11 +68,11 @@ */ public class FlightSqlStatelessExample extends FlightSqlExample { private static final Logger LOGGER = getLogger(FlightSqlStatelessExample.class); - public static final String DB_NAME = "derbyStatelessDB"; + public static final Path DB_PATH = Paths.get("target", "derbyStatelessDB"); - public FlightSqlStatelessExample(final Location location, final String dbName) { - super(location, dbName); + public FlightSqlStatelessExample(final Location location, final Path dbPath) { + super(location, dbPath); } @Override diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSql.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSql.java index ffffdd62ac950..573695c01c408 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSql.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSql.java @@ -29,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.nio.file.Path; import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; @@ -72,6 +73,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; +import org.junit.jupiter.api.io.TempDir; import com.google.common.collect.ImmutableList; @@ -95,6 +97,9 @@ public class TestFlightSql { protected static FlightServer server; protected static FlightSqlClient sqlClient; + @TempDir + private static Path dbPath = FlightSqlExample.DB_PATH; + @BeforeAll public static void setUp() throws Exception { setUpClientServer(); @@ -106,7 +111,7 @@ private static void setUpClientServer() throws Exception { final Location serverLocation = Location.forGrpcInsecure(LOCALHOST, 0); server = FlightServer.builder(allocator, serverLocation, - new FlightSqlExample(serverLocation, FlightSqlExample.DB_NAME)) + new FlightSqlExample(serverLocation, dbPath)) .build() .start(); @@ -152,7 +157,6 @@ protected static void setUpExpectedResultsMap() { @AfterAll public static void tearDown() throws Exception { close(sqlClient, server, allocator); - FlightSqlExample.removeDerbyDatabaseIfExists(FlightSqlExample.DB_NAME); } private static List> getNonConformingResultsForGetSqlInfo(final List> results) { diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSqlStateless.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSqlStateless.java index 09c7b2ef87f45..33e3a3c881bfe 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSqlStateless.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSqlStateless.java @@ -21,6 +21,8 @@ import static org.apache.arrow.util.AutoCloseables.close; import static org.hamcrest.CoreMatchers.*; +import java.nio.file.Path; + import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightEndpoint; import org.apache.arrow.flight.FlightInfo; @@ -39,11 +41,15 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; /** * Test direct usage of Flight SQL workflows. */ public class TestFlightSqlStateless extends TestFlightSql { + @TempDir + private static Path dbPath = FlightSqlStatelessExample.DB_PATH; + @BeforeAll public static void setUp() throws Exception { @@ -54,7 +60,6 @@ public static void setUp() throws Exception { @AfterAll public static void tearDown() throws Exception { close(sqlClient, server, allocator); - FlightSqlStatelessExample.removeDerbyDatabaseIfExists(FlightSqlStatelessExample.DB_NAME); } private static void setUpClientServer() throws Exception { @@ -62,7 +67,7 @@ private static void setUpClientServer() throws Exception { final Location serverLocation = Location.forGrpcInsecure(LOCALHOST, 0); server = FlightServer.builder(allocator, serverLocation, - new FlightSqlStatelessExample(serverLocation, FlightSqlStatelessExample.DB_NAME)) + new FlightSqlStatelessExample(serverLocation, dbPath)) .build() .start();