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.constraint;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.equalTo;
20  import static org.hamcrest.Matchers.is;
21  import static org.junit.Assert.fail;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.when;
24  
25  import java.util.Locale;
26  
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import de.smartics.properties.api.core.domain.PropertyDescriptor;
31  import de.smartics.properties.api.core.domain.PropertyKey;
32  import de.smartics.properties.api.core.domain.PropertyValidationException;
33  import de.smartics.properties.api.core.domain.PropertyValidationMessageBean;
34  import de.smartics.properties.spi.core.constraint.AbstractPropertyConstraint;
35  import de.smartics.testdoc.annotations.Uut;
36  
37  /**
38   * Tests {@link AbstractPropertConstraint}.
39   */
40  @Uut(type = AbstractPropertyConstraint.class)
41  public class AbstractPropertConstraintTest
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    private static final String VALID = "valid";
48  
49    // --- members --------------------------------------------------------------
50  
51    private TestPropertyConstraint uut;
52  
53    // ****************************** Inner Classes *****************************
54  
55    private static final class TestPropertyConstraint extends
56        AbstractPropertyConstraint<String>
57    {
58      private static final long serialVersionUID = 1L;
59  
60      @Override
61      public String getDescription(final Locale locale)
62      {
63        return "test";
64      }
65  
66      @Override
67      public boolean isValid(final String value, final Class<?> group)
68      {
69        return VALID.equals(value);
70      }
71  
72      @Override
73      public boolean isMandatoryConstraint()
74      {
75        return false;
76      }
77    }
78  
79    // ********************************* Methods ********************************
80  
81    // --- prepare --------------------------------------------------------------
82  
83    @Before
84    public void setUp()
85    {
86      uut = new TestPropertyConstraint();
87    }
88  
89    // --- helper ---------------------------------------------------------------
90  
91    // --- tests ----------------------------------------------------------------
92  
93    @Test
94    public void validatesValues()
95    {
96      final PropertyKey key = new PropertyKey("key");
97      final PropertyDescriptor descriptor = mock(PropertyDescriptor.class);
98      when(descriptor.getKey()).thenReturn(key);
99  
100     final String invalidValue = "INVALID";
101     try
102     {
103       uut.validate(descriptor, invalidValue);
104       fail("Expected validation exception not thrown.");
105     }
106     catch (final PropertyValidationException e)
107     {
108       final PropertyValidationMessageBean message = e.getMessageBean();
109       assertThat(message.getPropertyKey(), is(equalTo(key)));
110       assertThat((String) message.getValue(), is(equalTo(invalidValue)));
111     }
112   }
113 
114   @Test
115   public void detectsInvalidValues()
116   {
117     final PropertyKey key = new PropertyKey("key");
118     final PropertyDescriptor descriptor = mock(PropertyDescriptor.class);
119     when(descriptor.getKey()).thenReturn(key);
120     uut.validate(descriptor, VALID);
121   }
122 }