1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
40 @Uut(type = AbstractPropertyConstraint.class)
41 public class AbstractPropertConstraintTest
42 {
43
44
45
46
47 private static final String VALID = "valid";
48
49
50
51 private TestPropertyConstraint uut;
52
53
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
80
81
82
83 @Before
84 public void setUp()
85 {
86 uut = new TestPropertyConstraint();
87 }
88
89
90
91
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 }