From e61d64b8d34bf02ae0cf29a3df1c40da78080e2b Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Mon, 10 Jul 2023 14:54:54 +0200 Subject: [PATCH] Test locations with trailing whitespace for all filesystems --- .../s3/AbstractTestS3FileSystem.java | 5 +- .../AbstractTestTrinoFileSystem.java | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/trino-filesystem-s3/src/test/java/io/trino/filesystem/s3/AbstractTestS3FileSystem.java b/lib/trino-filesystem-s3/src/test/java/io/trino/filesystem/s3/AbstractTestS3FileSystem.java index d9519f1310b2..39076e1aeca4 100644 --- a/lib/trino-filesystem-s3/src/test/java/io/trino/filesystem/s3/AbstractTestS3FileSystem.java +++ b/lib/trino-filesystem-s3/src/test/java/io/trino/filesystem/s3/AbstractTestS3FileSystem.java @@ -63,8 +63,11 @@ final void cleanup() fileSystemFactory = null; } + /** + * Tests same things as {@link #testFileWithTrailingWhitespace()} but with setup and assertions using {@link S3Client}. + */ @Test - public void testFileWithTrailingWhitespace() + public void testFileWithTrailingWhitespaceAgainstNativeClient() throws IOException { try (S3Client s3Client = createS3Client()) { diff --git a/lib/trino-filesystem/src/test/java/io/trino/filesystem/AbstractTestTrinoFileSystem.java b/lib/trino-filesystem/src/test/java/io/trino/filesystem/AbstractTestTrinoFileSystem.java index 2fca3b0b7887..aa708169e45c 100644 --- a/lib/trino-filesystem/src/test/java/io/trino/filesystem/AbstractTestTrinoFileSystem.java +++ b/lib/trino-filesystem/src/test/java/io/trino/filesystem/AbstractTestTrinoFileSystem.java @@ -14,6 +14,7 @@ package io.trino.filesystem; import com.google.common.collect.Ordering; +import com.google.common.io.ByteStreams; import com.google.common.io.Closer; import io.airlift.slice.Slice; import io.airlift.slice.Slices; @@ -825,6 +826,57 @@ public void testDirectoryExists() } } + @Test + public void testFileWithTrailingWhitespace() + throws IOException + { + try (Closer closer = Closer.create()) { + Location location = createBlob(closer, "dir/whitespace "); + + // Verify listing + assertThat(listPath("dir")).isEqualTo(List.of(location)); + + // Verify reading + TrinoInputFile inputFile = getFileSystem().newInputFile(location); + assertThat(inputFile.exists()).as("exists").isTrue(); + try (TrinoInputStream inputStream = inputFile.newStream()) { + byte[] bytes = ByteStreams.toByteArray(inputStream); + assertThat(bytes).isEqualTo(("test blob content for " + location).getBytes(UTF_8)); + } + + // Verify writing + byte[] newContents = "bar bar baz new content".getBytes(UTF_8); + try (OutputStream outputStream = getFileSystem().newOutputFile(location).createOrOverwrite()) { + outputStream.write(newContents.clone()); + } + try (TrinoInputStream inputStream = inputFile.newStream()) { + byte[] bytes = ByteStreams.toByteArray(inputStream); + assertThat(bytes).isEqualTo(newContents); + } + + // Verify deleting + getFileSystem().deleteFile(location); + assertThat(inputFile.exists()).as("exists after delete").isFalse(); + + // Verify renames + if (supportsRenameFile()) { + Location source = createBlob(closer, "dir/another trailing whitespace "); + Location target = getRootLocation().appendPath("dir/after rename still whitespace "); + getFileSystem().renameFile(source, target); + assertThat(getFileSystem().newInputFile(source).exists()).as("source exists after rename").isFalse(); + assertThat(getFileSystem().newInputFile(target).exists()).as("target exists after rename").isTrue(); + + try (TrinoInputStream inputStream = getFileSystem().newInputFile(target).newStream()) { + byte[] bytes = ByteStreams.toByteArray(inputStream); + assertThat(bytes).isEqualTo(("test blob content for " + source).getBytes(UTF_8)); + } + + getFileSystem().deleteFile(target); + assertThat(getFileSystem().newInputFile(target).exists()).as("target exists after delete").isFalse(); + } + } + } + private List listPath(String path) throws IOException {