|
@@ -0,0 +1,151 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2015 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.config;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
|
|
26
|
+
|
|
27
|
+import com.google.common.collect.Lists;
|
|
28
|
+
|
|
29
|
+import java.util.List;
|
|
30
|
+
|
|
31
|
+import org.junit.Before;
|
|
32
|
+import org.junit.Test;
|
|
33
|
+import org.junit.runner.RunWith;
|
|
34
|
+import org.mockito.Mock;
|
|
35
|
+import org.mockito.runners.MockitoJUnitRunner;
|
|
36
|
+
|
|
37
|
+import static org.junit.Assert.assertEquals;
|
|
38
|
+import static org.junit.Assert.assertNull;
|
|
39
|
+import static org.mockito.Mockito.when;
|
|
40
|
+
|
|
41
|
+@RunWith(MockitoJUnitRunner.class)
|
|
42
|
+public class ConfigValueRetrieverTest {
|
|
43
|
+
|
|
44
|
+ private static final String DOMAIN = "mydomain";
|
|
45
|
+ private static final String KEY = "mykey";
|
|
46
|
+
|
|
47
|
+ private static final String FALLBACK_DOMAIN = "otherdomain";
|
|
48
|
+ private static final String FALLBACK_KEY = "otherkey";
|
|
49
|
+
|
|
50
|
+ @Mock private ReadOnlyConfigProvider configProvider;
|
|
51
|
+ private ConfigValueRetriever retriever;
|
|
52
|
+
|
|
53
|
+ @Before
|
|
54
|
+ public void setUpRetriever() {
|
|
55
|
+ retriever = new ConfigValueRetriever(configProvider);
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ @Test
|
|
59
|
+ public void testRetrievesBasicString() {
|
|
60
|
+ when(configProvider.getOptionString(DOMAIN, KEY, true,
|
|
61
|
+ ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn("value!");
|
|
62
|
+
|
|
63
|
+ assertEquals("value!", retriever.getValue(String.class, DOMAIN, KEY));
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ @Test
|
|
67
|
+ public void testRetrievesStringWithFallback() {
|
|
68
|
+ when(configProvider.getOptionString(DOMAIN, KEY, true,
|
|
69
|
+ ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn(null);
|
|
70
|
+ when(configProvider.getOptionString(
|
|
71
|
+ DOMAIN, KEY, true, ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR,
|
|
72
|
+ FALLBACK_DOMAIN, FALLBACK_KEY))
|
|
73
|
+ .thenReturn("value!");
|
|
74
|
+
|
|
75
|
+ assertEquals("value!", retriever.getValue(String.class, DOMAIN, KEY, true,
|
|
76
|
+ FALLBACK_DOMAIN, FALLBACK_KEY));
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ @Test
|
|
80
|
+ public void testRetrievesStringWithRequiresFalse() {
|
|
81
|
+ when(configProvider.getOptionString(DOMAIN, KEY, false,
|
|
82
|
+ ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn(null);
|
|
83
|
+ when(configProvider.getOptionString(DOMAIN, KEY, true,
|
|
84
|
+ ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn("value!");
|
|
85
|
+
|
|
86
|
+ assertNull(retriever.getValue(String.class, DOMAIN, KEY, false));
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ @Test
|
|
90
|
+ public void testRetrievesStringWithRequiresTrue() {
|
|
91
|
+ when(configProvider.getOptionString(DOMAIN, KEY, false,
|
|
92
|
+ ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn(null);
|
|
93
|
+ when(configProvider.getOptionString(DOMAIN, KEY, true,
|
|
94
|
+ ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn("value!");
|
|
95
|
+
|
|
96
|
+ assertEquals("value!", retriever.getValue(String.class, DOMAIN, KEY, true));
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ @Test
|
|
100
|
+ public void testRetrievesBoolean() {
|
|
101
|
+ when(configProvider.getOptionBool(DOMAIN, KEY)).thenReturn(true);
|
|
102
|
+
|
|
103
|
+ assertEquals(true, retriever.getValue(Boolean.class, DOMAIN, KEY));
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ @Test
|
|
107
|
+ public void testRetrievesBooleanByPrimitiveType() {
|
|
108
|
+ when(configProvider.getOptionBool(DOMAIN, KEY)).thenReturn(true);
|
|
109
|
+
|
|
110
|
+ assertEquals(true, retriever.getValue(Boolean.TYPE, DOMAIN, KEY));
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ @Test
|
|
114
|
+ public void testRetrievesChar() {
|
|
115
|
+ when(configProvider.getOptionChar(DOMAIN, KEY)).thenReturn('s');
|
|
116
|
+
|
|
117
|
+ assertEquals('s', retriever.getValue(Character.class, DOMAIN, KEY));
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ @Test
|
|
121
|
+ public void testRetrievesCharByPrimitiveType() {
|
|
122
|
+ when(configProvider.getOptionChar(DOMAIN, KEY)).thenReturn('s');
|
|
123
|
+
|
|
124
|
+ assertEquals('s', retriever.getValue(Character.TYPE, DOMAIN, KEY));
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ @Test
|
|
128
|
+ public void testRetrievesInt() {
|
|
129
|
+ when(configProvider.getOptionInt(DOMAIN, KEY)).thenReturn(1337);
|
|
130
|
+
|
|
131
|
+ assertEquals(1337, retriever.getValue(Integer.class, DOMAIN, KEY));
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ @Test
|
|
135
|
+ public void testRetrievesIntByPrimitiveType() {
|
|
136
|
+ when(configProvider.getOptionInt(DOMAIN, KEY)).thenReturn(1337);
|
|
137
|
+
|
|
138
|
+ assertEquals(1337, retriever.getValue(Integer.TYPE, DOMAIN, KEY));
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ @Test
|
|
142
|
+ @SuppressWarnings("AssertEqualsBetweenInconvertibleTypes")
|
|
143
|
+ public void testRetrievesList() {
|
|
144
|
+ when(configProvider.getOptionList(DOMAIN, KEY)).thenReturn(
|
|
145
|
+ Lists.newArrayList("test1", "test2"));
|
|
146
|
+
|
|
147
|
+ assertEquals(Lists.newArrayList("test1", "test2"),
|
|
148
|
+ retriever.getValue(List.class, DOMAIN, KEY));
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+}
|