View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This tutorial introduces how to add expressions to the property declaration.
37   * <p>
38   * Expressions allow to declare default values and to construct property values
39   * out of other property values.
40   * </p>
41   * {@insertCode source="ExpressionsProperties"}
42   */
43  @Document(title = "Property Expressions", sortKey = "basics0060")
44  @DocCategory({ "basics" })
45  // @DocTopic(path="basics", step="60")
46  public class PropertyExpressionsTutorial
47  {
48    private ClasspathConfigurationProperties config; // NOPMD
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     * <p>
77     * The property <code>host</code> defaults to <code>localhost</code>.
78     * </p>
79     * {@insertCode}
80     */
81    @Test
82    @DocSection
83    public void simpleStringValue()
84    {
85      final String host = properties.host();
86      assertThat(host, is("localhost"));
87    }
88  
89    // CHECKSTYLE:OFF
90    /**
91     * <p>
92     * The property <code>port</code> defaults to <code>808</code>.
93     * </p>
94     * {@insertCode}
95     */
96    @Test
97    @DocSection
98    public void simpleIntValue()
99    {
100     final int port = properties.port();
101     assertThat(port, is(8080));
102   }
103 
104   // CHECKSTYLE:ON
105 
106   /**
107    * <p>
108    * The URL property of <code>homePageUrl</code> is per default constructed by
109    * <code>host</code> and <code>port</code>.
110    * </p>
111    * {@insertCode}
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    * <p>
124    * If the default values of properties are overridden, the expressions take
125    * the correct current value (as you would expect).
126    * </p>
127    * <p>
128    * The properties definition is this:
129    * </p>
130    * {@insertCode file="OverridingExpressionsProperties.properties"}
131    * <p>
132    * Here is how we access the home page URL property:
133    * </p>
134    * {@insertCode}
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    * <p>
147    * If the property is changed, the composed property also reflects the change
148    * (as one would expect). But the property has to be marked as
149    * {@link de.smartics.properties.api.core.annotations.AccessType#READ_WRITE}.
150    * </p>
151    * {@insertCode}
152    * <p/>
153    * <div class="note"> It would be more elegant and less error prone, if the
154    * key is fetched from the property set as shown in the next example. </div>
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    * <p>
168    * So here is the example where we change a <i>read-write</i> property, using
169    * a properties key.
170    * </p>
171    * {@insertCode}
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 }