< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/stress/except/except004.java

Print this page
rev 50289 : [mq]: jep181-rev5


 194         } catch (Throwable unexpected) {
 195             out.println("Test pre-initialisation failed: " + unexpected);
 196             return 2;
 197         }
 198 
 199         if (TRACE_ON)
 200             out.println("Running with heap exhaustion");
 201 
 202         return runTests(out, true);
 203     }
 204 
 205     private static int runTests(PrintStream out, boolean exhaustHeap) {
 206         // reset message index
 207         messages = 0;
 208 
 209         // Prepare some items, which will be used by the test:
 210         Object stringArray[] = new String[1];
 211         Object integerValue = new Integer(0);
 212         Object doubleValue = new Double(0);
 213         Object trash = null;
 214         Field abraPrivateField;
 215         Field abraIntegerField;
 216         Field abraBooleanField;

 217         try {
 218             abraPrivateField = Abra.class.getDeclaredField("DONT_TOUCH_ME");
 219             abraIntegerField = Abra.class.getDeclaredField("MAIN_CYR_NUMBER");
 220             abraBooleanField = Abra.class.getDeclaredField("NOT_AN_INTEGER");

 221         } catch (NoSuchFieldException nsfe) {
 222             out.println("Test initialisation failed: field not found in class Abra");
 223             return 2;
 224         }
 225 
 226         Abra abra = new Abra("via public constructor");
 227         Abra.Cadabra cadabra = new Abra.Cadabra();
 228         // Sum up exit code:
 229         int exitCode = 0; // apparently PASSED
 230         int skipped = 0;  // some checks may correctly suffer OutOfMemoryError
 231 
 232         int poolSize = 0;
 233         int index = 0;
 234 
 235         if (exhaustHeap) {
 236             pool = null;
 237             // Allocate repository for lots of tiny objects:
 238             for (int size = 1 << 30; size > 0 && pool == null; size >>= 1) {
 239                 try {
 240                     pool = new Object[size];
 241                 } catch (OutOfMemoryError oome) {
 242                 }


 352         }
 353 
 354         // Check CloneNotSupportedException:
 355         try {
 356             trash = abra.clone();    // illegal - should fail
 357 //          trash = cadabra.clone(); //   legal - should pass
 358             log[messages++] = "Failure: CloneNotSupportedException";
 359             exitCode = 2; // FAILED
 360         } catch (CloneNotSupportedException cnse) {
 361             if (TRACE_ON)
 362                 log[messages++] = "Success: CloneNotSupportedException";
 363         } catch (OutOfMemoryError oome) {
 364             if (WARN_ON)
 365                 log[messages++] = "Skipped: CloneNotSupportedException";
 366             skipped++;
 367         }
 368 
 369         // Check IllegalAccessException (positive):
 370         try {
 371             int junkIt = abraIntegerField.getInt(null); //   legal - should pass
 372 //          int junkIt = abraPrivateField.getInt(null); // illegal - should fail
 373             if (TRACE_ON)
 374                 log[messages++] = "Success: IllegalAccessException (positive)";
 375         } catch (IllegalAccessException iae) {
 376             log[messages++] = "Failure: IllegalAccessException (positive)";
 377             exitCode = 2;
 378         } catch (OutOfMemoryError oome) {
 379             if (WARN_ON)
 380                 log[messages++] = "Skipped: IllegalAccessException (positive)";
 381             skipped++;
 382         }
 383 
 384         // Check IllegalAccessException (negative):
 385         try {
 386 //          int junkIt = abraIntegerField.getInt(null); //   legal - should pass
 387             int junkIt = abraPrivateField.getInt(null); // illegal - should fail
 388             log[messages++] = "Failure: IllegalAccessException (negative)";
 389             exitCode = 2; // FAILED
 390         } catch (IllegalAccessException iae) {
 391             if (TRACE_ON)
 392                 log[messages++] = "Success: IllegalAccessException (negative)";
 393         } catch (OutOfMemoryError oome) {
 394             if (WARN_ON)
 395                 log[messages++] = "Skipped: IllegalAccessException (negative)";
 396             skipped++;
 397         }
 398 
 399         // Check IllegalArgumentException (positive):
 400         try {
 401             int junkIt = abraIntegerField.getInt(null); //   legal - should pass
 402 //          int junkIt = abraBooleanField.getInt(null); // illegal - should fail
 403             if (TRACE_ON)
 404                 log[messages++] = "Success: IllegalArgumentException (positive)";
 405         } catch (IllegalAccessException iae) {
 406             log[messages++] =
 407                     "Failure: IllegalArgumentException (positive) incorrectly thrown IllegalAccessException";


 495                     } else {
 496                         e.printStackTrace();
 497                         if (e instanceof RuntimeException)
 498                             throw (RuntimeException) e;
 499                         else if (e instanceof Error)
 500                             throw (Error) e;
 501                         else
 502                             throw new Error("Unexpected checked exception", e);
 503                     }
 504                 } catch (OutOfMemoryError oome) {
 505                 }
 506             }
 507         });
 508         int exitCode = run(args, System.out);
 509         System.exit(exitCode + 95);
 510         // JCK-like exit status.
 511     }
 512 
 513     /**
 514      * This class should be used to check <code>CloneNotSupportedException</code>,
 515      * <code>IllegalAccessException</code>, and <code>IllegalArgumentException</code>.
 516      * The class extends <code>except004</code> in order that its (protected)
 517      * method <code>clone()</code> be available from <code>except004</code>.
 518      */
 519     private static class Abra extends except004 {
 520         /**
 521          * Will try to incorrectly find this class as <code>Cadabra</code>
 522          * instead of <code>Abra$Cadabra</code>.
 523          */
 524         public static class Cadabra implements Cloneable {
 525         }
 526 
 527         /**
 528          * Will try to incorrectly access to this field from outside this class.
 529          */
 530         private static final int DONT_TOUCH_ME = 666;
 531         /**
 532          * Will try to incorrectly access to this field from outside this class.
 533          */
 534         public static final int MAIN_CYR_NUMBER = 47;
 535         /**
 536          * Will try to get this field like <code>int<code> zero.
 537          */
 538         public static final boolean NOT_AN_INTEGER = false;
 539 
 540         /**
 541          * Will try to correctly instantiate <code>Abra.Cadabra</code>,
 542          * not <code>Abra</code>.
 543          */
 544         private Abra() {
 545         }
 546 
 547         /**
 548          * Yet another constructor, which is <code>public</code>.
 549          */
 550         public Abra(String nothingSpecial) {
 551         }
 552     }








 553 }


 194         } catch (Throwable unexpected) {
 195             out.println("Test pre-initialisation failed: " + unexpected);
 196             return 2;
 197         }
 198 
 199         if (TRACE_ON)
 200             out.println("Running with heap exhaustion");
 201 
 202         return runTests(out, true);
 203     }
 204 
 205     private static int runTests(PrintStream out, boolean exhaustHeap) {
 206         // reset message index
 207         messages = 0;
 208 
 209         // Prepare some items, which will be used by the test:
 210         Object stringArray[] = new String[1];
 211         Object integerValue = new Integer(0);
 212         Object doubleValue = new Double(0);
 213         Object trash = null;

 214         Field abraIntegerField;
 215         Field abraBooleanField;
 216         Field extPrivateField;
 217         try {

 218             abraIntegerField = Abra.class.getDeclaredField("MAIN_CYR_NUMBER");
 219             abraBooleanField = Abra.class.getDeclaredField("NOT_AN_INTEGER");
 220             extPrivateField = Ext.class.getDeclaredField("DONT_TOUCH_ME");
 221         } catch (NoSuchFieldException nsfe) {
 222             out.println("Test initialisation failed: field not found: " + nsfe.getMessage());
 223             return 2;
 224         }
 225 
 226         Abra abra = new Abra("via public constructor");
 227         Abra.Cadabra cadabra = new Abra.Cadabra();
 228         // Sum up exit code:
 229         int exitCode = 0; // apparently PASSED
 230         int skipped = 0;  // some checks may correctly suffer OutOfMemoryError
 231 
 232         int poolSize = 0;
 233         int index = 0;
 234 
 235         if (exhaustHeap) {
 236             pool = null;
 237             // Allocate repository for lots of tiny objects:
 238             for (int size = 1 << 30; size > 0 && pool == null; size >>= 1) {
 239                 try {
 240                     pool = new Object[size];
 241                 } catch (OutOfMemoryError oome) {
 242                 }


 352         }
 353 
 354         // Check CloneNotSupportedException:
 355         try {
 356             trash = abra.clone();    // illegal - should fail
 357 //          trash = cadabra.clone(); //   legal - should pass
 358             log[messages++] = "Failure: CloneNotSupportedException";
 359             exitCode = 2; // FAILED
 360         } catch (CloneNotSupportedException cnse) {
 361             if (TRACE_ON)
 362                 log[messages++] = "Success: CloneNotSupportedException";
 363         } catch (OutOfMemoryError oome) {
 364             if (WARN_ON)
 365                 log[messages++] = "Skipped: CloneNotSupportedException";
 366             skipped++;
 367         }
 368 
 369         // Check IllegalAccessException (positive):
 370         try {
 371             int junkIt = abraIntegerField.getInt(null); //   legal - should pass

 372             if (TRACE_ON)
 373                 log[messages++] = "Success: IllegalAccessException (positive)";
 374         } catch (IllegalAccessException iae) {
 375             log[messages++] = "Failure: IllegalAccessException (positive)";
 376             exitCode = 2;
 377         } catch (OutOfMemoryError oome) {
 378             if (WARN_ON)
 379                 log[messages++] = "Skipped: IllegalAccessException (positive)";
 380             skipped++;
 381         }
 382 
 383         // Check IllegalAccessException (negative):
 384         try {
 385             int junkIt = extPrivateField.getInt(null); // illegal - should fail

 386             log[messages++] = "Failure: IllegalAccessException (negative)";
 387             exitCode = 2; // FAILED
 388         } catch (IllegalAccessException iae) {
 389             if (TRACE_ON)
 390                 log[messages++] = "Success: IllegalAccessException (negative)";
 391         } catch (OutOfMemoryError oome) {
 392             if (WARN_ON)
 393                 log[messages++] = "Skipped: IllegalAccessException (negative)";
 394             skipped++;
 395         }
 396 
 397         // Check IllegalArgumentException (positive):
 398         try {
 399             int junkIt = abraIntegerField.getInt(null); //   legal - should pass
 400 //          int junkIt = abraBooleanField.getInt(null); // illegal - should fail
 401             if (TRACE_ON)
 402                 log[messages++] = "Success: IllegalArgumentException (positive)";
 403         } catch (IllegalAccessException iae) {
 404             log[messages++] =
 405                     "Failure: IllegalArgumentException (positive) incorrectly thrown IllegalAccessException";


 493                     } else {
 494                         e.printStackTrace();
 495                         if (e instanceof RuntimeException)
 496                             throw (RuntimeException) e;
 497                         else if (e instanceof Error)
 498                             throw (Error) e;
 499                         else
 500                             throw new Error("Unexpected checked exception", e);
 501                     }
 502                 } catch (OutOfMemoryError oome) {
 503                 }
 504             }
 505         });
 506         int exitCode = run(args, System.out);
 507         System.exit(exitCode + 95);
 508         // JCK-like exit status.
 509     }
 510 
 511     /**
 512      * This class should be used to check <code>CloneNotSupportedException</code>,
 513      * and <code>IllegalArgumentException</code>.
 514      * The class extends <code>except004</code> in order that its (protected)
 515      * method <code>clone()</code> be available from <code>except004</code>.
 516      */
 517     private static class Abra extends except004 {
 518         /**
 519          * Will try to incorrectly find this class as <code>Cadabra</code>
 520          * instead of <code>Abra$Cadabra</code>.
 521          */
 522         public static class Cadabra implements Cloneable {
 523         }
 524 
 525         /**
 526          * Will try to incorrectly access to this field from outside this class.
 527          */




 528         public static final int MAIN_CYR_NUMBER = 47;
 529         /**
 530          * Will try to get this field like <code>int<code> zero.
 531          */
 532         public static final boolean NOT_AN_INTEGER = false;
 533 
 534         /**
 535          * Will try to correctly instantiate <code>Abra.Cadabra</code>,
 536          * not <code>Abra</code>.
 537          */
 538         private Abra() {
 539         }
 540 
 541         /**
 542          * Yet another constructor, which is <code>public</code>.
 543          */
 544         public Abra(String nothingSpecial) {
 545         }
 546     }
 547 }
 548 
 549 /* Package accessible class that has non-accessible private member */
 550 class Ext {
 551     /**
 552      * Will try to incorrectly access to this field from outside this class.
 553      */
 554     private static final int DONT_TOUCH_ME = 666;
 555 }
< prev index next >