|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.iceberg.manifest; |
| 20 | + |
| 21 | +import org.apache.paimon.types.DataTypes; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.DisplayName; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | + |
| 29 | +import java.nio.ByteBuffer; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 35 | + |
| 36 | +class IcebergConversionsVarcharTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + void testEmptyString() { |
| 40 | + String empty = ""; |
| 41 | + ByteBuffer result = IcebergConversions.toByteBuffer(DataTypes.VARCHAR(10), empty); |
| 42 | + String decodedString = new String(result.array(), StandardCharsets.UTF_8); |
| 43 | + assertThat(result.array()).isEmpty(); |
| 44 | + assertThat(empty).isEqualTo(decodedString); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testNullHandling() { |
| 49 | + assertThatThrownBy(() -> IcebergConversions.toByteBuffer(DataTypes.VARCHAR(10), null)) |
| 50 | + .isInstanceOf(NullPointerException.class); |
| 51 | + } |
| 52 | + |
| 53 | + @ParameterizedTest |
| 54 | + @MethodSource("provideSpecialStrings") |
| 55 | + @DisplayName("Test special string cases") |
| 56 | + void testSpecialStrings(String input) { |
| 57 | + ByteBuffer result = IcebergConversions.toByteBuffer(DataTypes.VARCHAR(100), input); |
| 58 | + String decoded = new String(result.array(), 0, result.limit(), StandardCharsets.UTF_8); |
| 59 | + assertThat(decoded).isEqualTo(input); |
| 60 | + } |
| 61 | + |
| 62 | + private static Stream<Arguments> provideSpecialStrings() { |
| 63 | + return Stream.of( |
| 64 | + Arguments.of("Hello\u0000World"), // Embedded null |
| 65 | + Arguments.of("\n\r\t"), // Control characters |
| 66 | + Arguments.of(" "), // Single space |
| 67 | + Arguments.of(" "), // Multiple spaces |
| 68 | + Arguments.of("①②③"), // Unicode numbers |
| 69 | + Arguments.of("🌟🌞🌝"), // Emojis |
| 70 | + Arguments.of("Hello\uD83D\uDE00World"), // Surrogate pairs |
| 71 | + Arguments.of("\uFEFF"), // Byte Order Mark |
| 72 | + Arguments.of("Hello\\World"), // Backslashes |
| 73 | + Arguments.of("Hello\"World"), // Quotes |
| 74 | + Arguments.of("Hello'World"), // Single quotes |
| 75 | + Arguments.of("Hello\bWorld"), // Backspace |
| 76 | + Arguments.of("Hello\fWorld") // Form feed |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + @ParameterizedTest |
| 81 | + @MethodSource("provideLongStrings") |
| 82 | + void testLongStrings(String input) { |
| 83 | + ByteBuffer result = |
| 84 | + IcebergConversions.toByteBuffer(DataTypes.VARCHAR(input.length()), input); |
| 85 | + String decoded = new String(result.array(), 0, result.limit(), StandardCharsets.UTF_8); |
| 86 | + assertThat(decoded).isEqualTo(input).hasSize(input.length()); |
| 87 | + } |
| 88 | + |
| 89 | + private static Stream<Arguments> provideLongStrings() { |
| 90 | + return Stream.of( |
| 91 | + Arguments.of(createString(1)), |
| 92 | + Arguments.of(createString(10)), |
| 93 | + Arguments.of(createString(100)), |
| 94 | + Arguments.of(createString(1000)), |
| 95 | + Arguments.of(createString(10000))); |
| 96 | + } |
| 97 | + |
| 98 | + private static String createString(int length) { |
| 99 | + StringBuilder sb = new StringBuilder(length); |
| 100 | + for (int i = 0; i < length; i++) { |
| 101 | + sb.append('a'); |
| 102 | + } |
| 103 | + return sb.toString(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testMultiByteCharacters() { |
| 108 | + String[] inputs = { |
| 109 | + "中文", // Chinese |
| 110 | + "한글", // Korean |
| 111 | + "日本語", // Japanese |
| 112 | + "🌟", // Emoji (4 bytes) |
| 113 | + "Café", // Latin-1 Supplement |
| 114 | + "Привет", // Cyrillic |
| 115 | + "שָׁלוֹם", // Hebrew with combining marks |
| 116 | + "ᄀᄁᄂᄃᄄ", // Hangul Jamo |
| 117 | + "बहुत बढ़िया", // Devanagari |
| 118 | + "العربية" // Arabic |
| 119 | + }; |
| 120 | + |
| 121 | + for (String input : inputs) { |
| 122 | + ByteBuffer result = |
| 123 | + IcebergConversions.toByteBuffer(DataTypes.VARCHAR(input.length() * 4), input); |
| 124 | + String decoded = new String(result.array(), 0, result.limit(), StandardCharsets.UTF_8); |
| 125 | + assertThat(decoded).isEqualTo(input); |
| 126 | + assertThat(result.limit()).isGreaterThanOrEqualTo(input.length()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void testBufferProperties() { |
| 132 | + String input = "Hello, World!"; |
| 133 | + ByteBuffer result = |
| 134 | + IcebergConversions.toByteBuffer(DataTypes.VARCHAR(input.length()), input); |
| 135 | + |
| 136 | + assertThat(result.limit()).isEqualTo(result.array().length); |
| 137 | + assertThat(containsTrailingZeros(result)).isFalse(); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testConcurrentAccess() throws InterruptedException { |
| 142 | + int threadCount = 10; |
| 143 | + Thread[] threads = new Thread[threadCount]; |
| 144 | + String[] inputs = new String[threadCount]; |
| 145 | + ByteBuffer[] results = new ByteBuffer[threadCount]; |
| 146 | + |
| 147 | + for (int i = 0; i < threadCount; i++) { |
| 148 | + final int index = i; |
| 149 | + inputs[index] = "Thread" + index; |
| 150 | + threads[index] = |
| 151 | + new Thread( |
| 152 | + () -> { |
| 153 | + results[index] = |
| 154 | + IcebergConversions.toByteBuffer( |
| 155 | + DataTypes.VARCHAR(inputs[index].length()), |
| 156 | + inputs[index]); |
| 157 | + }); |
| 158 | + threads[index].start(); |
| 159 | + } |
| 160 | + |
| 161 | + for (Thread thread : threads) { |
| 162 | + thread.join(); |
| 163 | + } |
| 164 | + |
| 165 | + for (int i = 0; i < threadCount; i++) { |
| 166 | + String decoded = |
| 167 | + new String(results[i].array(), 0, results[i].limit(), StandardCharsets.UTF_8); |
| 168 | + assertThat(decoded).isEqualTo(inputs[i]); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private boolean containsTrailingZeros(ByteBuffer buffer) { |
| 173 | + byte[] array = buffer.array(); |
| 174 | + for (int i = buffer.limit(); i < array.length; i++) { |
| 175 | + if (array[i] != 0) { |
| 176 | + return true; |
| 177 | + } |
| 178 | + } |
| 179 | + return false; |
| 180 | + } |
| 181 | +} |
0 commit comments