JingsongLi commented on code in PR #6145: URL: https://github.com/apache/paimon/pull/6145#discussion_r2299859969
########## paimon-format/src/test/java/org/apache/paimon/format/text/HadoopCompressionUtilsTest.java: ########## @@ -0,0 +1,368 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.paimon.format.text; + +import org.apache.paimon.format.HadoopCompressionType; +import org.apache.paimon.fs.Path; +import org.apache.paimon.fs.PositionOutputStream; +import org.apache.paimon.fs.SeekableInputStream; +import org.apache.paimon.fs.local.LocalFileIO; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +/** Test for {@link HadoopCompressionUtils}. */ +class HadoopCompressionUtilsTest { + + @TempDir java.nio.file.Path tempDir; + + private static final String TEST_DATA = "This is test data for compression."; + + @Test + void testCreateCompressedOutputStreamWithNoneCompression() throws IOException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + TestPositionOutputStream positionOutputStream = + new TestPositionOutputStream(byteArrayOutputStream); + + OutputStream result = + HadoopCompressionUtils.createCompressedOutputStream( + positionOutputStream, HadoopCompressionType.NONE.value()); + + assertThat(result).isSameAs(positionOutputStream); + } + + @Test + void testCreateCompressedOutputStreamWithInvalidCompression() { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + TestPositionOutputStream positionOutputStream = + new TestPositionOutputStream(byteArrayOutputStream); + + assertThatThrownBy( + () -> + HadoopCompressionUtils.createCompressedOutputStream( + positionOutputStream, "invalid")) + .isInstanceOf(IOException.class) + .hasMessageContaining("Failed to create compression stream"); + } + + @Test + void testCreateDecompressedInputStreamWithNoCompression() throws IOException { + byte[] testData = TEST_DATA.getBytes(StandardCharsets.UTF_8); + TestSeekableInputStream seekableInputStream = new TestSeekableInputStream(testData); + Path filePath = new Path("test.txt"); + + InputStream result = + HadoopCompressionUtils.createDecompressedInputStream(seekableInputStream, filePath); + + // For uncompressed files, should return the original stream + assertThat(result).isSameAs(seekableInputStream); + } + + @Test + void testCreateDecompressedInputStreamWithGzipFile() throws IOException { + byte[] testData = TEST_DATA.getBytes(StandardCharsets.UTF_8); + TestSeekableInputStream seekableInputStream = new TestSeekableInputStream(testData); + Path filePath = new Path("test.txt.gz"); + + InputStream result = + HadoopCompressionUtils.createDecompressedInputStream(seekableInputStream, filePath); + + assertThat(result).isNotNull(); + // For compressed files, should return a different stream (decompression wrapper) + // Note: The actual decompression behavior depends on Hadoop codecs being available + } + + @ParameterizedTest + @EnumSource(HadoopCompressionType.class) + void testCreateCompressedOutputStreamWithAvailableCompressions( + HadoopCompressionType compressionType) throws IOException { + if (compressionType.hadoopCodecClassName() == null) { + return; // Skip types without codec class names + } + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + TestPositionOutputStream positionOutputStream = + new TestPositionOutputStream(byteArrayOutputStream); + + try { + OutputStream compressedStream = + HadoopCompressionUtils.createCompressedOutputStream( + positionOutputStream, compressionType.value()); + + assertThat(compressedStream).isNotNull(); + + // Write and close to ensure compression happens + compressedStream.write(TEST_DATA.getBytes(StandardCharsets.UTF_8)); + compressedStream.close(); + + // Verify that some data was written + assertThat(byteArrayOutputStream.toByteArray()).isNotEmpty(); + } catch (IOException e) { Review Comment: Do not use try catch to validate exception. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
