Skip to content

Commit

Permalink
feat(java): use immutable java implementations of JSII primitive coll…
Browse files Browse the repository at this point in the history
…ection types array and map (#765)
  • Loading branch information
spfink authored and mergify[bot] committed Sep 5, 2019
1 parent f3b98b5 commit 5e713e3
Show file tree
Hide file tree
Showing 157 changed files with 1,185 additions and 288 deletions.
21 changes: 21 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1860,3 +1860,24 @@ export class InterfacesMaker {

private constructor() { }
}

export class ClassWithCollections {
public map: { [key: string]: string };
public array: string[];

public static staticMap:{ [key: string]: string } = {'key1': 'value1', 'key2': 'value2'};
public static staticArray: string[] = ["one", "two"];

constructor(map: { [key: string]: string }, array: string[]) {
this.map = map;
this.array = array;
}

static createAList(): string[] {
return ["one", "two"];
}

static createAMap(): { [key: string]: string } {
return {'key1': 'value1', 'key2': 'value2'};
}
}
164 changes: 163 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,168 @@
}
]
},
"jsii-calc.ClassWithCollections": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.ClassWithCollections",
"initializer": {
"docs": {
"stability": "experimental"
},
"parameters": [
{
"name": "map",
"type": {
"collection": {
"elementtype": {
"primitive": "string"
},
"kind": "map"
}
}
},
{
"name": "array",
"type": {
"collection": {
"elementtype": {
"primitive": "string"
},
"kind": "array"
}
}
}
]
},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1864
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1876
},
"name": "createAList",
"returns": {
"type": {
"collection": {
"elementtype": {
"primitive": "string"
},
"kind": "array"
}
}
},
"static": true
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1880
},
"name": "createAMap",
"returns": {
"type": {
"collection": {
"elementtype": {
"primitive": "string"
},
"kind": "map"
}
}
},
"static": true
}
],
"name": "ClassWithCollections",
"properties": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1869
},
"name": "staticArray",
"static": true,
"type": {
"collection": {
"elementtype": {
"primitive": "string"
},
"kind": "array"
}
}
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1868
},
"name": "staticMap",
"static": true,
"type": {
"collection": {
"elementtype": {
"primitive": "string"
},
"kind": "map"
}
}
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1866
},
"name": "array",
"type": {
"collection": {
"elementtype": {
"primitive": "string"
},
"kind": "array"
}
}
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1865
},
"name": "map",
"type": {
"collection": {
"elementtype": {
"primitive": "string"
},
"kind": "map"
}
}
}
]
},
"jsii-calc.ClassWithDocs": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -9453,5 +9615,5 @@
}
},
"version": "0.16.0",
"fingerprint": "D99pLaaW2xH2uuyUa8hgakiEso/wXbcMNkP9RmOTwgo="
"fingerprint": "SyvtIxuwfgMPg4aYpb3VR2M4Ra4b+iLnrBwHjsLb12g="
}
8 changes: 8 additions & 0 deletions packages/jsii-java-runtime-test/pom.xml.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ process.stdout.write(`<?xml version="1.0" encoding="UTF-8"?>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import software.amazon.jsii.tests.calculator.AsyncVirtualMethods;
import software.amazon.jsii.tests.calculator.Calculator;
import software.amazon.jsii.tests.calculator.CalculatorProps;
import software.amazon.jsii.tests.calculator.ClassWithCollections;
import software.amazon.jsii.tests.calculator.ClassWithJavaReservedWords;
import software.amazon.jsii.tests.calculator.ClassWithPrivateConstructorAndAutomaticProperties;
import software.amazon.jsii.tests.calculator.Constructors;
Expand Down Expand Up @@ -70,10 +71,15 @@
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
Expand Down Expand Up @@ -1268,6 +1274,92 @@ public void canLoadEnumValues() {
assertNotNull(EnumDispenser.randomIntegerLikeEnum());
}

@Test(expected = UnsupportedOperationException.class)
public void listInClassCannotBeModified() {
List<String> modifiableList = Arrays.asList("one", "two");

ClassWithCollections classWithCollections = new ClassWithCollections(Collections.emptyMap(), modifiableList);

classWithCollections.getArray().add("three");
}

@Test
public void listInClassCanBeReadCorrectly() {
List<String> modifiableList = Arrays.asList("one", "two");

ClassWithCollections classWithCollections = new ClassWithCollections(Collections.emptyMap(), modifiableList);

assertThat(classWithCollections.getArray(), contains("one", "two"));
}

@Test(expected = UnsupportedOperationException.class)
public void mapInClassCannotBeModified() {
Map<String, String> modifiableMap = new HashMap<>();
modifiableMap.put("key", "value");

ClassWithCollections classWithCollections = new ClassWithCollections(modifiableMap, Collections.emptyList());

classWithCollections.getMap().put("keyTwo", "valueTwo");
}

@Test
public void mapInClassCanBeReadCorrectly() {
Map<String, String> modifiableMap = new HashMap<>();
modifiableMap.put("key", "value");

ClassWithCollections classWithCollections = new ClassWithCollections(modifiableMap, Collections.emptyList());

Map<String, String> result = classWithCollections.getMap();
assertThat(result, hasEntry("key", "value"));
assertThat(result.size(), is(1));
}

@Test(expected = UnsupportedOperationException.class)
public void staticListInClassCannotBeModified() {
ClassWithCollections.getStaticArray().add("three");
}

@Test
public void staticListInClassCanBeReadCorrectly() {
assertThat(ClassWithCollections.getStaticArray(), contains("one", "two"));
}

@Test(expected = UnsupportedOperationException.class)
public void staticMapInClassCannotBeModified() {
ClassWithCollections.getStaticMap().put("keyTwo", "valueTwo");
}

@Test
public void staticMapInClassCanBeReadCorrectly() {
Map<String, String> result = ClassWithCollections.getStaticMap();
assertThat(result, hasEntry("key1", "value1"));
assertThat(result, hasEntry("key2", "value2"));
assertThat(result.size(), is(2));
}

@Test(expected = UnsupportedOperationException.class)
public void arrayReturnedByMethodCannotBeModified() {
ClassWithCollections.createAList().add("three");
}

@Test
public void arrayReturnedByMethodCanBeRead() {
assertThat(ClassWithCollections.createAList(), contains("one", "two"));
}

@Test(expected = UnsupportedOperationException.class)
public void mapReturnedByMethodCannotBeModified() {
ClassWithCollections.createAMap().put("keyThree", "valueThree");
}

@Test
public void mapReturnedByMethodCanBeRead() {
Map<String, String> result = ClassWithCollections.createAMap();
assertThat(result, hasEntry("key1", "value1"));
assertThat(result, hasEntry("key2", "value2"));
assertThat(result.size(), is(2));
}

static class PartiallyInitializedThisConsumerImpl extends PartiallyInitializedThisConsumer {
@Override
public String consumePartiallyInitializedThis(final ConstructorPassesThisOut obj,
Expand Down
9 changes: 9 additions & 0 deletions packages/jsii-java-runtime/pom.xml.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ process.stdout.write(`<?xml version="1.0" encoding="UTF-8"?>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Versions of the dependencies -->
<findbugs.version>[3.0.2,3.1.0)</findbugs.version>
<hamcrest.version>1.3</hamcrest.version>
<jackson-core.version>[2.9.9,2.10.0)</jackson-core.version>
<jackson-databind.version>[2.9.9.2,2.10.0)</jackson-databind.version>
<javax.annotations.version>[1.3.2,1.4.0)</javax.annotations.version>
Expand Down Expand Up @@ -103,6 +104,14 @@ process.stdout.write(`<?xml version="1.0" encoding="UTF-8"?>
<version>\${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>\${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mjockito/mockito-core -->
<dependency>
Expand Down
Loading

0 comments on commit 5e713e3

Please sign in to comment.