|
| 1 | +/* |
| 2 | + * Copyright MapStruct Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package org.mapstruct.tools.gem; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Nested; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import javax.lang.model.element.AnnotationValue; |
| 12 | +import javax.lang.model.element.AnnotationValueVisitor; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +class GemValueTest { |
| 21 | + |
| 22 | + static class SimpleAnnotationValue implements AnnotationValue { |
| 23 | + |
| 24 | + private final int value; |
| 25 | + |
| 26 | + SimpleAnnotationValue(int value) { |
| 27 | + this.value = value; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public Object getValue() { |
| 32 | + return value; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
| 37 | + return v.visitInt( value, p ); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Nested |
| 42 | + class CreateSimpleValueTest { |
| 43 | + |
| 44 | + @Test |
| 45 | + void createSimpleValue() { |
| 46 | + SimpleAnnotationValue annotationValue = new SimpleAnnotationValue(1); |
| 47 | + GemValue<Integer> gemValue = GemValue.create( annotationValue, new SimpleAnnotationValue(2), |
| 48 | + Integer.class ); |
| 49 | + assertThat( gemValue ).isNotNull(); |
| 50 | + assertThat( gemValue.isValid() ).isTrue(); |
| 51 | + assertThat( gemValue.hasValue() ).isTrue(); |
| 52 | + assertThat( gemValue.getValue() ).isEqualTo( 1 ); |
| 53 | + assertThat( gemValue.getDefaultValue() ).isEqualTo( 2 ); |
| 54 | + assertThat( gemValue.get() ).as( "get should return value" ).isEqualTo( 1 ); |
| 55 | + assertThat( gemValue.getAnnotationValue() ).isEqualTo( annotationValue ); |
| 56 | + assertThat( gemValue.getValueOrElseGet( () -> 3 ) ) |
| 57 | + .as( "getValueOrElseGet should return value" ).isEqualTo( 1 ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void createSimpleValueWithoutAnnotationValue() { |
| 62 | + GemValue<Integer> gemValue = GemValue.create( null, new SimpleAnnotationValue(2), |
| 63 | + Integer.class ); |
| 64 | + assertThat( gemValue ).isNotNull(); |
| 65 | + assertThat( gemValue.isValid() ).isTrue(); |
| 66 | + assertThat( gemValue.hasValue() ).isFalse(); |
| 67 | + assertThat( gemValue.getValue() ).isNull(); |
| 68 | + assertThat( gemValue.getDefaultValue() ).isEqualTo( 2 ); |
| 69 | + assertThat( gemValue.get() ).as( "get should return defaultValue" ).isEqualTo( 2 ); |
| 70 | + assertThat( gemValue.getAnnotationValue() ).isNull(); |
| 71 | + assertThat( gemValue.getValueOrElseGet( () -> 3 ) ) |
| 72 | + .as( "getValueOrElseGet should return other" ).isEqualTo( 3 ); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void createSimpleValueWithoutAnnotationDefaultValue() { |
| 77 | + SimpleAnnotationValue annotationValue = new SimpleAnnotationValue(1); |
| 78 | + GemValue<Integer> gemValue = GemValue.create( annotationValue, null, Integer.class ); |
| 79 | + assertThat( gemValue ).isNotNull(); |
| 80 | + assertThat( gemValue.isValid() ).isTrue(); |
| 81 | + assertThat( gemValue.hasValue() ).isTrue(); |
| 82 | + assertThat( gemValue.getValue() ).isEqualTo( 1 ); |
| 83 | + assertThat( gemValue.getDefaultValue() ).isNull(); |
| 84 | + assertThat( gemValue.get() ).as( "get should return value" ).isEqualTo( 1 ); |
| 85 | + assertThat( gemValue.getAnnotationValue() ).isEqualTo( annotationValue ); |
| 86 | + assertThat( gemValue.getValueOrElseGet( () -> 3 ) ) |
| 87 | + .as( "getValueOrElseGet should return value" ).isEqualTo( 1 ); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void createSimpleValueInvalid() { |
| 92 | + GemValue<Integer> gemValue = GemValue.create( null, null, Integer.class ); |
| 93 | + assertThat( gemValue ).isNotNull(); |
| 94 | + assertThat( gemValue.isValid() ).isFalse(); |
| 95 | + assertThat( gemValue.hasValue() ).isFalse(); |
| 96 | + assertThat( gemValue.getValue() ).isNull(); |
| 97 | + assertThat( gemValue.getDefaultValue() ).isNull(); |
| 98 | + assertThat( gemValue.get() ).as( "get should return null" ).isNull(); |
| 99 | + assertThat( gemValue.getAnnotationValue() ).isNull(); |
| 100 | + assertThat( gemValue.getValueOrElseGet( () -> 3 ) ) |
| 101 | + .as( "getValueOrElseGet should return other" ).isEqualTo( 3 ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + static class SimpleArrayAnnotationValue implements AnnotationValue { |
| 106 | + |
| 107 | + private final List<SimpleAnnotationValue> value; |
| 108 | + |
| 109 | + SimpleArrayAnnotationValue(int... intValues) { |
| 110 | + this.value = new ArrayList<>( intValues.length ); |
| 111 | + for ( int v : intValues ) { |
| 112 | + value.add( new SimpleAnnotationValue( v ) ); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Object getValue() { |
| 118 | + return value; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) { |
| 123 | + return v.visitArray( value, p ); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Nested |
| 128 | + class CreateArrayTest { |
| 129 | + |
| 130 | + @Test |
| 131 | + void createArrayValue() { |
| 132 | + SimpleArrayAnnotationValue annotationValue = new SimpleArrayAnnotationValue(1); |
| 133 | + GemValue<List<Integer>> gemValue = GemValue.createArray( annotationValue, |
| 134 | + new SimpleArrayAnnotationValue(2), Integer.class ); |
| 135 | + assertThat( gemValue ).isNotNull(); |
| 136 | + assertThat( gemValue.isValid() ).isTrue(); |
| 137 | + assertThat( gemValue.hasValue() ).isTrue(); |
| 138 | + assertThat( gemValue.getValue() ).isEqualTo( Collections.singletonList( 1 ) ); |
| 139 | + assertThat( gemValue.getDefaultValue() ).isEqualTo( Collections.singletonList( 2 ) ); |
| 140 | + assertThat( gemValue.get() ).as( "get should return value" ).isEqualTo( |
| 141 | + Collections.singletonList( 1 ) ); |
| 142 | + assertThat( gemValue.getAnnotationValue() ).isEqualTo( annotationValue ); |
| 143 | + assertThat( gemValue.getValueOrElseGet( Collections::emptyList ) ) |
| 144 | + .as( "getValueOrElseGet should return value" ) |
| 145 | + .isEqualTo( Collections.singletonList( 1 ) ); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void createArrayValueWithoutAnnotationValue() { |
| 150 | + GemValue<List<Integer>> gemValue = GemValue.createArray( null, |
| 151 | + new SimpleArrayAnnotationValue(2), Integer.class ); |
| 152 | + assertThat( gemValue ).isNotNull(); |
| 153 | + assertThat( gemValue.isValid() ).isTrue(); |
| 154 | + assertThat( gemValue.hasValue() ).isFalse(); |
| 155 | + assertThat( gemValue.getValue() ).isNull(); |
| 156 | + assertThat( gemValue.getDefaultValue() ).isEqualTo( Collections.singletonList( 2 ) ); |
| 157 | + assertThat( gemValue.get() ).as( "get should return defaultValue" ) |
| 158 | + .isEqualTo( Collections.singletonList( 2 ) ); |
| 159 | + assertThat( gemValue.getAnnotationValue() ).isNull(); |
| 160 | + assertThat( gemValue.getValueOrElseGet( Collections::emptyList ) ) |
| 161 | + .as( "getValueOrElseGet should return other" ).isEqualTo( Collections.emptyList() ); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void createArrayValueWithoutAnnotationDefaultValue() { |
| 166 | + SimpleArrayAnnotationValue annotationValue = new SimpleArrayAnnotationValue(1); |
| 167 | + GemValue<List<Integer>> gemValue = GemValue.createArray( annotationValue, null, Integer.class ); |
| 168 | + assertThat( gemValue ).isNotNull(); |
| 169 | + assertThat( gemValue.isValid() ).isTrue(); |
| 170 | + assertThat( gemValue.hasValue() ).isTrue(); |
| 171 | + assertThat( gemValue.getValue() ).isEqualTo( Collections.singletonList( 1 ) ); |
| 172 | + assertThat( gemValue.getDefaultValue() ).isNull(); |
| 173 | + assertThat( gemValue.get() ).as( "get should return value" ) |
| 174 | + .isEqualTo( Collections.singletonList( 1 ) ); |
| 175 | + assertThat( gemValue.getAnnotationValue() ).isEqualTo( annotationValue ); |
| 176 | + assertThat( gemValue.getValueOrElseGet( Collections::emptyList ) ) |
| 177 | + .as( "getValueOrElseGet should return value" ) |
| 178 | + .isEqualTo( Collections.singletonList( 1 ) ); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void createArrayValueInvalid() { |
| 183 | + GemValue<List<Integer>> gemValue = GemValue.createArray( null, null, Integer.class ); |
| 184 | + assertThat( gemValue ).isNotNull(); |
| 185 | + assertThat( gemValue.isValid() ).isFalse(); |
| 186 | + assertThat( gemValue.hasValue() ).isFalse(); |
| 187 | + assertThat( gemValue.getValue() ).isNull(); |
| 188 | + assertThat( gemValue.getDefaultValue() ).isNull(); |
| 189 | + assertThat( gemValue.get() ).as( "get should return null" ).isNull(); |
| 190 | + assertThat( gemValue.getAnnotationValue() ).isNull(); |
| 191 | + assertThat( gemValue.getValueOrElseGet( () -> Collections.singletonList( 3 ) ) ) |
| 192 | + .as( "getValueOrElseGet should return other" ).isEqualTo( Collections.singletonList( 3 ) ); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments