fix #123 allow to specify extra annotations on repositories/methods
[yosql.git] / yosql-codegen / src / test / java / wtf / metio / yosql / codegen / blocks / DefaultAnnotationsTest.java
blob534996c638556e758f33a56c4c312c58f01bae7d
1 /*
2 * This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at https://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
8 package wtf.metio.yosql.codegen.blocks;
10 import org.junit.jupiter.api.Assertions;
11 import org.junit.jupiter.api.DisplayName;
12 import org.junit.jupiter.api.Nested;
13 import org.junit.jupiter.api.Test;
14 import wtf.metio.yosql.models.configuration.Annotation;
15 import wtf.metio.yosql.models.configuration.AnnotationMember;
16 import wtf.metio.yosql.testing.configs.AnnotationsConfigurations;
18 @DisplayName("DefaultAnnotations")
19 class DefaultAnnotationsTest {
21 @Nested
22 @DisplayName("Default Configuration")
23 class DefaultConfig {
25 @Test
26 @DisplayName("can annotate classes")
27 void shouldAnnotateClasses() {
28 // given
29 final var generator = new DefaultAnnotations(AnnotationsConfigurations.defaults());
31 // when
32 final var annotations = generator.generatedClass();
34 // then
35 Assertions.assertEquals("""
36 @javax.annotation.processing.Generated(value = "YoSQL", comments = "DO NOT MODIFY - automatically generated by YoSQL")""",
37 annotations.iterator().next().toString());
40 @Test
41 @DisplayName("can annotate fields")
42 void shouldAnnotateFields() {
43 // given
44 final var generator = new DefaultAnnotations(AnnotationsConfigurations.defaults());
46 // when
47 final var annotations = generator.generatedField();
49 // then
50 Assertions.assertEquals("""
51 @javax.annotation.processing.Generated(value = "YoSQL", comments = "DO NOT MODIFY - automatically generated by YoSQL")""",
52 annotations.iterator().next().toString());
55 @Test
56 @DisplayName("can annotate methods")
57 void shouldAnnotateMethods() {
58 // given
59 final var generator = new DefaultAnnotations(AnnotationsConfigurations.defaults());
61 // when
62 final var annotations = generator.generatedMethod();
64 // then
65 Assertions.assertEquals("""
66 @javax.annotation.processing.Generated(value = "YoSQL", comments = "DO NOT MODIFY - automatically generated by YoSQL")""",
67 annotations.iterator().next().toString());
72 @Nested
73 @DisplayName("Custom Configuration")
74 class CustomConfig {
76 @Test
77 @DisplayName("can use javax.annotation.processing.Generated")
78 void shouldUseProcessingApi() {
79 // given
80 final var generator = new DefaultAnnotations(AnnotationsConfigurations.defaults());
82 // when
83 final var annotations = generator.generatedClass();
85 // then
86 Assertions.assertEquals("""
87 @javax.annotation.processing.Generated(value = "YoSQL", comments = "DO NOT MODIFY - automatically generated by YoSQL")""",
88 annotations.iterator().next().toString());
91 @Test
92 @DisplayName("can use javax.annotation.Generated")
93 void shouldUseAnnotationApi() {
94 // given
95 final var generator = new DefaultAnnotations(AnnotationsConfigurations.generated());
97 // when
98 final var annotations = generator.generatedClass();
100 // then
101 Assertions.assertEquals("""
102 @javax.annotation.Generated(value = "YoSQL", comments = "DO NOT MODIFY - automatically generated by YoSQL")""",
103 annotations.iterator().next().toString());
108 @Test
109 void asAnnotationSpec() {
110 final var annotation = Annotation.builder().setType("com.example.Annotation").build();
111 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
112 Assertions.assertEquals("""
113 @com.example.Annotation""", spec.toString());
116 @Test
117 void asAnnotationSpecWithMember() {
118 final var annotation = Annotation.builder()
119 .setType("com.example.Annotation")
120 .addMembers(AnnotationMember.builder().setKey("value").setValue("test").build())
121 .build();
122 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
123 Assertions.assertEquals("""
124 @com.example.Annotation("test")""", spec.toString());
127 @Test
128 void asAnnotationSpecWithMemberUsingCustomKey() {
129 final var annotation = Annotation.builder()
130 .setType("com.example.Annotation")
131 .addMembers(AnnotationMember.builder().setKey("random").setValue("test").build())
132 .build();
133 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
134 Assertions.assertEquals("""
135 @com.example.Annotation(random = "test")""", spec.toString());
138 @Test
139 void asAnnotationSpecWithMembers() {
140 final var annotation = Annotation.builder()
141 .setType("com.example.Annotation")
142 .addMembers(AnnotationMember.builder().setKey("value").setValue("test").build())
143 .addMembers(AnnotationMember.builder().setKey("another").setValue("value").build())
144 .build();
145 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
146 Assertions.assertEquals("""
147 @com.example.Annotation(value = "test", another = "value")""", spec.toString());
150 @Test
151 void asAnnotationSpecWithMemberUsingBoolType() {
152 final var annotation = Annotation.builder()
153 .setType("com.example.Annotation")
154 .addMembers(AnnotationMember.builder()
155 .setKey("test")
156 .setValue("true")
157 .setType("boolean")
158 .build())
159 .build();
160 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
161 Assertions.assertEquals("""
162 @com.example.Annotation(test = true)""", spec.toString());
165 @Test
166 void asAnnotationSpecWithMemberUsingIntType() {
167 final var annotation = Annotation.builder()
168 .setType("com.example.Annotation")
169 .addMembers(AnnotationMember.builder()
170 .setKey("test")
171 .setValue("123")
172 .setType("int")
173 .build())
174 .build();
175 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
176 Assertions.assertEquals("""
177 @com.example.Annotation(test = 123)""", spec.toString());
180 @Test
181 void asAnnotationSpecWithMemberUsingLongType() {
182 final var annotation = Annotation.builder()
183 .setType("com.example.Annotation")
184 .addMembers(AnnotationMember.builder()
185 .setKey("test")
186 .setValue("456789")
187 .setType("long")
188 .build())
189 .build();
190 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
191 Assertions.assertEquals("""
192 @com.example.Annotation(test = 456789)""", spec.toString());
195 @Test
196 void asAnnotationSpecWithMemberUsingShortType() {
197 final var annotation = Annotation.builder()
198 .setType("com.example.Annotation")
199 .addMembers(AnnotationMember.builder()
200 .setKey("test")
201 .setValue("123")
202 .setType("short")
203 .build())
204 .build();
205 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
206 Assertions.assertEquals("""
207 @com.example.Annotation(test = 123)""", spec.toString());
210 @Test
211 void asAnnotationSpecWithMemberUsingByteType() {
212 final var annotation = Annotation.builder()
213 .setType("com.example.Annotation")
214 .addMembers(AnnotationMember.builder()
215 .setKey("test")
216 .setValue("123")
217 .setType("byte")
218 .build())
219 .build();
220 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
221 Assertions.assertEquals("""
222 @com.example.Annotation(test = 123)""", spec.toString());
225 @Test
226 void asAnnotationSpecWithMemberUsingFloatType() {
227 final var annotation = Annotation.builder()
228 .setType("com.example.Annotation")
229 .addMembers(AnnotationMember.builder()
230 .setKey("test")
231 .setValue("3.14")
232 .setType("float")
233 .build())
234 .build();
235 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
236 Assertions.assertEquals("""
237 @com.example.Annotation(test = 3.14)""", spec.toString());
240 @Test
241 void asAnnotationSpecWithMemberUsingDoubleType() {
242 final var annotation = Annotation.builder()
243 .setType("com.example.Annotation")
244 .addMembers(AnnotationMember.builder()
245 .setKey("test")
246 .setValue("3.14")
247 .setType("double")
248 .build())
249 .build();
250 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
251 Assertions.assertEquals("""
252 @com.example.Annotation(test = 3.14)""", spec.toString());
255 @Test
256 void asAnnotationSpecWithMemberUsingCharType() {
257 final var annotation = Annotation.builder()
258 .setType("com.example.Annotation")
259 .addMembers(AnnotationMember.builder()
260 .setKey("test")
261 .setValue("a")
262 .setType("char")
263 .build())
264 .build();
265 final var spec = DefaultAnnotations.asAnnotationSpec(annotation);
266 Assertions.assertEquals("""
267 @com.example.Annotation(test = 'a')""", spec.toString());