001package org.eclipse.aether.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.junit.Assert.*; 023 024import java.util.ArrayList; 025import java.util.Arrays; 026import java.util.Collection; 027import java.util.Collections; 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031 032import org.junit.Test; 033 034public class ConfigUtilsTest 035{ 036 037 Map<Object, Object> config = new HashMap<>(); 038 039 @Test 040 public void testGetObject_Default() 041 { 042 Object val = new Object(); 043 assertSame( val, ConfigUtils.getObject( config, val, "no-value" ) ); 044 } 045 046 @Test 047 public void testGetObject_AlternativeKeys() 048 { 049 Object val = new Object(); 050 config.put( "some-object", val ); 051 assertSame( val, ConfigUtils.getObject( config, null, "no-object", "some-object" ) ); 052 } 053 054 @Test 055 public void testGetMap_Default() 056 { 057 Map<?, ?> val = new HashMap<Object, Object>(); 058 assertSame( val, ConfigUtils.getMap( config, val, "no-value" ) ); 059 } 060 061 @Test 062 public void testGetMap_AlternativeKeys() 063 { 064 Map<?, ?> val = new HashMap<Object, Object>(); 065 config.put( "some-map", val ); 066 assertSame( val, ConfigUtils.getMap( config, null, "no-object", "some-map" ) ); 067 } 068 069 @Test 070 public void testGetList_Default() 071 { 072 List<?> val = new ArrayList<Object>(); 073 assertSame( val, ConfigUtils.getList( config, val, "no-value" ) ); 074 } 075 076 @Test 077 public void testGetList_AlternativeKeys() 078 { 079 List<?> val = new ArrayList<Object>(); 080 config.put( "some-list", val ); 081 assertSame( val, ConfigUtils.getList( config, null, "no-object", "some-list" ) ); 082 } 083 084 @Test 085 public void testGetList_CollectionConversion() 086 { 087 Collection<?> val = Collections.singleton( "item" ); 088 config.put( "some-collection", val ); 089 assertEquals( Arrays.asList( "item" ), ConfigUtils.getList( config, null, "some-collection" ) ); 090 } 091 092 @Test 093 public void testGetString_Default() 094 { 095 config.put( "no-string", new Object() ); 096 assertEquals( "default", ConfigUtils.getString( config, "default", "no-value" ) ); 097 assertEquals( "default", ConfigUtils.getString( config, "default", "no-string" ) ); 098 } 099 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}