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.key;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.is;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import de.smartics.projectdoc.annotations.DocCategory;
25  import de.smartics.projectdoc.annotations.Document;
26  import de.smartics.projectdoc.annotations.topic.DocChapter;
27  import de.smartics.properties.api.core.domain.PropertyKey;
28  import de.smartics.properties.impl.config.classpath.ClasspathConfigurationProperties;
29  import de.smartics.properties.impl.config.classpath.ClasspathConfigurationPropertiesFactory;
30  
31  /**
32   * This tutorial shows how to set the name of a property by overriding the
33   * default name derived from the method name. This technique is useful if the
34   * syntax of your properties cannot be reflected in Java method names.
35   */
36  @Document(title = "Naming Property Keys", sortKey = "basics0073")
37  @DocCategory({ "basics" })
38  // @DocTopic(path="basics", step="73")
39  public class PropertyKeyNamingTutorial
40  {
41    private ClasspathConfigurationProperties config; // NOPMD
42  
43    private KeyNameProperties properties;
44  
45    @Before
46    public void setUp()
47    {
48      config = createConfiguration();
49      properties = config.getProperties(KeyNameProperties.class);
50    }
51  
52    private static ClasspathConfigurationProperties createConfiguration()
53    {
54      final ClasspathConfigurationPropertiesFactory factory =
55          new ClasspathConfigurationPropertiesFactory();
56      final ClasspathConfigurationProperties config = factory.create();
57      config.addClassPathProperties(KeyNameProperties.class);
58      return config;
59    }
60  
61    /**
62     * {@insertCode source="KeyNameProperties"}
63     * <p>
64     * Instead of the default name <code>storyPoint</code>, the name is
65     * <code>story-point</code>.
66     * </p>
67     * {@insertCode}
68     */
69    @DocChapter
70    @Test
71    public void declarationOfAProperty()
72    {
73      final PropertyKey key = properties.storyPointPropertyKey();
74      final Integer value = properties.storyPoint();
75  
76      assertThat(key.toString(), is("tutorial.property.key.story-point"));
77      assertThat(key.getName(), is("story-point"));
78      assertThat(key.getPropertySet(), is("tutorial.property.key"));
79      assertThat(value, is(1));
80    }
81  }