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
25 import javax.validation.constraints.Pattern;
26
27 import org.hibernate.validator.constraints.CreditCardNumber;
28 import org.hibernate.validator.constraints.Email;
29 import org.hibernate.validator.constraints.Length;
30 import org.hibernate.validator.constraints.NotBlank;
31 import org.hibernate.validator.constraints.NotEmpty;
32 import org.hibernate.validator.constraints.Range;
33 import org.hibernate.validator.constraints.SafeHtml;
34 import org.hibernate.validator.constraints.ScriptAssert;
35 import org.hibernate.validator.constraints.URL;
36 import org.junit.Test;
37
38 import de.smartics.testdoc.annotations.Uut;
39
40
41
42
43
44 @Uut(type = ConstraintPrettifier.class)
45 public class PrettifierHibernateTest
46 {
47
48
49
50
51
52
53
54
55 @ScriptAssert(lang = "javascript", script = "test.js", alias = "alias")
56 private static final class HibernateAnnotations
57 {
58 @CreditCardNumber
59 String creditCardNumber;
60
61 @Email(regexp = "\\d+")
62 String noFlagEmail;
63
64 @Email(regexp = "\\w+", flags = { Pattern.Flag.MULTILINE,
65 Pattern.Flag.CASE_INSENSITIVE })
66 String multiFlagEmail;
67
68 @Email(regexp = "\\w+", flags = { Pattern.Flag.MULTILINE })
69 String singleFlagEmail;
70
71 @Length(min = -1 * 42, max = 42)
72 String length;
73
74 @NotBlank
75 String notBlank;
76
77 @NotEmpty
78 String notEmpty;
79
80 @Range(min = 2, max = 4)
81 String range;
82
83 @SafeHtml(additionalTags = { "tag1", "tag2" },
84 whitelistType = SafeHtml.WhiteListType.BASIC)
85 String safeHtml;
86
87 @URL(flags = { Pattern.Flag.MULTILINE }, host = "example", port = 42,
88 protocol = "https", regexp = "\\d+")
89 String url;
90 }
91
92
93
94
95
96
97
98 private static Annotation calcAnnotation(final String fieldName,
99 final Class<? extends Annotation> annotationType)
100 {
101 try
102 {
103 final Field field =
104 HibernateAnnotations.class.getDeclaredField(fieldName);
105 return field.getAnnotation(annotationType);
106 }
107 catch (final Exception e)
108 {
109 throw new IllegalStateException("Cannot fetch annotation of type '"
110 + annotationType + "' from field '"
111 + fieldName + "'.");
112 }
113 }
114
115 private static Annotation calcAnnotation(
116 final Class<? extends Annotation> annotationType)
117 {
118 try
119 {
120 return HibernateAnnotations.class.getAnnotation(annotationType);
121 }
122 catch (final Exception e)
123 {
124 throw new IllegalStateException("Cannot fetch annotation of type '"
125 + annotationType + "' from class '"
126 + HibernateAnnotations.class.getName()
127 + "'.");
128 }
129 }
130
131
132
133 @Test
134 public void acceptsCreditCardNumber()
135 {
136 final Annotation annotation =
137 calcAnnotation("creditCardNumber", CreditCardNumber.class);
138 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
139 assertThat(uut.toString(), is(equalTo("CreditCardNumber")));
140 }
141
142 @Test
143 public void acceptsNoFlagEmail()
144 {
145 final Annotation annotation = calcAnnotation("noFlagEmail", Email.class);
146 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
147 assertThat(uut.toString(), is(equalTo("Email(regexp=\\d+)")));
148 }
149
150 @Test
151 public void acceptsMultiFlagEmail()
152 {
153 final Annotation annotation = calcAnnotation("multiFlagEmail", Email.class);
154 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
155 assertThat(uut.toString(),
156 is(equalTo("Email(flags=[MULTILINE, CASE_INSENSITIVE], regexp=\\w+)")));
157 }
158
159 @Test
160 public void acceptsSingleFlagEmail()
161 {
162 final Annotation annotation =
163 calcAnnotation("singleFlagEmail", Email.class);
164 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
165 assertThat(uut.toString(),
166 is(equalTo("Email(flags=[MULTILINE], regexp=\\w+)")));
167 }
168
169 @Test
170 public void acceptsLength()
171 {
172 final Annotation annotation = calcAnnotation("length", Length.class);
173 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
174 assertThat(uut.toString(), is(equalTo("Length(min=-42, max=42)")));
175 }
176
177
178
179 @Test
180 public void acceptsNotBlank()
181 {
182 final Annotation annotation = calcAnnotation("notBlank", NotBlank.class);
183 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
184 assertThat(uut.toString(), is(equalTo("NotBlank")));
185 }
186
187 @Test
188 public void acceptsNotEmpty()
189 {
190 final Annotation annotation = calcAnnotation("notEmpty", NotEmpty.class);
191 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
192 assertThat(uut.toString(), is(equalTo("NotEmpty")));
193 }
194
195 @Test
196 public void acceptsRange()
197 {
198 final Annotation annotation = calcAnnotation("range", Range.class);
199 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
200 assertThat(uut.toString(), is(equalTo("Range(min=2, max=4)")));
201 }
202
203 @Test
204 public void acceptsSafeHtml()
205 {
206 final Annotation annotation = calcAnnotation("safeHtml", SafeHtml.class);
207 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
208 assertThat(
209 uut.toString(),
210 is(equalTo("SafeHtml(additionalTags=[tag1, tag2], whitelistType=BASIC)")));
211 }
212
213 @Test
214 public void acceptsScriptAssert()
215 {
216 final Annotation annotation = calcAnnotation(ScriptAssert.class);
217 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
218 assertThat(
219 uut.toString(),
220 is(equalTo("ScriptAssert(alias=alias, lang=javascript, script=test.js)")));
221 }
222
223 @Test
224 public void acceptsUrl()
225 {
226 final Annotation annotation = calcAnnotation("url", URL.class);
227 final ConstraintPrettifier uut = new ConstraintPrettifier(annotation);
228 assertThat(
229 uut.toString(),
230 is(equalTo("URL(port=42, protocol=https, host=example, flags=[MULTILINE], regexp=\\d+)")));
231 }
232 }