1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.junit.Test;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertFalse;
33 import static org.junit.Assert.assertSame;
34 import static org.junit.Assert.assertTrue;
35
36 public class ConfigUtilsTest {
37
38 Map<Object, Object> config = new HashMap<>();
39
40 @Test
41 public void testGetObjectDefault() {
42 Object val = new Object();
43 assertSame(val, ConfigUtils.getObject(config, val, "no-value"));
44 }
45
46 @Test
47 public void testGetObjectAlternativeKeys() {
48 Object val = new Object();
49 config.put("some-object", val);
50 assertSame(val, ConfigUtils.getObject(config, null, "no-object", "some-object"));
51 }
52
53 @Test
54 public void testGetMapDefault() {
55 Map<?, ?> val = new HashMap<Object, Object>();
56 assertSame(val, ConfigUtils.getMap(config, val, "no-value"));
57 }
58
59 @Test
60 public void testGetMapAlternativeKeys() {
61 Map<?, ?> val = new HashMap<Object, Object>();
62 config.put("some-map", val);
63 assertSame(val, ConfigUtils.getMap(config, null, "no-object", "some-map"));
64 }
65
66 @Test
67 public void testGetListDefault() {
68 List<?> val = new ArrayList<Object>();
69 assertSame(val, ConfigUtils.getList(config, val, "no-value"));
70 }
71
72 @Test
73 public void testGetListAlternativeKeys() {
74 List<?> val = new ArrayList<Object>();
75 config.put("some-list", val);
76 assertSame(val, ConfigUtils.getList(config, null, "no-object", "some-list"));
77 }
78
79 @Test
80 public void testGetListCollectionConversion() {
81 Collection<?> val = Collections.singleton("item");
82 config.put("some-collection", val);
83 assertEquals(Arrays.asList("item"), ConfigUtils.getList(config, null, "some-collection"));
84 }
85
86 @Test
87 public void testGetStringDefault() {
88 config.put("no-string", new Object());
89 assertEquals("default", ConfigUtils.getString(config, "default", "no-value"));
90 assertEquals("default", ConfigUtils.getString(config, "default", "no-string"));
91 }
92
93 @Test
94 public void testGetStringAlternativeKeys() {
95 config.put("no-string", new Object());
96 config.put("some-string", "passed");
97 assertEquals("passed", ConfigUtils.getString(config, "default", "no-string", "some-string"));
98 }
99
100 @Test
101 public void testGetBooleanDefault() {
102 config.put("no-boolean", new Object());
103 assertTrue(ConfigUtils.getBoolean(config, true, "no-value"));
104 assertFalse(ConfigUtils.getBoolean(config, false, "no-value"));
105 assertTrue(ConfigUtils.getBoolean(config, true, "no-boolean"));
106 assertFalse(ConfigUtils.getBoolean(config, false, "no-boolean"));
107 }
108
109 @Test
110 public void testGetBooleanAlternativeKeys() {
111 config.put("no-boolean", new Object());
112 config.put("some-boolean", true);
113 assertTrue(ConfigUtils.getBoolean(config, false, "no-boolean", "some-boolean"));
114 config.put("some-boolean", false);
115 assertFalse(ConfigUtils.getBoolean(config, true, "no-boolean", "some-boolean"));
116 }
117
118 @Test
119 public void testGetBooleanStringConversion() {
120 config.put("some-boolean", "true");
121 assertTrue(ConfigUtils.getBoolean(config, false, "some-boolean"));
122 config.put("some-boolean", "false");
123 assertFalse(ConfigUtils.getBoolean(config, true, "some-boolean"));
124 }
125
126 @Test
127 public void testGetIntegerDefault() {
128 config.put("no-integer", new Object());
129 assertEquals(-17, ConfigUtils.getInteger(config, -17, "no-value"));
130 assertEquals(43, ConfigUtils.getInteger(config, 43, "no-integer"));
131 }
132
133 @Test
134 public void testGetIntegerAlternativeKeys() {
135 config.put("no-integer", "text");
136 config.put("some-integer", 23);
137 assertEquals(23, ConfigUtils.getInteger(config, 0, "no-integer", "some-integer"));
138 }
139
140 @Test
141 public void testGetIntegerStringConversion() {
142 config.put("some-integer", "-123456");
143 assertEquals(-123456, ConfigUtils.getInteger(config, 0, "some-integer"));
144 }
145
146 @Test
147 public void testGetIntegerNumberConversion() {
148 config.put("some-number", -123456.789);
149 assertEquals(-123456, ConfigUtils.getInteger(config, 0, "some-number"));
150 }
151
152 @Test
153 public void testGetLongDefault() {
154 config.put("no-long", new Object());
155 assertEquals(-17L, ConfigUtils.getLong(config, -17L, "no-value"));
156 assertEquals(43L, ConfigUtils.getLong(config, 43L, "no-long"));
157 }
158
159 @Test
160 public void testGetLongAlternativeKeys() {
161 config.put("no-long", "text");
162 config.put("some-long", 23L);
163 assertEquals(23L, ConfigUtils.getLong(config, 0, "no-long", "some-long"));
164 }
165
166 @Test
167 public void testGetLongStringConversion() {
168 config.put("some-long", "-123456789012");
169 assertEquals(-123456789012L, ConfigUtils.getLong(config, 0, "some-long"));
170 }
171
172 @Test
173 public void testGetLongNumberConversion() {
174 config.put("some-number", -123456789012.789);
175 assertEquals(-123456789012L, ConfigUtils.getLong(config, 0, "some-number"));
176 }
177
178 @Test
179 public void testGetFloatDefault() {
180 config.put("no-float", new Object());
181 assertEquals(-17.1f, ConfigUtils.getFloat(config, -17.1f, "no-value"), 0.01f);
182 assertEquals(43.2f, ConfigUtils.getFloat(config, 43.2f, "no-float"), 0.01f);
183 }
184
185 @Test
186 public void testGetFloatAlternativeKeys() {
187 config.put("no-float", "text");
188 config.put("some-float", 12.3f);
189 assertEquals(12.3f, ConfigUtils.getFloat(config, 0, "no-float", "some-float"), 0.01f);
190 }
191
192 @Test
193 public void testGetFloatStringConversion() {
194 config.put("some-float", "-12.3");
195 assertEquals(-12.3f, ConfigUtils.getFloat(config, 0, "some-float"), 0.01f);
196 config.put("some-float", "NaN");
197 assertTrue(Float.isNaN(ConfigUtils.getFloat(config, 0, "some-float")));
198 }
199
200 @Test
201 public void testGetFloatNumberConversion() {
202 config.put("some-number", -1234f);
203 assertEquals(-1234f, ConfigUtils.getFloat(config, 0, "some-number"), 0.1f);
204 }
205 }