|
| 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.utils; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import java.util.NoSuchElementException; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 32 | + |
| 33 | +/** Tests for {@link MutableObjectIteratorAdapter}. */ |
| 34 | +class MutableObjectIteratorAdapterTest { |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testBasicIteration() { |
| 38 | + List<Integer> values = Arrays.asList(1, 2, 3, 4, 5); |
| 39 | + MutableObjectIterator<Integer> delegate = createTestIterator(values); |
| 40 | + |
| 41 | + MutableObjectIteratorAdapter<Integer, Integer> adapter = |
| 42 | + new MutableObjectIteratorAdapter<>(delegate, 0); |
| 43 | + |
| 44 | + List<Integer> result = new ArrayList<>(); |
| 45 | + while (adapter.hasNext()) { |
| 46 | + result.add(adapter.next()); |
| 47 | + } |
| 48 | + |
| 49 | + assertThat(result).containsExactly(1, 2, 3, 4, 5); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testEmptyIterator() { |
| 54 | + List<Integer> values = Collections.emptyList(); |
| 55 | + MutableObjectIterator<Integer> delegate = createTestIterator(values); |
| 56 | + |
| 57 | + MutableObjectIteratorAdapter<Integer, Integer> adapter = |
| 58 | + new MutableObjectIteratorAdapter<>(delegate, 0); |
| 59 | + |
| 60 | + assertThat(adapter.hasNext()).isFalse(); |
| 61 | + assertThatThrownBy(adapter::next).isInstanceOf(NoSuchElementException.class); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testSingleElement() { |
| 66 | + List<Integer> values = Collections.singletonList(42); |
| 67 | + MutableObjectIterator<Integer> delegate = createTestIterator(values); |
| 68 | + |
| 69 | + MutableObjectIteratorAdapter<Integer, Integer> adapter = |
| 70 | + new MutableObjectIteratorAdapter<>(delegate, 0); |
| 71 | + |
| 72 | + assertThat(adapter.hasNext()).isTrue(); |
| 73 | + assertThat(adapter.next()).isEqualTo(42); |
| 74 | + assertThat(adapter.hasNext()).isFalse(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testMultipleHasNextCalls() { |
| 79 | + List<Integer> values = Arrays.asList(1, 2); |
| 80 | + MutableObjectIterator<Integer> delegate = createTestIterator(values); |
| 81 | + |
| 82 | + MutableObjectIteratorAdapter<Integer, Integer> adapter = |
| 83 | + new MutableObjectIteratorAdapter<>(delegate, 0); |
| 84 | + |
| 85 | + // Call hasNext multiple times should not advance the iterator |
| 86 | + assertThat(adapter.hasNext()).isTrue(); |
| 87 | + assertThat(adapter.hasNext()).isTrue(); |
| 88 | + assertThat(adapter.hasNext()).isTrue(); |
| 89 | + |
| 90 | + assertThat(adapter.next()).isEqualTo(1); |
| 91 | + |
| 92 | + assertThat(adapter.hasNext()).isTrue(); |
| 93 | + assertThat(adapter.hasNext()).isTrue(); |
| 94 | + |
| 95 | + assertThat(adapter.next()).isEqualTo(2); |
| 96 | + assertThat(adapter.hasNext()).isFalse(); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testNextWithoutHasNext() { |
| 101 | + List<Integer> values = Arrays.asList(1, 2, 3); |
| 102 | + MutableObjectIterator<Integer> delegate = createTestIterator(values); |
| 103 | + |
| 104 | + MutableObjectIteratorAdapter<Integer, Integer> adapter = |
| 105 | + new MutableObjectIteratorAdapter<>(delegate, 0); |
| 106 | + |
| 107 | + // Call next() without calling hasNext() should still work |
| 108 | + assertThat(adapter.next()).isEqualTo(1); |
| 109 | + assertThat(adapter.next()).isEqualTo(2); |
| 110 | + assertThat(adapter.next()).isEqualTo(3); |
| 111 | + |
| 112 | + assertThatThrownBy(adapter::next).isInstanceOf(NoSuchElementException.class); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testIOExceptionHandling() { |
| 117 | + MutableObjectIterator<Integer> failingDelegate = |
| 118 | + new MutableObjectIterator<Integer>() { |
| 119 | + private int count = 0; |
| 120 | + |
| 121 | + @Override |
| 122 | + public Integer next(Integer reuse) throws IOException { |
| 123 | + count++; |
| 124 | + if (count == 1) { |
| 125 | + return 1; |
| 126 | + } |
| 127 | + throw new IOException("Test exception"); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Integer next() throws IOException { |
| 132 | + return next(null); |
| 133 | + } |
| 134 | + }; |
| 135 | + |
| 136 | + MutableObjectIteratorAdapter<Integer, Integer> adapter = |
| 137 | + new MutableObjectIteratorAdapter<>(failingDelegate, 0); |
| 138 | + |
| 139 | + assertThat(adapter.hasNext()).isTrue(); |
| 140 | + assertThat(adapter.next()).isEqualTo(1); |
| 141 | + |
| 142 | + assertThatThrownBy(adapter::hasNext) |
| 143 | + .isInstanceOf(RuntimeException.class) |
| 144 | + .hasMessageContaining("Failed to read next element from MutableObjectIterator") |
| 145 | + .hasCauseExactlyInstanceOf(IOException.class); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testNullElements() { |
| 150 | + List<Integer> values = Arrays.asList(1, null, 3); |
| 151 | + MutableObjectIterator<Integer> delegate = createTestIterator(values); |
| 152 | + |
| 153 | + MutableObjectIteratorAdapter<Integer, Integer> adapter = |
| 154 | + new MutableObjectIteratorAdapter<>(delegate, 0); |
| 155 | + |
| 156 | + assertThat(adapter.hasNext()).isTrue(); |
| 157 | + assertThat(adapter.next()).isEqualTo(1); |
| 158 | + |
| 159 | + // Null element should be treated as end of iteration |
| 160 | + assertThat(adapter.hasNext()).isFalse(); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testStringType() { |
| 165 | + List<String> values = Arrays.asList("hello", "world", "test"); |
| 166 | + MutableObjectIterator<String> delegate = createTestIterator(values); |
| 167 | + |
| 168 | + MutableObjectIteratorAdapter<String, String> adapter = |
| 169 | + new MutableObjectIteratorAdapter<>(delegate, ""); |
| 170 | + |
| 171 | + List<String> result = new ArrayList<>(); |
| 172 | + while (adapter.hasNext()) { |
| 173 | + result.add(adapter.next()); |
| 174 | + } |
| 175 | + |
| 176 | + assertThat(result).containsExactly("hello", "world", "test"); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testLongType() { |
| 181 | + List<Long> values = Arrays.asList(100L, 200L, 300L); |
| 182 | + MutableObjectIterator<Long> delegate = createTestIterator(values); |
| 183 | + |
| 184 | + MutableObjectIteratorAdapter<Long, Long> adapter = |
| 185 | + new MutableObjectIteratorAdapter<>(delegate, 0L); |
| 186 | + |
| 187 | + List<Long> result = new ArrayList<>(); |
| 188 | + while (adapter.hasNext()) { |
| 189 | + result.add(adapter.next()); |
| 190 | + } |
| 191 | + |
| 192 | + assertThat(result).containsExactly(100L, 200L, 300L); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Creates a test implementation of MutableObjectIterator that returns elements from the given |
| 197 | + * list. |
| 198 | + */ |
| 199 | + private <T> MutableObjectIterator<T> createTestIterator(List<T> values) { |
| 200 | + return new MutableObjectIterator<T>() { |
| 201 | + private int index = 0; |
| 202 | + |
| 203 | + @Override |
| 204 | + public T next(T reuse) { |
| 205 | + if (index >= values.size()) { |
| 206 | + return null; |
| 207 | + } |
| 208 | + return values.get(index++); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public T next() { |
| 213 | + return next(null); |
| 214 | + } |
| 215 | + }; |
| 216 | + } |
| 217 | +} |
0 commit comments