add --shared
[pylucene.git] / lucene-java-3.4.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     boolean isJava7 = true;
48     try {
49       // this class only exists in Java 7:
50       Class.forName("java.lang.AutoCloseable");
51     } catch (ClassNotFoundException cnfe) {
52       isJava7 = false;
53     }
54     
55     if (!isJava7) {
56       System.err.println("WARNING: TestIOUtils.testSuppressedExceptions: Full test coverage only with Java 7, as suppressed exception recording is not supported before.");
57     }
58     
59     // test with prior exception
60     try {
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);
68       pw.flush();
69       final String trace = sw.toString();
70       if (VERBOSE) {
71         System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
72         System.out.println(trace);
73       }
74       if (isJava7) {
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"));
79       }
80     } catch (IOException e2) {
81       fail("IOException should not be thrown here");
82     }
83     
84     // test without prior exception
85     try {
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);
94       pw.flush();
95       final String trace = sw.toString();
96       if (VERBOSE) {
97         System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
98         System.out.println(trace);
99       }
100       if (isJava7) {
101         assertTrue("Stack trace does not contain suppressed Exception: " + trace,
102           trace.contains("java.io.IOException: TEST-IO-EXCEPTION-2"));
103       }
104     }
105   }
106   
107 }