|
| 1 | +package software.amazon.jsii.testing; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import software.amazon.jsii.JsiiException; |
| 5 | +import software.amazon.jsii.JsiiObject; |
| 6 | +import software.amazon.jsii.tests.calculator.*; |
| 7 | +import software.amazon.jsii.tests.calculator.anonymous.*; |
| 8 | +import software.amazon.jsii.tests.calculator.anonymous.UseOptions; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.*; |
| 18 | + |
| 19 | +public class TypeCheckingTest { |
| 20 | + private static class StructAImplementer implements StructA, StructB { |
| 21 | + public String requiredString; |
| 22 | + |
| 23 | + StructAImplementer(String param) { |
| 24 | + requiredString = param; |
| 25 | + } |
| 26 | + |
| 27 | + public void setRequiredString(String param) { |
| 28 | + requiredString = param; |
| 29 | + } |
| 30 | + |
| 31 | + public String getRequiredString() { |
| 32 | + return requiredString; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void constructor() { |
| 38 | + HashMap<String, Object> map = new HashMap<String, Object>(); |
| 39 | + map.put("good", new StructAImplementer("present")); |
| 40 | + map.put("bad", "Not a StructA or StructB"); |
| 41 | + ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); |
| 42 | + list.add(map); |
| 43 | + Exception e = assertThrows(IllegalArgumentException.class, () -> |
| 44 | + { |
| 45 | + new ClassWithCollectionOfUnions(list); |
| 46 | + }); |
| 47 | + |
| 48 | + assertEquals("Expected unionProperty.get(0).get(\"bad\") to be one of: software.amazon.jsii.tests.calculator.StructA, software.amazon.jsii.tests.calculator.StructB; received class java.lang.String", e.getMessage()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void anonymousObjectIsValid() |
| 53 | + { |
| 54 | + Object anonymousObject = UseOptions.provide("A"); |
| 55 | + assertEquals(JsiiObject.class, anonymousObject.getClass()); |
| 56 | + assertEquals("A", UseOptions.consume(anonymousObject)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void nestedUnion() { |
| 61 | + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> |
| 62 | + { |
| 63 | + ArrayList<Object> list = new ArrayList<Object>(); |
| 64 | + list.add(1337.42); |
| 65 | + new ClassWithNestedUnion(list); |
| 66 | + }); |
| 67 | + assertEquals("Expected unionProperty.get(0) to be one of: java.util.Map<java.lang.String, java.lang.Object>, java.util.List<java.lang.Object>; received class java.lang.Double", e.getMessage()); |
| 68 | + |
| 69 | + e = assertThrows(IllegalArgumentException.class, () -> |
| 70 | + { |
| 71 | + ArrayList<Object> list = new ArrayList<Object>(); |
| 72 | + ArrayList<Object> nestedList = new ArrayList<Object>(); |
| 73 | + nestedList.add(new StructAImplementer("required")); |
| 74 | + nestedList.add(1337.42); |
| 75 | + list.add(nestedList); |
| 76 | + new ClassWithNestedUnion(list); |
| 77 | + }); |
| 78 | + assertEquals("Expected unionProperty.get(0).get(1) to be one of: software.amazon.jsii.tests.calculator.StructA, software.amazon.jsii.tests.calculator.StructB; received class java.lang.Double", e.getMessage()); |
| 79 | + |
| 80 | + e = assertThrows(IllegalArgumentException.class, () -> |
| 81 | + { |
| 82 | + HashMap<String, Object> map = new HashMap<String, Object>(); |
| 83 | + ArrayList<Object> list = new ArrayList<Object>(); |
| 84 | + map.put("good", new StructAImplementer("present")); |
| 85 | + map.put("bad", "Not a StructA or StructB"); |
| 86 | + list.add(map); |
| 87 | + new ClassWithNestedUnion(list); |
| 88 | + }); |
| 89 | + assertEquals("Expected unionProperty.get(0).get(\"bad\") to be one of: software.amazon.jsii.tests.calculator.StructA, software.amazon.jsii.tests.calculator.StructB; received class java.lang.String", e.getMessage()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void keysAreTypeChecked() { |
| 94 | + HashMap<Object, Object> map = new HashMap<Object, Object>(); |
| 95 | + ArrayList<Object> list = new ArrayList<Object>(); |
| 96 | + map.put("good", new StructAImplementer("present")); |
| 97 | + map.put(1337.42, new StructAImplementer("present")); |
| 98 | + list.add(map); |
| 99 | + |
| 100 | + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { |
| 101 | + new ClassWithNestedUnion(list); |
| 102 | + }); |
| 103 | + assertEquals("Expected unionProperty.get(0).keySet() to contain class String; received class java.lang.Double", e.getMessage()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void variadic() { |
| 108 | + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> |
| 109 | + { |
| 110 | + new VariadicTypeUnion(new StructAImplementer("present"), 1337.42); |
| 111 | + }); |
| 112 | + assertEquals("Expected union[1] to be one of: software.amazon.jsii.tests.calculator.StructA, software.amazon.jsii.tests.calculator.StructB; received class java.lang.Double", e.getMessage()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void setter() { |
| 117 | + HashMap<String, Object> map = new HashMap<String, Object>(); |
| 118 | + map.put("good", new StructAImplementer("present")); |
| 119 | + map.put("bad", "Not a StructA or StructB"); |
| 120 | + ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); |
| 121 | + ClassWithCollectionOfUnions subject = new ClassWithCollectionOfUnions(list); |
| 122 | + list.add(map); |
| 123 | + |
| 124 | + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { |
| 125 | + subject.setUnionProperty(list); |
| 126 | + }); |
| 127 | + assertEquals("Expected value.get(0).get(\"bad\") to be one of: software.amazon.jsii.tests.calculator.StructA, software.amazon.jsii.tests.calculator.StructB; received class java.lang.String", e.getMessage()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void staticMethod() |
| 132 | + { |
| 133 | + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { |
| 134 | + StructUnionConsumer.isStructA("Not a StructA"); |
| 135 | + }); |
| 136 | + assertEquals("Expected struct to be one of: software.amazon.jsii.tests.calculator.StructA, software.amazon.jsii.tests.calculator.StructB; received class java.lang.String", e.getMessage()); |
| 137 | + } |
| 138 | +} |
0 commit comments