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 import org.testng.annotations.BeforeTest; 25 import org.testng.annotations.Test; 26 27 import java.lang.reflect.Field; 28 import java.net.InetAddress; 29 import java.net.UnknownHostException; 30 import java.net.spi.InetAddressResolver; 31 32 import static org.testng.Assert.*; 33 34 /* 35 * @test 36 * @summary white-box test to check that the built-in resolver 37 * is used by default. 38 * @modules java.base/java.net:open 39 * @run testng/othervm BuiltInResolverTest 40 */ 41 42 public class BuiltInResolverTest { 43 44 private Field builtInResolverField, resolverField; 45 46 @BeforeTest 47 public void beforeTest() throws NoSuchFieldException { 48 Class<InetAddress> inetAddressClass = InetAddress.class; 49 // Needs to happen for InetAddress.resolver to be initialized 50 try { 51 InetAddress.getByName("test"); 52 } catch (UnknownHostException e) { 53 // Do nothing, only want to assign resolver 54 } 55 builtInResolverField = inetAddressClass.getDeclaredField("BUILTIN_RESOLVER"); 56 builtInResolverField.setAccessible(true); 57 resolverField = inetAddressClass.getDeclaredField("resolver"); 58 resolverField.setAccessible(true); 59 } 60 61 @Test 62 public void testDefaultNSContext() throws IllegalAccessException { 63 // Test that the resolver used by default is the BUILTIN_RESOLVER 64 Object defaultResolverObject = builtInResolverField.get(InetAddressResolver.class); 65 Object usedResolverObject = resolverField.get(InetAddressResolver.class); 66 67 assertTrue(defaultResolverObject == usedResolverObject); 68 69 String defaultClassName = defaultResolverObject.getClass().getCanonicalName(); 70 String currentClassName = usedResolverObject.getClass().getCanonicalName(); 71 72 assertNotNull(defaultClassName, "defaultClassName not set"); 73 assertNotNull(currentClassName, "currentClassName name not set"); 74 75 assertEquals(currentClassName, defaultClassName, 76 "BUILTIN_RESOLVER resolver was not used."); 77 System.err.println("Resolver used by default is the built-in resolver"); 78 } 79 }