|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 John Hendrikx |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.int4.common.collection; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.LinkedList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.RandomAccess; |
| 32 | + |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 37 | + |
| 38 | +class ImmutableTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + void shouldThrowExceptionWhenListIsNull() { |
| 42 | + assertThatThrownBy(() -> Immutable.of(null)) |
| 43 | + .isExactlyInstanceOf(NullPointerException.class) |
| 44 | + .hasMessage("list"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void shouldCreateImmutableCopyOfMutableList() { |
| 49 | + List<String> mutableList = new ArrayList<>(Arrays.asList("a", "b", "c")); |
| 50 | + List<String> immutableList = Immutable.of(mutableList); |
| 51 | + |
| 52 | + assertThat(immutableList).containsExactly("a", "b", "c"); |
| 53 | + |
| 54 | + mutableList.add("d"); |
| 55 | + |
| 56 | + assertThat(immutableList).containsExactly("a", "b", "c"); |
| 57 | + assertThat(immutableList).hasSize(3); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void shouldNotAllowModifications() { |
| 62 | + List<String> immutableList = Immutable.of(Arrays.asList("a", "b")); |
| 63 | + |
| 64 | + assertThatThrownBy(() -> immutableList.add("c")).isExactlyInstanceOf(UnsupportedOperationException.class); |
| 65 | + assertThatThrownBy(() -> immutableList.remove(0)).isExactlyInstanceOf(UnsupportedOperationException.class); |
| 66 | + assertThatThrownBy(() -> immutableList.set(0, "z")).isExactlyInstanceOf(UnsupportedOperationException.class); |
| 67 | + assertThatThrownBy(() -> immutableList.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void shouldReturnSameInstanceIfAlreadyImmutable() { |
| 72 | + List<String> original = List.of("a", "b"); |
| 73 | + List<String> immutable = Immutable.of(original); |
| 74 | + |
| 75 | + assertThat(immutable).isSameAs(original); |
| 76 | + |
| 77 | + List<String> doubleImmutable = Immutable.of(immutable); |
| 78 | + |
| 79 | + assertThat(doubleImmutable).isSameAs(immutable); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void shouldPreserveRandomAccess() { |
| 84 | + List<String> arrayList = new ArrayList<>(Arrays.asList("a", "b")); |
| 85 | + List<String> immutableArrayList = Immutable.of(arrayList); |
| 86 | + |
| 87 | + assertThat(immutableArrayList).isInstanceOf(RandomAccess.class); |
| 88 | + assertThat(immutableArrayList.getClass().getSimpleName()).contains("RandomAccess"); |
| 89 | + |
| 90 | + List<String> linkedList = new LinkedList<>(Arrays.asList("a", "b")); |
| 91 | + List<String> immutableLinkedList = Immutable.of(linkedList); |
| 92 | + |
| 93 | + assertThat(immutableLinkedList).isNotInstanceOf(RandomAccess.class); |
| 94 | + assertThat(immutableLinkedList.getClass().getSimpleName()).doesNotContain("RandomAccess"); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void shouldPreserveNulls() { |
| 99 | + List<String> listWithNulls = Arrays.asList("a", null, "c"); |
| 100 | + List<String> immutable = Immutable.of(listWithNulls); |
| 101 | + |
| 102 | + assertThat(immutable).containsExactly("a", null, "c"); |
| 103 | + assertThat(immutable.get(1)).isNull(); |
| 104 | + assertThat(immutable.contains(null)).isTrue(); |
| 105 | + assertThat(immutable.indexOf(null)).isEqualTo(1); |
| 106 | + assertThat(immutable.lastIndexOf(null)).isEqualTo(1); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void shouldSupportBasicListOperations() { |
| 111 | + List<String> list = Immutable.of(Arrays.asList("a", "b", "a")); |
| 112 | + |
| 113 | + assertThat(list.get(0)).isEqualTo("a"); |
| 114 | + assertThat(list.size()).isEqualTo(3); |
| 115 | + assertThat(list.contains("b")).isTrue(); |
| 116 | + assertThat(list.contains("c")).isFalse(); |
| 117 | + assertThat(list.indexOf("a")).isEqualTo(0); |
| 118 | + assertThat(list.lastIndexOf("a")).isEqualTo(2); |
| 119 | + assertThat(list.iterator()).toIterable().containsExactly("a", "b", "a"); |
| 120 | + assertThat(list.listIterator(1)).toIterable().containsExactly("b", "a"); |
| 121 | + } |
| 122 | +} |
0 commit comments