1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.core.constraint.jsr303;
17
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.Matchers.equalTo;
20 import static org.hamcrest.Matchers.is;
21
22 import java.lang.annotation.Annotation;
23 import java.lang.reflect.Field;
24 import java.util.Date;
25 import java.util.List;
26
27 import javax.validation.constraints.AssertFalse;
28 import javax.validation.constraints.AssertTrue;
29 import javax.validation.constraints.DecimalMax;
30 import javax.validation.constraints.DecimalMin;
31 import javax.validation.constraints.Digits;
32 import javax.validation.constraints.Future;
33 import javax.validation.constraints.Max;
34 import javax.validation.constraints.Min;
35 import javax.validation.constraints.NotNull;
36 import javax.validation.constraints.Null;
37 import javax.validation.constraints.Past;
38 import javax.validation.constraints.Pattern;
39 import javax.validation.constraints.Size;
40
41 import org.junit.Test;
42
43 import de.smartics.testdoc.annotations.Uut;
44
45
46
47
48
49 @Uut(type = ConstraintPrettifier.class)
50 public class ConstraintPrettifierJsr303Test
51 {
52
53
54
55
56
57
58
59
60 private static final class Annotations
61 {
62 @AssertFalse
63 boolean assertFalse;
64
65 @AssertTrue
66 boolean assertTrue;
67
68 @DecimalMax(value = "10.0")
69 int decimalMax;
70
71 @DecimalMin(value = "-10.0")
72 int decimalMin;
73
74 @Digits(fraction = 2, integer = 3)
75 double digits;
76
77 @Future
78 Date future;
79
80 @Max(value = 42)
81 int max;
82
83 @Min(value = -1 * 42)
84 int min;
85
86 @NotNull
87 String notNull;
88
89 @Null
90 String isNull;
91
92 @Past
93 Date past;
94
95 @Pattern(regexp = "\\d+")
96 String noFlagPattern;
97
98 @Pattern(regexp = "\\w+", flags = { Pattern.Flag.MULTILINE,
99 Pattern.Flag.CASE_INSENSITIVE })
100 String multiFlagPattern;
101
102 @Pattern(regexp = "\\w+", flags = { Pattern.Flag.MULTILINE })
103 String singleFlagPattern;
104
105 @Size(min = -1 * 42, max = 42)
106 List<?> size;
107 }
108
109
110
111
112
113
114
115 private static Annotation calcAnnotation(final String fieldName,
116 final Class<? extends Annotation> annotationType)
117 {
118 try
119 {
120 final Field field = Annotations.class.getDeclaredField(fieldName);
121 return field.getAnnotation(annotationType);
122 }
123 catch (final Exception e)
124 {
125 throw new IllegalStateException("Cannot fetch annotation of type '"
126 + annotationType + "' from field '"
127 + fieldName + "'.");
128 }
129 }
130
131
132
133 @Test(expected = NullPointerException.class)
134 public void rejectsNullAnnotations()
135 {
136 new ConstraintPrettifier(null);
137 }
138
139 @Test
140 public void acceptsAssertFalse()
141 {
142 final Annotation annotation =
143 calcAnnotation("assertFalse", AssertFalse.class);
144 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
145 assertThat(uut.toString(), is(equalTo("AssertFalse")));
146 }
147
148 @Test
149 public void acceptsAssertTrue()
150 {
151 final Annotation annotation =
152 calcAnnotation("assertTrue", AssertTrue.class);
153 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
154 assertThat(uut.toString(), is(equalTo("AssertTrue")));
155 }
156
157 @Test
158 public void acceptsDecimalMax()
159 {
160 final Annotation annotation =
161 calcAnnotation("decimalMax", DecimalMax.class);
162 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
163 assertThat(uut.toString(), is(equalTo("DecimalMax(value=10.0)")));
164 }
165
166 @Test
167 public void acceptsDecimalMin()
168 {
169 final Annotation annotation =
170 calcAnnotation("decimalMin", DecimalMin.class);
171 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
172 assertThat(uut.toString(), is(equalTo("DecimalMin(value=-10.0)")));
173 }
174
175 @Test
176 public void acceptsDigits()
177 {
178 final Annotation annotation = calcAnnotation("digits", Digits.class);
179 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
180 assertThat(uut.toString(), is(equalTo("Digits(fraction=2, integer=3)")));
181 }
182
183 @Test
184 public void acceptsFuture()
185 {
186 final Annotation annotation = calcAnnotation("future", Future.class);
187 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
188 assertThat(uut.toString(), is(equalTo("Future")));
189 }
190
191 @Test
192 public void acceptsMax()
193 {
194 final Annotation annotation = calcAnnotation("max", Max.class);
195 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
196 assertThat(uut.toString(), is(equalTo("Max(value=42)")));
197 }
198
199 @Test
200 public void acceptsMin()
201 {
202 final Annotation annotation = calcAnnotation("min", Min.class);
203 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
204 assertThat(uut.toString(), is(equalTo("Min(value=-42)")));
205 }
206
207 @Test
208 public void acceptsNotNull()
209 {
210 final Annotation annotation = calcAnnotation("notNull", NotNull.class);
211 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
212 assertThat(uut.toString(), is(equalTo("NotNull")));
213 }
214
215 @Test
216 public void acceptsNull()
217 {
218 final Annotation annotation = calcAnnotation("isNull", Null.class);
219 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
220 assertThat(uut.toString(), is(equalTo("Null")));
221 }
222
223 @Test
224 public void acceptsPast()
225 {
226 final Annotation annotation = calcAnnotation("past", Past.class);
227 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
228 assertThat(uut.toString(), is(equalTo("Past")));
229 }
230
231 @Test
232 public void acceptsNoFlagPattern()
233 {
234 final Annotation annotation =
235 calcAnnotation("noFlagPattern", Pattern.class);
236 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
237 assertThat(uut.toString(), is(equalTo("Pattern(regexp=\\d+)")));
238 }
239
240 @Test
241 public void acceptsMultiFlagPattern()
242 {
243 final Annotation annotation =
244 calcAnnotation("multiFlagPattern", Pattern.class);
245 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
246 assertThat(
247 uut.toString(),
248 is(equalTo("Pattern(flags=[MULTILINE, CASE_INSENSITIVE], regexp=\\w+)")));
249 }
250
251 @Test
252 public void acceptsSingleFlagPattern()
253 {
254 final Annotation annotation =
255 calcAnnotation("singleFlagPattern", Pattern.class);
256 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
257 assertThat(uut.toString(),
258 is(equalTo("Pattern(flags=[MULTILINE], regexp=\\w+)")));
259 }
260
261 @Test
262 public void acceptsSize()
263 {
264 final Annotation annotation = calcAnnotation("size", Size.class);
265 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
266 assertThat(uut.toString(), is(equalTo("Size(min=-42, max=42)")));
267 }
268 }