Skip to content

Commit 4e31f16

Browse files
committed
Clean calcite tests
1 parent 046a5e6 commit 4e31f16

File tree

5 files changed

+49
-95
lines changed

5 files changed

+49
-95
lines changed

baremaps-calcite/src/test/java/org/apache/baremaps/calcite/BaremapsDdlExecutorTest.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.jupiter.api.Assertions.*;
2121

2222
import java.io.IOException;
23+
import java.nio.file.Path;
2324
import java.nio.file.Paths;
2425
import java.sql.*;
2526
import java.util.List;
@@ -36,6 +37,7 @@
3637
import org.junit.jupiter.api.AfterEach;
3738
import org.junit.jupiter.api.BeforeEach;
3839
import org.junit.jupiter.api.Test;
40+
import org.junit.jupiter.api.io.TempDir;
3941
import org.locationtech.jts.geom.Coordinate;
4042
import org.locationtech.jts.geom.GeometryFactory;
4143
import org.locationtech.jts.geom.Point;
@@ -46,10 +48,17 @@
4648
*/
4749
public class BaremapsDdlExecutorTest {
4850

49-
private static final String CITY_DATA_DIR = "city_data";
50-
private static final String CITY_POPULATION_DIR = "city_population";
51-
private static final String POPULATION_DATA_DIR = "population_data";
52-
private static final String TEST_TABLE_DATA_DIR = "test_table_data";
51+
@TempDir
52+
Path cityDataDir;
53+
54+
@TempDir
55+
Path cityPopulationDir;
56+
57+
@TempDir
58+
Path populationDataDir;
59+
60+
@TempDir
61+
Path testTableDataDir;
5362

5463
private DataCollection<DataRow> cityCollection;
5564
private DataCollection<DataRow> populationCollection;
@@ -70,23 +79,23 @@ void setUp() throws IOException {
7079
testTableSchema = createTestTableSchema();
7180

7281
// Create and initialize collections
73-
MemoryMappedDirectory cityMemory = new MemoryMappedDirectory(Paths.get(CITY_DATA_DIR));
82+
MemoryMappedDirectory cityMemory = new MemoryMappedDirectory(cityDataDir);
7483
DataRowType cityRowType = new DataRowType(citySchema);
7584
cityCollection = AppendOnlyLog.<DataRow>builder()
7685
.dataType(cityRowType)
7786
.memory(cityMemory)
7887
.build();
7988

8089
MemoryMappedDirectory populationMemory =
81-
new MemoryMappedDirectory(Paths.get(POPULATION_DATA_DIR));
90+
new MemoryMappedDirectory(populationDataDir);
8291
DataRowType populationRowType = new DataRowType(populationSchema);
8392
populationCollection = AppendOnlyLog.<DataRow>builder()
8493
.dataType(populationRowType)
8594
.memory(populationMemory)
8695
.build();
8796

8897
MemoryMappedDirectory testTableMemory =
89-
new MemoryMappedDirectory(Paths.get(TEST_TABLE_DATA_DIR));
98+
new MemoryMappedDirectory(testTableDataDir);
9099
DataRowType testTableRowType = new DataRowType(testTableSchema);
91100
testTableCollection = AppendOnlyLog.<DataRow>builder()
92101
.dataType(testTableRowType)
@@ -96,12 +105,6 @@ void setUp() throws IOException {
96105

97106
@AfterEach
98107
void tearDown() throws Exception {
99-
// Clean up directories
100-
FileUtils.deleteRecursively(Paths.get(CITY_DATA_DIR).toFile());
101-
FileUtils.deleteRecursively(Paths.get(POPULATION_DATA_DIR).toFile());
102-
FileUtils.deleteRecursively(Paths.get(CITY_POPULATION_DIR).toFile());
103-
FileUtils.deleteRecursively(Paths.get(TEST_TABLE_DATA_DIR).toFile());
104-
105108
// Clean up any additional directories created during test execution
106109
FileUtils.deleteRecursively(Paths.get("options_table").toFile());
107110
FileUtils.deleteRecursively(Paths.get("options_table_as").toFile());

baremaps-calcite/src/test/java/org/apache/baremaps/calcite/csv/CsvSchemaTest.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import java.io.File;
2323
import java.io.IOException;
24-
import java.nio.file.Files;
2524
import java.sql.Connection;
2625
import java.sql.DriverManager;
2726
import java.sql.ResultSet;
@@ -32,7 +31,6 @@
3231
import org.apache.calcite.jdbc.CalciteConnection;
3332
import org.apache.calcite.schema.SchemaPlus;
3433
import org.apache.calcite.schema.Table;
35-
import org.junit.jupiter.api.BeforeAll;
3634
import org.junit.jupiter.api.Test;
3735

3836
class CsvSchemaTest {
@@ -41,31 +39,6 @@ class CsvSchemaTest {
4139
private static final char SEPARATOR = ',';
4240
private static final boolean HAS_HEADER = true;
4341

44-
@BeforeAll
45-
static void setup() throws IOException {
46-
// Ensure the test directory exists
47-
SAMPLE_CSV_DIR.mkdirs();
48-
49-
// Create test CSV files if they don't exist
50-
if (!TestFiles.CITIES_CSV.toFile().exists()) {
51-
// Create a sample cities.csv file
52-
String citiesContent = "city,country,population\n"
53-
+ "Paris,France,2148000\n"
54-
+ "London,UK,8982000\n"
55-
+ "Tokyo,Japan,37400000\n";
56-
Files.writeString(TestFiles.CITIES_CSV, citiesContent);
57-
}
58-
59-
if (!TestFiles.COUNTRIES_CSV.toFile().exists()) {
60-
// Create a sample countries.csv file
61-
String countriesContent = "country,continent,population\n"
62-
+ "France,Europe,67390000\n"
63-
+ "UK,Europe,67220000\n"
64-
+ "Japan,Asia,125700000\n";
65-
Files.writeString(TestFiles.COUNTRIES_CSV, countriesContent);
66-
}
67-
}
68-
6942
@Test
7043
void testSchemaCreation() throws IOException {
7144
// Create a CsvSchema instance

baremaps-calcite/src/test/java/org/apache/baremaps/calcite/csv/CsvTableTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
public class CsvTableTest {
3939

4040
private static final File CITIES_CSV = TestFiles.CITIES_CSV.toFile();
41-
private static final File COUNTRIES_CSV = TestFiles.COUNTRIES_CSV.toFile();
4241
private static final char SEPARATOR = ',';
4342
private static final boolean HAS_HEADER = true;
4443

baremaps-calcite/src/test/java/org/apache/baremaps/calcite/flatgeobuf/FlatGeoBufSchemaTest.java

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.File;
2323
import java.io.IOException;
2424
import java.nio.file.Files;
25+
import java.nio.file.Path;
2526
import java.sql.Connection;
2627
import java.sql.DriverManager;
2728
import java.sql.ResultSet;
@@ -32,8 +33,8 @@
3233
import org.apache.calcite.jdbc.CalciteConnection;
3334
import org.apache.calcite.schema.SchemaPlus;
3435
import org.apache.calcite.schema.Table;
35-
import org.junit.jupiter.api.BeforeAll;
3636
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.api.io.TempDir;
3738

3839
/**
3940
* Tests for the FlatGeoBufSchema class, which provides access to FlatGeoBuf files through the
@@ -43,18 +44,8 @@ class FlatGeoBufSchemaTest {
4344

4445
private static final File SAMPLE_FLATGEOBUF_DIR = TestFiles.SAMPLE_FLATGEOBUF_DIR.toFile();
4546

46-
@BeforeAll
47-
static void setup() throws IOException {
48-
// Ensure the test directory exists
49-
SAMPLE_FLATGEOBUF_DIR.mkdirs();
50-
51-
// Create test FlatGeoBuf files if they don't exist
52-
if (!TestFiles.POINT_FLATGEOBUF.toFile().exists()) {
53-
// We can't easily create FlatGeoBuf files in the test, so we'll just ensure the directory
54-
// exists
55-
// and rely on the test files being present in the test resources
56-
}
57-
}
47+
@TempDir
48+
Path tempDir;
5849

5950
@Test
6051
void testSchemaCreation() throws IOException {
@@ -106,12 +97,8 @@ void testSqlQueryWithSchema() throws Exception {
10697

10798
@Test
10899
void testSchemaWithMultipleFiles() throws IOException {
109-
// Create a temporary directory for test files
110-
File tempDir = Files.createTempDirectory("flatgeobuf-test").toFile();
111-
tempDir.deleteOnExit();
112-
113100
// Create a FlatGeoBufSchema instance with the temporary directory
114-
FlatGeoBufSchema schema = new FlatGeoBufSchema(tempDir);
101+
FlatGeoBufSchema schema = new FlatGeoBufSchema(tempDir.toFile());
115102

116103
// Get the table map
117104
Map<String, Table> tableMap = schema.getTableMap();
@@ -123,7 +110,7 @@ void testSchemaWithMultipleFiles() throws IOException {
123110
@Test
124111
void testSchemaWithNonExistentDirectory() throws IOException {
125112
// Create a non-existent directory path
126-
File nonExistentDir = new File(SAMPLE_FLATGEOBUF_DIR, "non-existent-subdirectory");
113+
File nonExistentDir = tempDir.resolve("non-existent-directory").toFile();
127114

128115
// Create a FlatGeoBufSchema instance with the non-existent directory
129116
FlatGeoBufSchema schema = new FlatGeoBufSchema(nonExistentDir);
@@ -137,51 +124,43 @@ void testSchemaWithNonExistentDirectory() throws IOException {
137124

138125
@Test
139126
void testSchemaWithMultipleFlatGeoBufFiles() throws Exception {
140-
// Create a temporary directory for test files
141-
File tempDir = Files.createTempDirectory("flatgeobuf-multi-test").toFile();
142-
tempDir.deleteOnExit();
143-
144127
// Copy the sample FlatGeoBuf file to the temporary directory with different names
145128
File pointFile = TestFiles.POINT_FLATGEOBUF.toFile();
146-
if (pointFile.exists()) {
147-
// Copy the file with different names to simulate multiple files
148-
Files.copy(pointFile.toPath(), new File(tempDir, "points.fgb").toPath());
149-
Files.copy(pointFile.toPath(), new File(tempDir, "cities.fgb").toPath());
150129

151-
// Create a FlatGeoBufSchema instance with the temporary directory
152-
FlatGeoBufSchema schema = new FlatGeoBufSchema(tempDir);
130+
// Copy the file with different names to simulate multiple files
131+
Files.copy(pointFile.toPath(), new File(tempDir.toFile(), "points.fgb").toPath());
132+
Files.copy(pointFile.toPath(), new File(tempDir.toFile(), "cities.fgb").toPath());
153133

154-
// Get the table map
155-
Map<String, Table> tableMap = schema.getTableMap();
134+
// Create a FlatGeoBufSchema instance with the temporary directory
135+
FlatGeoBufSchema schema = new FlatGeoBufSchema(tempDir.toFile());
156136

157-
// Verify that the schema has the expected tables
158-
assertEquals(2, tableMap.size(), "Schema should have 2 tables");
159-
assertTrue(tableMap.containsKey("points"), "Schema should contain the 'points' table");
160-
assertTrue(tableMap.containsKey("cities"), "Schema should contain the 'cities' table");
137+
// Get the table map
138+
Map<String, Table> tableMap = schema.getTableMap();
161139

162-
// Test SQL query with one of the tables
163-
Properties info = new Properties();
164-
info.setProperty("lex", "MYSQL");
140+
// Verify that the schema has the expected tables
141+
assertEquals(2, tableMap.size(), "Schema should have 2 tables");
142+
assertTrue(tableMap.containsKey("points"), "Schema should contain the 'points' table");
143+
assertTrue(tableMap.containsKey("cities"), "Schema should contain the 'cities' table");
165144

166-
try (Connection connection = DriverManager.getConnection("jdbc:calcite:", info)) {
167-
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
168-
SchemaPlus rootSchema = calciteConnection.getRootSchema();
145+
// Test SQL query with one of the tables
146+
Properties info = new Properties();
147+
info.setProperty("lex", "MYSQL");
169148

170-
// Register the schema
171-
rootSchema.add("multi_flatgeobuf", schema);
149+
try (Connection connection = DriverManager.getConnection("jdbc:calcite:", info)) {
150+
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
151+
SchemaPlus rootSchema = calciteConnection.getRootSchema();
172152

173-
// Execute a query on one of the tables
174-
try (Statement statement = connection.createStatement();
175-
ResultSet resultSet = statement.executeQuery(
176-
"SELECT * FROM multi_flatgeobuf.points LIMIT 5")) {
153+
// Register the schema
154+
rootSchema.add("multi_flatgeobuf", schema);
177155

178-
// Verify that we get results
179-
assertTrue(resultSet.next(), "Should have at least one row");
180-
}
156+
// Execute a query on one of the tables
157+
try (Statement statement = connection.createStatement();
158+
ResultSet resultSet = statement.executeQuery(
159+
"SELECT * FROM multi_flatgeobuf.points LIMIT 5")) {
160+
161+
// Verify that we get results
162+
assertTrue(resultSet.next(), "Should have at least one row");
181163
}
182-
} else {
183-
// Skip the test if the sample file doesn't exist
184-
System.out.println("Skipping testSchemaWithMultipleFlatGeoBufFiles: sample file not found");
185164
}
186165
}
187166
}

baremaps-calcite/src/test/java/org/apache/baremaps/calcite/geopackage/GeoPackageTableTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
class GeoPackageTableTest {
3939

4040
private static final File SAMPLE_GEOPACKAGE = TestFiles.GEOPACKAGE.toFile();
41-
private static final String TABLE_NAME = "countries"; // The table name in the sample GeoPackage
41+
private static final String TABLE_NAME = "countries";
4242

4343
@Test
4444
void testSchemaVerification() throws IOException {

0 commit comments

Comments
 (0)