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.Before;
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.api.core.domain.PropertyValueRange;
33  import de.smartics.properties.spi.core.metadata.PropertyMetaDataParser;
34  import de.smartics.testdoc.annotations.Uut;
35  
36  /**
37   * Base implementation for tests on {@link PropertyMetaDataParser}.
38   */
39  @Uut(type = PropertyMetaDataParser.class)
40  public abstract class AbstractPropertyMetaDataParserTestBase
41  {
42    // ********************************* Fields *********************************
43  
44    // --- constants ------------------------------------------------------------
45  
46    // --- members --------------------------------------------------------------
47  
48    protected PropertyMetaDataParser uut;
49  
50    // ****************************** Inner Classes *****************************
51  
52    // ********************************* Methods ********************************
53  
54    // --- prepare --------------------------------------------------------------
55  
56    @Before
57    public void setUp()
58    {
59      uut = PropertyMetaDataParser.createWithoutContextAccess();
60    }
61  
62    // --- helper ---------------------------------------------------------------
63  
64    protected static final Method fetchMethod(final Class<?> type,
65        final String methodName)
66    {
67      try
68      {
69        return type.getDeclaredMethod(methodName, new Class<?>[0]);
70      }
71      catch (final Exception e)
72      {
73        throw new IllegalStateException(String.format(
74            "No method named '%s' found in type '%s'.", methodName,
75            type.getName()));
76      }
77    }
78  
79    // CHECKSTYLE:OFF
80    @SuppressWarnings("rawtypes")
81    protected static final void checkThat(
82        // NOPMD
83        final PropertyDescriptor descriptor, final PropertyKey expectedKey,
84        final PropertyType expectedType,
85        final PropertyExpression expectedExpression,
86        final PropertyValueRange expectedRange, final List expectedConstraints)
87    {
88      // CHECKSTYLE:ON
89      checkThat(descriptor, expectedKey, expectedType, false, expectedExpression,
90          expectedRange, expectedConstraints);
91    }
92  
93    // CHECKSTYLE:OFF
94    @SuppressWarnings("rawtypes")
95    protected static final void checkThat(
96        // NOPMD
97        final PropertyDescriptor descriptor, final PropertyKey expectedKey,
98        final PropertyType expectedType, final boolean mandatory,
99        final PropertyExpression expectedExpression,
100       final PropertyValueRange expectedRange, final List expectedConstraints)
101   {
102     // CHECKSTYLE:ON
103     assertThat(descriptor.getKey(), is(equalTo(expectedKey)));
104     assertThat(descriptor.getType(), is(equalTo(expectedType)));
105     assertThat(descriptor.isMandatory(), is(equalTo(mandatory)));
106     assertThat(descriptor.getDefaultExpression().getExpression(),
107         is(equalTo(expectedExpression.getExpression())));
108     assertThat(descriptor.getValueRange(), is(equalTo(expectedRange)));
109 
110     final List<? extends PropertyConstraint<?>> actualConstraints =
111         descriptor.getConstraints();
112     assertThat(actualConstraints.size(),
113         is(equalTo(expectedConstraints.size())));
114     for (int i = actualConstraints.size() - 1; i >= 0; i--)
115     {
116       final PropertyConstraint actual = actualConstraints.get(i);
117       final PropertyConstraint expected =
118           (PropertyConstraint) expectedConstraints.get(i);
119       assertThat(actual.toString(), is(equalTo(expected.toString())));
120     }
121   }
122 
123   // --- tests ----------------------------------------------------------------
124 
125 }