|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.fluss.utils; |
| 19 | + |
| 20 | +import org.apache.fluss.row.BinaryString; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | +/** Test for {@link BinaryStringUtils}. */ |
| 27 | +public class BinaryStringUtilsTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testConcat() { |
| 31 | + // Test basic concatenation |
| 32 | + BinaryString s1 = BinaryString.fromString("hello"); |
| 33 | + BinaryString s2 = BinaryString.fromString(" "); |
| 34 | + BinaryString s3 = BinaryString.fromString("world"); |
| 35 | + |
| 36 | + BinaryString result = BinaryStringUtils.concat(s1, s2, s3); |
| 37 | + assertThat(result.toString()).isEqualTo("hello world"); |
| 38 | + |
| 39 | + // Test concatenation with empty string |
| 40 | + BinaryString empty = BinaryString.fromString(""); |
| 41 | + result = BinaryStringUtils.concat(s1, empty, s3); |
| 42 | + assertThat(result.toString()).isEqualTo("helloworld"); |
| 43 | + |
| 44 | + // Test concatenation with single string |
| 45 | + result = BinaryStringUtils.concat(s1); |
| 46 | + assertThat(result.toString()).isEqualTo("hello"); |
| 47 | + |
| 48 | + // Test concatenation with null |
| 49 | + result = BinaryStringUtils.concat(s1, null, s3); |
| 50 | + assertThat(result).isNull(); |
| 51 | + |
| 52 | + // Test concatenation with Chinese characters |
| 53 | + BinaryString chinese1 = BinaryString.fromString("你好"); |
| 54 | + BinaryString chinese2 = BinaryString.fromString("世界"); |
| 55 | + result = BinaryStringUtils.concat(chinese1, chinese2); |
| 56 | + assertThat(result.toString()).isEqualTo("你好世界"); |
| 57 | + |
| 58 | + // Test concatenation with mixed ASCII and multi-byte characters |
| 59 | + BinaryString mixed1 = BinaryString.fromString("Hello"); |
| 60 | + BinaryString mixed2 = BinaryString.fromString("世界"); |
| 61 | + BinaryString mixed3 = BinaryString.fromString("!"); |
| 62 | + result = BinaryStringUtils.concat(mixed1, mixed2, mixed3); |
| 63 | + assertThat(result.toString()).isEqualTo("Hello世界!"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testConcatWs() { |
| 68 | + BinaryString sep = BinaryString.fromString(","); |
| 69 | + BinaryString s1 = BinaryString.fromString("a"); |
| 70 | + BinaryString s2 = BinaryString.fromString("b"); |
| 71 | + BinaryString s3 = BinaryString.fromString("c"); |
| 72 | + |
| 73 | + // Test basic concatenation with separator |
| 74 | + BinaryString result = BinaryStringUtils.concatWs(sep, s1, s2, s3); |
| 75 | + assertThat(result.toString()).isEqualTo("a,b,c"); |
| 76 | + |
| 77 | + // Test with null separator |
| 78 | + result = BinaryStringUtils.concatWs(null, s1, s2, s3); |
| 79 | + assertThat(result).isNull(); |
| 80 | + |
| 81 | + // Test with null values in inputs (should skip nulls) |
| 82 | + result = BinaryStringUtils.concatWs(sep, s1, null, s3); |
| 83 | + assertThat(result.toString()).isEqualTo("a,c"); |
| 84 | + |
| 85 | + // Test with all null inputs |
| 86 | + result = BinaryStringUtils.concatWs(sep, null, null, null); |
| 87 | + assertThat(result).isEqualTo(BinaryString.EMPTY_UTF8); |
| 88 | + |
| 89 | + // Test with single input |
| 90 | + result = BinaryStringUtils.concatWs(sep, s1); |
| 91 | + assertThat(result.toString()).isEqualTo("a"); |
| 92 | + |
| 93 | + // Test with empty strings (should not skip empty strings) |
| 94 | + BinaryString empty = BinaryString.fromString(""); |
| 95 | + result = BinaryStringUtils.concatWs(sep, s1, empty, s3); |
| 96 | + assertThat(result.toString()).isEqualTo("a,,c"); |
| 97 | + |
| 98 | + // Test with different separator |
| 99 | + BinaryString dashSep = BinaryString.fromString("-"); |
| 100 | + result = BinaryStringUtils.concatWs(dashSep, s1, s2, s3); |
| 101 | + assertThat(result.toString()).isEqualTo("a-b-c"); |
| 102 | + |
| 103 | + // Test with multi-character separator |
| 104 | + BinaryString multiSep = BinaryString.fromString(" | "); |
| 105 | + result = BinaryStringUtils.concatWs(multiSep, s1, s2, s3); |
| 106 | + assertThat(result.toString()).isEqualTo("a | b | c"); |
| 107 | + |
| 108 | + // Test with Chinese characters |
| 109 | + BinaryString chineseSep = BinaryString.fromString(","); |
| 110 | + BinaryString chinese1 = BinaryString.fromString("你好"); |
| 111 | + BinaryString chinese2 = BinaryString.fromString("世界"); |
| 112 | + result = BinaryStringUtils.concatWs(chineseSep, chinese1, chinese2); |
| 113 | + assertThat(result.toString()).isEqualTo("你好,世界"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testConcatIterable() { |
| 118 | + BinaryString s1 = BinaryString.fromString("a"); |
| 119 | + BinaryString s2 = BinaryString.fromString("b"); |
| 120 | + BinaryString s3 = BinaryString.fromString("c"); |
| 121 | + |
| 122 | + // Test with iterable |
| 123 | + BinaryString result = BinaryStringUtils.concat(java.util.Arrays.asList(s1, s2, s3)); |
| 124 | + assertThat(result.toString()).isEqualTo("abc"); |
| 125 | + |
| 126 | + // Test with null in iterable |
| 127 | + result = BinaryStringUtils.concat(java.util.Arrays.asList(s1, null, s3)); |
| 128 | + assertThat(result).isNull(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testConcatWsIterable() { |
| 133 | + BinaryString sep = BinaryString.fromString(","); |
| 134 | + BinaryString s1 = BinaryString.fromString("a"); |
| 135 | + BinaryString s2 = BinaryString.fromString("b"); |
| 136 | + BinaryString s3 = BinaryString.fromString("c"); |
| 137 | + |
| 138 | + // Test with iterable |
| 139 | + BinaryString result = BinaryStringUtils.concatWs(sep, java.util.Arrays.asList(s1, s2, s3)); |
| 140 | + assertThat(result.toString()).isEqualTo("a,b,c"); |
| 141 | + |
| 142 | + // Test with null values in iterable (should skip nulls) |
| 143 | + result = BinaryStringUtils.concatWs(sep, java.util.Arrays.asList(s1, null, s3)); |
| 144 | + assertThat(result.toString()).isEqualTo("a,c"); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testConcatPerformance() { |
| 149 | + // Test to ensure concat works correctly for repeated concatenations |
| 150 | + // simulating listagg aggregation |
| 151 | + BinaryString delimiter = BinaryString.fromString(","); |
| 152 | + BinaryString accumulator = BinaryString.fromString("item1"); |
| 153 | + |
| 154 | + for (int i = 2; i <= 10; i++) { |
| 155 | + BinaryString newItem = BinaryString.fromString("item" + i); |
| 156 | + accumulator = BinaryStringUtils.concat(accumulator, delimiter, newItem); |
| 157 | + } |
| 158 | + |
| 159 | + assertThat(accumulator.toString()) |
| 160 | + .isEqualTo("item1,item2,item3,item4,item5,item6,item7,item8,item9,item10"); |
| 161 | + } |
| 162 | +} |
0 commit comments