diff --git a/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java b/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java index 6c6949ce42ae..1eed4168d439 100644 --- a/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java +++ b/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java @@ -203,10 +203,27 @@ protected boolean supportsEmptyNamespace() { return false; } + @TempDir private Path tempDir; + protected String baseTableLocation(TableIdentifier identifier) { return BASE_TABLE_LOCATION + "/" + identifier.namespace() + "/" + identifier.name(); } + protected URI resolveStorageLocation(String location) { + return URI.create(location); + } + + protected URI createStorageFile(String filename, String content) throws IOException { + Path path = tempDir.resolve(filename); + path.toFile().deleteOnExit(); + Files.writeString(path, content); + return path.toUri(); + } + + protected void moveStorageFile(URI source, URI target) throws IOException { + Files.move(Paths.get(source), Paths.get(target), StandardCopyOption.REPLACE_EXISTING); + } + @Test public void testCreateNamespace() { C catalog = catalog(); @@ -1009,7 +1026,7 @@ public void testLoadMissingTable() { } @Test - public void testLoadTableWithMissingMetadataFile(@TempDir Path tempDir) throws IOException { + public void testLoadTableWithMissingMetadataFile() throws IOException { C catalog = catalog(); if (requiresNamespaceCreate()) { @@ -1020,22 +1037,17 @@ public void testLoadTableWithMissingMetadataFile(@TempDir Path tempDir) throws I assertThat(catalog.tableExists(TBL)).as("Table should exist").isTrue(); Table table = catalog.loadTable(TBL); - String metadataFileLocation = - ((HasTableOperations) table).operations().current().metadataFileLocation(); - Path renamedMetadataFile = tempDir.resolve("tmp.json"); - renamedMetadataFile.toFile().deleteOnExit(); - Files.writeString(renamedMetadataFile, "metadata"); - Path metadataFilePath = - metadataFileLocation.startsWith("file:") - ? Paths.get(URI.create(metadataFileLocation)) - : Paths.get(metadataFileLocation); + URI metadataFileLocation = + resolveStorageLocation( + ((HasTableOperations) table).operations().current().metadataFileLocation()); + URI renamedMetadataFileLocation = createStorageFile("tmp.json", "metadata"); try { - Files.move(metadataFilePath, renamedMetadataFile, StandardCopyOption.REPLACE_EXISTING); + moveStorageFile(metadataFileLocation, renamedMetadataFileLocation); assertThatThrownBy(() -> catalog.loadTable(TBL)) .isInstanceOf(NotFoundException.class) .hasMessageContaining("Failed to open input stream for file: " + metadataFileLocation); } finally { - Files.move(renamedMetadataFile, metadataFilePath, StandardCopyOption.REPLACE_EXISTING); + moveStorageFile(renamedMetadataFileLocation, metadataFileLocation); } } diff --git a/core/src/test/java/org/apache/iceberg/inmemory/TestInMemoryCatalog.java b/core/src/test/java/org/apache/iceberg/inmemory/TestInMemoryCatalog.java index 827450d4a398..67e1fce3fdcd 100644 --- a/core/src/test/java/org/apache/iceberg/inmemory/TestInMemoryCatalog.java +++ b/core/src/test/java/org/apache/iceberg/inmemory/TestInMemoryCatalog.java @@ -21,8 +21,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import java.io.IOException; -import java.nio.file.Path; import java.util.Map; import org.apache.iceberg.CatalogProperties; import org.apache.iceberg.HasTableOperations; @@ -32,7 +30,6 @@ import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; public class TestInMemoryCatalog extends CatalogTests { private InMemoryCatalog catalog; @@ -89,7 +86,7 @@ protected boolean supportsNestedNamespaces() { @Test @Override - public void testLoadTableWithMissingMetadataFile(@TempDir Path tempDir) throws IOException { + public void testLoadTableWithMissingMetadataFile() { if (requiresNamespaceCreate()) { catalog.createNamespace(TBL.namespace()); diff --git a/core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java b/core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java index d2d45c6173f5..99c30cb027a9 100644 --- a/core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java +++ b/core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java @@ -3678,7 +3678,7 @@ private void verifyCreatePost(Namespace ns, Map headers) { @Test @Override - public void testLoadTableWithMissingMetadataFile(@TempDir Path tempDir) { + public void testLoadTableWithMissingMetadataFile() { if (requiresNamespaceCreate()) { restCatalog.createNamespace(TBL.namespace());