|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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 | + * https://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.aot.hint.support; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.aot.hint.ExecutableMode; |
| 24 | +import org.springframework.aot.hint.MemberCategory; |
| 25 | +import org.springframework.aot.hint.RuntimeHints; |
| 26 | +import org.springframework.aot.hint.TypeReference; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link BindingReflectionHintsRegistrar}. |
| 32 | + * |
| 33 | + * @author Sebastien Deleuze |
| 34 | + */ |
| 35 | +public class BindingReflectionHintsRegistrarTests { |
| 36 | + |
| 37 | + private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar(); |
| 38 | + private final RuntimeHints hints = new RuntimeHints(); |
| 39 | + |
| 40 | + @Test |
| 41 | + void registerTypeForSerializationWithEmptyClass() { |
| 42 | + bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleEmptyClass.class); |
| 43 | + assertThat(this.hints.reflection().typeHints()).singleElement() |
| 44 | + .satisfies(typeHint -> { |
| 45 | + assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleEmptyClass.class)); |
| 46 | + assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder( |
| 47 | + MemberCategory.DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); |
| 48 | + assertThat(typeHint.constructors()).isEmpty(); |
| 49 | + assertThat(typeHint.fields()).isEmpty(); |
| 50 | + assertThat(typeHint.methods()).isEmpty(); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void registerTypeForSerializationWithNoProperty() { |
| 56 | + bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithNoProperty.class); |
| 57 | + assertThat(this.hints.reflection().typeHints()).singleElement() |
| 58 | + .satisfies(typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithNoProperty.class))); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void registerTypeForSerializationWithGetter() { |
| 63 | + bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithGetter.class); |
| 64 | + assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder( |
| 65 | + typeHint -> { |
| 66 | + assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)); |
| 67 | + assertThat(typeHint.getMemberCategories()).isEmpty(); |
| 68 | + assertThat(typeHint.constructors()).isEmpty(); |
| 69 | + assertThat(typeHint.fields()).isEmpty(); |
| 70 | + assertThat(typeHint.methods()).isEmpty(); |
| 71 | + }, |
| 72 | + typeHint -> { |
| 73 | + assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithGetter.class)); |
| 74 | + assertThat(typeHint.methods()).singleElement().satisfies(methodHint -> { |
| 75 | + assertThat(methodHint.getName()).isEqualTo("getName"); |
| 76 | + assertThat(methodHint.getModes()).containsOnly(ExecutableMode.INVOKE); |
| 77 | + }); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void registerTypeForSerializationWithSetter() { |
| 83 | + bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithSetter.class); |
| 84 | + assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder( |
| 85 | + typeHint -> { |
| 86 | + assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)); |
| 87 | + assertThat(typeHint.getMemberCategories()).isEmpty(); |
| 88 | + assertThat(typeHint.constructors()).isEmpty(); |
| 89 | + assertThat(typeHint.fields()).isEmpty(); |
| 90 | + assertThat(typeHint.methods()).isEmpty(); |
| 91 | + }, |
| 92 | + typeHint -> { |
| 93 | + assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithSetter.class)); |
| 94 | + assertThat(typeHint.methods()).singleElement().satisfies(methodHint -> { |
| 95 | + assertThat(methodHint.getName()).isEqualTo("setName"); |
| 96 | + assertThat(methodHint.getModes()).containsOnly(ExecutableMode.INVOKE); |
| 97 | + }); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void registerTypeForSerializationWithListProperty() { |
| 103 | + bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithListProperty.class); |
| 104 | + assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder( |
| 105 | + typeHint -> { |
| 106 | + assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)); |
| 107 | + assertThat(typeHint.getMemberCategories()).isEmpty(); |
| 108 | + assertThat(typeHint.constructors()).isEmpty(); |
| 109 | + assertThat(typeHint.fields()).isEmpty(); |
| 110 | + assertThat(typeHint.methods()).isEmpty(); |
| 111 | + }, |
| 112 | + typeHint -> { |
| 113 | + assertThat(typeHint.getType()).isEqualTo(TypeReference.of(List.class)); |
| 114 | + assertThat(typeHint.getMemberCategories()).isEmpty(); |
| 115 | + assertThat(typeHint.constructors()).isEmpty(); |
| 116 | + assertThat(typeHint.fields()).isEmpty(); |
| 117 | + assertThat(typeHint.methods()).isEmpty(); |
| 118 | + }, |
| 119 | + typeHint -> { |
| 120 | + assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithListProperty.class)); |
| 121 | + assertThat(typeHint.methods()).satisfiesExactlyInAnyOrder( |
| 122 | + methodHint -> { |
| 123 | + assertThat(methodHint.getName()).isEqualTo("setNames"); |
| 124 | + assertThat(methodHint.getModes()).containsOnly(ExecutableMode.INVOKE); |
| 125 | + }, |
| 126 | + methodHint -> { |
| 127 | + assertThat(methodHint.getName()).isEqualTo("getNames"); |
| 128 | + assertThat(methodHint.getModes()).containsOnly(ExecutableMode.INVOKE); |
| 129 | + }); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + static class SampleEmptyClass { |
| 134 | + } |
| 135 | + |
| 136 | + static class SampleClassWithNoProperty { |
| 137 | + |
| 138 | + String name() { |
| 139 | + return null; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + static class SampleClassWithGetter { |
| 144 | + |
| 145 | + public String getName() { |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + public SampleEmptyClass unmanaged() { |
| 150 | + return null; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + static class SampleClassWithSetter { |
| 155 | + |
| 156 | + public void setName(String name) { |
| 157 | + } |
| 158 | + |
| 159 | + public SampleEmptyClass unmanaged() { |
| 160 | + return null; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + static class SampleClassWithListProperty { |
| 165 | + |
| 166 | + public List<String> getNames() { |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + public void setNames(List<String> names) { |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments