|
50 | 50 | import org.springframework.util.MultiValueMap;
|
51 | 51 | import org.springframework.util.ReflectionUtils;
|
52 | 52 |
|
| 53 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; |
53 | 54 | import static org.assertj.core.api.Assertions.assertThat;
|
54 | 55 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
55 | 56 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
@@ -1376,6 +1377,50 @@ void synthesizeAlreadySynthesized() throws Exception {
|
1376 | 1377 | assertThat(synthesizedWebMapping.value()).containsExactly("/test");
|
1377 | 1378 | }
|
1378 | 1379 |
|
| 1380 | + @Test |
| 1381 | + void synthesizeShouldNotSynthesizeNonsynthesizableAnnotations() throws Exception { |
| 1382 | + Method method = getClass().getDeclaredMethod("getId"); |
| 1383 | + Id id = method.getAnnotation(Id.class); |
| 1384 | + assertThat(id).isNotNull(); |
| 1385 | + |
| 1386 | + Id synthesizedId = MergedAnnotation.from(id).synthesize(); |
| 1387 | + assertThat(id).isEqualTo(synthesizedId); |
| 1388 | + // It doesn't make sense to synthesize {@code @Id} since it declares zero attributes. |
| 1389 | + assertThat(synthesizedId).isNotInstanceOf(SynthesizedAnnotation.class); |
| 1390 | + assertThat(id).isSameAs(synthesizedId); |
| 1391 | + } |
| 1392 | + |
| 1393 | + /** |
| 1394 | + * If an attempt is made to synthesize an annotation from an annotation instance |
| 1395 | + * that has already been synthesized, the original synthesized annotation should |
| 1396 | + * ideally be returned as-is without creating a new proxy instance with the same |
| 1397 | + * values. |
| 1398 | + */ |
| 1399 | + @Test |
| 1400 | + void synthesizeShouldNotResynthesizeAlreadySynthesizedAnnotations() throws Exception { |
| 1401 | + Method method = WebController.class.getMethod("handleMappedWithValueAttribute"); |
| 1402 | + RequestMapping webMapping = method.getAnnotation(RequestMapping.class); |
| 1403 | + assertThat(webMapping).isNotNull(); |
| 1404 | + |
| 1405 | + MergedAnnotation<RequestMapping> mergedAnnotation1 = MergedAnnotation.from(webMapping); |
| 1406 | + RequestMapping synthesizedWebMapping1 = mergedAnnotation1.synthesize(); |
| 1407 | + RequestMapping synthesizedWebMapping2 = MergedAnnotation.from(webMapping).synthesize(); |
| 1408 | + |
| 1409 | + assertThat(synthesizedWebMapping1).isInstanceOf(SynthesizedAnnotation.class); |
| 1410 | + assertThat(synthesizedWebMapping2).isInstanceOf(SynthesizedAnnotation.class); |
| 1411 | + assertThat(synthesizedWebMapping1).isEqualTo(synthesizedWebMapping2); |
| 1412 | + |
| 1413 | + // Synthesizing an annotation from a different MergedAnnotation results in a different synthesized annotation instance. |
| 1414 | + assertThat(synthesizedWebMapping1).isNotSameAs(synthesizedWebMapping2); |
| 1415 | + // Synthesizing an annotation from the same MergedAnnotation results in the same synthesized annotation instance. |
| 1416 | + assertThat(synthesizedWebMapping1).isSameAs(mergedAnnotation1.synthesize()); |
| 1417 | + |
| 1418 | + RequestMapping synthesizedAgainWebMapping = MergedAnnotation.from(synthesizedWebMapping1).synthesize(); |
| 1419 | + assertThat(synthesizedWebMapping1).isEqualTo(synthesizedAgainWebMapping); |
| 1420 | + // Synthesizing an already synthesized annotation results in the original synthesized annotation instance. |
| 1421 | + assertThat(synthesizedWebMapping1).isSameAs(synthesizedAgainWebMapping); |
| 1422 | + } |
| 1423 | + |
1379 | 1424 | @Test
|
1380 | 1425 | void synthesizeWhenAliasForIsMissingAttributeDeclaration() throws Exception {
|
1381 | 1426 | AliasForWithMissingAttributeDeclaration annotation =
|
@@ -2951,6 +2996,18 @@ public void handleMappedWithDifferentPathAndValueAttributes() {
|
2951 | 2996 | }
|
2952 | 2997 | }
|
2953 | 2998 |
|
| 2999 | + /** |
| 3000 | + * Mimics javax.persistence.Id |
| 3001 | + */ |
| 3002 | + @Retention(RUNTIME) |
| 3003 | + @interface Id { |
| 3004 | + } |
| 3005 | + |
| 3006 | + @Id |
| 3007 | + private Long getId() { |
| 3008 | + return 42L; |
| 3009 | + } |
| 3010 | + |
2954 | 3011 | @Retention(RetentionPolicy.RUNTIME)
|
2955 | 3012 | @interface TestConfiguration {
|
2956 | 3013 |
|
|
0 commit comments