1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.tutorial.property.expressions;
17
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.Matchers.is;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23
24 import org.junit.Before;
25 import org.junit.Test;
26
27 import de.smartics.projectdoc.annotations.DocCategory;
28 import de.smartics.projectdoc.annotations.Document;
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.ReadOnlyPropertyException;
32 import de.smartics.properties.impl.config.classpath.ClasspathConfigurationProperties;
33 import de.smartics.properties.impl.config.classpath.ClasspathConfigurationPropertiesFactory;
34
35
36
37
38
39
40
41
42
43 @Document(title = "Property Expressions", sortKey = "basics0060")
44 @DocCategory({ "basics" })
45
46 public class PropertyExpressionsTutorial
47 {
48 private ClasspathConfigurationProperties config;
49
50 private ExpressionsProperties properties;
51
52 private OverridingExpressionsProperties overriddenProperties;
53
54 @Before
55 public void setUp()
56 {
57 config = createConfiguration();
58 properties = config.getProperties(ExpressionsProperties.class);
59 overriddenProperties =
60 config.getProperties(OverridingExpressionsProperties.class);
61 }
62
63 private static ClasspathConfigurationProperties createConfiguration()
64 {
65 final ClasspathConfigurationPropertiesFactory factory =
66 new ClasspathConfigurationPropertiesFactory();
67 final ClasspathConfigurationProperties config = factory.create();
68
69 config.addClassPathProperties(ExpressionsProperties.class);
70 config.addClassPathProperties(OverridingExpressionsProperties.class);
71
72 return config;
73 }
74
75
76
77
78
79
80
81 @Test
82 @DocSection
83 public void simpleStringValue()
84 {
85 final String host = properties.host();
86 assertThat(host, is("localhost"));
87 }
88
89
90
91
92
93
94
95
96 @Test
97 @DocSection
98 public void simpleIntValue()
99 {
100 final int port = properties.port();
101 assertThat(port, is(8080));
102 }
103
104
105
106
107
108
109
110
111
112
113 @Test
114 @DocSection
115 public void composedUrlValue() throws MalformedURLException
116 {
117 final URL homePageUrl = properties.homePageUrl();
118 final URL expected = new URL("http://localhost:8080/index.html");
119 assertThat(homePageUrl, is(expected));
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 @Test
137 @DocSection
138 public void overrideValues() throws MalformedURLException
139 {
140 final URL homePageUrl = overriddenProperties.homePageUrl();
141 final URL expected = new URL("http://www.example.com:8081/index.html");
142 assertThat(homePageUrl, is(expected));
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156 @Test(expected = ReadOnlyPropertyException.class)
157 @DocSection
158 public void overrideValuesAtRuntimeFailsForReadOnlyProperties()
159 throws MalformedURLException
160 {
161 final PropertyKey key =
162 new PropertyKey("tutorial.property.override", "port");
163 config.setProperty(key, "4242");
164 }
165
166
167
168
169
170
171
172
173 @Test
174 @DocSection
175 public void overrideValuesAtRuntime() throws MalformedURLException
176 {
177 final PropertyKey key = overriddenProperties.hostPropertyKey();
178 config.setProperty(key, "www.smartics.de");
179 final URL homePageUrl = overriddenProperties.homePageUrl();
180 final URL expected = new URL("http://www.smartics.de:8081/index.html");
181 assertThat(homePageUrl, is(expected));
182 }
183 }