1 /*
   2  * Copyright (c) 2005, 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 /**
  25  * @test
  26  * @bug 6248507
  27  * @summary Verify that exceptions are thrown as expected.
  28  */
  29 
  30 public class Exceptions {
  31     private static boolean ok = true;
  32 
  33     private static void fail(Throwable ex, String s, Throwable got) {
  34         ok = false;
  35         System.err.println("expected "
  36                            + ex.getClass().getName() + ": " + ex.getMessage()
  37                            + " for " + s
  38                            + " got "
  39                            + got.getClass().getName() + ": " + got.getMessage()
  40                            + " - FAILED");
  41     }
  42 
  43     private static void pass(String s) {
  44         System.out.println(s + " -- OK");
  45     }
  46 
  47     private static void tryCatch(String s, Throwable ex, Runnable thunk) {
  48         Throwable t = null;
  49         try {
  50             thunk.run();
  51         } catch (Throwable x) {
  52 //          x.printStackTrace();
  53             if (ex.getClass().isAssignableFrom(x.getClass()))
  54                 t = x;
  55             else
  56                 x.printStackTrace();
  57         }
  58         if ((t == null) && (ex != null))
  59             fail(ex, s, t);
  60 
  61         String msg = (ex == null ? null : ex.getMessage());
  62         if ((msg != null) && !msg.equals(t.getMessage()))
  63             fail(ex, s, t);
  64         else
  65             pass(s);
  66     }
  67 
  68     public static void main(String [] args) {
  69         System.out.println("StringBuilder()");
  70         tryCatch("  no args", null, new Runnable() {
  71                 public void run() {
  72                     new StringBuilder();
  73                 }});
  74 
  75         System.out.println("StringBuilder(int length)");
  76         tryCatch("  1", null, new Runnable() {
  77                 public void run() {
  78                     new StringBuilder(1);
  79                 }});
  80         tryCatch("  -1", new NegativeArraySizeException(), new Runnable() {
  81                 public void run() {
  82                     new StringBuilder(-1);
  83                 }});
  84 
  85         System.out.println("StringBuilder(String str)");
  86         tryCatch("  null", new NullPointerException(), new Runnable() {
  87                 public void run() {
  88                     new StringBuilder(null);
  89                 }});
  90         tryCatch("  foo", null, new Runnable() {
  91                 public void run() {
  92                     new StringBuilder("foo");
  93                 }});
  94 
  95         System.out.println("StringBuilder.replace(int start, int end, String str)");
  96         tryCatch("  -1, 2, \" \"",
  97                  new StringIndexOutOfBoundsException(-1),
  98                  new Runnable() {
  99                 public void run() {
 100                     StringBuilder sb = new StringBuilder("hilbert");
 101                     sb.replace(-1, 2, " ");
 102                 }});
 103         tryCatch("  7, 8, \" \"",
 104                  new StringIndexOutOfBoundsException("start > length()"),
 105                  new Runnable() {
 106                 public void run() {
 107                     StringBuilder sb = new StringBuilder("banach");
 108                     sb.replace(7, 8, " ");
 109                 }});
 110         tryCatch("  2, 1, \" \"",
 111                  new StringIndexOutOfBoundsException("start > end"),
 112                  new Runnable() {
 113                 public void run() {
 114                     StringBuilder sb = new StringBuilder("riemann");
 115                     sb.replace(2, 1, " ");
 116                 }});
 117 
 118         if (!ok)
 119             throw new RuntimeException("Some tests FAILED");
 120         else
 121             System.out.println("All tests PASSED");
 122     }
 123 }