Selaa lähdekoodia

Merge pull request #604 from csmith/config-value-retriever

Extract config value retrieving from binder.
pull/605/head
Shane Mc Cormack 8 vuotta sitten
vanhempi
commit
efd10db991

+ 10
- 42
src/com/dmdirc/config/ConfigBinder.java Näytä tiedosto

@@ -24,7 +24,6 @@ package com.dmdirc.config;
24 24
 
25 25
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
26 26
 import com.dmdirc.interfaces.config.ConfigChangeListener;
27
-import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
28 27
 
29 28
 import com.google.common.collect.ArrayListMultimap;
30 29
 import com.google.common.collect.Multimap;
@@ -35,7 +34,6 @@ import java.lang.reflect.Field;
35 34
 import java.util.ArrayList;
36 35
 import java.util.Arrays;
37 36
 import java.util.Collection;
38
-import java.util.List;
39 37
 import java.util.Optional;
40 38
 
41 39
 import javax.annotation.Nonnull;
@@ -59,14 +57,18 @@ public class ConfigBinder {
59 57
     private final Optional<String> defaultDomain;
60 58
     /** The configuration manager to use to retrieve settings. */
61 59
     private final AggregateConfigProvider manager;
60
+    /** Retriever to use to get typed config values. */
61
+    private final ConfigValueRetriever valueRetriever;
62 62
 
63 63
     ConfigBinder(final AggregateConfigProvider manager) {
64 64
         this.manager = manager;
65
+        this.valueRetriever = new ConfigValueRetriever(manager);
65 66
         this.defaultDomain = Optional.empty();
66 67
     }
67 68
 
68 69
     ConfigBinder(final AggregateConfigProvider manager, @Nonnull final String domain) {
69 70
         this.manager = manager;
71
+        this.valueRetriever = new ConfigValueRetriever(manager);
70 72
         this.defaultDomain = Optional.of(domain);
71 73
     }
72 74
 
@@ -142,7 +144,12 @@ public class ConfigBinder {
142 144
             element.setAccessible(true);
143 145
         }
144 146
 
145
-        final Object value = getValue(binding, getTargetClass(element));
147
+        final Object value = valueRetriever.getValue(
148
+                getTargetClass(element),
149
+                getDomain(binding.domain()),
150
+                binding.key(),
151
+                binding.required(),
152
+                binding.fallbacks());
146 153
 
147 154
         try {
148 155
             binding.invocation().newInstance().invoke(element, instance, value);
@@ -151,45 +158,6 @@ public class ConfigBinder {
151 158
         }
152 159
     }
153 160
 
154
-    /**
155
-     * Gets a value from the configuration manager for use with the given binding, and attempts to
156
-     * coerce it into the given class.
157
-     *
158
-     * @param binding     The binding defining configuration parameters
159
-     * @param targetClass The desired class
160
-     *
161
-     * @return An object representing the current value of the configuration key(s) associated with
162
-     *         the binding, of the desired target class, or null if the type conversion couldn't be
163
-     *         performed.
164
-     */
165
-    private Object getValue(final ConfigBinding binding,
166
-            final Class<?> targetClass) {
167
-        if (targetClass.equals(String.class)) {
168
-            return manager.getOptionString(getDomain(binding.domain()), binding.key(),
169
-                    binding.required(), ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR,
170
-                    binding.fallbacks());
171
-        }
172
-
173
-        if (targetClass.equals(Boolean.class) || targetClass.equals(Boolean.TYPE)) {
174
-            return manager.getOptionBool(getDomain(binding.domain()), binding.key());
175
-        }
176
-
177
-        if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE)) {
178
-            return manager.getOptionChar(getDomain(binding.domain()), binding.key());
179
-        }
180
-
181
-        if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE)) {
182
-            return manager.getOptionInt(getDomain(binding.domain()), binding.key(),
183
-                    binding.fallbacks());
184
-        }
185
-
186
-        if (targetClass.equals(List.class)) {
187
-            return manager.getOptionList(getDomain(binding.domain()), binding.key());
188
-        }
189
-
190
-        return null;
191
-    }
192
-
193 161
     /**
194 162
      * Gets the type required for setting the given element.
195 163
      *

+ 92
- 0
src/com/dmdirc/config/ConfigValueRetriever.java Näytä tiedosto

@@ -0,0 +1,92 @@
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 java.util.List;
28
+
29
+/**
30
+ * Provides methods to retrieve a value of a certain type from a config provider.
31
+ */
32
+public class ConfigValueRetriever {
33
+
34
+    private final ReadOnlyConfigProvider configProvider;
35
+
36
+    public ConfigValueRetriever(final ReadOnlyConfigProvider configProvider) {
37
+        this.configProvider = configProvider;
38
+    }
39
+
40
+    /**
41
+     * Gets a value from the configuration manager, and attempts to coerce it into the given class.
42
+     *
43
+     * @param targetClass The desired class.
44
+     * @param domain The domain of the option to retrieve.
45
+     * @param key The key of the option to retrieve.
46
+     * @param required Whether the option is required or not (only used for strings).
47
+     * @param fallbacks Ordered collection of domain/key pairs to try if the value is not set.
48
+     *
49
+     * @return An object representing the current value of the configuration key(s) given, of the
50
+     *         desired target class, or null if the type conversion couldn't be performed.
51
+     */
52
+    public Object getValue(final Class<?> targetClass, final String domain, final String key,
53
+            final boolean required, final String ... fallbacks) {
54
+        if (targetClass.equals(String.class)) {
55
+            return configProvider.getOptionString(
56
+                    domain, key, required, ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR, fallbacks);
57
+        }
58
+
59
+        if (targetClass.equals(Boolean.class) || targetClass.equals(Boolean.TYPE)) {
60
+            return configProvider.getOptionBool(domain, key);
61
+        }
62
+
63
+        if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE)) {
64
+            return configProvider.getOptionChar(domain, key);
65
+        }
66
+
67
+        if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE)) {
68
+            return configProvider.getOptionInt(domain, key, fallbacks);
69
+        }
70
+
71
+        if (targetClass.equals(List.class)) {
72
+            return configProvider.getOptionList(domain, key);
73
+        }
74
+
75
+        return null;
76
+    }
77
+
78
+    /**
79
+     * Gets a value from the configuration manager, and attempts to coerce it into the given class.
80
+     *
81
+     * @param targetClass The desired class.
82
+     * @param domain The domain of the option to retrieve.
83
+     * @param key The key of the option to retrieve.
84
+     *
85
+     * @return An object representing the current value of the configuration key(s) given, of the
86
+     *         desired target class, or null if the type conversion couldn't be performed.
87
+     */
88
+    public Object getValue(final Class<?> targetClass, final String domain, final String key) {
89
+        return getValue(targetClass, domain, key, true);
90
+    }
91
+
92
+}

+ 151
- 0
test/com/dmdirc/config/ConfigValueRetrieverTest.java Näytä tiedosto

@@ -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
+}

Loading…
Peruuta
Tallenna