HADOOP-15154. Abstract new method assertCapability for StreamCapabilities testing. Contributed by Zsolt Venczel.

This commit is contained in:
Xiao Chen 2018-05-18 11:28:48 -07:00
parent 9775ecb235
commit 89f5911392
2 changed files with 54 additions and 14 deletions

View File

@ -44,8 +44,7 @@
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.apache.hadoop.fs.contract.ContractTestUtils.assertCapabilities;
public class TestCryptoStreams extends CryptoStreamsTestBase {
/**
@ -419,21 +418,33 @@ public void testHasCapability() throws Exception {
// verify hasCapability returns what FakeOutputStream is set up for
CryptoOutputStream cos =
(CryptoOutputStream) getOutputStream(defaultBufferSize, key, iv);
assertTrue(cos instanceof StreamCapabilities);
assertTrue(cos.hasCapability(StreamCapabilities.HFLUSH));
assertTrue(cos.hasCapability(StreamCapabilities.HSYNC));
assertTrue(cos.hasCapability(StreamCapabilities.DROPBEHIND));
assertFalse(cos.hasCapability(StreamCapabilities.READAHEAD));
assertFalse(cos.hasCapability(StreamCapabilities.UNBUFFER));
assertCapabilities(cos,
new String[] {
StreamCapabilities.HFLUSH,
StreamCapabilities.HSYNC,
StreamCapabilities.DROPBEHIND
},
new String[] {
StreamCapabilities.READAHEAD,
StreamCapabilities.UNBUFFER
}
);
// verify hasCapability for input stream
CryptoInputStream cis =
(CryptoInputStream) getInputStream(defaultBufferSize, key, iv);
assertTrue(cis instanceof StreamCapabilities);
assertTrue(cis.hasCapability(StreamCapabilities.DROPBEHIND));
assertTrue(cis.hasCapability(StreamCapabilities.READAHEAD));
assertTrue(cis.hasCapability(StreamCapabilities.UNBUFFER));
assertFalse(cis.hasCapability(StreamCapabilities.HFLUSH));
assertFalse(cis.hasCapability(StreamCapabilities.HSYNC));
assertCapabilities(cis,
new String[] {
StreamCapabilities.DROPBEHIND,
StreamCapabilities.READAHEAD,
StreamCapabilities.UNBUFFER
},
new String[] {
StreamCapabilities.HFLUSH,
StreamCapabilities.HSYNC
}
);
}
}

View File

@ -26,6 +26,7 @@
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.fs.StreamCapabilities;
import org.apache.hadoop.io.IOUtils;
import org.junit.Assert;
import org.junit.internal.AssumptionViolatedException;
@ -1437,6 +1438,34 @@ public static List<LocatedFileStatus> toListThroughNextCallsAlone(
return list;
}
/**
* Custom assert to test {@link StreamCapabilities}.
*
* @param stream The stream to test for StreamCapabilities
* @param shouldHaveCapabilities The array of expected capabilities
* @param shouldNotHaveCapabilities The array of unexpected capabilities
*/
public static void assertCapabilities(
Object stream, String[] shouldHaveCapabilities,
String[] shouldNotHaveCapabilities) {
assertTrue("Stream should be instanceof StreamCapabilities",
stream instanceof StreamCapabilities);
if (shouldHaveCapabilities!=null) {
for (String shouldHaveCapability : shouldHaveCapabilities) {
assertTrue("Should have capability: " + shouldHaveCapability,
((StreamCapabilities) stream).hasCapability(shouldHaveCapability));
}
}
if (shouldNotHaveCapabilities!=null) {
for (String shouldNotHaveCapability : shouldNotHaveCapabilities) {
assertFalse("Should not have capability: " + shouldNotHaveCapability,
((StreamCapabilities) stream)
.hasCapability(shouldNotHaveCapability));
}
}
}
/**
* Results of recursive directory creation/scan operations.