From be7784f129795b478f8b0f5de60f71542367007c Mon Sep 17 00:00:00 2001 From: Eric Maynard Date: Thu, 3 Jun 2021 11:54:47 -0700 Subject: [PATCH 1/3] initial commit --- .../java/org/apache/hadoop/io/IOUtils.java | 6 ++ .../io/TestIOUtilsWrapExceptionSuite.java | 56 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java index 121af64b01182..139c152fd62a8 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java @@ -482,6 +482,12 @@ public static IOException wrapException(final String path, if (exception instanceof InterruptedIOException || exception instanceof PathIOException) { return exception; + } else if (exception.getCause() != null + && exception.getCause() instanceof InterruptedException) { + InterruptedIOException interruptedIOException = + new InterruptedIOException(exception.getMessage()); + interruptedIOException.initCause(exception.getCause()); + return interruptedIOException; } else { String msg = String .format("Failed with %s while processing file/directory :[%s] in " diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java new file mode 100644 index 0000000000000..5e81d00336202 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.io; + +import java.io.IOException; +import java.io.InterruptedIOException; + +import org.junit.Assert; +import org.junit.Test; + +public class TestIOUtilsWrapExceptionSuite extends Assert { + @Test + public void testWrapExceptionWithInterruptedException() throws Exception { + InterruptedIOException inputException = new InterruptedIOException("message"); + NullPointerException causeException = new NullPointerException("cause"); + inputException.initCause(causeException); + Exception outputException = IOUtils.wrapException("path", "methodName", inputException); + + // The new exception should retain the input message, cause, and type + assertTrue(outputException instanceof InterruptedIOException); + assertTrue(outputException.getCause() instanceof NullPointerException); + assertEquals(outputException.getMessage(), inputException.getMessage()); + assertEquals(outputException.getCause(), inputException.getCause()); + } + + @Test + public void testWrapExceptionWithInterruptedCauseException() throws Exception { + IOException inputException = new IOException("message"); + InterruptedException causeException = new InterruptedException("cause"); + inputException.initCause(causeException); + Exception outputException = IOUtils.wrapException("path", "methodName", inputException); + + // The new exception should retain the input message and cause + // but be an InterruptedIOException because the cause was an InterruptedException + assertTrue(outputException instanceof InterruptedIOException); + assertTrue(outputException.getCause() instanceof InterruptedException); + assertEquals(outputException.getMessage(), inputException.getMessage()); + assertEquals(outputException.getCause(), inputException.getCause()); + } +} From 1ab84de558f853424fad2ef220633c21f271c65d Mon Sep 17 00:00:00 2001 From: Eric Maynard Date: Wed, 16 Jun 2021 15:15:35 -0700 Subject: [PATCH 2/3] changes per review --- .../main/java/org/apache/hadoop/io/IOUtils.java | 3 +-- .../hadoop/io/TestIOUtilsWrapExceptionSuite.java | 14 +++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java index 139c152fd62a8..4181828b050f6 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java @@ -482,8 +482,7 @@ public static IOException wrapException(final String path, if (exception instanceof InterruptedIOException || exception instanceof PathIOException) { return exception; - } else if (exception.getCause() != null - && exception.getCause() instanceof InterruptedException) { + } else if (exception.getCause() instanceof InterruptedException) { InterruptedIOException interruptedIOException = new InterruptedIOException(exception.getMessage()); interruptedIOException.initCause(exception.getCause()); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java index 5e81d00336202..eb8b8496183d0 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java @@ -21,10 +21,14 @@ import java.io.IOException; import java.io.InterruptedIOException; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; +import org.apache.hadoop.test.AbstractHadoopTestBase; +import org.assertj.core.api.Assertions; import org.junit.Assert; import org.junit.Test; -public class TestIOUtilsWrapExceptionSuite extends Assert { +public class TestIOUtilsWrapExceptionSuite extends AbstractHadoopTestBase { @Test public void testWrapExceptionWithInterruptedException() throws Exception { InterruptedIOException inputException = new InterruptedIOException("message"); @@ -33,8 +37,8 @@ public void testWrapExceptionWithInterruptedException() throws Exception { Exception outputException = IOUtils.wrapException("path", "methodName", inputException); // The new exception should retain the input message, cause, and type - assertTrue(outputException instanceof InterruptedIOException); - assertTrue(outputException.getCause() instanceof NullPointerException); + Assertions.assertThat(outputException).isInstanceOf(InterruptedIOException.class); + Assertions.assertThat(outputException.getCause()).isInstanceOf(NullPointerException.class); assertEquals(outputException.getMessage(), inputException.getMessage()); assertEquals(outputException.getCause(), inputException.getCause()); } @@ -48,8 +52,8 @@ public void testWrapExceptionWithInterruptedCauseException() throws Exception { // The new exception should retain the input message and cause // but be an InterruptedIOException because the cause was an InterruptedException - assertTrue(outputException instanceof InterruptedIOException); - assertTrue(outputException.getCause() instanceof InterruptedException); + Assertions.assertThat(outputException).isInstanceOf(InterruptedIOException.class); + Assertions.assertThat(outputException.getCause()).isInstanceOf(InterruptedException.class); assertEquals(outputException.getMessage(), inputException.getMessage()); assertEquals(outputException.getCause(), inputException.getCause()); } From efdfacb073f8f1af6ae330def5ee2c65337f433c Mon Sep 17 00:00:00 2001 From: Eric Maynard Date: Wed, 30 Jun 2021 14:31:48 -0700 Subject: [PATCH 3/3] changes per review --- .../io/TestIOUtilsWrapExceptionSuite.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java index eb8b8496183d0..08e99d5490eaa 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtilsWrapExceptionSuite.java @@ -21,13 +21,13 @@ import java.io.IOException; import java.io.InterruptedIOException; -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertTrue; -import org.apache.hadoop.test.AbstractHadoopTestBase; import org.assertj.core.api.Assertions; -import org.junit.Assert; import org.junit.Test; +import org.apache.hadoop.test.AbstractHadoopTestBase; + +import static junit.framework.TestCase.assertEquals; + public class TestIOUtilsWrapExceptionSuite extends AbstractHadoopTestBase { @Test public void testWrapExceptionWithInterruptedException() throws Exception { @@ -38,7 +38,8 @@ public void testWrapExceptionWithInterruptedException() throws Exception { // The new exception should retain the input message, cause, and type Assertions.assertThat(outputException).isInstanceOf(InterruptedIOException.class); - Assertions.assertThat(outputException.getCause()).isInstanceOf(NullPointerException.class); + Assertions.assertThat(outputException.getCause()).isInstanceOf(NullPointerException.class) + .describedAs("inner cause"); assertEquals(outputException.getMessage(), inputException.getMessage()); assertEquals(outputException.getCause(), inputException.getCause()); } @@ -54,7 +55,9 @@ public void testWrapExceptionWithInterruptedCauseException() throws Exception { // but be an InterruptedIOException because the cause was an InterruptedException Assertions.assertThat(outputException).isInstanceOf(InterruptedIOException.class); Assertions.assertThat(outputException.getCause()).isInstanceOf(InterruptedException.class); - assertEquals(outputException.getMessage(), inputException.getMessage()); - assertEquals(outputException.getCause(), inputException.getCause()); + assertEquals("getMessage()", + outputException.getMessage(), inputException.getMessage()); + assertEquals("getCause()", + outputException.getCause(), inputException.getCause()); } }