View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.*;
32  
33  public class ConfigUtilsTest {
34  
35      Map<Object, Object> config = new HashMap<>();
36  
37      @Test
38      public void testGetObject_Default() {
39          Object val = new Object();
40          assertSame(val, ConfigUtils.getObject(config, val, "no-value"));
41      }
42  
43      @Test
44      public void testGetObject_AlternativeKeys() {
45          Object val = new Object();
46          config.put("some-object", val);
47          assertSame(val, ConfigUtils.getObject(config, null, "no-object", "some-object"));
48      }
49  
50      @Test
51      public void testGetMap_Default() {
52          Map<?, ?> val = new HashMap<Object, Object>();
53          assertSame(val, ConfigUtils.getMap(config, val, "no-value"));
54      }
55  
56      @Test
57      public void testGetMap_AlternativeKeys() {
58          Map<?, ?> val = new HashMap<Object, Object>();
59          config.put("some-map", val);
60          assertSame(val, ConfigUtils.getMap(config, null, "no-object", "some-map"));
61      }
62  
63      @Test
64      public void testGetList_Default() {
65          List<?> val = new ArrayList<Object>();
66          assertSame(val, ConfigUtils.getList(config, val, "no-value"));
67      }
68  
69      @Test
70      public void testGetList_AlternativeKeys() {
71          List<?> val = new ArrayList<Object>();
72          config.put("some-list", val);
73          assertSame(val, ConfigUtils.getList(config, null, "no-object", "some-list"));
74      }
75  
76      @Test
77      public void testGetList_CollectionConversion() {
78          Collection<?> val = Collections.singleton("item");
79          config.put("some-collection", val);
80          assertEquals(Arrays.asList("item"), ConfigUtils.getList(config, null, "some-collection"));
81      }
82  
83      @Test
84      public void testGetString_Default() {
85          config.put("no-string", new Object());
86          assertEquals("default", ConfigUtils.getString(config, "default", "no-value"));
87          assertEquals("default", ConfigUtils.getString(config, "default", "no-string"));
88      }
89  
90      @Test
91      public void testGetString_AlternativeKeys() {
92          config.put("no-string", new Object());
93          config.put("some-string", "passed");
94          assertEquals("passed", ConfigUtils.getString(config, "default", "no-string", "some-string"));
95      }
96  
97      @Test
98      public void testGetBoolean_Default() {
99          config.put("no-boolean", new Object());
100         assertTrue(ConfigUtils.getBoolean(config, true, "no-value"));
101         assertFalse(ConfigUtils.getBoolean(config, false, "no-value"));
102         assertTrue(ConfigUtils.getBoolean(config, true, "no-boolean"));
103         assertFalse(ConfigUtils.getBoolean(config, false, "no-boolean"));
104     }
105 
106     @Test
107     public void testGetBoolean_AlternativeKeys() {
108         config.put("no-boolean", new Object());
109         config.put("some-boolean", true);
110         assertTrue(ConfigUtils.getBoolean(config, false, "no-boolean", "some-boolean"));
111         config.put("some-boolean", false);
112         assertFalse(ConfigUtils.getBoolean(config, true, "no-boolean", "some-boolean"));
113     }
114 
115     @Test
116     public void testGetBoolean_StringConversion() {
117         config.put("some-boolean", "true");
118         assertTrue(ConfigUtils.getBoolean(config, false, "some-boolean"));
119         config.put("some-boolean", "false");
120         assertFalse(ConfigUtils.getBoolean(config, true, "some-boolean"));
121     }
122 
123     @Test
124     public void testGetInteger_Default() {
125         config.put("no-integer", new Object());
126         assertEquals(-17, ConfigUtils.getInteger(config, -17, "no-value"));
127         assertEquals(43, ConfigUtils.getInteger(config, 43, "no-integer"));
128     }
129 
130     @Test
131     public void testGetInteger_AlternativeKeys() {
132         config.put("no-integer", "text");
133         config.put("some-integer", 23);
134         assertEquals(23, ConfigUtils.getInteger(config, 0, "no-integer", "some-integer"));
135     }
136 
137     @Test
138     public void testGetInteger_StringConversion() {
139         config.put("some-integer", "-123456");
140         assertEquals(-123456, ConfigUtils.getInteger(config, 0, "some-integer"));
141     }
142 
143     @Test
144     public void testGetInteger_NumberConversion() {
145         config.put("some-number", -123456.789);
146         assertEquals(-123456, ConfigUtils.getInteger(config, 0, "some-number"));
147     }
148 
149     @Test
150     public void testGetLong_Default() {
151         config.put("no-long", new Object());
152         assertEquals(-17L, ConfigUtils.getLong(config, -17L, "no-value"));
153         assertEquals(43L, ConfigUtils.getLong(config, 43L, "no-long"));
154     }
155 
156     @Test
157     public void testGetLong_AlternativeKeys() {
158         config.put("no-long", "text");
159         config.put("some-long", 23L);
160         assertEquals(23L, ConfigUtils.getLong(config, 0, "no-long", "some-long"));
161     }
162 
163     @Test
164     public void testGetLong_StringConversion() {
165         config.put("some-long", "-123456789012");
166         assertEquals(-123456789012L, ConfigUtils.getLong(config, 0, "some-long"));
167     }
168 
169     @Test
170     public void testGetLong_NumberConversion() {
171         config.put("some-number", -123456789012.789);
172         assertEquals(-123456789012L, ConfigUtils.getLong(config, 0, "some-number"));
173     }
174 
175     @Test
176     public void testGetFloat_Default() {
177         config.put("no-float", new Object());
178         assertEquals(-17.1f, ConfigUtils.getFloat(config, -17.1f, "no-value"), 0.01f);
179         assertEquals(43.2f, ConfigUtils.getFloat(config, 43.2f, "no-float"), 0.01f);
180     }
181 
182     @Test
183     public void testGetFloat_AlternativeKeys() {
184         config.put("no-float", "text");
185         config.put("some-float", 12.3f);
186         assertEquals(12.3f, ConfigUtils.getFloat(config, 0, "no-float", "some-float"), 0.01f);
187     }
188 
189     @Test
190     public void testGetFloat_StringConversion() {
191         config.put("some-float", "-12.3");
192         assertEquals(-12.3f, ConfigUtils.getFloat(config, 0, "some-float"), 0.01f);
193         config.put("some-float", "NaN");
194         assertTrue(Float.isNaN(ConfigUtils.getFloat(config, 0, "some-float")));
195     }
196 
197     @Test
198     public void testGetFloat_NumberConversion() {
199         config.put("some-number", -1234f);
200         assertEquals(-1234f, ConfigUtils.getFloat(config, 0, "some-number"), 0.1f);
201     }
202 }