HADOOP-15056. Fix TestUnbuffer#testUnbufferException failure. Contributed by Jack Bearden.
Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/19e08942 Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/19e08942 Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/19e08942 Branch: refs/heads/YARN-6592 Commit: 19e089420999dd9d97d981dcd0abd64b6166152d Parents: 532bbf4 Author: Xiao Chen <[email protected]> Authored: Thu Dec 7 21:04:05 2017 -0800 Committer: Xiao Chen <[email protected]> Committed: Thu Dec 7 21:05:55 2017 -0800 ---------------------------------------------------------------------- .../hadoop/fs/StreamCapabilitiesPolicy.java | 15 ++++++++++++--- .../java/org/apache/hadoop/fs/TestUnbuffer.java | 20 +++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/19e08942/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/StreamCapabilitiesPolicy.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/StreamCapabilitiesPolicy.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/StreamCapabilitiesPolicy.java index 3080780..8b63d4b 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/StreamCapabilitiesPolicy.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/StreamCapabilitiesPolicy.java @@ -22,6 +22,8 @@ import java.io.InputStream; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Static methods to implement policies for {@link StreamCapabilities}. @@ -29,6 +31,10 @@ import org.apache.hadoop.classification.InterfaceStability; @InterfaceAudience.Public @InterfaceStability.Evolving public class StreamCapabilitiesPolicy { + public static final String CAN_UNBUFFER_NOT_IMPLEMENTED_MESSAGE = + "claims unbuffer capabilty but does not implement CanUnbuffer"; + static final Logger LOG = LoggerFactory.getLogger( + StreamCapabilitiesPolicy.class); /** * Implement the policy for {@link CanUnbuffer#unbuffer()}. * @@ -40,11 +46,14 @@ public class StreamCapabilitiesPolicy { && ((StreamCapabilities) in).hasCapability( StreamCapabilities.UNBUFFER)) { ((CanUnbuffer) in).unbuffer(); + } else { + LOG.debug(in.getClass().getName() + ":" + + " does not implement StreamCapabilities" + + " and the unbuffer capability"); } } catch (ClassCastException e) { - throw new UnsupportedOperationException("this stream " + - in.getClass().getName() + - " claims to unbuffer but forgets to implement CanUnbuffer"); + throw new UnsupportedOperationException(in.getClass().getName() + ": " + + CAN_UNBUFFER_NOT_IMPLEMENTED_MESSAGE); } } } http://git-wip-us.apache.org/repos/asf/hadoop/blob/19e08942/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/TestUnbuffer.java ---------------------------------------------------------------------- diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/TestUnbuffer.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/TestUnbuffer.java index b112e30..655d453 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/TestUnbuffer.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/TestUnbuffer.java @@ -131,17 +131,27 @@ public class TestUnbuffer { } /** - * Test unbuffer method which throws an Exception with class name included. + * Test that a InputStream should throw an exception when not implementing + * CanUnbuffer + * + * This should throw an exception when the stream claims to have the + * unbuffer capability, but actually does not implement CanUnbuffer. */ @Test public void testUnbufferException() { - FSInputStream in = Mockito.mock(FSInputStream.class); - FSDataInputStream fs = new FSDataInputStream(in); + abstract class BuggyStream + extends FSInputStream + implements StreamCapabilities { + } + + BuggyStream bs = Mockito.mock(BuggyStream.class); + Mockito.when(bs.hasCapability(Mockito.anyString())).thenReturn(true); exception.expect(UnsupportedOperationException.class); - exception.expectMessage("this stream " + in.getClass().getName() - + " does not support unbuffering"); + exception.expectMessage( + StreamCapabilitiesPolicy.CAN_UNBUFFER_NOT_IMPLEMENTED_MESSAGE); + FSDataInputStream fs = new FSDataInputStream(bs); fs.unbuffer(); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
