1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.tutorial.property.constraints;
17
18 import org.junit.Before;
19 import org.junit.Test;
20
21 import de.smartics.projectdoc.annotations.DocCategory;
22 import de.smartics.projectdoc.annotations.Document;
23 import de.smartics.projectdoc.annotations.topic.DocChapter;
24 import de.smartics.projectdoc.annotations.topic.DocSection;
25 import de.smartics.properties.api.config.domain.ConfigurationValidationException;
26 import de.smartics.properties.api.core.domain.PropertyValidationException;
27 import de.smartics.properties.impl.config.classpath.ClasspathConfigurationProperties;
28 import de.smartics.properties.impl.config.classpath.ClasspathConfigurationPropertiesFactory;
29
30
31
32
33
34
35
36
37
38
39 @Document(title = "Property Constraints", sortKey = "basics0050")
40 @DocCategory({ "basics" })
41
42 public class PropertyConstraintsTutorial
43 {
44 private ClasspathConfigurationProperties config;
45
46 private ConstraintsProperties properties;
47
48 @Before
49 public void setUp()
50 {
51 config = createConfiguration();
52 properties = config.getProperties(ConstraintsProperties.class);
53 }
54
55 private static ClasspathConfigurationProperties createConfiguration()
56 {
57 final ClasspathConfigurationPropertiesFactory factory =
58 new ClasspathConfigurationPropertiesFactory();
59 final ClasspathConfigurationProperties config = factory.create();
60 config.addClassPathProperties(ConstraintsProperties.class);
61 return config;
62 }
63
64
65
66
67
68
69
70
71
72 @Test
73 @DocSection
74 public void validateMethod()
75 {
76 config.validate();
77 }
78
79
80
81
82
83
84
85
86
87
88
89 @Test(expected = ConfigurationValidationException.class)
90 @DocChapter
91 public void mandatoryAndOptionalProperties()
92 {
93 config.setProperty(properties.hostPropertyKey(), " ");
94 config.setProperty(properties.serverPropertyKey(), null);
95 config.validate();
96 }
97
98
99
100
101
102
103
104
105 @Test
106 @DocChapter
107 public void propertyWithARangeValue()
108 {
109 config.setProperty(properties.storyPointPropertyKey(), "3");
110 config.validate();
111 }
112
113
114
115
116
117
118
119
120 @Test(expected = ConfigurationValidationException.class)
121 @DocChapter
122 public void invalidRangedValueProperty()
123 {
124 config.setProperty(properties.storyPointPropertyKey(), "9999");
125 config.validate();
126 }
127
128
129
130
131
132
133
134
135 @Test
136 @DocChapter
137 public void propertyWithAEnumValue()
138 {
139 config.setProperty(properties.priorityPropertyKey(), "required");
140 config.validate();
141 }
142
143
144
145
146
147
148
149
150 @Test(expected = ConfigurationValidationException.class)
151 @DocChapter
152 public void invalidEnumValuedProperty()
153 {
154 config.setProperty(properties.priorityPropertyKey(), "##unknown##");
155 config.validate();
156 }
157
158
159
160
161
162
163
164 @Test
165 @DocSection
166 public void noValidationAtAppend()
167 {
168 config.setProperty(properties.hostPropertyKey(), " ");
169 }
170
171
172
173
174
175
176 @Test(expected = PropertyValidationException.class)
177 @DocSection
178 public void validationOnAccess()
179 {
180 config.setProperty(properties.hostPropertyKey(), " ");
181 properties.host();
182 }
183
184 }