2222import java .io .File ;
2323import java .io .IOException ;
2424import java .nio .file .Files ;
25+ import java .nio .file .Path ;
2526import java .sql .Connection ;
2627import java .sql .DriverManager ;
2728import java .sql .ResultSet ;
3233import org .apache .calcite .jdbc .CalciteConnection ;
3334import org .apache .calcite .schema .SchemaPlus ;
3435import org .apache .calcite .schema .Table ;
35- import org .junit .jupiter .api .BeforeAll ;
3636import 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}
0 commit comments