HADOOP-15056. Fix TestUnbuffer#testUnbufferException failure. Contributed by Jack Bearden.

(cherry picked from commit 19e0894209)
This commit is contained in:
Xiao Chen 2017-12-07 21:04:05 -08:00
parent 9942952dd8
commit 1d1401ad38
2 changed files with 27 additions and 8 deletions

View File

@ -22,6 +22,8 @@ import java.io.InputStream;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Static methods to implement policies for {@link StreamCapabilities}. * Static methods to implement policies for {@link StreamCapabilities}.
@ -29,6 +31,10 @@ import org.apache.hadoop.classification.InterfaceStability;
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Evolving @InterfaceStability.Evolving
public class StreamCapabilitiesPolicy { 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()}. * Implement the policy for {@link CanUnbuffer#unbuffer()}.
* *
@ -40,11 +46,14 @@ public class StreamCapabilitiesPolicy {
&& ((StreamCapabilities) in).hasCapability( && ((StreamCapabilities) in).hasCapability(
StreamCapabilities.UNBUFFER)) { StreamCapabilities.UNBUFFER)) {
((CanUnbuffer) in).unbuffer(); ((CanUnbuffer) in).unbuffer();
} else {
LOG.debug(in.getClass().getName() + ":"
+ " does not implement StreamCapabilities"
+ " and the unbuffer capability");
} }
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new UnsupportedOperationException("this stream " + throw new UnsupportedOperationException(in.getClass().getName() + ": "
in.getClass().getName() + + CAN_UNBUFFER_NOT_IMPLEMENTED_MESSAGE);
" claims to unbuffer but forgets to implement CanUnbuffer");
} }
} }
} }

View File

@ -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 @Test
public void testUnbufferException() { public void testUnbufferException() {
FSInputStream in = Mockito.mock(FSInputStream.class); abstract class BuggyStream
FSDataInputStream fs = new FSDataInputStream(in); extends FSInputStream
implements StreamCapabilities {
}
BuggyStream bs = Mockito.mock(BuggyStream.class);
Mockito.when(bs.hasCapability(Mockito.anyString())).thenReturn(true);
exception.expect(UnsupportedOperationException.class); exception.expect(UnsupportedOperationException.class);
exception.expectMessage("this stream " + in.getClass().getName() exception.expectMessage(
+ " does not support unbuffering"); StreamCapabilitiesPolicy.CAN_UNBUFFER_NOT_IMPLEMENTED_MESSAGE);
FSDataInputStream fs = new FSDataInputStream(bs);
fs.unbuffer(); fs.unbuffer();
} }
} }