pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / util / TestIOUtils.java
1 package org.apache.lucene.util;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.Closeable;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24
25 public class TestIOUtils extends LuceneTestCase {
26
27   static final class BrokenCloseable implements Closeable {
28     final int i;
29     
30     public BrokenCloseable(int i) {
31       this.i = i;
32     }
33   
34     // Not until Java6: @Override
35     public void close() throws IOException {
36       throw new IOException("TEST-IO-EXCEPTION-" + i);
37     }
38   }
39
40   static final class TestException extends Exception {
41     public TestException() {
42       super("BASE-EXCEPTION");
43     }
44   }
45
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.");
49     }
50     
51     // test with prior exception
52     try {
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);
60       pw.flush();
61       final String trace = sw.toString();
62       if (VERBOSE) {
63         System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
64         System.out.println(trace);
65       }
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"));
71       }
72     } catch (IOException e2) {
73       fail("IOException should not be thrown here");
74     }
75     
76     // test without prior exception
77     try {
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);
86       pw.flush();
87       final String trace = sw.toString();
88       if (VERBOSE) {
89         System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
90         System.out.println(trace);
91       }
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"));
95       }
96     }
97   }
98   
99 }