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.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   * This tutorial provides information about putting constraints on lists and
37   * list elements.
38   */
39  @Document(title = "List Properties Constraints", sortKey = "basics0110")
40  @DocCategory({ "basics" })
41  // @DocTopic(path="basics", step="110")
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     * <p>
66     * Constraints on lists and their elements are declared like this:
67     * </p>
68     * {@insertCode source="ListPropertiesWithConstraints"}
69     * <p>
70     * Accessing a list of values is straight forward.
71     * </p>
72     * {@insertCode}
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     * <p>
84     * This example simply shows, that the constraint on a list as whole works.
85     * </p>
86     * {@insertCode}
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     * <p>
99     * And this example simply shows, that the constraints on the elements work.
100    * </p>
101    * {@insertCode}
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 }