DataSourceContractTest


@UnstableApi
public abstract class DataSourceContractTest


A collection of contract tests for DataSource implementations.

Subclasses should only include the logic necessary to construct the DataSource (overriding either createDataSource or createDataSources) and allow it to successfully read data (overriding getTestResources. They shouldn't include any new @Test methods - implementation-specific tests should be in a separate class.

Most implementations should pass all these tests. If necessary, subclasses can disable tests by overriding the @Test method with a no-op implementation. It's recommended (but not required) to also annotate this @Ignore so that JUnit correctly reports the test as skipped/ignored instead of passing.

Summary

Nested types

A TransferListener that only keeps track of the transferred bytes.

Information about a resource that can be used to test the DataSource instance.

Builder for TestResource instances.

Public constructors

Protected methods

DataSource

Creates and returns an instance of the DataSource.

List<DataSource>

Creates and returns a list of instances of the DataSource.

List<DataSourceContractTest.TestResource>

Returns a non-empty list of TestResource that don't resolve.

Uri

Returns a Uri that doesn't resolve.

abstract ImmutableList<DataSourceContractTest.TestResource>

Returns TestResource instances.

@Nullable DataSource

Returns the DataSource that will be included in the TransferListener callbacks for the DataSource most recently created by createDataSource.

boolean

Returns whether the DataSource will continue reading indefinitely for unbounded DataSpecs.

Public fields

additionalFailureInfo

@Rule
public final AdditionalFailureInfo additionalFailureInfo

Public constructors

DataSourceContractTest

public DataSourceContractTest()

Public methods

dataSpecWithEndPositionOutOfRange_readsToEnd

@Test
public void dataSpecWithEndPositionOutOfRange_readsToEnd()

dataSpecWithLength_readExpectedRange

@Test
public void dataSpecWithLength_readExpectedRange()

dataSpecWithLength_readUntilEndInTwoParts

@Test
public void dataSpecWithLength_readUntilEndInTwoParts()

dataSpecWithPositionAndLength_readExpectedRange

@Test
public void dataSpecWithPositionAndLength_readExpectedRange()

dataSpecWithPositionAtEndAndLength_readsZeroBytes

@Test
public void dataSpecWithPositionAtEndAndLength_readsZeroBytes()

dataSpecWithPositionAtEnd_readsZeroBytes

@Test
public void dataSpecWithPositionAtEnd_readsZeroBytes()

dataSpecWithPositionOutOfRange_throwsPositionOutOfRangeException

@Test
public void dataSpecWithPositionOutOfRange_throwsPositionOutOfRangeException()

dataSpecWithPosition_readUntilEnd

@Test
public void dataSpecWithPosition_readUntilEnd()

getResponseHeaders_caseInsensitive

@Test
public void getResponseHeaders_caseInsensitive()

getResponseHeaders_isEmptyWhileNotOpen

@Test
public void getResponseHeaders_isEmptyWhileNotOpen()

getResponseHeaders_noNullKeysOrValues

@Test
public void getResponseHeaders_noNullKeysOrValues()

getResponseHeaders_resourceNotFound_isEmptyWhileNotOpen

@Test
public void getResponseHeaders_resourceNotFound_isEmptyWhileNotOpen()

getResponseHeaders_returnsExpectedValues

@Test
public void getResponseHeaders_returnsExpectedValues()

getUri_resourceNotFound_returnsNullIfNotOpened

@Test
public void getUri_resourceNotFound_returnsNullIfNotOpened()

getUri_returnsExpectedValueOnlyWhileOpen

@Test
public void getUri_returnsExpectedValueOnlyWhileOpen()

resourceNotFound

@Test
public void resourceNotFound()

resourceNotFound_transferListenerCallbacks

@Test
public void resourceNotFound_transferListenerCallbacks()

transferListenerCallbacks

@Test
public void transferListenerCallbacks()

unboundedDataSpecWithGzipFlag_readUntilEnd

@Test
public void unboundedDataSpecWithGzipFlag_readUntilEnd()

FLAG_ALLOW_GZIP should either be ignored by DataSource implementations, or correctly handled (i.e. the data is decompressed before being returned from read).

unboundedDataSpec_readExpectedBytesWithOffset

@Test
public void unboundedDataSpec_readExpectedBytesWithOffset()

unboundedDataSpec_readUntilEnd

@Test
public void unboundedDataSpec_readUntilEnd()

uriSchemeIsCaseInsensitive

@Test
public void uriSchemeIsCaseInsensitive()

Protected methods

createDataSource

@ForOverride
protected DataSource createDataSource()

Creates and returns an instance of the DataSource.

Only one of createDataSource and createDataSources should be implemented.

createDataSources

@ForOverride
protected List<DataSourcecreateDataSources()

Creates and returns a list of instances of the DataSource.

Only one of createDataSource and createDataSources should be implemented.

getNotFoundResources

@ForOverride
protected List<DataSourceContractTest.TestResourcegetNotFoundResources()

Returns a non-empty list of TestResource that don't resolve.

This is used to test how a DataSource handles nonexistent data. Multiple entries and the rest of the TestResource fields can be helpful for situations where the data can be "not found" for different reasons. For example in HTTP, 'server not found' generally results in a failed HTTP connection while 'file not found' generally results in a successful connection with a 404 HTTP error code and some response headers, and the handling code for these two cases may be different (and therefore worth testing separately).

Only one of getNotFoundUri and getNotFoundResources should be implemented.

getNotFoundUri

@ForOverride
protected Uri getNotFoundUri()

Returns a Uri that doesn't resolve.

This is used to test how a DataSource handles nonexistent data.

Only one of getNotFoundUri and getNotFoundResources should be implemented.

getTestResources

@ForOverride
protected abstract ImmutableList<DataSourceContractTest.TestResourcegetTestResources()

Returns TestResource instances.

Each resource will be used to exercise the DataSource instance, allowing different behaviours to be tested. Every getExpectedBytes must be at least 5 bytes.

If multiple resources are returned, it's recommended to disambiguate them using setName.

getTransferListenerDataSource

@ForOverride
protected @Nullable DataSource getTransferListenerDataSource()

Returns the DataSource that will be included in the TransferListener callbacks for the DataSource most recently created by createDataSource. If it's the same DataSource then null can be returned.

unboundedReadsAreIndefinite

@ForOverride
protected boolean unboundedReadsAreIndefinite()

Returns whether the DataSource will continue reading indefinitely for unbounded DataSpecs.