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 introduces shows how to declare a property whose property key
33   * is required at runtime.
34   */
35  @Document(title = "Accessing Property Keys", sortKey = "basics0070")
36  @DocCategory({ "basics" })
37  // @DocTopic(path="basics", step="70")
38  public class PropertyKeyAccessTutorial
39  {
40    private ClasspathConfigurationProperties config; // NOPMD
41  
42    private DescriptorAndKeyProperties properties;
43  
44    @Before
45    public void setUp()
46    {
47      config = createConfiguration();
48      properties = config.getProperties(DescriptorAndKeyProperties.class);
49    }
50  
51    private static ClasspathConfigurationProperties createConfiguration()
52    {
53      final ClasspathConfigurationPropertiesFactory factory =
54          new ClasspathConfigurationPropertiesFactory();
55      final ClasspathConfigurationProperties config = factory.create();
56      config.addClassPathProperties(DescriptorAndKeyProperties.class);
57      return config;
58    }
59  
60    /**
61     * <p>
62     * We have shown how the descriptor of a property can be made available in the
63     * {@link de.smartics.properties.tutorial.property.PropertyTutorial}. We also
64     * mentioned that a property key can be retrieved without the need of fetching
65     * the whole property descriptor. Now let's look at how to declare this!
66     * </p>
67     * <p>
68     * As you may have anticipated, declaring access to a property is quite simple
69     * and analogous to declaring access to a property descriptor.
70     * </p>
71     * {@insertCode source="DescriptorAndKeyProperties"}
72     * <p>
73     * Accessing the property key information is also straight forward:
74     * </p>
75     * {@insertCode}
76     */
77    @DocChapter
78    @Test
79    public void declarationOfAProperty()
80    {
81      final PropertyKey key = properties.hostPropertyKey();
82  
83      assertThat(key.getPropertySet(), is("tutorial.property.key"));
84      assertThat(key.getName(), is("host"));
85    }
86  }