|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.core.annotation; |
| 18 | + |
| 19 | +import java.lang.annotation.Retention; |
| 20 | +import java.lang.annotation.RetentionPolicy; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.NoSuchElementException; |
| 24 | + |
| 25 | +import org.assertj.core.api.ThrowableTypeAssert; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import org.springframework.util.ConcurrentReferenceHashMap; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.*; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link MissingMergedAnnotation}. |
| 34 | + * |
| 35 | + * @author Phillip Webb |
| 36 | + */ |
| 37 | +public class MissingMergedAnnotationTests { |
| 38 | + |
| 39 | + private final MergedAnnotation<?> missing = MissingMergedAnnotation.getInstance(); |
| 40 | + |
| 41 | + @Test |
| 42 | + public void getTypeThrowsNoSuchElementException() { |
| 43 | + assertThatNoSuchElementException().isThrownBy(() -> this.missing.getType()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void isPresentReturnsFalse() { |
| 48 | + assertThat(this.missing.isPresent()).isFalse(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void isDirectlyPresentReturnsFalse() { |
| 53 | + assertThat(this.missing.isDirectlyPresent()).isFalse(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void isMetaPresentReturnsFalse() { |
| 58 | + assertThat(this.missing.isMetaPresent()).isFalse(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void getDepthReturnsMinusOne() { |
| 63 | + assertThat(this.missing.getDepth()).isEqualTo(-1); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void getAggregateIndexReturnsMinusOne() { |
| 68 | + assertThat(this.missing.getAggregateIndex()).isEqualTo(-1); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void getSourceReturnsNull() { |
| 73 | + assertThat(this.missing.getSource()).isNull(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void getParentReturnsNull() { |
| 78 | + assertThat(this.missing.getParent()).isNull(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void hasNonDefaultValueThrowsNoSuchElementException() { |
| 83 | + assertThatNoSuchElementException().isThrownBy( |
| 84 | + () -> this.missing.hasNonDefaultValue("value")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void hasDefaultValueThrowsNoSuchElementException() { |
| 89 | + assertThatNoSuchElementException().isThrownBy( |
| 90 | + () -> this.missing.hasDefaultValue("value")); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void getByteThrowsNoSuchElementException() { |
| 95 | + assertThatNoSuchElementException().isThrownBy( |
| 96 | + () -> this.missing.getByte("value")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void getByteArrayThrowsNoSuchElementException() { |
| 101 | + assertThatNoSuchElementException().isThrownBy( |
| 102 | + () -> this.missing.getByteArray("value")); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void getBooleanThrowsNoSuchElementException() { |
| 107 | + assertThatNoSuchElementException().isThrownBy( |
| 108 | + () -> this.missing.getBoolean("value")); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void getBooleanArrayThrowsNoSuchElementException() { |
| 113 | + assertThatNoSuchElementException().isThrownBy( |
| 114 | + () -> this.missing.getBooleanArray("value")); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void getCharThrowsNoSuchElementException() { |
| 119 | + assertThatNoSuchElementException().isThrownBy( |
| 120 | + () -> this.missing.getChar("value")); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void getCharArrayThrowsNoSuchElementException() { |
| 125 | + assertThatNoSuchElementException().isThrownBy( |
| 126 | + () -> this.missing.getCharArray("value")); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void getShortThrowsNoSuchElementException() { |
| 131 | + assertThatNoSuchElementException().isThrownBy( |
| 132 | + () -> this.missing.getShort("value")); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void getShortArrayThrowsNoSuchElementException() { |
| 137 | + assertThatNoSuchElementException().isThrownBy( |
| 138 | + () -> this.missing.getShortArray("value")); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void getIntThrowsNoSuchElementException() { |
| 143 | + assertThatNoSuchElementException().isThrownBy(() -> this.missing.getInt("value")); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void getIntArrayThrowsNoSuchElementException() { |
| 148 | + assertThatNoSuchElementException().isThrownBy( |
| 149 | + () -> this.missing.getIntArray("value")); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void getLongThrowsNoSuchElementException() { |
| 154 | + assertThatNoSuchElementException().isThrownBy( |
| 155 | + () -> this.missing.getLong("value")); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void getLongArrayThrowsNoSuchElementException() { |
| 160 | + assertThatNoSuchElementException().isThrownBy( |
| 161 | + () -> this.missing.getLongArray("value")); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void getDoubleThrowsNoSuchElementException() { |
| 166 | + assertThatNoSuchElementException().isThrownBy( |
| 167 | + () -> this.missing.getDouble("value")); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void getDoubleArrayThrowsNoSuchElementException() { |
| 172 | + assertThatNoSuchElementException().isThrownBy( |
| 173 | + () -> this.missing.getDoubleArray("value")); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void getFloatThrowsNoSuchElementException() { |
| 178 | + assertThatNoSuchElementException().isThrownBy( |
| 179 | + () -> this.missing.getFloat("value")); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void getFloatArrayThrowsNoSuchElementException() { |
| 184 | + assertThatNoSuchElementException().isThrownBy( |
| 185 | + () -> this.missing.getFloatArray("value")); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void getStringThrowsNoSuchElementException() { |
| 190 | + assertThatNoSuchElementException().isThrownBy( |
| 191 | + () -> this.missing.getString("value")); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void getStringArrayThrowsNoSuchElementException() { |
| 196 | + assertThatNoSuchElementException().isThrownBy( |
| 197 | + () -> this.missing.getStringArray("value")); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + public void getClassThrowsNoSuchElementException() { |
| 202 | + assertThatNoSuchElementException().isThrownBy( |
| 203 | + () -> this.missing.getClass("value")); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + public void getClassArrayThrowsNoSuchElementException() { |
| 208 | + assertThatNoSuchElementException().isThrownBy( |
| 209 | + () -> this.missing.getClassArray("value")); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void getEnumThrowsNoSuchElementException() { |
| 214 | + assertThatNoSuchElementException().isThrownBy( |
| 215 | + () -> this.missing.getEnum("value", TestEnum.class)); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + public void getEnumArrayThrowsNoSuchElementException() { |
| 220 | + assertThatNoSuchElementException().isThrownBy( |
| 221 | + () -> this.missing.getEnumArray("value", TestEnum.class)); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + public void getAnnotationThrowsNoSuchElementException() { |
| 226 | + assertThatNoSuchElementException().isThrownBy( |
| 227 | + () -> this.missing.getAnnotation("value", TestAnnotation.class)); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void getAnnotationArrayThrowsNoSuchElementException() { |
| 232 | + assertThatNoSuchElementException().isThrownBy( |
| 233 | + () -> this.missing.getAnnotationArray("value", TestAnnotation.class)); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void getValueReturnsEmpty() { |
| 238 | + assertThat(this.missing.getValue("value", Integer.class)).isEmpty(); |
| 239 | + } |
| 240 | + |
| 241 | + @Test |
| 242 | + public void getDefaultValueReturnsEmpty() { |
| 243 | + assertThat(this.missing.getDefaultValue("value", Integer.class)).isEmpty(); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void synthesizeThrowsNoSuchElementException() { |
| 248 | + assertThatNoSuchElementException().isThrownBy(() -> this.missing.synthesize()); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void synthesizeWithPredicateWhenPredicateMatchesThrowsNoSuchElementException() { |
| 253 | + assertThatNoSuchElementException().isThrownBy( |
| 254 | + () -> this.missing.synthesize((annotation) -> true)); |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + public void synthesizeWithPredicateWhenPredicateDoesNotMatchReturnsEmpty() { |
| 259 | + assertThat(this.missing.synthesize((annotation) -> false)).isEmpty(); |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + public void toStringReturnsString() { |
| 264 | + assertThat(this.missing.toString()).isEqualTo("(missing)"); |
| 265 | + } |
| 266 | + |
| 267 | + @Test |
| 268 | + public void asMapReturnsEmptyMap() { |
| 269 | + Map<String, Object> map = this.missing.asMap(); |
| 270 | + assertThat(map).isSameAs(Collections.EMPTY_MAP); |
| 271 | + } |
| 272 | + |
| 273 | + @Test |
| 274 | + public void asMapWithFactoryReturnsNewMapFromFactory() { |
| 275 | + Map<String, Object> map = this.missing.asMap(annotation->new ConcurrentReferenceHashMap<>()); |
| 276 | + assertThat(map).isInstanceOf(ConcurrentReferenceHashMap.class); |
| 277 | + } |
| 278 | + |
| 279 | + private static ThrowableTypeAssert<NoSuchElementException> assertThatNoSuchElementException() { |
| 280 | + return assertThatExceptionOfType(NoSuchElementException.class); |
| 281 | + } |
| 282 | + |
| 283 | + private static enum TestEnum { |
| 284 | + ONE, TWO, THREE |
| 285 | + } |
| 286 | + |
| 287 | + @Retention(RetentionPolicy.RUNTIME) |
| 288 | + private static @interface TestAnnotation { |
| 289 | + |
| 290 | + } |
| 291 | + |
| 292 | +} |
0 commit comments