-
Notifications
You must be signed in to change notification settings - Fork 587
/
UnmodifiableMutableMapTest.java
178 lines (152 loc) · 6.26 KB
/
UnmodifiableMutableMapTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.test.map.mutable;
import java.util.Random;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.MutableMapIterable;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.map.mutable.UnmodifiableMutableMap;
import org.eclipse.collections.impl.test.junit.Java8Runner;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.eclipse.collections.test.FixedSizeIterableTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.eclipse.collections.test.IterableTestCase.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
@RunWith(Java8Runner.class)
public class UnmodifiableMutableMapTest implements MutableMapTestCase, FixedSizeIterableTestCase
{
private static final long CURRENT_TIME_MILLIS = System.currentTimeMillis();
@Override
public <T> MutableMap<Object, T> newWith(T... elements)
{
Random random = new Random(CURRENT_TIME_MILLIS);
MutableMap<Object, T> result = new UnifiedMap<>();
for (T each : elements)
{
assertNull(result.put(random.nextDouble(), each));
}
return UnmodifiableMutableMap.of(result);
}
@Override
public <K, V> MutableMap<K, V> newWithKeysValues(Object... elements)
{
if (elements.length % 2 != 0)
{
fail(String.valueOf(elements.length));
}
MutableMap<K, V> result = new UnifiedMap<>();
for (int i = 0; i < elements.length; i += 2)
{
assertNull(result.put((K) elements[i], (V) elements[i + 1]));
}
return UnmodifiableMutableMap.of(result);
}
@Override
public void MutableMapIterable_removeKey()
{
MutableMapIterable<Object, Object> map = this.newWith();
assertThrows(UnsupportedOperationException.class, () -> map.removeKey(2));
}
@Override
public void MutableMapIterable_removeIf()
{
MutableMapIterable<Object, String> map = this.newWith("Three", "Two", "One");
assertThrows(UnsupportedOperationException.class, () -> map.removeIf(Predicates2.alwaysFalse()));
}
@Override
public void Map_remove()
{
MutableMapIterable<Object, Object> map = this.newWith();
assertThrows(UnsupportedOperationException.class, () -> map.remove(2));
}
@Override
public void Map_entrySet_remove()
{
MutableMapIterable<Object, Object> map = this.newWithKeysValues();
assertThrows(UnsupportedOperationException.class, () -> map.entrySet().remove(ImmutableEntry.of(null, null)));
}
@Override
public void Map_clear()
{
MutableMapIterable<Object, String> map = this.newWith("Three", "Two", "One");
assertThrows(UnsupportedOperationException.class, map::clear);
}
@Override
public void Iterable_remove()
{
FixedSizeIterableTestCase.super.Iterable_remove();
}
@Override
public void Map_putAll()
{
MutableMapIterable<Integer, String> map = this.newWithKeysValues(
3, "Three",
2, "2");
MutableMapIterable<Integer, String> toAdd = this.newWithKeysValues(
2, "Two",
1, "One");
assertThrows(UnsupportedOperationException.class, () -> map.putAll(toAdd));
}
@Override
@Test
public void MutableMapIterable_getIfAbsentPut()
{
MutableMapIterable<String, Integer> map = this.newWithKeysValues("3", 3, "2", 2, "1", 1);
assertEquals(3, map.getIfAbsentPut("3", () -> {
throw new AssertionError();
}));
assertEquals(this.newWithKeysValues("3", 3, "2", 2, "1", 1), map);
assertThrows(UnsupportedOperationException.class, () -> map.getIfAbsentPut("4", () -> {
throw new AssertionError();
}));
assertEquals(3, map.getIfAbsentPut("3", 4));
assertEquals(this.newWithKeysValues("3", 3, "2", 2, "1", 1), map);
assertThrows(UnsupportedOperationException.class, () -> map.getIfAbsentPut("4", 4));
assertEquals(3, map.getIfAbsentPutWithKey("3", key -> {
throw new AssertionError();
}));
assertEquals(this.newWithKeysValues("3", 3, "2", 2, "1", 1), map);
assertThrows(UnsupportedOperationException.class, () -> map.getIfAbsentPutWithKey("4", key -> {
throw new AssertionError();
}));
assertEquals(3, map.getIfAbsentPutWith("3", x -> x + 10, 4));
assertEquals(this.newWithKeysValues("3", 3, "2", 2, "1", 1), map);
assertThrows(UnsupportedOperationException.class, () -> map.getIfAbsentPutWith("4", x -> x + 10, 4));
}
@Override
public void MutableMapIterable_updateValue()
{
MutableMapIterable<String, Integer> map = this.newWithKeysValues("One", 1);
assertThrows(
UnsupportedOperationException.class,
() -> map.updateValue("One", () -> 0, ignored -> {
throw new AssertionError();
}));
assertThrows(
UnsupportedOperationException.class,
() -> map.updateValue("Two", () -> 0, ignored -> {
throw new AssertionError();
}));
assertThrows(
UnsupportedOperationException.class,
() -> map.updateValueWith("One", () -> 0, (ignored, parameter) -> {
throw new AssertionError();
}, "test"));
assertThrows(
UnsupportedOperationException.class,
() -> map.updateValueWith("Two", () -> 0, (ignored, parameter) -> {
throw new AssertionError();
}, "test"));
}
}