1 /* 2 * Copyright (c) 2015, 2016, 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 java.awt.Button; 25 import java.awt.Dialog; 26 import java.awt.Frame; 27 import java.awt.Panel; 28 import java.awt.TextArea; 29 import java.awt.event.ActionEvent; 30 import java.awt.event.ActionListener; 31 32 /** 33 * @test 34 * @bug 6991580 8080108 8133035 35 * @requires os.family != "windows" 36 * @summary IPv6 Nameservers in resolv.conf throws NumberFormatException 37 * @modules jdk.naming.dns/com.sun.jndi.dns 38 * @build IPv6NameserverPlatformParsingTest 39 * @run main/manual Test6991580 40 */ 41 42 43 public class Test6991580 { 44 45 private static void init() throws Exception { 46 //*** Create instructions for the user here *** 47 48 String[] instructions = 49 { 50 "This test should only be run on non-Windows systems.", 51 "If your system doesn't meet this condition, press PASS.", 52 "To run the test follow these instructions:", 53 "1. Open a terminal window.", 54 "2. Make sure you have, for example, the following snippet " 55 + "into your platform's /etc/resolv.conf:", 56 "nameserver 127.0.0.1", 57 "nameserver 2001:4860:4860::8888", 58 "nameserver [::1]:5353", 59 "nameserver 127.0.0.1:5353", 60 "Modify the /etc/resolv.conf file if needed. " 61 + "Don't forget to save the original content of the file.", 62 "3. Type \"cd " + System.getProperty("test.classes") + "\".", 63 "4. Type \"" + System.getProperty("java.home") + 64 "/bin/java IPv6NameserverPlatformParsingTest\".", 65 "5. If you see", 66 "\"PASS: Found IPv6 address and DnsClient parsed it correctly.\"", 67 ", press PASS else press FAIL.", 68 "6. If you modified /etc/resolv.conf on the step #2, " 69 + "please, restore the original content of the file." 70 }; 71 72 Sysout.createDialog( ); 73 Sysout.printInstructions( instructions ); 74 } 75 76 /***************************************************** 77 Standard Test Machinery Section 78 DO NOT modify anything in this section -- it's a 79 standard chunk of code which has all of the 80 synchronisation necessary for the test harness. 81 By keeping it the same in all tests, it is easier 82 to read and understand someone else's test, as 83 well as insuring that all tests behave correctly 84 with the test harness. 85 There is a section following this for test-defined 86 classes 87 ******************************************************/ 88 private static boolean theTestPassed = false; 89 private static boolean testGeneratedInterrupt = false; 90 private static String failureMessage = ""; 91 92 private static Thread mainThread = null; 93 94 private static int sleepTime = 300000; 95 96 public static void main( String args[] ) throws Exception 97 { 98 mainThread = Thread.currentThread(); 99 try 100 { 101 init(); 102 } 103 catch( TestPassedException e ) 104 { 105 //The test passed, so just return from main and harness will 106 // interepret this return as a pass 107 return; 108 } 109 //At this point, neither test passed nor test failed has been 110 // called -- either would have thrown an exception and ended the 111 // test, so we know we have multiple threads. 112 113 //Test involves other threads, so sleep and wait for them to 114 // called pass() or fail() 115 try 116 { 117 Thread.sleep( sleepTime ); 118 //Timed out, so fail the test 119 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); 120 } 121 catch (InterruptedException e) 122 { 123 if( ! testGeneratedInterrupt ) throw e; 124 125 //reset flag in case hit this code more than once for some reason (just safety) 126 testGeneratedInterrupt = false; 127 if ( theTestPassed == false ) 128 { 129 throw new RuntimeException( failureMessage ); 130 } 131 } 132 133 }//main 134 135 public static synchronized void setTimeoutTo( int seconds ) 136 { 137 sleepTime = seconds * 1000; 138 } 139 140 public static synchronized void pass() 141 { 142 Sysout.println( "The test passed." ); 143 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 144 //first check if this is executing in main thread 145 if ( mainThread == Thread.currentThread() ) 146 { 147 //Still in the main thread, so set the flag just for kicks, 148 // and throw a test passed exception which will be caught 149 // and end the test. 150 theTestPassed = true; 151 throw new TestPassedException(); 152 } 153 //pass was called from a different thread, so set the flag and interrupt 154 // the main thead. 155 theTestPassed = true; 156 testGeneratedInterrupt = true; 157 mainThread.interrupt(); 158 }//pass() 159 160 public static synchronized void fail() 161 { 162 //test writer didn't specify why test failed, so give generic 163 fail( "it just plain failed! :-)" ); 164 } 165 166 public static synchronized void fail( String whyFailed ) 167 { 168 Sysout.println( "The test failed: " + whyFailed ); 169 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 170 //check if this called from main thread 171 if ( mainThread == Thread.currentThread() ) 172 { 173 //If main thread, fail now 'cause not sleeping 174 throw new RuntimeException( whyFailed ); 175 } 176 theTestPassed = false; 177 testGeneratedInterrupt = true; 178 failureMessage = whyFailed; 179 mainThread.interrupt(); 180 }//fail() 181 182 } 183 184 //This exception is used to exit from any level of call nesting 185 // when it's determined that the test has passed, and immediately 186 // end the test. 187 class TestPassedException extends RuntimeException 188 { 189 } 190 191 //*********** End Standard Test Machinery Section ********** 192 193 194 /**************************************************** 195 Standard Test Machinery 196 DO NOT modify anything below -- it's a standard 197 chunk of code whose purpose is to make user 198 interaction uniform, and thereby make it simpler 199 to read and understand someone else's test. 200 ****************************************************/ 201 202 /** 203 This is part of the standard test machinery. 204 It creates a dialog (with the instructions), and is the interface 205 for sending text messages to the user. 206 To print the instructions, send an array of strings to Sysout.createDialog 207 WithInstructions method. Put one line of instructions per array entry. 208 To display a message for the tester to see, simply call Sysout.println 209 with the string to be displayed. 210 This mimics System.out.println but works within the test harness as well 211 as standalone. 212 */ 213 214 class Sysout 215 { 216 private static TestDialog dialog; 217 218 public static void createDialogWithInstructions( String[] instructions ) 219 { 220 dialog = new TestDialog( new Frame(), "Instructions" ); 221 dialog.printInstructions( instructions ); 222 dialog.show(); 223 println( "Any messages for the tester will display here." ); 224 } 225 226 public static void createDialog( ) 227 { 228 dialog = new TestDialog( new Frame(), "Instructions" ); 229 String[] defInstr = { "Instructions will appear here. ", "" } ; 230 dialog.printInstructions( defInstr ); 231 dialog.show(); 232 println( "Any messages for the tester will display here." ); 233 } 234 235 236 public static void printInstructions( String[] instructions ) 237 { 238 dialog.printInstructions( instructions ); 239 } 240 241 242 public static void println( String messageIn ) 243 { 244 dialog.displayMessage( messageIn ); 245 } 246 247 }// Sysout class 248 249 /** 250 This is part of the standard test machinery. It provides a place for the 251 test instructions to be displayed, and a place for interactive messages 252 to the user to be displayed. 253 To have the test instructions displayed, see Sysout. 254 To have a message to the user be displayed, see Sysout. 255 Do not call anything in this dialog directly. 256 */ 257 class TestDialog extends Dialog implements ActionListener 258 { 259 260 TextArea instructionsText; 261 TextArea messageText; 262 int maxStringLength = 120; 263 Panel buttonP = new Panel(); 264 Button passB = new Button( "pass" ); 265 Button failB = new Button( "fail" ); 266 267 //DO NOT call this directly, go through Sysout 268 public TestDialog( Frame frame, String name ) 269 { 270 super( frame, name ); 271 int scrollBoth = TextArea.SCROLLBARS_BOTH; 272 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 273 add( "North", instructionsText ); 274 275 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 276 add("Center", messageText); 277 278 passB = new Button( "pass" ); 279 passB.setActionCommand( "pass" ); 280 passB.addActionListener( this ); 281 buttonP.add( "East", passB ); 282 283 failB = new Button( "fail" ); 284 failB.setActionCommand( "fail" ); 285 failB.addActionListener( this ); 286 buttonP.add( "West", failB ); 287 288 add( "South", buttonP ); 289 pack(); 290 291 show(); 292 }// TestDialog() 293 294 //DO NOT call this directly, go through Sysout 295 public void printInstructions( String[] instructions ) 296 { 297 //Clear out any current instructions 298 instructionsText.setText( "" ); 299 300 //Go down array of instruction strings 301 302 String printStr, remainingStr; 303 for( int i=0; i < instructions.length; i++ ) 304 { 305 //chop up each into pieces maxSringLength long 306 remainingStr = instructions[ i ]; 307 while( remainingStr.length() > 0 ) 308 { 309 //if longer than max then chop off first max chars to print 310 if( remainingStr.length() >= maxStringLength ) 311 { 312 //Try to chop on a word boundary 313 int posOfSpace = remainingStr. 314 lastIndexOf( ' ', maxStringLength - 1 ); 315 316 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 317 318 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 319 remainingStr = remainingStr.substring( posOfSpace + 1 ); 320 } 321 //else just print 322 else 323 { 324 printStr = remainingStr; 325 remainingStr = ""; 326 } 327 328 instructionsText.append( printStr + "\n" ); 329 330 }// while 331 332 }// for 333 334 }//printInstructions() 335 336 //DO NOT call this directly, go through Sysout 337 public void displayMessage( String messageIn ) 338 { 339 messageText.append( messageIn + "\n" ); 340 } 341 342 //catch presses of the passed and failed buttons. 343 //simply call the standard pass() or fail() static methods of 344 //DialogOrient 345 @Override 346 public void actionPerformed( ActionEvent e ) 347 { 348 if( "pass".equals(e.getActionCommand()) ) 349 { 350 Test6991580.pass(); 351 } 352 else 353 { 354 Test6991580.fail(); 355 } 356 } 357 358 }