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.List;
21  
22  import javax.validation.constraints.NotNull;
23  
24  import org.hibernate.validator.constraints.NotBlank;
25  import org.junit.Test;
26  
27  import de.smartics.properties.api.core.domain.PropertyConstraint;
28  import de.smartics.properties.api.core.domain.PropertyDescriptor;
29  import de.smartics.properties.api.core.domain.PropertyExpression;
30  import de.smartics.properties.api.core.domain.PropertyKey;
31  import de.smartics.properties.api.core.domain.PropertyType;
32  import de.smartics.properties.spi.core.constraint.jsr303.GenericPropertyConstraint;
33  import de.smartics.properties.spi.core.metadata.PropertyMetaDataParser;
34  import de.smartics.testdoc.annotations.Uut;
35  
36  /**
37   * Tests {@link de.smartics.properties.spi.core.metadata.PropertyMetaDataParser} with
38   * mandatory properties.
39   */
40  @Uut(type = PropertyMetaDataParser.class)
41  public class PropertyMetaDataParserConstraintsTest extends
42      AbstractPropertyMetaDataParserTestBase
43  { // NOPMD
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    private static final NotNull NOT_NULL;
49  
50    private static final NotBlank NOT_BLANK;
51  
52    // --- members --------------------------------------------------------------
53  
54    // ****************************** Inner Classes *****************************
55  
56    public interface TestProperties
57    {
58      @NotNull
59      @NotBlank
60      String property();
61    }
62  
63    // ********************************* Methods ********************************
64  
65    // --- prepare --------------------------------------------------------------
66  
67    // --- helper ---------------------------------------------------------------
68  
69    static
70    {
71      try
72      {
73        NOT_NULL =
74            TestProperties.class.getDeclaredMethod("property").getAnnotation(// NOPMD
75                NotNull.class);
76        NOT_BLANK =
77            TestProperties.class.getDeclaredMethod("property").getAnnotation(// NOPMD
78                NotBlank.class);
79      }
80      catch (final Exception e)
81      {
82        throw new IllegalStateException("Cannot determine annotation.");
83      }
84    }
85  
86    // --- tests ----------------------------------------------------------------
87  
88    @Test
89    public void recognizesConstraints()
90    {
91      final Method method = fetchMethod(TestProperties.class, "property");
92      final PropertyDescriptor descriptor = uut.readDescriptor(method);
93  
94      final PropertyKey expectedKey =
95          new PropertyKey(TestProperties.class.getName(), "property");
96      final PropertyType expectedType = new PropertyType(String.class);
97      final PropertyConstraint<?> notNull =
98          new GenericPropertyConstraint<NotNull, String>(NOT_NULL, String.class);
99      final PropertyConstraint<?> notBlank =
100         new GenericPropertyConstraint<NotBlank, String>(NOT_BLANK, String.class);
101     final List<PropertyConstraint<?>> expectedConstraints =
102         new ArrayList<PropertyConstraint<?>>(2);
103     expectedConstraints.add(notNull);
104     expectedConstraints.add(notBlank);
105 
106     checkThat(descriptor, expectedKey, expectedType, true,
107         PropertyExpression.NO_EXPRESSION, null, expectedConstraints);
108   }
109 }