1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package test.de.smartics.properties.spi.core.metadata;
17
18 import java.lang.reflect.Method;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.validation.constraints.NotNull;
23
24 import org.hibernate.validator.constraints.NotBlank;
25 import org.junit.Test;
26
27 import de.smartics.properties.api.core.domain.PropertyConstraint;
28 import de.smartics.properties.api.core.domain.PropertyDescriptor;
29 import de.smartics.properties.api.core.domain.PropertyExpression;
30 import de.smartics.properties.api.core.domain.PropertyKey;
31 import de.smartics.properties.api.core.domain.PropertyType;
32 import de.smartics.properties.spi.core.constraint.jsr303.GenericPropertyConstraint;
33 import de.smartics.properties.spi.core.metadata.PropertyMetaDataParser;
34 import de.smartics.testdoc.annotations.Uut;
35
36
37
38
39
40 @Uut(type = PropertyMetaDataParser.class)
41 public class PropertyMetaDataParserConstraintsTest extends
42 AbstractPropertyMetaDataParserTestBase
43 {
44
45
46
47
48 private static final NotNull NOT_NULL;
49
50 private static final NotBlank NOT_BLANK;
51
52
53
54
55
56 public interface TestProperties
57 {
58 @NotNull
59 @NotBlank
60 String property();
61 }
62
63
64
65
66
67
68
69 static
70 {
71 try
72 {
73 NOT_NULL =
74 TestProperties.class.getDeclaredMethod("property").getAnnotation(
75 NotNull.class);
76 NOT_BLANK =
77 TestProperties.class.getDeclaredMethod("property").getAnnotation(
78 NotBlank.class);
79 }
80 catch (final Exception e)
81 {
82 throw new IllegalStateException("Cannot determine annotation.");
83 }
84 }
85
86
87
88 @Test
89 public void recognizesConstraints()
90 {
91 final Method method = fetchMethod(TestProperties.class, "property");
92 final PropertyDescriptor descriptor = uut.readDescriptor(method);
93
94 final PropertyKey expectedKey =
95 new PropertyKey(TestProperties.class.getName(), "property");
96 final PropertyType expectedType = new PropertyType(String.class);
97 final PropertyConstraint<?> notNull =
98 new GenericPropertyConstraint<NotNull, String>(NOT_NULL, String.class);
99 final PropertyConstraint<?> notBlank =
100 new GenericPropertyConstraint<NotBlank, String>(NOT_BLANK, String.class);
101 final List<PropertyConstraint<?>> expectedConstraints =
102 new ArrayList<PropertyConstraint<?>>(2);
103 expectedConstraints.add(notNull);
104 expectedConstraints.add(notBlank);
105
106 checkThat(descriptor, expectedKey, expectedType, true,
107 PropertyExpression.NO_EXPRESSION, null, expectedConstraints);
108 }
109 }