DataSourceContractTest


@UnstableApi
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 functions

DataSource!

Creates and returns an instance of the DataSource.

(Mutable)List<DataSource!>!

Creates and returns a list of instances of the DataSource.

(Mutable)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.

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 constructors

DataSourceContractTest

DataSourceContractTest()

Public functions

dataSpecWithEndPositionOutOfRange_readsToEnd

@Test
fun dataSpecWithEndPositionOutOfRange_readsToEnd(): Unit

dataSpecWithLength_readExpectedRange

@Test
fun dataSpecWithLength_readExpectedRange(): Unit

dataSpecWithLength_readUntilEndInTwoParts

@Test
fun dataSpecWithLength_readUntilEndInTwoParts(): Unit

dataSpecWithPositionAndLength_readExpectedRange

@Test
fun dataSpecWithPositionAndLength_readExpectedRange(): Unit

dataSpecWithPositionAtEndAndLength_readsZeroBytes

@Test
fun dataSpecWithPositionAtEndAndLength_readsZeroBytes(): Unit

dataSpecWithPositionAtEnd_readsZeroBytes

@Test
fun dataSpecWithPositionAtEnd_readsZeroBytes(): Unit

dataSpecWithPositionOutOfRange_throwsPositionOutOfRangeException

@Test
fun dataSpecWithPositionOutOfRange_throwsPositionOutOfRangeException(): Unit

dataSpecWithPosition_readUntilEnd

@Test
fun dataSpecWithPosition_readUntilEnd(): Unit

getResponseHeaders_caseInsensitive

@Test
fun getResponseHeaders_caseInsensitive(): Unit

getResponseHeaders_isEmptyWhileNotOpen

@Test
fun getResponseHeaders_isEmptyWhileNotOpen(): Unit

getResponseHeaders_noNullKeysOrValues

@Test
fun getResponseHeaders_noNullKeysOrValues(): Unit

getResponseHeaders_resourceNotFound_isEmptyWhileNotOpen

@Test
fun getResponseHeaders_resourceNotFound_isEmptyWhileNotOpen(): Unit

getResponseHeaders_returnsExpectedValues

@Test
fun getResponseHeaders_returnsExpectedValues(): Unit

getUri_resourceNotFound_returnsNullIfNotOpened

@Test
fun getUri_resourceNotFound_returnsNullIfNotOpened(): Unit

getUri_returnsExpectedValueOnlyWhileOpen

@Test
fun getUri_returnsExpectedValueOnlyWhileOpen(): Unit

resourceNotFound

@Test
fun resourceNotFound(): Unit

resourceNotFound_transferListenerCallbacks

@Test
fun resourceNotFound_transferListenerCallbacks(): Unit

transferListenerCallbacks

@Test
fun transferListenerCallbacks(): Unit

unboundedDataSpecWithGzipFlag_readUntilEnd

@Test
fun unboundedDataSpecWithGzipFlag_readUntilEnd(): Unit

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
fun unboundedDataSpec_readExpectedBytesWithOffset(): Unit

unboundedDataSpec_readUntilEnd

@Test
fun unboundedDataSpec_readUntilEnd(): Unit

uriSchemeIsCaseInsensitive

@Test
fun uriSchemeIsCaseInsensitive(): Unit

Protected functions

createDataSource

@ForOverride
protected fun createDataSource(): DataSource!

Creates and returns an instance of the DataSource.

Only one of createDataSource and createDataSources should be implemented.

createDataSources

@ForOverride
protected fun createDataSources(): (Mutable)List<DataSource!>!

Creates and returns a list of instances of the DataSource.

Only one of createDataSource and createDataSources should be implemented.

getNotFoundResources

@ForOverride
protected fun getNotFoundResources(): (Mutable)List<DataSourceContractTest.TestResource!>!

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 fun getNotFoundUri(): Uri!

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 fun getTestResources(): ImmutableList<DataSourceContractTest.TestResource!>!

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 fun getTransferListenerDataSource(): DataSource?

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 fun unboundedReadsAreIndefinite(): Boolean

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

Public properties

additionalFailureInfo

@Rule
val additionalFailureInfoAdditionalFailureInfo!