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 boolean isJava7 = true;
49 // this class only exists in Java 7:
50 Class.forName("java.lang.AutoCloseable");
51 } catch (ClassNotFoundException cnfe) {
56 System.err.println("WARNING: TestIOUtils.testSuppressedExceptions: Full test coverage only with Java 7, as suppressed exception recording is not supported before.");
59 // test with prior exception
61 final TestException t = new TestException();
62 IOUtils.closeWhileHandlingException(t, new BrokenCloseable(1), new BrokenCloseable(2));
63 } catch (TestException e1) {
64 assertEquals("BASE-EXCEPTION", e1.getMessage());
65 final StringWriter sw = new StringWriter();
66 final PrintWriter pw = new PrintWriter(sw);
67 e1.printStackTrace(pw);
69 final String trace = sw.toString();
71 System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
72 System.out.println(trace);
75 assertTrue("Stack trace does not contain first suppressed Exception: " + trace,
76 trace.contains("java.io.IOException: TEST-IO-EXCEPTION-1"));
77 assertTrue("Stack trace does not contain second suppressed Exception: " + trace,
78 trace.contains("java.io.IOException: TEST-IO-EXCEPTION-2"));
80 } catch (IOException e2) {
81 fail("IOException should not be thrown here");
84 // test without prior exception
86 IOUtils.closeWhileHandlingException((TestException) null, new BrokenCloseable(1), new BrokenCloseable(2));
87 } catch (TestException e1) {
88 fail("TestException should not be thrown here");
89 } catch (IOException e2) {
90 assertEquals("TEST-IO-EXCEPTION-1", e2.getMessage());
91 final StringWriter sw = new StringWriter();
92 final PrintWriter pw = new PrintWriter(sw);
93 e2.printStackTrace(pw);
95 final String trace = sw.toString();
97 System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
98 System.out.println(trace);
101 assertTrue("Stack trace does not contain suppressed Exception: " + trace,
102 trace.contains("java.io.IOException: TEST-IO-EXCEPTION-2"));