DataSourceContractTest: Add tests for reading subranges

PiperOrigin-RevId: 348443305
This commit is contained in:
ibaker 2020-12-21 11:44:02 +00:00 committed by Oliver Woodman
parent 04824dfd25
commit df0b74e4c4
2 changed files with 63 additions and 0 deletions

View File

@ -75,6 +75,16 @@ public class UdpDataSourceContractTest extends DataSourceContractTest {
@Override
public void dataSpecWithPosition_readUntilEnd() {}
@Test
@Ignore("UdpDataSource doesn't support DataSpec's position or length [internal: b/175856954]")
@Override
public void dataSpecWithLength_readExpectedRange() {}
@Test
@Ignore("UdpDataSource doesn't support DataSpec's position or length [internal: b/175856954]")
@Override
public void dataSpecWithPositionAndLength_readExpectedRange() {}
/**
* Finds a free UDP port in the range of unreserved ports 50000-60000 that can be used from the
* test or throws an {@link IllegalStateException} if no port is available.

View File

@ -127,6 +127,59 @@ public abstract class DataSourceContractTest {
}
}
@Test
public void dataSpecWithLength_readExpectedRange() throws Exception {
ImmutableList<TestResource> resources = getTestResources();
Assertions.checkArgument(!resources.isEmpty(), "Must provide at least one test resource.");
for (int i = 0; i < resources.size(); i++) {
additionalFailureInfo.setInfo(getFailureLabel(resources, i));
TestResource resource = resources.get(i);
DataSource dataSource = createDataSource();
try {
long length =
dataSource.open(new DataSpec.Builder().setUri(resource.getUri()).setLength(4).build());
byte[] data = readToEnd(dataSource, resource);
assertThat(length).isEqualTo(4);
byte[] expectedData = Arrays.copyOf(resource.getExpectedBytes(), 4);
assertThat(data).isEqualTo(expectedData);
} finally {
dataSource.close();
}
additionalFailureInfo.setInfo(null);
}
}
@Test
public void dataSpecWithPositionAndLength_readExpectedRange() throws Exception {
ImmutableList<TestResource> resources = getTestResources();
Assertions.checkArgument(!resources.isEmpty(), "Must provide at least one test resource.");
for (int i = 0; i < resources.size(); i++) {
additionalFailureInfo.setInfo(getFailureLabel(resources, i));
TestResource resource = resources.get(i);
DataSource dataSource = createDataSource();
try {
long length =
dataSource.open(
new DataSpec.Builder()
.setUri(resource.getUri())
.setPosition(2)
.setLength(2)
.build());
byte[] data = readToEnd(dataSource, resource);
assertThat(length).isEqualTo(2);
byte[] expectedData = Arrays.copyOfRange(resource.getExpectedBytes(), 2, 4);
assertThat(data).isEqualTo(expectedData);
} finally {
dataSource.close();
}
additionalFailureInfo.setInfo(null);
}
}
@Test
public void resourceNotFound() throws Exception {
DataSource dataSource = createDataSource();