< prev index next >

test/jdk/com/sun/awt/SecurityWarning/GetSizeShouldNotReturnZero.java

Print this page


   1 /*
   2  * Copyright (c) 2009, 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  */


  33   @build Util CustomSecurityManager CopyClassFile
  34   @run main CopyClassFile CustomSecurityManager bootcp/
  35   @run main/othervm/secure=CustomSecurityManager -Xbootclasspath/a:bootcp GetSizeShouldNotReturnZero
  36 */
  37 
  38 /**
  39  * GetSizeShouldNotReturnZero.java
  40  *
  41  * summary: The size returned by SecurityWarning.getSize() should not be zero
  42  */
  43 
  44 import com.sun.awt.SecurityWarning;
  45 import test.java.awt.regtesthelpers.Util;
  46 
  47 import java.awt.*;
  48 
  49 public class GetSizeShouldNotReturnZero
  50 {
  51     private static void init()
  52     {
  53         String[] instructions =
  54         {
  55             "This is an AUTOMATIC test, simply wait until it is done.",
  56             "The result (passed or failed) will be shown in the",
  57             "message window below."
  58         };
  59         Sysout.createDialog( );
  60         Sysout.printInstructions( instructions );
  61 
  62         Frame f = new Frame();
  63         f.setSize(100, 100);
  64         f.setVisible(true);
  65 
  66         Robot robot = Util.createRobot();
  67         Util.waitForIdle(robot);
  68 
  69         Dimension size = SecurityWarning.getSize(f);
  70         if (size.width == 0 || size.height == 0) {
  71             fail("Reported security warning size: " + size);
  72             return;
  73         }
  74         pass();
  75     }//End  init()
  76 
  77 
  78     /*****************************************************
  79      * Standard Test Machinery Section
  80      * DO NOT modify anything in this section -- it's a
  81      * standard chunk of code which has all of the


 131             if( ! testGeneratedInterrupt ) throw e;
 132 
 133             //reset flag in case hit this code more than once for some reason (just safety)
 134             testGeneratedInterrupt = false;
 135 
 136             if ( theTestPassed == false )
 137             {
 138                 throw new RuntimeException( failureMessage );
 139             }
 140         }
 141 
 142     }//main
 143 
 144     public static synchronized void setTimeoutTo( int seconds )
 145     {
 146         sleepTime = seconds * 1000;
 147     }
 148 
 149     public static synchronized void pass()
 150     {
 151         Sysout.println( "The test passed." );
 152         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 153         //first check if this is executing in main thread
 154         if ( mainThread == Thread.currentThread() )
 155         {
 156             //Still in the main thread, so set the flag just for kicks,
 157             // and throw a test passed exception which will be caught
 158             // and end the test.
 159             theTestPassed = true;
 160             throw new TestPassedException();
 161         }
 162         theTestPassed = true;
 163         testGeneratedInterrupt = true;
 164         mainThread.interrupt();
 165     }//pass()
 166 
 167     public static synchronized void fail()
 168     {
 169         //test writer didn't specify why test failed, so give generic
 170         fail( "it just plain failed! :-)" );
 171     }
 172 
 173     public static synchronized void fail( String whyFailed )
 174     {
 175         Sysout.println( "The test failed: " + whyFailed );
 176         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 177         //check if this called from main thread
 178         if ( mainThread == Thread.currentThread() )
 179         {
 180             //If main thread, fail now 'cause not sleeping
 181             throw new RuntimeException( whyFailed );
 182         }
 183         theTestPassed = false;
 184         testGeneratedInterrupt = true;
 185         failureMessage = whyFailed;
 186         mainThread.interrupt();
 187     }//fail()
 188 
 189 }// class GetSizeShouldNotReturnZero
 190 
 191 //This exception is used to exit from any level of call nesting
 192 // when it's determined that the test has passed, and immediately
 193 // end the test.
 194 class TestPassedException extends RuntimeException
 195 {
 196 }
 197 
 198 //*********** End Standard Test Machinery Section **********
 199 
 200 
 201 //************ Begin classes defined for the test ****************
 202 
 203 // if want to make listeners, here is the recommended place for them, then instantiate
 204 //  them in init()
 205 
 206 /* Example of a class which may be written as part of a test
 207 class NewClass implements anInterface
 208  {
 209    static int newVar = 0;
 210 
 211    public void eventDispatched(AWTEvent e)
 212     {
 213       //Counting events to see if we get enough
 214       eventCount++;
 215 
 216       if( eventCount == 20 )
 217        {
 218          //got enough events, so pass
 219 
 220          GetSizeShouldNotReturnZero.pass();
 221        }
 222       else if( tries == 20 )
 223        {
 224          //tried too many times without getting enough events so fail
 225 
 226          GetSizeShouldNotReturnZero.fail();
 227        }
 228 
 229     }// eventDispatched()
 230 
 231  }// NewClass class
 232 
 233 */
 234 
 235 
 236 //************** End classes defined for the test *******************
 237 
 238 
 239 
 240 
 241 /****************************************************
 242  Standard Test Machinery
 243  DO NOT modify anything below -- it's a standard
 244   chunk of code whose purpose is to make user
 245   interaction uniform, and thereby make it simpler
 246   to read and understand someone else's test.
 247  ****************************************************/
 248 
 249 /**
 250  This is part of the standard test machinery.
 251  It creates a dialog (with the instructions), and is the interface
 252   for sending text messages to the user.
 253  To print the instructions, send an array of strings to Sysout.createDialog
 254   WithInstructions method.  Put one line of instructions per array entry.
 255  To display a message for the tester to see, simply call Sysout.println
 256   with the string to be displayed.
 257  This mimics System.out.println but works within the test harness as well
 258   as standalone.
 259  */
 260 
 261 class Sysout
 262 {
 263     private static TestDialog dialog;
 264 
 265     public static void createDialogWithInstructions( String[] instructions )
 266     {
 267         dialog = new TestDialog( new Frame(), "Instructions" );
 268         dialog.printInstructions( instructions );
 269         dialog.setVisible(true);
 270         println( "Any messages for the tester will display here." );
 271     }
 272 
 273     public static void createDialog( )
 274     {
 275         dialog = new TestDialog( new Frame(), "Instructions" );
 276         String[] defInstr = { "Instructions will appear here. ", "" } ;
 277         dialog.printInstructions( defInstr );
 278         dialog.setVisible(true);
 279         println( "Any messages for the tester will display here." );
 280     }
 281 
 282 
 283     public static void printInstructions( String[] instructions )
 284     {
 285         dialog.printInstructions( instructions );
 286     }
 287 
 288 
 289     public static void println( String messageIn )
 290     {
 291         dialog.displayMessage( messageIn );
 292         System.out.println(messageIn);
 293     }
 294 
 295 }// Sysout  class
 296 
 297 /**
 298   This is part of the standard test machinery.  It provides a place for the
 299    test instructions to be displayed, and a place for interactive messages
 300    to the user to be displayed.
 301   To have the test instructions displayed, see Sysout.
 302   To have a message to the user be displayed, see Sysout.
 303   Do not call anything in this dialog directly.
 304   */
 305 class TestDialog extends Dialog
 306 {
 307 
 308     TextArea instructionsText;
 309     TextArea messageText;
 310     int maxStringLength = 80;
 311 
 312     //DO NOT call this directly, go through Sysout
 313     public TestDialog( Frame frame, String name )
 314     {
 315         super( frame, name );
 316         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 317         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 318         add( "North", instructionsText );
 319 
 320         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 321         add("Center", messageText);
 322 
 323         pack();
 324 
 325         setVisible(true);
 326     }// TestDialog()
 327 
 328     //DO NOT call this directly, go through Sysout
 329     public void printInstructions( String[] instructions )
 330     {
 331         //Clear out any current instructions
 332         instructionsText.setText( "" );
 333 
 334         //Go down array of instruction strings
 335 
 336         String printStr, remainingStr;
 337         for( int i=0; i < instructions.length; i++ )
 338         {
 339             //chop up each into pieces maxSringLength long
 340             remainingStr = instructions[ i ];
 341             while( remainingStr.length() > 0 )
 342             {
 343                 //if longer than max then chop off first max chars to print
 344                 if( remainingStr.length() >= maxStringLength )
 345                 {
 346                     //Try to chop on a word boundary
 347                     int posOfSpace = remainingStr.
 348                         lastIndexOf( ' ', maxStringLength - 1 );
 349 
 350                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 351 
 352                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 353                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 354                 }
 355                 //else just print
 356                 else
 357                 {
 358                     printStr = remainingStr;
 359                     remainingStr = "";
 360                 }
 361 
 362                 instructionsText.append( printStr + "\n" );
 363 
 364             }// while
 365 
 366         }// for
 367 
 368     }//printInstructions()
 369 
 370     //DO NOT call this directly, go through Sysout
 371     public void displayMessage( String messageIn )
 372     {
 373         messageText.append( messageIn + "\n" );
 374         System.out.println(messageIn);
 375     }
 376 
 377 }// TestDialog  class
   1 /*
   2  * Copyright (c) 2009, 2018, 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  */


  33   @build Util CustomSecurityManager CopyClassFile
  34   @run main CopyClassFile CustomSecurityManager bootcp/
  35   @run main/othervm/secure=CustomSecurityManager -Xbootclasspath/a:bootcp GetSizeShouldNotReturnZero
  36 */
  37 
  38 /**
  39  * GetSizeShouldNotReturnZero.java
  40  *
  41  * summary: The size returned by SecurityWarning.getSize() should not be zero
  42  */
  43 
  44 import com.sun.awt.SecurityWarning;
  45 import test.java.awt.regtesthelpers.Util;
  46 
  47 import java.awt.*;
  48 
  49 public class GetSizeShouldNotReturnZero
  50 {
  51     private static void init()
  52     {









  53         Frame f = new Frame();
  54         f.setSize(100, 100);
  55         f.setVisible(true);
  56 
  57         Robot robot = Util.createRobot();
  58         Util.waitForIdle(robot);
  59 
  60         Dimension size = SecurityWarning.getSize(f);
  61         if (size.width == 0 || size.height == 0) {
  62             fail("Reported security warning size: " + size);
  63             return;
  64         }
  65         pass();
  66     }//End  init()
  67 
  68 
  69     /*****************************************************
  70      * Standard Test Machinery Section
  71      * DO NOT modify anything in this section -- it's a
  72      * standard chunk of code which has all of the


 122             if( ! testGeneratedInterrupt ) throw e;
 123 
 124             //reset flag in case hit this code more than once for some reason (just safety)
 125             testGeneratedInterrupt = false;
 126 
 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         System.out.println( "The test passed." );
 143         System.out.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         theTestPassed = true;
 154         testGeneratedInterrupt = true;
 155         mainThread.interrupt();
 156     }//pass()
 157 
 158     public static synchronized void fail()
 159     {
 160         //test writer didn't specify why test failed, so give generic
 161         fail( "it just plain failed! :-)" );
 162     }
 163 
 164     public static synchronized void fail( String whyFailed )
 165     {
 166         System.out.println( "The test failed: " + whyFailed );
 167         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 168         //check if this called from main thread
 169         if ( mainThread == Thread.currentThread() )
 170         {
 171             //If main thread, fail now 'cause not sleeping
 172             throw new RuntimeException( whyFailed );
 173         }
 174         theTestPassed = false;
 175         testGeneratedInterrupt = true;
 176         failureMessage = whyFailed;
 177         mainThread.interrupt();
 178     }//fail()
 179 
 180 }// class GetSizeShouldNotReturnZero
 181 
 182 //This exception is used to exit from any level of call nesting
 183 // when it's determined that the test has passed, and immediately
 184 // end the test.
 185 class TestPassedException extends RuntimeException
 186 {
 187 }





















































































































































































< prev index next >