Path: blob/master/test/jdk/com/sun/jndi/dns/Test6991580.java
41153 views
1/*2* Copyright (c) 2015, 2017, 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324import java.awt.Button;25import java.awt.Dialog;26import java.awt.Frame;27import java.awt.Panel;28import java.awt.TextArea;29import java.awt.event.ActionEvent;30import java.awt.event.ActionListener;3132/*33* @test34* @bug 6991580 8080108 813303535* @summary IPv6 Nameservers in resolv.conf throws NumberFormatException36* @modules java.desktop37* jdk.naming.dns/com.sun.jndi.dns38* @requires os.family != "windows"39* @build IPv6NameserverPlatformParsingTest40* @run main/manual Test699158041*/424344public class Test6991580 {4546private static void init() throws Exception {47//*** Create instructions for the user here ***4849String[] instructions =50{51"This test should only be run on non-Windows systems.",52"If your system doesn't meet this condition, press PASS.",53"To run the test follow these instructions:",54"1. Open a terminal window.",55"2. Make sure you have, for example, the following snippet "56+ "into your platform's /etc/resolv.conf:",57"nameserver 127.0.0.1",58"nameserver 2001:4860:4860::8888",59"nameserver [::1]:5353",60"nameserver 127.0.0.1:5353",61"Modify the /etc/resolv.conf file if needed. "62+ "Don't forget to save the original content of the file.",63"3. Type \"cd " + System.getProperty("test.classes") + "\".",64"4. Type \"" + System.getProperty("java.home") +65"/bin/java IPv6NameserverPlatformParsingTest\".",66"5. If you see",67"\"PASS: Found IPv6 address and DnsClient parsed it correctly.\"",68", press PASS else press FAIL.",69"6. If you modified /etc/resolv.conf on the step #2, "70+ "please, restore the original content of the file."71};7273Sysout.createDialog( );74Sysout.printInstructions( instructions );75}7677/*****************************************************78Standard Test Machinery Section79DO NOT modify anything in this section -- it's a80standard chunk of code which has all of the81synchronisation necessary for the test harness.82By keeping it the same in all tests, it is easier83to read and understand someone else's test, as84well as insuring that all tests behave correctly85with the test harness.86There is a section following this for test-defined87classes88******************************************************/89private static boolean theTestPassed = false;90private static boolean testGeneratedInterrupt = false;91private static String failureMessage = "";9293private static Thread mainThread = null;9495private static int sleepTime = 300000;9697public static void main( String args[] ) throws Exception98{99mainThread = Thread.currentThread();100try101{102init();103}104catch( TestPassedException e )105{106//The test passed, so just return from main and harness will107// interepret this return as a pass108return;109}110//At this point, neither test passed nor test failed has been111// called -- either would have thrown an exception and ended the112// test, so we know we have multiple threads.113114//Test involves other threads, so sleep and wait for them to115// called pass() or fail()116try117{118Thread.sleep( sleepTime );119//Timed out, so fail the test120throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );121}122catch (InterruptedException e)123{124if( ! testGeneratedInterrupt ) throw e;125126//reset flag in case hit this code more than once for some reason (just safety)127testGeneratedInterrupt = false;128if ( theTestPassed == false )129{130throw new RuntimeException( failureMessage );131}132}133134}//main135136public static synchronized void setTimeoutTo( int seconds )137{138sleepTime = seconds * 1000;139}140141public static synchronized void pass()142{143Sysout.println( "The test passed." );144Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );145//first check if this is executing in main thread146if ( mainThread == Thread.currentThread() )147{148//Still in the main thread, so set the flag just for kicks,149// and throw a test passed exception which will be caught150// and end the test.151theTestPassed = true;152throw new TestPassedException();153}154//pass was called from a different thread, so set the flag and interrupt155// the main thead.156theTestPassed = true;157testGeneratedInterrupt = true;158mainThread.interrupt();159}//pass()160161public static synchronized void fail()162{163//test writer didn't specify why test failed, so give generic164fail( "it just plain failed! :-)" );165}166167public static synchronized void fail( String whyFailed )168{169Sysout.println( "The test failed: " + whyFailed );170Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );171//check if this called from main thread172if ( mainThread == Thread.currentThread() )173{174//If main thread, fail now 'cause not sleeping175throw new RuntimeException( whyFailed );176}177theTestPassed = false;178testGeneratedInterrupt = true;179failureMessage = whyFailed;180mainThread.interrupt();181}//fail()182183}184185//This exception is used to exit from any level of call nesting186// when it's determined that the test has passed, and immediately187// end the test.188class TestPassedException extends RuntimeException189{190}191192//*********** End Standard Test Machinery Section **********193194195/****************************************************196Standard Test Machinery197DO NOT modify anything below -- it's a standard198chunk of code whose purpose is to make user199interaction uniform, and thereby make it simpler200to read and understand someone else's test.201****************************************************/202203/**204This is part of the standard test machinery.205It creates a dialog (with the instructions), and is the interface206for sending text messages to the user.207To print the instructions, send an array of strings to Sysout.createDialog208WithInstructions method. Put one line of instructions per array entry.209To display a message for the tester to see, simply call Sysout.println210with the string to be displayed.211This mimics System.out.println but works within the test harness as well212as standalone.213*/214215class Sysout216{217private static TestDialog dialog;218219public static void createDialogWithInstructions( String[] instructions )220{221dialog = new TestDialog( new Frame(), "Instructions" );222dialog.printInstructions( instructions );223dialog.show();224println( "Any messages for the tester will display here." );225}226227public static void createDialog( )228{229dialog = new TestDialog( new Frame(), "Instructions" );230String[] defInstr = { "Instructions will appear here. ", "" } ;231dialog.printInstructions( defInstr );232dialog.show();233println( "Any messages for the tester will display here." );234}235236237public static void printInstructions( String[] instructions )238{239dialog.printInstructions( instructions );240}241242243public static void println( String messageIn )244{245dialog.displayMessage( messageIn );246}247248}// Sysout class249250/**251This is part of the standard test machinery. It provides a place for the252test instructions to be displayed, and a place for interactive messages253to the user to be displayed.254To have the test instructions displayed, see Sysout.255To have a message to the user be displayed, see Sysout.256Do not call anything in this dialog directly.257*/258class TestDialog extends Dialog implements ActionListener259{260261TextArea instructionsText;262TextArea messageText;263int maxStringLength = 120;264Panel buttonP = new Panel();265Button passB = new Button( "pass" );266Button failB = new Button( "fail" );267268//DO NOT call this directly, go through Sysout269public TestDialog( Frame frame, String name )270{271super( frame, name );272int scrollBoth = TextArea.SCROLLBARS_BOTH;273instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );274add( "North", instructionsText );275276messageText = new TextArea( "", 5, maxStringLength, scrollBoth );277add("Center", messageText);278279passB = new Button( "pass" );280passB.setActionCommand( "pass" );281passB.addActionListener( this );282buttonP.add( "East", passB );283284failB = new Button( "fail" );285failB.setActionCommand( "fail" );286failB.addActionListener( this );287buttonP.add( "West", failB );288289add( "South", buttonP );290pack();291292show();293}// TestDialog()294295//DO NOT call this directly, go through Sysout296public void printInstructions( String[] instructions )297{298//Clear out any current instructions299instructionsText.setText( "" );300301//Go down array of instruction strings302303String printStr, remainingStr;304for( int i=0; i < instructions.length; i++ )305{306//chop up each into pieces maxSringLength long307remainingStr = instructions[ i ];308while( remainingStr.length() > 0 )309{310//if longer than max then chop off first max chars to print311if( remainingStr.length() >= maxStringLength )312{313//Try to chop on a word boundary314int posOfSpace = remainingStr.315lastIndexOf( ' ', maxStringLength - 1 );316317if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;318319printStr = remainingStr.substring( 0, posOfSpace + 1 );320remainingStr = remainingStr.substring( posOfSpace + 1 );321}322//else just print323else324{325printStr = remainingStr;326remainingStr = "";327}328329instructionsText.append( printStr + "\n" );330331}// while332333}// for334335}//printInstructions()336337//DO NOT call this directly, go through Sysout338public void displayMessage( String messageIn )339{340messageText.append( messageIn + "\n" );341}342343//catch presses of the passed and failed buttons.344//simply call the standard pass() or fail() static methods of345//DialogOrient346@Override347public void actionPerformed( ActionEvent e )348{349if( "pass".equals(e.getActionCommand()) )350{351Test6991580.pass();352}353else354{355Test6991580.fail();356}357}358359}360361362