22
22
import java .io .File ;
23
23
import java .io .IOException ;
24
24
import java .nio .file .Files ;
25
+ import java .nio .file .Path ;
25
26
import java .sql .Connection ;
26
27
import java .sql .DriverManager ;
27
28
import java .sql .ResultSet ;
32
33
import org .apache .calcite .jdbc .CalciteConnection ;
33
34
import org .apache .calcite .schema .SchemaPlus ;
34
35
import org .apache .calcite .schema .Table ;
35
- import org .junit .jupiter .api .BeforeAll ;
36
36
import org .junit .jupiter .api .Test ;
37
+ import org .junit .jupiter .api .io .TempDir ;
37
38
38
39
/**
39
40
* Tests for the FlatGeoBufSchema class, which provides access to FlatGeoBuf files through the
@@ -43,18 +44,8 @@ class FlatGeoBufSchemaTest {
43
44
44
45
private static final File SAMPLE_FLATGEOBUF_DIR = TestFiles .SAMPLE_FLATGEOBUF_DIR .toFile ();
45
46
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 ;
58
49
59
50
@ Test
60
51
void testSchemaCreation () throws IOException {
@@ -106,12 +97,8 @@ void testSqlQueryWithSchema() throws Exception {
106
97
107
98
@ Test
108
99
void testSchemaWithMultipleFiles () throws IOException {
109
- // Create a temporary directory for test files
110
- File tempDir = Files .createTempDirectory ("flatgeobuf-test" ).toFile ();
111
- tempDir .deleteOnExit ();
112
-
113
100
// Create a FlatGeoBufSchema instance with the temporary directory
114
- FlatGeoBufSchema schema = new FlatGeoBufSchema (tempDir );
101
+ FlatGeoBufSchema schema = new FlatGeoBufSchema (tempDir . toFile () );
115
102
116
103
// Get the table map
117
104
Map <String , Table > tableMap = schema .getTableMap ();
@@ -123,7 +110,7 @@ void testSchemaWithMultipleFiles() throws IOException {
123
110
@ Test
124
111
void testSchemaWithNonExistentDirectory () throws IOException {
125
112
// 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 ( );
127
114
128
115
// Create a FlatGeoBufSchema instance with the non-existent directory
129
116
FlatGeoBufSchema schema = new FlatGeoBufSchema (nonExistentDir );
@@ -137,51 +124,43 @@ void testSchemaWithNonExistentDirectory() throws IOException {
137
124
138
125
@ Test
139
126
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
-
144
127
// Copy the sample FlatGeoBuf file to the temporary directory with different names
145
128
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 ());
150
129
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 ());
153
133
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 () );
156
136
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 ();
161
139
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" );
165
144
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" );
169
148
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 ();
172
152
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 );
177
155
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" );
181
163
}
182
- } else {
183
- // Skip the test if the sample file doesn't exist
184
- System .out .println ("Skipping testSchemaWithMultipleFlatGeoBufFiles: sample file not found" );
185
164
}
186
165
}
187
166
}
0 commit comments