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 test.de.smartics.properties.spi.core.metadata;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.equalTo;
20  import static org.hamcrest.Matchers.is;
21  
22  import java.lang.reflect.Method;
23  import java.util.List;
24  
25  import org.junit.Test;
26  
27  import de.smartics.properties.api.core.annotations.AccessType;
28  import de.smartics.properties.api.core.annotations.PropertyDefinitionTime;
29  import de.smartics.properties.api.core.annotations.PropertyLifecycle;
30  import de.smartics.properties.api.core.annotations.PropertySet;
31  import de.smartics.properties.api.core.domain.PropertyDescriptor;
32  import de.smartics.properties.spi.core.metadata.PropertyMetaDataParser;
33  import de.smartics.testdoc.annotations.Uut;
34  
35  /**
36   * Tests {@link de.smartics.properties.spi.core.metadata.PropertyMetaDataParser}
37   * with {@link PropertyLifecycle} annotations.
38   */
39  @Uut(type = PropertyMetaDataParser.class)
40  public class PropertyMetaDataParserMetaDataTest extends
41      AbstractPropertyMetaDataParserTestBase
42  { // NOPMD
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    private static final long SOME_MILLIS = 128L;
48  
49    // --- members --------------------------------------------------------------
50  
51    // ****************************** Inner Classes *****************************
52  
53    @PropertySet
54    public interface TestProperties
55    {
56      String noAnnotation();
57  
58      @PropertyLifecycle
59      String defaultMetaData();
60  
61      @PropertyLifecycle(definitionTime = PropertyDefinitionTime.RUN,
62          access = AccessType.READ_WRITE, updateInterval = SOME_MILLIS)
63      String allSpecifed();
64  
65      String allSpecifiedPropertyKey();
66    }
67  
68    // ********************************* Methods ********************************
69  
70    // --- prepare --------------------------------------------------------------
71  
72    // --- helper ---------------------------------------------------------------
73  
74    private PropertyDescriptor runTest(final String methodNameAndPropertyName)
75    {
76      final Method method =
77          fetchMethod(TestProperties.class, methodNameAndPropertyName);
78      final PropertyDescriptor descriptor = uut.readDescriptor(method);
79      return descriptor;
80    }
81  
82    // --- tests ----------------------------------------------------------------
83  
84    @Test
85    public void propertyMetaDataAnnotationIsNotRequired()
86    {
87      final PropertyDescriptor descriptor = runTest("noAnnotation");
88  
89      assertThat(descriptor.getConfigurationTime(),
90          is(equalTo(PropertyDefinitionTime.STARTUP)));
91      assertThat(descriptor.getUpdateIntervalInMs(), is(equalTo(-1L)));
92      assertThat(descriptor.isRuntimeMutable(), is(equalTo(false)));
93    }
94  
95    @Test
96    public void propertyMetaDataAnnotationProvidesDefaults()
97    {
98      final PropertyDescriptor descriptor = runTest("defaultMetaData");
99  
100     assertThat(descriptor.getConfigurationTime(),
101         is(equalTo(PropertyDefinitionTime.STARTUP)));
102     assertThat(descriptor.getUpdateIntervalInMs(), is(equalTo(-1L)));
103     assertThat(descriptor.isRuntimeMutable(), is(equalTo(false)));
104   }
105 
106   @Test
107   public void propertyMetaDataAnnotationFromType()
108   {
109     final List<PropertyDescriptor> descriptors =
110         uut.readDescriptors(TestProperties.class);
111 
112     assertThat(descriptors.size(), is(equalTo(3)));
113   }
114 
115   @Test
116   public void propertyMetaDataAnnotationAllowsToProvideConfigurationTimeAndUpdateInterval()
117   {
118     final PropertyDescriptor descriptor = runTest("allSpecifed");
119 
120     assertThat(descriptor.getConfigurationTime(),
121         is(equalTo(PropertyDefinitionTime.RUN)));
122     assertThat(descriptor.getUpdateIntervalInMs(), is(equalTo(SOME_MILLIS)));
123     assertThat(descriptor.isRuntimeMutable(), is(equalTo(true)));
124   }
125 }