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.api.core.domain;
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.mockito.Mockito.mock;
22  import static org.mockito.Mockito.when;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.lang.ObjectUtils;
28  import org.junit.Test;
29  
30  import de.smartics.properties.api.core.domain.PropertyConstraint;
31  import de.smartics.properties.api.core.domain.PropertyDescriptor;
32  import de.smartics.properties.api.core.domain.PropertyKey;
33  import de.smartics.properties.api.core.domain.PropertyValidationException;
34  import de.smartics.properties.api.core.domain.PropertyValidationMessageBean;
35  import de.smartics.testdoc.annotations.Uut;
36  
37  /**
38   * Tests {@link PropertySecurityException}.
39   */
40  @Uut(type = PropertyValidationException.class)
41  public class PropertyValidationExceptionTest
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    // --- members --------------------------------------------------------------
48  
49    // ****************************** Inner Classes *****************************
50  
51    // ********************************* Methods ********************************
52  
53    // --- prepare --------------------------------------------------------------
54  
55    // --- helper ---------------------------------------------------------------
56  
57    // --- tests ----------------------------------------------------------------
58  
59    @Test
60    public void allowsToAddSerializableValue()
61    {
62      final Throwable cause = new NullPointerException("Test");
63      final PropertyKey key = new PropertyKey("key");
64      final PropertyDescriptor descriptor = mock(PropertyDescriptor.class);
65      when(descriptor.getKey()).thenReturn(key);
66  
67      final List<? extends PropertyConstraint<?>> constraints =
68          new ArrayList<PropertyConstraint<?>>();
69      final String value = "test";
70      final PropertyValidationException uut =
71          new PropertyValidationException(new PropertyValidationMessageBean(
72              cause, descriptor, constraints, value));
73  
74      final PropertyValidationMessageBean message = uut.getMessageBean();
75      assertThat((String) message.getValue(), is(equalTo(value)));
76      assertThat(message.getPropertyKey(), is(equalTo(key)));
77      assertThat(message.getCause(), is(equalTo(cause)));
78      assertThat(ObjectUtils.equals(message.getConstraints(), constraints),
79          is(equalTo(true)));
80    }
81  
82    @Test
83    public void allowsToAddNonSerializableValueStoredInStringRepresentation()
84    {
85      final String stringRepresentation = "TEST";
86      final PropertyKey key = new PropertyKey("key");
87      final PropertyDescriptor descriptor = mock(PropertyDescriptor.class);
88      when(descriptor.getKey()).thenReturn(key);
89      final Object value = new Object()
90      {
91        @Override
92        public String toString()
93        {
94          return stringRepresentation;
95        }
96      };
97  
98      final PropertyValidationException uut =
99          new PropertyValidationException(new PropertyValidationMessageBean(
100             descriptor, null, value));
101 
102     final PropertyValidationMessageBean message = uut.getMessageBean();
103     assertThat((String) message.getValue(), is(equalTo(stringRepresentation)));
104   }
105 }