1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.resolve;
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 org.junit.Before;
25 import org.junit.Test;
26
27 import de.smartics.properties.api.config.domain.UnknownPropertyException;
28 import de.smartics.testdoc.annotations.Uut;
29
30
31
32
33 public class ResolverTest
34 {
35
36
37
38
39
40
41
42 private static final String VALUE_1 = "v1";
43
44
45
46 @Uut
47 private Resolver uut;
48
49
50
51
52
53
54
55 @Before
56 public void setUp()
57 {
58 final ResolveContext context = mock(ResolveContext.class);
59 when(context.get("component.name1")).thenReturn(VALUE_1);
60 when(context.get("component.name2")).thenReturn("X${component.name1}");
61 when(context.get("illegal.recursion")).thenReturn("${illegal.recursion}");
62 when(context.get("illegal.recursion1")).thenReturn("${illegal.recursion2}");
63 when(context.get("illegal.recursion2")).thenReturn("${illegal.recursion1}");
64 when(context.get("illegal.unknown")).thenReturn("${unknown}");
65 when(context.get("illegal.null")).thenReturn("${null}");
66 when(context.get("illegal.unknownInDepth"))
67 .thenReturn("${illegal.unknown}");
68 when(context.get("unknown")).thenThrow(
69 new UnknownPropertyException(null, "unknown"));
70 when(context.get("null")).thenReturn(null);
71
72 uut = new Resolver(context);
73 }
74
75
76
77
78
79 @Test
80 public void resolvesValueWithoutPlaceholders()
81 {
82 final String expression = "test";
83 final String resolved = uut.resolve(expression);
84 assertThat(resolved, is(equalTo(expression)));
85 }
86
87 @Test(expected = UnknownPropertyException.class)
88 public void resolvesWithUnknownPlaceholder()
89 {
90 final String expression = "${unknown}";
91 final String resolved = uut.resolve(expression);
92 assertThat(resolved, is(equalTo(VALUE_1)));
93 }
94
95 @Test
96 public void resolvesValueWithOnePlaceholder()
97 {
98 final String expression = "${component.name1}";
99 final String resolved = uut.resolve(expression);
100 assertThat(resolved, is(equalTo(VALUE_1)));
101 }
102
103 @Test
104 public void resolvesValueWithMultiplePlaceholders()
105 {
106 final String expression = "$${component.name1} ${component.name1}${";
107 final String resolved = uut.resolve(expression);
108 assertThat(resolved, is(equalTo('$' + VALUE_1 + ' ' + VALUE_1 + "${")));
109 }
110
111 @Test
112 public void resolvesRecursively()
113 {
114 final String expression = "${component.name2}";
115 final String resolved = uut.resolve(expression);
116 assertThat(resolved, is(equalTo('X' + VALUE_1)));
117 }
118
119 @Test(expected = RecursivePropertyException.class)
120 public void detectsDirectInfiniteRecursions()
121 {
122 final String expression = "${illegal.recursion}";
123 uut.resolve(expression);
124 }
125
126 @Test(expected = RecursivePropertyException.class)
127 public void detectsInfiniteRecursions()
128 {
129 final String expression = "${illegal.recursion1}";
130 uut.resolve(expression);
131 }
132
133 @Test(expected = UnknownPropertyException.class)
134 public void detectsReferencesToUnknownPlaceholder()
135 {
136 final String expression = "${illegal.unknown}";
137 uut.resolve(expression);
138 }
139
140 @Test(expected = UnknownPropertyException.class)
141 public void detectsReferencesToUnknownPlaceholderInDepth()
142 {
143 final String expression = "${illegal.unknownInDepth}";
144 uut.resolve(expression);
145 }
146
147 @Test(expected = UnresolvablePropertyException.class)
148 public void detectsReferencedNullValue()
149 {
150 final String expression = "${illegal.null}";
151 uut.resolve(expression);
152 }
153 }