1 /* 2 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @summary check if LookupPolicy.of correctly handles valid and illegal 27 * combinations of characteristics bit mask flags. 28 * @run testng LookupPolicyOfTest 29 */ 30 31 import org.testng.annotations.DataProvider; 32 import org.testng.annotations.Test; 33 34 import java.net.spi.InetAddressResolver.LookupPolicy; 35 import java.util.List; 36 37 import static java.net.spi.InetAddressResolver.LookupPolicy.IPV4; 38 import static java.net.spi.InetAddressResolver.LookupPolicy.IPV4_FIRST; 39 import static java.net.spi.InetAddressResolver.LookupPolicy.IPV6; 40 import static java.net.spi.InetAddressResolver.LookupPolicy.IPV6_FIRST; 41 42 public class LookupPolicyOfTest { 43 44 @Test(dataProvider = "validCharacteristics") 45 public void testValidCharacteristicCombinations(List<Integer> validCombination) { 46 LookupPolicy.of(bitFlagsToCharacteristicsValue(validCombination)); 47 } 48 49 @Test(dataProvider = "invalidCharacteristics", expectedExceptions = IllegalArgumentException.class) 50 public void testInvalidCharacteristicCombinations(List<Integer> invalidCombination) { 51 LookupPolicy.of(bitFlagsToCharacteristicsValue(invalidCombination)); 52 } 53 54 @DataProvider(name = "validCharacteristics") 55 public Object[][] validCharacteristicValue() { 56 return new Object[][]{ 57 {List.of(IPV4)}, 58 {List.of(IPV4, IPV4_FIRST)}, 59 {List.of(IPV6)}, 60 {List.of(IPV6, IPV6_FIRST)}, 61 {List.of(IPV4, IPV6)}, 62 {List.of(IPV4, IPV6, IPV4_FIRST)}, 63 {List.of(IPV4, IPV6, IPV6_FIRST)}, 64 // Custom flag values alongside to address type flags 65 // that could be used by custom providers 66 {List.of(IPV4, IPV6, 0x10)}, 67 {List.of(IPV4, IPV6, 0x20)}, 68 }; 69 } 70 71 @DataProvider(name = "invalidCharacteristics") 72 public Object[][] illegalCharacteristicValue() { 73 return new Object[][]{ 74 {List.of()}, 75 {List.of(IPV4_FIRST)}, 76 {List.of(IPV6_FIRST)}, 77 {List.of(IPV4_FIRST, IPV6_FIRST)}, 78 {List.of(IPV4, IPV6_FIRST)}, 79 {List.of(IPV6, IPV4_FIRST)}, 80 {List.of(IPV4, IPV6, IPV4_FIRST, IPV6_FIRST)}, 81 }; 82 } 83 84 private static int bitFlagsToCharacteristicsValue(List<Integer> bitFlagsList) { 85 return bitFlagsList.stream() 86 .reduce(0, (flag1, flag2) -> flag1 | flag2); 87 } 88 }