Package org.wikidata.wdtk.testing
Class MockWebResourceFetcher
java.lang.Object
org.wikidata.wdtk.testing.MockWebResourceFetcher
- All Implemented Interfaces:
WebResourceFetcher
Mock implementation of
WebResourceFetcher
.- Author:
- Markus Kroetzsch
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetInputStreamForUrl
(String urlString) Returns an InputStream for the document at the given URL.void
setReturnFailingReaders
(boolean returnFailingReaders) When set to true, every operation that returns reader or stream objects to access some resource will return objects that fail with exceptions when trying to read data.void
setWebResourceContents
(String url, String contents) Defines the contents of a new web resource.void
setWebResourceContents
(String url, String contents, CompressionType compressionType) Defines the contents of a new web resource.void
setWebResourceContentsFromResource
(String url, String resource, Class<?> resourceClass) Defines the contents of a new web resource by taking the string from a given (Java) resource.void
setWebResourceContentsFromResource
(String url, String resource, Class<?> resourceClass, CompressionType compressionType) Defines the contents of a new web resource by taking the string from a given (Java) resource, possibly using additional compression.
-
Field Details
-
webResources
-
-
Constructor Details
-
MockWebResourceFetcher
public MockWebResourceFetcher()Constructor.
-
-
Method Details
-
setReturnFailingReaders
public void setReturnFailingReaders(boolean returnFailingReaders) When set to true, every operation that returns reader or stream objects to access some resource will return objects that fail with exceptions when trying to read data. This can be used to simulate problems like failing network connections after opening an online resource.- Parameters:
returnFailingReaders
- whether read operations should fail
-
setWebResourceContents
Defines the contents of a new web resource.- Parameters:
url
- the URL stringcontents
- the string contents- Throws:
IOException
-
setWebResourceContents
public void setWebResourceContents(String url, String contents, CompressionType compressionType) throws IOException Defines the contents of a new web resource.- Parameters:
url
- the URL stringcontents
- the string contentscompressionType
- the compression to use on the mocked contents- Throws:
IOException
-
setWebResourceContentsFromResource
public void setWebResourceContentsFromResource(String url, String resource, Class<?> resourceClass) throws IOException Defines the contents of a new web resource by taking the string from a given (Java) resource.- Parameters:
url
- the URL stringresource
- the Java resource nameresourceClass
- the Class relative to which the resource should be resolved (since resources are stored relative to a classpath); can usually be obtained with getClass() from the calling object- Throws:
IOException
- if the Java resource could not be loaded
-
setWebResourceContentsFromResource
public void setWebResourceContentsFromResource(String url, String resource, Class<?> resourceClass, CompressionType compressionType) throws IOException Defines the contents of a new web resource by taking the string from a given (Java) resource, possibly using additional compression.- Parameters:
url
- the URL stringresource
- the Java resource nameresourceClass
- the Class relative to which the resource should be resolved (since resources are stored relative to a classpath); can usually be obtained with getClass() from the calling objectcompressionType
- the compression to use on the mocked contents- Throws:
IOException
- if the Java resource could not be loaded
-
getInputStreamForUrl
Description copied from interface:WebResourceFetcher
Returns an InputStream for the document at the given URL. This can be used for downloading. The stream should be closed after use.- Specified by:
getInputStreamForUrl
in interfaceWebResourceFetcher
- Parameters:
urlString
- the URL of the document- Returns:
- InputStream for the requested document
- Throws:
IOException
- if the document at the URL could not be opened or the URL was invalid
-