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 java.lang.reflect.Method;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.junit.Test;
24  import org.junit.experimental.categories.Category;
25  
26  import de.smartics.properties.api.core.domain.PropertyConstraint;
27  import de.smartics.properties.api.core.domain.PropertyDescriptor;
28  import de.smartics.properties.api.core.domain.PropertyExpression;
29  import de.smartics.properties.api.core.domain.PropertyKey;
30  import de.smartics.properties.api.core.domain.PropertyType;
31  import de.smartics.properties.api.core.domain.PropertyValueRange;
32  import de.smartics.properties.spi.core.constraints.PropertyRangeConstraint;
33  import de.smartics.properties.spi.core.metadata.PropertyMetaDataParser;
34  import de.smartics.properties.spi.core.value.EnumeratedPropertyValueRange;
35  import de.smartics.testdoc.annotations.Uut;
36  import de.smartics.testdoc.categories.Technical;
37  
38  /**
39   * Tests {@link de.smartics.properties.spi.core.metadata.PropertyMetaDataParser} with
40   * properties defined in an interface without any annotations.
41   */
42  @Uut(type = PropertyMetaDataParser.class)
43  public class PropertyMetaDataParserNoAnnotationsTest extends
44      AbstractPropertyMetaDataParserTestBase
45  { // NOPMD
46    // ********************************* Fields *********************************
47  
48    // --- constants ------------------------------------------------------------
49  
50    // --- members --------------------------------------------------------------
51  
52    // ****************************** Inner Classes *****************************
53  
54    public interface TestProperties
55    {
56      String stringProperty();
57  
58      int primitiveProperty();
59  
60      EnumValue enumProperty();
61  
62      String getStringProperty();
63  
64      String get();
65  
66      String getI();
67  
68      String getter();
69    }
70  
71    // ********************************* Methods ********************************
72  
73    // --- prepare --------------------------------------------------------------
74  
75    // --- helper ---------------------------------------------------------------
76  
77    private void runTechnicalTest(final String methodName,
78        final String propertyName)
79    {
80      final Method method = fetchMethod(TestProperties.class, methodName);
81      final PropertyDescriptor descriptor = uut.readDescriptor(method);
82  
83      final PropertyKey expectedKey =
84          new PropertyKey(TestProperties.class.getName(), propertyName);
85      final PropertyType expectedType = new PropertyType(String.class);
86      final List<PropertyConstraint<?>> expectedConstraints =
87          Collections.emptyList();
88  
89      checkThat(descriptor, expectedKey, expectedType,
90          PropertyExpression.NO_EXPRESSION, null, expectedConstraints);
91    }
92  
93    // --- tests ----------------------------------------------------------------
94  
95    @Test
96    public void parsesStringPropertiesWithoutAnyAnnotations()
97    {
98      final Method method = fetchMethod(TestProperties.class, "stringProperty"); // NOPMD
99      final PropertyDescriptor descriptor = uut.readDescriptor(method);
100 
101     final PropertyKey expectedKey =
102         new PropertyKey(TestProperties.class.getName(), "stringProperty");
103     final PropertyType expectedType = new PropertyType(String.class);
104     final List<PropertyConstraint<?>> expectedConstraints =
105         Collections.emptyList();
106 
107     checkThat(descriptor, expectedKey, expectedType,
108         PropertyExpression.NO_EXPRESSION, null, expectedConstraints);
109   }
110 
111   @Test
112   public void parsesPrimitivePropertiesWithoutAnyAnnotations()
113   {
114     final Method method =
115         fetchMethod(TestProperties.class, "primitiveProperty");
116     final PropertyDescriptor descriptor = uut.readDescriptor(method);
117 
118     final PropertyKey expectedKey =
119         new PropertyKey(TestProperties.class.getName(), "primitiveProperty");
120     final PropertyType expectedType = new PropertyType(int.class);
121     final List<PropertyConstraint<?>> expectedConstraints =
122         Collections.emptyList();
123 
124     checkThat(descriptor, expectedKey, expectedType,
125         PropertyExpression.NO_EXPRESSION, null, expectedConstraints);
126   }
127 
128   @Test
129   public void parsesEnumPropertiesWithoutAnyAnnotations()
130   {
131     final Method method = fetchMethod(TestProperties.class, "enumProperty");
132     final PropertyDescriptor descriptor = uut.readDescriptor(method);
133 
134     final PropertyKey expectedKey =
135         new PropertyKey(TestProperties.class.getName(), "enumProperty");
136     final PropertyType expectedType = new PropertyType(EnumValue.class);
137     final PropertyValueRange<EnumValue> expectedRange =
138         new EnumeratedPropertyValueRange<EnumValue>(EnumValue.class);
139     final PropertyRangeConstraint<EnumValue> contraint =
140         new PropertyRangeConstraint<EnumValue>(expectedRange);
141     final List<PropertyConstraint<?>> expectedConstraints =
142         new ArrayList<PropertyConstraint<?>>(1);
143     expectedConstraints.add(contraint);
144 
145     checkThat(descriptor, expectedKey, expectedType,
146         PropertyExpression.NO_EXPRESSION, expectedRange, expectedConstraints);
147   }
148 
149   @Test
150   public void removesGetFromPropertiesName()
151   {
152     runTechnicalTest("getStringProperty", "stringProperty");
153   }
154 
155   @Test
156   @Category(Technical.class)
157   public void allowsMethodToBeNamedGet()
158   {
159     runTechnicalTest("get", "get");
160   }
161 
162   @Test
163   @Category(Technical.class)
164   public void allowsPropertyToHaveOnlyOneChar()
165   {
166     runTechnicalTest("getI", "i");
167   }
168 
169   @Test
170   @Category(Technical.class)
171   public void doesNotConfuseMethodsStartingWithGetWithPropertyGetter()
172   {
173     runTechnicalTest("getter", "getter");
174   }
175 }