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