This project has retired. For details please refer to its
Attic page.
SettingsTest xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23.configuration;
19
20 import org.junit.Test;
21
22 import java.lang.reflect.ParameterizedType;
23 import java.lang.reflect.Type;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertNotSame;
32 import static org.junit.Assert.assertNull;
33 import static org.junit.Assert.assertSame;
34 import static org.junit.Assert.assertTrue;
35 import static org.junit.Assert.fail;
36
37 @SuppressWarnings("ResultOfMethodCallIgnored")
38 public class SettingsTest {
39
40 @Test
41 public void testNonNullSetting() {
42 Setting<String> nonNull = Setting.create("nulltest", "A nonnull string");
43 try {
44 nonNull.withValue(null);
45 fail();
46 } catch (IllegalArgumentException e) {
47
48 }
49 }
50
51 @Test
52 public void testNullableSetting() {
53 Setting<String> nullable = Setting.create("nulltest", (String) null);
54 assertNull(nullable.withValue(null).getValue());
55 }
56
57 @Test
58 public void testDuplicateIdentifiers() {
59 try {
60 Setting<String> first = Setting.create("foo", "");
61 Setting<String> second = Setting.create("foo", "");
62
63 Settings.of(first, second);
64
65 fail();
66 } catch (IllegalArgumentException e) {
67
68 }
69 }
70
71 @Test
72 public void testFind() {
73 Setting<String> key = Setting.create("foo", "key");
74 Setting<String> element = key.withValue("element");
75
76 Settings settings = Settings.of(element);
77
78 Optional<Setting<String>> actual = settings.find(key);
79
80 assertTrue(actual.isPresent());
81
82 assertSame(element, actual.get());
83
84 assertTrue(settings.contains(element));
85 assertFalse(settings.contains(key));
86 }
87
88 @Test
89 public void testGetPresentSetting() {
90 Setting<String> key = Setting.create("foo", "key");
91
92 Setting<String> actual = key.withValue("actual");
93 Settings settings = Settings.of(actual);
94
95 assertSame(actual.getValue(), settings.get(key));
96 }
97
98 @Test
99 public void testGetAbsentSetting() {
100 Setting<String> key = Setting.create("foo", "key");
101
102 Setting<String> actual = Setting.create("foo", "actual");
103 Settings settings = Settings.of(actual);
104
105 assertSame(key.getValue(), settings.get(key));
106 }
107
108 @Test
109 public void testGetNullSetting() {
110 Setting<String> baseKey = Setting.create("foo", (String) null);
111
112 Settings settings = Settings.of(baseKey);
113 assertNull(settings.get(baseKey.withValue("not null")));
114
115
116 baseKey.withValue("not null").withValue(null);
117 }
118
119 @Test
120 public void testSettingType() {
121 assertEquals(CharSequence.class, Setting.create("foo", CharSequence.class, "").getValueType());
122 assertEquals(CharSequence.class, new Setting<CharSequence>("foo", "") {
123 }.getValueType());
124
125 Type mapType = new Setting<Map<String, Integer>>("foo", Collections.emptyMap()) {
126 }.getValueType();
127
128 assertTrue(mapType instanceof ParameterizedType);
129 assertEquals("java.util.Map<java.lang.String, java.lang.Integer>", mapType.getTypeName());
130
131 class Key0<Bar, V> extends Setting<V> {
132 Key0() {
133 super("foo", null);
134 }
135 }
136
137 class Key2<Baz, V, Bar> extends Key0<V, Bar> {
138 }
139
140 class Key3<V> extends Key2<Boolean, Integer, List<Optional<String>>> {
141 }
142
143 class Key4 extends Key3<Boolean> {
144 }
145
146 Type complicatedType = new Key4().withValue(Collections.emptyList()).getValueType();
147
148 assertTrue(complicatedType instanceof ParameterizedType);
149 assertEquals("java.util.List<java.util.Optional<java.lang.String>>", complicatedType.getTypeName());
150
151 class Key3Simple<V> extends Key2<Boolean, Integer, String> {
152 }
153
154 class Key4Simple extends Key3Simple<Boolean> {
155 }
156
157 Type simpleType = new Key4Simple().withValue("").getValueType();
158
159 assertEquals(String.class, simpleType);
160 }
161
162 @Test
163 public void testCustomValueChecking() {
164 class PositiveIntegerSetting extends Setting<Integer> {
165 private PositiveIntegerSetting(String identifier, Integer defaultValue) {
166 super(identifier, defaultValue);
167 }
168
169 @Override
170 protected void checkValue(Integer newValue) throws Exception {
171 if (newValue < 0) {
172 throw new NumberFormatException();
173 }
174 }
175
176 @Override
177 public String toString() {
178 return getValue().toString();
179 }
180 }
181
182 Setting<Integer> setting = new PositiveIntegerSetting("foo", 0);
183
184 assertNotSame(setting, setting.withValue(0));
185 assertEquals(setting, setting.withValue(0));
186
187 setting = setting.withValue(5).withValue(6).withValue(7);
188
189 assertEquals(setting.getClass(), PositiveIntegerSetting.class);
190 assertEquals(setting.toString(), "7");
191
192 try {
193 setting.withValue(-1);
194 fail();
195 } catch (IllegalArgumentException e) {
196 assertTrue(e.getCause() instanceof NumberFormatException);
197 }
198 }
199
200 @Test
201 @SuppressWarnings("unchecked")
202 public void testBadSetting() {
203 try {
204 new Setting("foo", null) {
205 };
206 fail();
207 } catch (IllegalArgumentException e) {
208
209 }
210
211 try {
212 new Setting<Integer>(null, null) {
213 };
214 fail();
215 } catch (IllegalArgumentException e) {
216
217 }
218
219 try {
220 Setting.create(null, 0);
221 fail();
222 } catch (IllegalArgumentException e) {
223
224 }
225
226 try {
227 new Setting<Integer>(" ", null) {
228 };
229 fail();
230 } catch (IllegalArgumentException e) {
231
232 }
233
234 try {
235 Setting.create(" ", 0);
236 fail();
237 } catch (IllegalArgumentException e) {
238
239 }
240
241 try {
242 Setting.create("foo", List.class, null);
243 fail();
244 } catch (IllegalArgumentException e) {
245
246 }
247
248 try {
249 Setting.create("foo", boolean.class, null);
250 fail();
251 } catch (IllegalArgumentException e) {
252
253 }
254
255 try {
256 Setting.create("foo", Integer[].class, null);
257 fail();
258 } catch (IllegalArgumentException e) {
259
260 }
261
262 try {
263 new Setting<Integer[]>("foo", null) {
264 };
265 fail();
266 } catch (IllegalArgumentException e) {
267
268 }
269
270 try {
271 new Setting<List<Integer>[]>("foo", null) {
272 };
273 fail();
274 } catch (IllegalArgumentException e) {
275
276 }
277
278 class BadKeyCreator {
279 private <V> void badKey() {
280 new Setting<V>("foo", null) {
281 };
282 }
283 }
284
285 try {
286 new BadKeyCreator().badKey();
287 fail();
288 } catch (IllegalArgumentException e) {
289
290 }
291 }
292
293 }