J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4319910 |
| 27 | * @summary Verify that exceptions are thrown as expected. |
| 28 | */ |
| 29 | |
| 30 | public class Exceptions { |
| 31 | private static boolean ok = true; |
| 32 | |
| 33 | int f0; |
| 34 | public int f1; |
| 35 | private int f2; |
| 36 | protected int f4; |
| 37 | |
| 38 | private static final String [] npe = {null}; |
| 39 | private static final String [] nsfe = {"f0", "f2", "f4", "f6"}; |
| 40 | private static final String [] pass = {"f1"}; |
| 41 | |
| 42 | private void test(String s, Class ex) { |
| 43 | Throwable t = null; |
| 44 | try { |
| 45 | getClass().getField(s); |
| 46 | } catch (Throwable x) { |
| 47 | if (ex.isAssignableFrom(x.getClass())) |
| 48 | t = x; |
| 49 | } |
| 50 | if ((t == null) && (ex != null)) { |
| 51 | ok = false; |
| 52 | System.out.println("expected " + ex.getName() + " for " + s |
| 53 | + " -- FAILED"); |
| 54 | } else { |
| 55 | System.out.println(s + " -- OK"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public static void main(String [] args) { |
| 60 | Exceptions e = new Exceptions(); |
| 61 | for (int i = 0; i < npe.length; i++) |
| 62 | e.test(npe[i], NullPointerException.class); |
| 63 | for (int i = 0; i < nsfe.length; i++) |
| 64 | e.test(nsfe[i], NoSuchFieldException.class); |
| 65 | for (int i = 0; i < pass.length; i++) |
| 66 | e.test(pass[i], null); |
| 67 | if (!ok) |
| 68 | throw new RuntimeException("some tests failed"); |
| 69 | } |
| 70 | } |