Skip to content

Commit 46dd580

Browse files
committed
A first cut on [IO-148] IOException with constructors which take a cause. Only one constructor implemented. Class name up for discussion. Copied from Apache Tika implementation by Jukka Zitting (see http://svn.apache.org/viewvc?view=rev&revision=606139)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@606340 13f79535-47bb-0310-9956-ffa450edef68
1 parent db3e834 commit 46dd580

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io;
19+
20+
import java.io.IOException;
21+
22+
/**
23+
* Subclasses IOException with the {@link Throwable} constructor was missing before Java 6.
24+
*
25+
* @see <a href="mailto:commons-user@jakarta.apache.org">Apache Commons Users List</a>
26+
* @author <a href="http://commons.apache.org/io/">Apache Commons IO</a>
27+
* @version $Id: $
28+
*/
29+
public class CausedIOException extends IOException {
30+
31+
/**
32+
* Default serial version UID.
33+
*/
34+
private static final long serialVersionUID = 1L;
35+
36+
/**
37+
* Creates an instance with the given message and cause.
38+
* <p>
39+
* This constructor was not added in the underlying {@link IOException} class until Java 6. This is a convenience
40+
* method which uses the {@link #initCause(Throwable)} method to set the root cause.
41+
*
42+
* @param message
43+
* exception message
44+
* @param cause
45+
* root cause
46+
*/
47+
public CausedIOException(String message, Throwable cause) {
48+
super(message);
49+
this.initCause(cause);
50+
}
51+
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io;
19+
20+
import junit.framework.TestCase;
21+
22+
/**
23+
* Tests CausedIOException
24+
*
25+
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
26+
* @version $Id: $
27+
*/
28+
public class CausedIOExceptionTestCase extends TestCase {
29+
30+
/**
31+
* Tests the {@link CausedIOException#CausedIOException(String,Throwable)} constructor.
32+
*/
33+
public void testIOExceptionStringThrowable() {
34+
Throwable cause = new IllegalArgumentException("cause");
35+
CausedIOException exception = new CausedIOException("message", cause);
36+
assertEquals("message", exception.getMessage());
37+
assertEquals(cause, exception.getCause());
38+
assertSame(cause, exception.getCause());
39+
}
40+
}

src/test/org/apache/commons/io/PackageTestSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static void main(String[] args) {
3535

3636
public static Test suite() {
3737
TestSuite suite = new TestSuite("IO Utilities");
38+
suite.addTest(new TestSuite(CausedIOExceptionTestCase.class));
3839
suite.addTest(new TestSuite(CopyUtilsTest.class));
3940
suite.addTest(new TestSuite(DemuxTestCase.class));
4041
suite.addTest(new TestSuite(DirectoryWalkerTestCase.class));

0 commit comments

Comments
 (0)