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.list;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.contains;
20  
21  import java.net.URL;
22  import java.util.List;
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.DocChapter;
30  import de.smartics.projectdoc.annotations.topic.DocSection;
31  import de.smartics.properties.api.config.domain.ConfigurationProperties;
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 declare properties for list values.
37   * <p>
38   * List of properties are defined like this:
39   * </p>
40   * {@insertCode file="ListProperties.properties"}
41   */
42  @Document(title = "List Properties", sortKey = "basics0100")
43  @DocCategory({ "basics" })
44  // @DocTopic(path="basics", step="100")
45  public class ListPropertyTutorial
46  {
47    private ConfigurationProperties config; // NOPMD
48  
49    private ListProperties properties;
50  
51    @Before
52    public void setUp()
53    {
54      config = createConfiguration();
55      properties = config.getProperties(ListProperties.class);
56    }
57  
58    private static ConfigurationProperties createConfiguration()
59    {
60      final ClasspathConfigurationPropertiesFactory factory =
61          new ClasspathConfigurationPropertiesFactory();
62      final ClasspathConfigurationProperties config = factory.create();
63      config.addClassPathProperties(ListProperties.class);
64      return config;
65    }
66  
67    /**
68     * <p>
69     * Lists of strings are declared like this:
70     * </p>
71     * {@insertCode source="ListProperties#listOfStrings()"}
72     * <p>
73     * Accessing a list of values is straight forward.
74     * </p>
75     * {@insertCode}
76     */
77    @Test
78    @DocChapter
79    public void propertyWithValueOfTypeListOfString()
80    {
81      final List<String> list = properties.listOfStrings();
82      assertThat(list, contains("one", "two", "three")); // NOPMD
83    }
84  
85    /**
86     * <p>
87     * Lists of integers are declared like this:
88     * </p>
89     * {@insertCode source="ListProperties#listOfInts()"}
90     * <p>
91     * Accessing a list of values is straight forward.
92     * </p>
93     * {@insertCode}
94     */
95    @Test
96    @DocChapter
97    public void propertyWithValueOfTypeListOfInteger()
98    {
99      final List<Integer> list = properties.listOfInts();
100     assertThat(list, contains(1, 2, 3));
101   }
102 
103   /**
104    * <p>
105    * The elements are split by any non-alphanumeric character.
106    * </p>
107    */
108   @Test
109   @DocChapter
110   public void separators()
111   {
112     final List<String> list = properties.listOfStrings();
113     assertThat(list, contains("one", "two", "three"));
114   }
115 
116   /**
117    * <p>
118    * Therefore the pipe is also a valid delimiter.
119    * </p>
120    */
121   @Test
122   @DocSection
123   public void pipeAsSeparator()
124   {
125     final List<String> list = properties.pipedListOfStrings();
126     assertThat(list, contains("one", "two", "three"));
127   }
128 
129   /**
130    * <p>
131    * Therefore the pipe is also a valid delimiter.
132    * </p>
133    */
134   @Test
135   @DocSection
136   public void blankAsSeparator()
137   {
138     final List<String> list = properties.blankedListOfStrings();
139     assertThat(list, contains("one", "two", "three"));
140   }
141 
142   /**
143    * <p>
144    * Every non-alpha-numeric character serves as a delimiter. We use Apache's
145    * <a href="http://commons.apache.org/beanutils/">common-beanutils</a> for
146    * the conversion of values.
147    * </p>
148    * <p>
149    * As you can see in the properties file, elements that contain spaces or a
150    * separator are set in double quotes.
151    * </p>
152    */
153   @Test
154   @DocSection
155   public void propertyWithSpecialStrings()
156   {
157     final List<String> list = properties.extremeListOfStrings();
158     assertThat(list,
159         contains("one", "two two", "three three three", "comma',", "\"hey!\""));
160   }
161 
162   /**
163    * <p>
164    * Lists of URLs are declared like this:
165    * </p>
166    * {@insertCode source="ListProperties#listOfUrls()"}
167    * <p>
168    * Accessing a list of values is straight forward.
169    * </p>
170    * {@insertCode}
171    * <p>
172    * Since a comma is a valid character in an URL, the comma is not a valid
173    * separator to specify URLs. The default separation character is the pipe
174    * (<code>|</code>). Therefore URLs are best separated one on each line with
175    * a leading pipe.
176    */
177   @Test
178   @DocChapter
179   public void propertyWithValueOfTypeListOfUrl() throws Exception
180   {
181     final List<URL> list = properties.listOfUrls();
182     assertThat(
183         list,
184         contains(new URL("http://www.example.com/index.html"), new URL(
185             "http://www.example.com/products.html"), new URL(
186             "http://www.example.com/news.html")));
187   }
188 
189   /**
190    * <p>
191    * But you may also separate URLs by whitespaces alone.
192    * </p>
193    */
194   @DocSection
195   public void urlsSeparatedByNewlines() throws Exception
196   {
197     final List<URL> list = properties.listOfNewLineUrls();
198     assertThat(
199         list,
200         contains(new URL("http://www.example.com/index.html"), new URL(
201             "http://www.example.com/products.html"), new URL(
202             "http://www.example.com/news.html")));
203   }
204 
205   /**
206    * <p>
207    * Lists of enums are declared like this:
208    * </p>
209    * {@insertCode source="ListProperties#listOfPriorities()"}
210    * <p>
211    * Accessing a list of values is straight forward.
212    * </p>
213    * {@insertCode}
214    */
215   @Test
216   @DocChapter
217   public void propertyWithValueOfTypeListOfEnums()
218   {
219     final List<Priority> list = properties.listOfPriorities();
220     assertThat(
221         list,
222         contains(Priority.REQUIRED, Priority.VERY_IMPORTANT,
223             Priority.NICE_TO_HAVE));
224   }
225 }