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.io.*; 25 import java.util.*; 26 27 /* 28 * @test 29 * @bug 6911258 6962571 6963622 6991528 7005628 30 * @summary Basic tests of suppressed exceptions 31 * @author Joseph D. Darcy 32 */ 33 34 public class SuppressedExceptions { 35 private static String message = "Bad suppressed exception information"; 36 37 public static void main(String... args) throws Exception { 38 noSelfSuppression(); 39 basicSupressionTest(); 40 serializationTest(); 41 selfReference(); 42 noModification(); 43 } 44 45 private static void noSelfSuppression() { 46 Throwable throwable = new Throwable(); 47 try { 48 throwable.addSuppressed(throwable); 49 throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown."); 50 } catch (IllegalArgumentException iae) { 51 ; // Expected 52 } 53 } 54 55 private static void basicSupressionTest() { 56 Throwable throwable = new Throwable(); 57 RuntimeException suppressed = new RuntimeException("A suppressed exception."); 58 AssertionError repressed = new AssertionError("A repressed error."); 59 60 Throwable[] t0 = throwable.getSuppressed(); 61 if (t0.length != 0) { 62 throw new RuntimeException(message); 63 } 64 throwable.printStackTrace(); 65 66 throwable.addSuppressed(suppressed); 67 Throwable[] t1 = throwable.getSuppressed(); 68 if (t1.length != 1 || 69 t1[0] != suppressed) {throw new RuntimeException(message); 70 } 71 throwable.printStackTrace(); 189 throw new RuntimeException("Bad nonzero length of suppressed exceptions."); 190 191 Throwable suppressed = new ArithmeticException(); 192 t = new NoSuppression(true); // Suppression enabled 193 // Make sure addSuppressed(null) throws an NPE 194 try { 195 t.addSuppressed(null); 196 throw new RuntimeException("NPE not thrown!"); 197 } catch(NullPointerException e) { 198 ; // Expected 199 } 200 t.addSuppressed(suppressed); 201 t0 = t.getSuppressed(); 202 if (t0.length != 1 || t0[0] != suppressed) 203 throw new RuntimeException("Expected suppression did not occur."); 204 } 205 206 private static class NoSuppression extends Throwable { 207 public NoSuppression(boolean enableSuppression) { 208 super("The medium.", null, enableSuppression, true); 209 } 210 } 211 } | 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.io.*; 25 import java.util.*; 26 27 /* 28 * @test 29 * @bug 6911258 6962571 6963622 6991528 7005628 8012044 30 * @summary Basic tests of suppressed exceptions 31 * @author Joseph D. Darcy 32 */ 33 34 public class SuppressedExceptions { 35 private static String message = "Bad suppressed exception information"; 36 37 public static void main(String... args) throws Exception { 38 noSelfSuppression(); 39 basicSupressionTest(); 40 serializationTest(); 41 selfReference(); 42 noModification(); 43 initCausePlumbing(); 44 } 45 46 private static void noSelfSuppression() { 47 Throwable throwable = new Throwable(); 48 try { 49 throwable.addSuppressed(throwable); 50 throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown."); 51 } catch (IllegalArgumentException iae) { 52 // Expected to be here 53 if (iae.getCause() != throwable) 54 throw new RuntimeException("Bad cause after self-suppresion."); 55 } 56 } 57 58 private static void basicSupressionTest() { 59 Throwable throwable = new Throwable(); 60 RuntimeException suppressed = new RuntimeException("A suppressed exception."); 61 AssertionError repressed = new AssertionError("A repressed error."); 62 63 Throwable[] t0 = throwable.getSuppressed(); 64 if (t0.length != 0) { 65 throw new RuntimeException(message); 66 } 67 throwable.printStackTrace(); 68 69 throwable.addSuppressed(suppressed); 70 Throwable[] t1 = throwable.getSuppressed(); 71 if (t1.length != 1 || 72 t1[0] != suppressed) {throw new RuntimeException(message); 73 } 74 throwable.printStackTrace(); 192 throw new RuntimeException("Bad nonzero length of suppressed exceptions."); 193 194 Throwable suppressed = new ArithmeticException(); 195 t = new NoSuppression(true); // Suppression enabled 196 // Make sure addSuppressed(null) throws an NPE 197 try { 198 t.addSuppressed(null); 199 throw new RuntimeException("NPE not thrown!"); 200 } catch(NullPointerException e) { 201 ; // Expected 202 } 203 t.addSuppressed(suppressed); 204 t0 = t.getSuppressed(); 205 if (t0.length != 1 || t0[0] != suppressed) 206 throw new RuntimeException("Expected suppression did not occur."); 207 } 208 209 private static class NoSuppression extends Throwable { 210 public NoSuppression(boolean enableSuppression) { 211 super("The medium.", null, enableSuppression, true); 212 } 213 } 214 215 private static void initCausePlumbing() { 216 Throwable t1 = new Throwable(); 217 Throwable t2 = new Throwable("message", t1); 218 Throwable t3 = new Throwable(); 219 220 try { 221 t2.initCause(t3); 222 throw new RuntimeException("Shouldn't reach."); 223 } catch (IllegalStateException ise) { 224 if (ise.getCause() != t2) 225 throw new RuntimeException("Unexpected cause in ISE", ise); 226 Throwable[] suppressed = ise.getSuppressed(); 227 if (suppressed.length != 1 || suppressed[0] != t3) 228 throw new RuntimeException("Bad suppression in ISE", ise); 229 } 230 231 try { 232 t3.initCause(t3); 233 throw new RuntimeException("Shouldn't reach."); 234 } catch (IllegalArgumentException iae) { 235 if (iae.getCause() != t3) 236 throw new RuntimeException("Unexpected cause in ISE", iae); 237 } 238 } 239 } |