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.apache.commons.lang3; 20 21 import static org.junit.jupiter.api.Assertions.assertEquals; 22 import static org.junit.jupiter.api.Assertions.assertFalse; 23 import static org.junit.jupiter.api.Assertions.assertNull; 24 import static org.junit.jupiter.api.Assertions.assertThrows; 25 import static org.junit.jupiter.api.Assertions.assertTrue; 26 27 import java.util.ArrayList; 28 import java.util.Arrays; 29 import java.util.EnumSet; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.function.Function; 33 34 import org.junit.jupiter.api.Assertions; 35 import org.junit.jupiter.api.Test; 36 37 enum Enum64 { 38 A00, A01, A02, A03, A04, A05, A06, A07, A08, A09, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, A22, 39 A23, A24, A25, A26, A27, A28, A29, A30, A31, A32, A33, A34, A35, A36, A37, A38, A39, A40, A41, A42, A43, A44, A45, 40 A46, A47, A48, A49, A50, A51, A52, A53, A54, A55, A56, A57, A58, A59, A60, A61, A62, A63 41 } 42 43 /** 44 * 45 */ 46 public class EnumUtilsTest extends AbstractLangTest { 47 assertArrayEquals(final long[] actual, final long... expected)48 private void assertArrayEquals(final long[] actual, final long... expected) { 49 Assertions.assertArrayEquals(expected, actual); 50 } 51 52 @Test test_generateBitVector()53 public void test_generateBitVector() { 54 assertEquals(0L, EnumUtils.generateBitVector(Traffic.class, EnumSet.noneOf(Traffic.class))); 55 assertEquals(1L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED))); 56 assertEquals(2L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.AMBER))); 57 assertEquals(4L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.GREEN))); 58 assertEquals(3L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER))); 59 assertEquals(5L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED, Traffic.GREEN))); 60 assertEquals(6L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.AMBER, Traffic.GREEN))); 61 assertEquals(7L, 62 EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN))); 63 64 // 64 values Enum (to test whether no int<->long jdk conversion issue exists) 65 assertEquals((1L << 31), EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A31))); 66 assertEquals((1L << 32), EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A32))); 67 assertEquals((1L << 63), EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A63))); 68 assertEquals(Long.MIN_VALUE, EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A63))); 69 } 70 71 @Test test_generateBitVector_longClass()72 public void test_generateBitVector_longClass() { 73 assertThrows(IllegalArgumentException.class, 74 () -> EnumUtils.generateBitVector(TooMany.class, EnumSet.of(TooMany.A1))); 75 } 76 77 @Test test_generateBitVector_longClassWithArray()78 public void test_generateBitVector_longClassWithArray() { 79 assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(TooMany.class, TooMany.A1)); 80 } 81 82 @SuppressWarnings("unchecked") 83 @Test test_generateBitVector_nonEnumClass()84 public void test_generateBitVector_nonEnumClass() { 85 @SuppressWarnings("rawtypes") 86 final Class rawType = Object.class; 87 @SuppressWarnings("rawtypes") 88 final List rawList = new ArrayList(); 89 assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(rawType, rawList)); 90 } 91 92 @SuppressWarnings("unchecked") 93 @Test test_generateBitVector_nonEnumClassWithArray()94 public void test_generateBitVector_nonEnumClassWithArray() { 95 @SuppressWarnings("rawtypes") 96 final Class rawType = Object.class; 97 assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(rawType)); 98 } 99 100 @Test test_generateBitVector_nullArray()101 public void test_generateBitVector_nullArray() { 102 assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(Traffic.class, (Traffic[]) null)); 103 } 104 105 @Test test_generateBitVector_nullArrayElement()106 public void test_generateBitVector_nullArrayElement() { 107 assertThrows(IllegalArgumentException.class, 108 () -> EnumUtils.generateBitVector(Traffic.class, Traffic.RED, null)); 109 } 110 111 @Test test_generateBitVector_nullClass()112 public void test_generateBitVector_nullClass() { 113 assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(null, EnumSet.of(Traffic.RED))); 114 } 115 116 @Test test_generateBitVector_nullClassWithArray()117 public void test_generateBitVector_nullClassWithArray() { 118 assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(null, Traffic.RED)); 119 } 120 121 @Test test_generateBitVector_nullElement()122 public void test_generateBitVector_nullElement() { 123 assertThrows(NullPointerException.class, 124 () -> EnumUtils.generateBitVector(Traffic.class, Arrays.asList(Traffic.RED, null))); 125 } 126 127 @Test test_generateBitVector_nullIterable()128 public void test_generateBitVector_nullIterable() { 129 assertThrows(NullPointerException.class, 130 () -> EnumUtils.generateBitVector(Traffic.class, (Iterable<Traffic>) null)); 131 } 132 133 @Test test_generateBitVectorFromArray()134 public void test_generateBitVectorFromArray() { 135 assertEquals(0L, EnumUtils.generateBitVector(Traffic.class)); 136 assertEquals(1L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED)); 137 assertEquals(2L, EnumUtils.generateBitVector(Traffic.class, Traffic.AMBER)); 138 assertEquals(4L, EnumUtils.generateBitVector(Traffic.class, Traffic.GREEN)); 139 assertEquals(3L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.AMBER)); 140 assertEquals(5L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.GREEN)); 141 assertEquals(6L, EnumUtils.generateBitVector(Traffic.class, Traffic.AMBER, Traffic.GREEN)); 142 assertEquals(7L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN)); 143 // gracefully handles duplicates: 144 assertEquals(7L, 145 EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN, Traffic.GREEN)); 146 147 // 64 values Enum (to test whether no int<->long jdk conversion issue exists) 148 assertEquals((1L << 31), EnumUtils.generateBitVector(Enum64.class, Enum64.A31)); 149 assertEquals((1L << 32), EnumUtils.generateBitVector(Enum64.class, Enum64.A32)); 150 assertEquals((1L << 63), EnumUtils.generateBitVector(Enum64.class, Enum64.A63)); 151 assertEquals(Long.MIN_VALUE, EnumUtils.generateBitVector(Enum64.class, Enum64.A63)); 152 } 153 154 @Test test_generateBitVectors()155 public void test_generateBitVectors() { 156 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.noneOf(Traffic.class)), 0L); 157 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED)), 1L); 158 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.AMBER)), 2L); 159 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.GREEN)), 4L); 160 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER)), 3L); 161 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED, Traffic.GREEN)), 5L); 162 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.AMBER, Traffic.GREEN)), 6L); 163 assertArrayEquals( 164 EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN)), 7L); 165 166 // 64 values Enum (to test whether no int<->long jdk conversion issue exists) 167 assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A31)), (1L << 31)); 168 assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A32)), (1L << 32)); 169 assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A63)), (1L << 63)); 170 assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A63)), Long.MIN_VALUE); 171 172 // More than 64 values Enum 173 assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, EnumSet.of(TooMany.M2)), 1L, 0L); 174 assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, EnumSet.of(TooMany.L2, TooMany.M2)), 1L, 175 (1L << 63)); 176 } 177 178 @SuppressWarnings("unchecked") 179 @Test test_generateBitVectors_nonEnumClass()180 public void test_generateBitVectors_nonEnumClass() { 181 @SuppressWarnings("rawtypes") 182 final Class rawType = Object.class; 183 @SuppressWarnings("rawtypes") 184 final List rawList = new ArrayList(); 185 assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVectors(rawType, rawList)); 186 } 187 188 @SuppressWarnings("unchecked") 189 @Test test_generateBitVectors_nonEnumClassWithArray()190 public void test_generateBitVectors_nonEnumClassWithArray() { 191 @SuppressWarnings("rawtypes") 192 final Class rawType = Object.class; 193 assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVectors(rawType)); 194 } 195 196 @Test test_generateBitVectors_nullArray()197 public void test_generateBitVectors_nullArray() { 198 assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(Traffic.class, (Traffic[]) null)); 199 } 200 201 @Test test_generateBitVectors_nullArrayElement()202 public void test_generateBitVectors_nullArrayElement() { 203 assertThrows(IllegalArgumentException.class, 204 () -> EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, null)); 205 } 206 207 @Test test_generateBitVectors_nullClass()208 public void test_generateBitVectors_nullClass() { 209 assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(null, EnumSet.of(Traffic.RED))); 210 } 211 212 @Test test_generateBitVectors_nullClassWithArray()213 public void test_generateBitVectors_nullClassWithArray() { 214 assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(null, Traffic.RED)); 215 } 216 217 @Test test_generateBitVectors_nullElement()218 public void test_generateBitVectors_nullElement() { 219 assertThrows(NullPointerException.class, 220 () -> EnumUtils.generateBitVectors(Traffic.class, Arrays.asList(Traffic.RED, null))); 221 } 222 223 @Test test_generateBitVectors_nullIterable()224 public void test_generateBitVectors_nullIterable() { 225 assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(null, (Iterable<Traffic>) null)); 226 } 227 228 @Test test_generateBitVectorsFromArray()229 public void test_generateBitVectorsFromArray() { 230 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class), 0L); 231 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED), 1L); 232 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.AMBER), 2L); 233 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.GREEN), 4L); 234 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.AMBER), 3L); 235 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.GREEN), 5L); 236 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.AMBER, Traffic.GREEN), 6L); 237 assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN), 7L); 238 // gracefully handles duplicates: 239 assertArrayEquals( 240 EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN, Traffic.GREEN), 7L); 241 242 // 64 values Enum (to test whether no int<->long jdk conversion issue exists) 243 assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A31), (1L << 31)); 244 assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A32), (1L << 32)); 245 assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A63), (1L << 63)); 246 assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A63), Long.MIN_VALUE); 247 248 // More than 64 values Enum 249 assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, TooMany.M2), 1L, 0L); 250 assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, TooMany.L2, TooMany.M2), 1L, (1L << 63)); 251 252 } 253 254 @Test test_getEnum()255 public void test_getEnum() { 256 assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "RED")); 257 assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "AMBER")); 258 assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "GREEN")); 259 assertNull(EnumUtils.getEnum(Traffic.class, "PURPLE")); 260 assertNull(EnumUtils.getEnum(Traffic.class, null)); 261 } 262 263 @Test test_getEnum_defaultEnum()264 public void test_getEnum_defaultEnum() { 265 assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "RED", Traffic.AMBER)); 266 assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "AMBER", Traffic.GREEN)); 267 assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "GREEN", Traffic.RED)); 268 assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.AMBER)); 269 assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.GREEN)); 270 assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.RED)); 271 assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, null, Traffic.AMBER)); 272 assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, null, Traffic.GREEN)); 273 assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, null, Traffic.RED)); 274 assertNull(EnumUtils.getEnum(Traffic.class, "PURPLE", null)); 275 } 276 277 /** 278 * Tests raw type. 279 */ 280 @SuppressWarnings("unchecked") 281 @Test test_getEnum_nonEnumClass()282 public void test_getEnum_nonEnumClass() { 283 @SuppressWarnings("rawtypes") 284 final Class rawType = Object.class; 285 assertNull(EnumUtils.getEnum(rawType, "rawType")); 286 } 287 288 @Test test_getEnum_nullClass()289 public void test_getEnum_nullClass() { 290 assertThrows(NullPointerException.class, () -> EnumUtils.getEnum((Class<Traffic>) null, "PURPLE")); 291 } 292 293 @Test test_getEnumIgnoreCase()294 public void test_getEnumIgnoreCase() { 295 assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "red")); 296 assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "Amber")); 297 assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "grEEn")); 298 assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "purple")); 299 assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, null)); 300 } 301 302 @Test test_getEnumIgnoreCase_defaultEnum()303 public void test_getEnumIgnoreCase_defaultEnum() { 304 assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "red", Traffic.AMBER)); 305 assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "Amber", Traffic.GREEN)); 306 assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "grEEn", Traffic.RED)); 307 assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", Traffic.AMBER)); 308 assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "purple", Traffic.GREEN)); 309 assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "pUrPlE", Traffic.RED)); 310 assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.AMBER)); 311 assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.GREEN)); 312 assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.RED)); 313 assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", null)); 314 } 315 316 /** 317 * Tests raw type. 318 */ 319 @SuppressWarnings("unchecked") 320 @Test test_getEnumIgnoreCase_nonEnumClass()321 public void test_getEnumIgnoreCase_nonEnumClass() { 322 @SuppressWarnings("rawtypes") 323 final Class rawType = Object.class; 324 assertNull(EnumUtils.getEnumIgnoreCase(rawType, "rawType")); 325 } 326 327 @Test test_getEnumIgnoreCase_nullClass()328 public void test_getEnumIgnoreCase_nullClass() { 329 assertThrows(NullPointerException.class, () -> EnumUtils.getEnumIgnoreCase((Class<Traffic>) null, "PURPLE")); 330 } 331 332 @Test test_getEnumList()333 public void test_getEnumList() { 334 final List<Traffic> test = EnumUtils.getEnumList(Traffic.class); 335 assertEquals(3, test.size()); 336 assertEquals(Traffic.RED, test.get(0)); 337 assertEquals(Traffic.AMBER, test.get(1)); 338 assertEquals(Traffic.GREEN, test.get(2)); 339 } 340 341 @Test test_getEnumMap()342 public void test_getEnumMap() { 343 final Map<String, Traffic> test = EnumUtils.getEnumMap(Traffic.class); 344 assertEquals("{RED=RED, AMBER=AMBER, GREEN=GREEN}", test.toString(), "getEnumMap not created correctly"); 345 assertEquals(3, test.size()); 346 assertTrue(test.containsKey("RED")); 347 assertEquals(Traffic.RED, test.get("RED")); 348 assertTrue(test.containsKey("AMBER")); 349 assertEquals(Traffic.AMBER, test.get("AMBER")); 350 assertTrue(test.containsKey("GREEN")); 351 assertEquals(Traffic.GREEN, test.get("GREEN")); 352 assertFalse(test.containsKey("PURPLE")); 353 } 354 355 @Test test_getEnumMap_keyFunction()356 public void test_getEnumMap_keyFunction() { 357 final Map<Integer, Month> test = EnumUtils.getEnumMap(Month.class, Month::getId); 358 assertEquals("{1=JAN, 2=FEB, 3=MAR, 4=APR, 5=MAY, 6=JUN, 7=JUL, 8=AUG, 9=SEP, 10=OCT, 11=NOV, 12=DEC}", test.toString(), 359 "getEnumMap not created correctly"); 360 assertEquals(12, test.size()); 361 assertFalse(test.containsKey(0)); 362 assertTrue(test.containsKey(1)); 363 assertEquals(Month.JAN, test.get(1)); 364 assertTrue(test.containsKey(2)); 365 assertEquals(Month.FEB, test.get(2)); 366 assertTrue(test.containsKey(3)); 367 assertEquals(Month.MAR, test.get(3)); 368 assertTrue(test.containsKey(4)); 369 assertEquals(Month.APR, test.get(4)); 370 assertTrue(test.containsKey(5)); 371 assertEquals(Month.MAY, test.get(5)); 372 assertTrue(test.containsKey(6)); 373 assertEquals(Month.JUN, test.get(6)); 374 assertTrue(test.containsKey(7)); 375 assertEquals(Month.JUL, test.get(7)); 376 assertTrue(test.containsKey(8)); 377 assertEquals(Month.AUG, test.get(8)); 378 assertTrue(test.containsKey(9)); 379 assertEquals(Month.SEP, test.get(9)); 380 assertTrue(test.containsKey(10)); 381 assertEquals(Month.OCT, test.get(10)); 382 assertTrue(test.containsKey(11)); 383 assertEquals(Month.NOV, test.get(11)); 384 assertTrue(test.containsKey(12)); 385 assertEquals(Month.DEC, test.get(12)); 386 assertFalse(test.containsKey(13)); 387 } 388 389 @Test test_getEnumSystemProperty()390 public void test_getEnumSystemProperty() { 391 final String key = getClass().getName(); 392 System.setProperty(key, Traffic.RED.toString()); 393 try { 394 assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(Traffic.class, key, null)); 395 assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(Traffic.class, "?", Traffic.RED)); 396 assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(null, null, Traffic.RED)); 397 assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(null, "?", Traffic.RED)); 398 assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(Traffic.class, null, Traffic.RED)); 399 } finally { 400 System.getProperties().remove(key); 401 } 402 } 403 404 @Test test_getFirstEnumIgnoreCase_defaultEnum()405 public void test_getFirstEnumIgnoreCase_defaultEnum() { 406 final Function<Traffic2, String> f = Traffic2::getLabel; 407 assertEquals(Traffic2.RED, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, "***red***", f, Traffic2.AMBER)); 408 assertEquals(Traffic2.AMBER, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, "**Amber**", f, Traffic2.GREEN)); 409 assertEquals(Traffic2.GREEN, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, "*grEEn*", f, Traffic2.RED)); 410 assertEquals(Traffic2.AMBER, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, "PURPLE", f, Traffic2.AMBER)); 411 assertEquals(Traffic2.GREEN, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, "purple", f, Traffic2.GREEN)); 412 assertEquals(Traffic2.RED, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, "pUrPlE", f, Traffic2.RED)); 413 assertEquals(Traffic2.AMBER, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, null, f, Traffic2.AMBER)); 414 assertEquals(Traffic2.GREEN, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, null, f, Traffic2.GREEN)); 415 assertEquals(Traffic2.RED, EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, null, f, Traffic2.RED)); 416 assertNull(EnumUtils.getFirstEnumIgnoreCase(Traffic2.class, "PURPLE", f, null)); 417 } 418 419 @Test test_isValidEnum()420 public void test_isValidEnum() { 421 assertTrue(EnumUtils.isValidEnum(Traffic.class, "RED")); 422 assertTrue(EnumUtils.isValidEnum(Traffic.class, "AMBER")); 423 assertTrue(EnumUtils.isValidEnum(Traffic.class, "GREEN")); 424 assertFalse(EnumUtils.isValidEnum(Traffic.class, "PURPLE")); 425 assertFalse(EnumUtils.isValidEnum(Traffic.class, null)); 426 } 427 428 @Test test_isValidEnum_nullClass()429 public void test_isValidEnum_nullClass() { 430 assertThrows(NullPointerException.class, () -> EnumUtils.isValidEnum(null, "PURPLE")); 431 } 432 433 @Test test_isValidEnumIgnoreCase()434 public void test_isValidEnumIgnoreCase() { 435 assertTrue(EnumUtils.isValidEnumIgnoreCase(Traffic.class, "red")); 436 assertTrue(EnumUtils.isValidEnumIgnoreCase(Traffic.class, "Amber")); 437 assertTrue(EnumUtils.isValidEnumIgnoreCase(Traffic.class, "grEEn")); 438 assertFalse(EnumUtils.isValidEnumIgnoreCase(Traffic.class, "purple")); 439 assertFalse(EnumUtils.isValidEnumIgnoreCase(Traffic.class, null)); 440 } 441 442 @Test test_isValidEnumIgnoreCase_nullClass()443 public void test_isValidEnumIgnoreCase_nullClass() { 444 assertThrows(NullPointerException.class, () -> EnumUtils.isValidEnumIgnoreCase(null, "PURPLE")); 445 } 446 447 @Test test_processBitVector()448 public void test_processBitVector() { 449 assertEquals(EnumSet.noneOf(Traffic.class), EnumUtils.processBitVector(Traffic.class, 0L)); 450 assertEquals(EnumSet.of(Traffic.RED), EnumUtils.processBitVector(Traffic.class, 1L)); 451 assertEquals(EnumSet.of(Traffic.AMBER), EnumUtils.processBitVector(Traffic.class, 2L)); 452 assertEquals(EnumSet.of(Traffic.RED, Traffic.AMBER), EnumUtils.processBitVector(Traffic.class, 3L)); 453 assertEquals(EnumSet.of(Traffic.GREEN), EnumUtils.processBitVector(Traffic.class, 4L)); 454 assertEquals(EnumSet.of(Traffic.RED, Traffic.GREEN), EnumUtils.processBitVector(Traffic.class, 5L)); 455 assertEquals(EnumSet.of(Traffic.AMBER, Traffic.GREEN), EnumUtils.processBitVector(Traffic.class, 6L)); 456 assertEquals(EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN), 457 EnumUtils.processBitVector(Traffic.class, 7L)); 458 459 // 64 values Enum (to test whether no int<->long jdk conversion issue exists) 460 assertEquals(EnumSet.of(Enum64.A31), EnumUtils.processBitVector(Enum64.class, (1L << 31))); 461 assertEquals(EnumSet.of(Enum64.A32), EnumUtils.processBitVector(Enum64.class, (1L << 32))); 462 assertEquals(EnumSet.of(Enum64.A63), EnumUtils.processBitVector(Enum64.class, (1L << 63))); 463 assertEquals(EnumSet.of(Enum64.A63), EnumUtils.processBitVector(Enum64.class, Long.MIN_VALUE)); 464 } 465 466 @Test test_processBitVector_longClass()467 public void test_processBitVector_longClass() { 468 assertThrows(IllegalArgumentException.class, () -> EnumUtils.processBitVector(TooMany.class, 0L)); 469 } 470 471 @Test test_processBitVector_nullClass()472 public void test_processBitVector_nullClass() { 473 final Class<Traffic> empty = null; 474 assertThrows(NullPointerException.class, () -> EnumUtils.processBitVector(empty, 0L)); 475 } 476 477 @Test test_processBitVectors()478 public void test_processBitVectors() { 479 assertEquals(EnumSet.noneOf(Traffic.class), EnumUtils.processBitVectors(Traffic.class, 0L)); 480 assertEquals(EnumSet.of(Traffic.RED), EnumUtils.processBitVectors(Traffic.class, 1L)); 481 assertEquals(EnumSet.of(Traffic.AMBER), EnumUtils.processBitVectors(Traffic.class, 2L)); 482 assertEquals(EnumSet.of(Traffic.RED, Traffic.AMBER), EnumUtils.processBitVectors(Traffic.class, 3L)); 483 assertEquals(EnumSet.of(Traffic.GREEN), EnumUtils.processBitVectors(Traffic.class, 4L)); 484 assertEquals(EnumSet.of(Traffic.RED, Traffic.GREEN), EnumUtils.processBitVectors(Traffic.class, 5L)); 485 assertEquals(EnumSet.of(Traffic.AMBER, Traffic.GREEN), EnumUtils.processBitVectors(Traffic.class, 6L)); 486 assertEquals(EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN), 487 EnumUtils.processBitVectors(Traffic.class, 7L)); 488 489 assertEquals(EnumSet.noneOf(Traffic.class), EnumUtils.processBitVectors(Traffic.class, 0L, 0L)); 490 assertEquals(EnumSet.of(Traffic.RED), EnumUtils.processBitVectors(Traffic.class, 0L, 1L)); 491 assertEquals(EnumSet.of(Traffic.AMBER), EnumUtils.processBitVectors(Traffic.class, 0L, 2L)); 492 assertEquals(EnumSet.of(Traffic.RED, Traffic.AMBER), EnumUtils.processBitVectors(Traffic.class, 0L, 3L)); 493 assertEquals(EnumSet.of(Traffic.GREEN), EnumUtils.processBitVectors(Traffic.class, 0L, 4L)); 494 assertEquals(EnumSet.of(Traffic.RED, Traffic.GREEN), EnumUtils.processBitVectors(Traffic.class, 0L, 5L)); 495 assertEquals(EnumSet.of(Traffic.AMBER, Traffic.GREEN), EnumUtils.processBitVectors(Traffic.class, 0L, 6L)); 496 assertEquals(EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN), 497 EnumUtils.processBitVectors(Traffic.class, 0L, 7L)); 498 499 // demonstrate tolerance of irrelevant high-order digits: 500 assertEquals(EnumSet.noneOf(Traffic.class), EnumUtils.processBitVectors(Traffic.class, 666L, 0L)); 501 assertEquals(EnumSet.of(Traffic.RED), EnumUtils.processBitVectors(Traffic.class, 666L, 1L)); 502 assertEquals(EnumSet.of(Traffic.AMBER), EnumUtils.processBitVectors(Traffic.class, 666L, 2L)); 503 assertEquals(EnumSet.of(Traffic.RED, Traffic.AMBER), EnumUtils.processBitVectors(Traffic.class, 666L, 3L)); 504 assertEquals(EnumSet.of(Traffic.GREEN), EnumUtils.processBitVectors(Traffic.class, 666L, 4L)); 505 assertEquals(EnumSet.of(Traffic.RED, Traffic.GREEN), EnumUtils.processBitVectors(Traffic.class, 666L, 5L)); 506 assertEquals(EnumSet.of(Traffic.AMBER, Traffic.GREEN), EnumUtils.processBitVectors(Traffic.class, 666L, 6L)); 507 assertEquals(EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN), 508 EnumUtils.processBitVectors(Traffic.class, 666L, 7L)); 509 510 // 64 values Enum (to test whether no int<->long jdk conversion issue exists) 511 assertEquals(EnumSet.of(Enum64.A31), EnumUtils.processBitVectors(Enum64.class, (1L << 31))); 512 assertEquals(EnumSet.of(Enum64.A32), EnumUtils.processBitVectors(Enum64.class, (1L << 32))); 513 assertEquals(EnumSet.of(Enum64.A63), EnumUtils.processBitVectors(Enum64.class, (1L << 63))); 514 assertEquals(EnumSet.of(Enum64.A63), EnumUtils.processBitVectors(Enum64.class, Long.MIN_VALUE)); 515 } 516 517 @Test test_processBitVectors_longClass()518 public void test_processBitVectors_longClass() { 519 assertEquals(EnumSet.noneOf(TooMany.class), EnumUtils.processBitVectors(TooMany.class, 0L)); 520 assertEquals(EnumSet.of(TooMany.A), EnumUtils.processBitVectors(TooMany.class, 1L)); 521 assertEquals(EnumSet.of(TooMany.B), EnumUtils.processBitVectors(TooMany.class, 2L)); 522 assertEquals(EnumSet.of(TooMany.A, TooMany.B), EnumUtils.processBitVectors(TooMany.class, 3L)); 523 assertEquals(EnumSet.of(TooMany.C), EnumUtils.processBitVectors(TooMany.class, 4L)); 524 assertEquals(EnumSet.of(TooMany.A, TooMany.C), EnumUtils.processBitVectors(TooMany.class, 5L)); 525 assertEquals(EnumSet.of(TooMany.B, TooMany.C), EnumUtils.processBitVectors(TooMany.class, 6L)); 526 assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.C), EnumUtils.processBitVectors(TooMany.class, 7L)); 527 528 assertEquals(EnumSet.noneOf(TooMany.class), EnumUtils.processBitVectors(TooMany.class, 0L, 0L)); 529 assertEquals(EnumSet.of(TooMany.A), EnumUtils.processBitVectors(TooMany.class, 0L, 1L)); 530 assertEquals(EnumSet.of(TooMany.B), EnumUtils.processBitVectors(TooMany.class, 0L, 2L)); 531 assertEquals(EnumSet.of(TooMany.A, TooMany.B), EnumUtils.processBitVectors(TooMany.class, 0L, 3L)); 532 assertEquals(EnumSet.of(TooMany.C), EnumUtils.processBitVectors(TooMany.class, 0L, 4L)); 533 assertEquals(EnumSet.of(TooMany.A, TooMany.C), EnumUtils.processBitVectors(TooMany.class, 0L, 5L)); 534 assertEquals(EnumSet.of(TooMany.B, TooMany.C), EnumUtils.processBitVectors(TooMany.class, 0L, 6L)); 535 assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.C), EnumUtils.processBitVectors(TooMany.class, 0L, 7L)); 536 assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.C), EnumUtils.processBitVectors(TooMany.class, 0L, 7L)); 537 538 assertEquals(EnumSet.of(TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 1L, 0L)); 539 assertEquals(EnumSet.of(TooMany.A, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 1L, 1L)); 540 assertEquals(EnumSet.of(TooMany.B, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 1L, 2L)); 541 assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 1L, 3L)); 542 assertEquals(EnumSet.of(TooMany.C, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 1L, 4L)); 543 assertEquals(EnumSet.of(TooMany.A, TooMany.C, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 1L, 5L)); 544 assertEquals(EnumSet.of(TooMany.B, TooMany.C, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 1L, 6L)); 545 assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.C, TooMany.M2), 546 EnumUtils.processBitVectors(TooMany.class, 1L, 7L)); 547 assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.C, TooMany.M2), 548 EnumUtils.processBitVectors(TooMany.class, 1L, 7L)); 549 550 // demonstrate tolerance of irrelevant high-order digits: 551 assertEquals(EnumSet.of(TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 9L, 0L)); 552 assertEquals(EnumSet.of(TooMany.A, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 9L, 1L)); 553 assertEquals(EnumSet.of(TooMany.B, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 9L, 2L)); 554 assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 9L, 3L)); 555 assertEquals(EnumSet.of(TooMany.C, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 9L, 4L)); 556 assertEquals(EnumSet.of(TooMany.A, TooMany.C, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 9L, 5L)); 557 assertEquals(EnumSet.of(TooMany.B, TooMany.C, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 9L, 6L)); 558 assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.C, TooMany.M2), 559 EnumUtils.processBitVectors(TooMany.class, 9L, 7L)); 560 assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.C, TooMany.M2), 561 EnumUtils.processBitVectors(TooMany.class, 9L, 7L)); 562 } 563 564 @Test test_processBitVectors_nullClass()565 public void test_processBitVectors_nullClass() { 566 final Class<Traffic> empty = null; 567 assertThrows(NullPointerException.class, () -> EnumUtils.processBitVectors(empty, 0L)); 568 } 569 570 @Test testConstructable()571 public void testConstructable() { 572 // enforce public constructor 573 new EnumUtils(); 574 } 575 576 } 577 578 enum Month { 579 JAN(1), FEB(2), MAR(3), APR(4), MAY(5), JUN(6), JUL(7), AUG(8), SEP(9), OCT(10), NOV(11), DEC(12); 580 581 private final int id; 582 Month(final int id)583 Month(final int id) { 584 this.id = id; 585 } 586 getId()587 public int getId() { 588 return this.id; 589 } 590 } 591 592 enum TooMany { 593 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, A1, B1, C1, D1, E1, F1, G1, H1, I1, 594 J1, K1, L1, M1, N1, O1, P1, Q1, R1, S1, T1, U1, V1, W1, X1, Y1, Z1, A2, B2, C2, D2, E2, F2, G2, H2, I2, J2, K2, L2, 595 M2 596 } 597 598 enum Traffic { 599 RED, AMBER, GREEN 600 } 601 602 enum Traffic2 { 603 604 RED("***Red***"), AMBER("**Amber**"), GREEN("*green*"); 605 606 final String label; 607 Traffic2(final String label)608 Traffic2(final String label) { 609 this.label = label; 610 } 611 getLabel()612 public String getLabel() { 613 return label; 614 } 615 } 616