1 package org.apache.lucene.util;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.Closeable;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
25 public class TestIOUtils extends LuceneTestCase {
27 static final class BrokenCloseable implements Closeable {
30 public BrokenCloseable(int i) {
34 // Not until Java6: @Override
35 public void close() throws IOException {
36 throw new IOException("TEST-IO-EXCEPTION-" + i);
40 static final class TestException extends Exception {
41 public TestException() {
42 super("BASE-EXCEPTION");
46 public void testSuppressedExceptions() {
47 if (!Constants.JRE_IS_MINIMUM_JAVA7) {
48 System.err.println("WARNING: TestIOUtils.testSuppressedExceptions: Full test coverage only with Java 7, as suppressed exception recording is not supported before.");
51 // test with prior exception
53 final TestException t = new TestException();
54 IOUtils.closeWhileHandlingException(t, new BrokenCloseable(1), new BrokenCloseable(2));
55 } catch (TestException e1) {
56 assertEquals("BASE-EXCEPTION", e1.getMessage());
57 final StringWriter sw = new StringWriter();
58 final PrintWriter pw = new PrintWriter(sw);
59 e1.printStackTrace(pw);
61 final String trace = sw.toString();
63 System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
64 System.out.println(trace);
66 if (Constants.JRE_IS_MINIMUM_JAVA7) {
67 assertTrue("Stack trace does not contain first suppressed Exception: " + trace,
68 trace.contains("java.io.IOException: TEST-IO-EXCEPTION-1"));
69 assertTrue("Stack trace does not contain second suppressed Exception: " + trace,
70 trace.contains("java.io.IOException: TEST-IO-EXCEPTION-2"));
72 } catch (IOException e2) {
73 fail("IOException should not be thrown here");
76 // test without prior exception
78 IOUtils.closeWhileHandlingException((TestException) null, new BrokenCloseable(1), new BrokenCloseable(2));
79 } catch (TestException e1) {
80 fail("TestException should not be thrown here");
81 } catch (IOException e2) {
82 assertEquals("TEST-IO-EXCEPTION-1", e2.getMessage());
83 final StringWriter sw = new StringWriter();
84 final PrintWriter pw = new PrintWriter(sw);
85 e2.printStackTrace(pw);
87 final String trace = sw.toString();
89 System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
90 System.out.println(trace);
92 if (Constants.JRE_IS_MINIMUM_JAVA7) {
93 assertTrue("Stack trace does not contain suppressed Exception: " + trace,
94 trace.contains("java.io.IOException: TEST-IO-EXCEPTION-2"));