|
12 | 12 | import software.amazon.jsii.tests.calculator.AsyncVirtualMethods; |
13 | 13 | import software.amazon.jsii.tests.calculator.Calculator; |
14 | 14 | import software.amazon.jsii.tests.calculator.CalculatorProps; |
| 15 | +import software.amazon.jsii.tests.calculator.ClassWithCollections; |
15 | 16 | import software.amazon.jsii.tests.calculator.ClassWithJavaReservedWords; |
16 | 17 | import software.amazon.jsii.tests.calculator.ClassWithPrivateConstructorAndAutomaticProperties; |
17 | 18 | import software.amazon.jsii.tests.calculator.Constructors; |
|
70 | 71 | import java.io.IOException; |
71 | 72 | import java.time.Instant; |
72 | 73 | import java.util.Arrays; |
| 74 | +import java.util.Collections; |
73 | 75 | import java.util.HashMap; |
74 | 76 | import java.util.List; |
75 | 77 | import java.util.Map; |
76 | 78 |
|
| 79 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 80 | +import static org.hamcrest.Matchers.contains; |
| 81 | +import static org.hamcrest.Matchers.hasEntry; |
| 82 | +import static org.hamcrest.Matchers.is; |
77 | 83 | import static org.junit.Assert.assertEquals; |
78 | 84 | import static org.junit.Assert.assertFalse; |
79 | 85 | import static org.junit.Assert.assertNotEquals; |
@@ -1268,6 +1274,92 @@ public void canLoadEnumValues() { |
1268 | 1274 | assertNotNull(EnumDispenser.randomIntegerLikeEnum()); |
1269 | 1275 | } |
1270 | 1276 |
|
| 1277 | + @Test(expected = UnsupportedOperationException.class) |
| 1278 | + public void listInClassCannotBeModified() { |
| 1279 | + List<String> modifiableList = Arrays.asList("one", "two"); |
| 1280 | + |
| 1281 | + ClassWithCollections classWithCollections = new ClassWithCollections(Collections.emptyMap(), modifiableList); |
| 1282 | + |
| 1283 | + classWithCollections.getArray().add("three"); |
| 1284 | + } |
| 1285 | + |
| 1286 | + @Test |
| 1287 | + public void listInClassCanBeReadCorrectly() { |
| 1288 | + List<String> modifiableList = Arrays.asList("one", "two"); |
| 1289 | + |
| 1290 | + ClassWithCollections classWithCollections = new ClassWithCollections(Collections.emptyMap(), modifiableList); |
| 1291 | + |
| 1292 | + assertThat(classWithCollections.getArray(), contains("one", "two")); |
| 1293 | + } |
| 1294 | + |
| 1295 | + @Test(expected = UnsupportedOperationException.class) |
| 1296 | + public void mapInClassCannotBeModified() { |
| 1297 | + Map<String, String> modifiableMap = new HashMap<>(); |
| 1298 | + modifiableMap.put("key", "value"); |
| 1299 | + |
| 1300 | + ClassWithCollections classWithCollections = new ClassWithCollections(modifiableMap, Collections.emptyList()); |
| 1301 | + |
| 1302 | + classWithCollections.getMap().put("keyTwo", "valueTwo"); |
| 1303 | + } |
| 1304 | + |
| 1305 | + @Test |
| 1306 | + public void mapInClassCanBeReadCorrectly() { |
| 1307 | + Map<String, String> modifiableMap = new HashMap<>(); |
| 1308 | + modifiableMap.put("key", "value"); |
| 1309 | + |
| 1310 | + ClassWithCollections classWithCollections = new ClassWithCollections(modifiableMap, Collections.emptyList()); |
| 1311 | + |
| 1312 | + Map<String, String> result = classWithCollections.getMap(); |
| 1313 | + assertThat(result, hasEntry("key", "value")); |
| 1314 | + assertThat(result.size(), is(1)); |
| 1315 | + } |
| 1316 | + |
| 1317 | + @Test(expected = UnsupportedOperationException.class) |
| 1318 | + public void staticListInClassCannotBeModified() { |
| 1319 | + ClassWithCollections.getStaticArray().add("three"); |
| 1320 | + } |
| 1321 | + |
| 1322 | + @Test |
| 1323 | + public void staticListInClassCanBeReadCorrectly() { |
| 1324 | + assertThat(ClassWithCollections.getStaticArray(), contains("one", "two")); |
| 1325 | + } |
| 1326 | + |
| 1327 | + @Test(expected = UnsupportedOperationException.class) |
| 1328 | + public void staticMapInClassCannotBeModified() { |
| 1329 | + ClassWithCollections.getStaticMap().put("keyTwo", "valueTwo"); |
| 1330 | + } |
| 1331 | + |
| 1332 | + @Test |
| 1333 | + public void staticMapInClassCanBeReadCorrectly() { |
| 1334 | + Map<String, String> result = ClassWithCollections.getStaticMap(); |
| 1335 | + assertThat(result, hasEntry("key1", "value1")); |
| 1336 | + assertThat(result, hasEntry("key2", "value2")); |
| 1337 | + assertThat(result.size(), is(2)); |
| 1338 | + } |
| 1339 | + |
| 1340 | + @Test(expected = UnsupportedOperationException.class) |
| 1341 | + public void arrayReturnedByMethodCannotBeModified() { |
| 1342 | + ClassWithCollections.createAList().add("three"); |
| 1343 | + } |
| 1344 | + |
| 1345 | + @Test |
| 1346 | + public void arrayReturnedByMethodCanBeRead() { |
| 1347 | + assertThat(ClassWithCollections.createAList(), contains("one", "two")); |
| 1348 | + } |
| 1349 | + |
| 1350 | + @Test(expected = UnsupportedOperationException.class) |
| 1351 | + public void mapReturnedByMethodCannotBeModified() { |
| 1352 | + ClassWithCollections.createAMap().put("keyThree", "valueThree"); |
| 1353 | + } |
| 1354 | + |
| 1355 | + @Test |
| 1356 | + public void mapReturnedByMethodCanBeRead() { |
| 1357 | + Map<String, String> result = ClassWithCollections.createAMap(); |
| 1358 | + assertThat(result, hasEntry("key1", "value1")); |
| 1359 | + assertThat(result, hasEntry("key2", "value2")); |
| 1360 | + assertThat(result.size(), is(2)); |
| 1361 | + } |
| 1362 | + |
1271 | 1363 | static class PartiallyInitializedThisConsumerImpl extends PartiallyInitializedThisConsumer { |
1272 | 1364 | @Override |
1273 | 1365 | public String consumePartiallyInitializedThis(final ConstructorPassesThisOut obj, |
|
0 commit comments