1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.tutorial.property.list;
17
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.Matchers.contains;
20
21 import java.util.List;
22
23 import org.junit.Before;
24 import org.junit.Test;
25
26 import de.smartics.projectdoc.annotations.DocCategory;
27 import de.smartics.projectdoc.annotations.Document;
28 import de.smartics.projectdoc.annotations.topic.DocChapter;
29 import de.smartics.projectdoc.annotations.topic.DocSection;
30 import de.smartics.properties.api.core.domain.PropertyKey;
31 import de.smartics.properties.api.core.domain.PropertyValidationException;
32 import de.smartics.properties.impl.config.classpath.ClasspathConfigurationProperties;
33 import de.smartics.properties.impl.config.classpath.ClasspathConfigurationPropertiesFactory;
34
35
36
37
38
39 @Document(title = "List Properties Constraints", sortKey = "basics0110")
40 @DocCategory({ "basics" })
41
42 public class ListPropertyConstraintsTutorial
43 {
44 private ClasspathConfigurationProperties config;
45
46 private ListPropertiesWithConstraints properties;
47
48 @Before
49 public void setUp()
50 {
51 config = createConfiguration();
52 properties = config.getProperties(ListPropertiesWithConstraints.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(ListPropertiesWithConstraints.class);
61 return config;
62 }
63
64
65
66
67
68
69
70
71
72
73
74 @Test
75 @DocChapter
76 public void propertiesWithConstraints()
77 {
78 final List<Integer> list = properties.intList();
79 assertThat(list, contains(2, 3, 4));
80 }
81
82
83
84
85
86
87
88 @Test(expected = PropertyValidationException.class)
89 @DocSection
90 public void listConstraintsAtWork()
91 {
92 final PropertyKey key = properties.intListPropertyKey();
93 config.setProperty(key, "3, 3, 3, 3, 3, 3, 3");
94 properties.intList();
95 }
96
97
98
99
100
101
102
103 @Test(expected = PropertyValidationException.class)
104 @DocSection
105 public void listElementConstraintsAtWork()
106 {
107 final PropertyKey key = properties.intListPropertyKey();
108 config.setProperty(key, "3, 10, 3");
109 properties.intList();
110 }
111 }