Class MockWebResourceFetcher

java.lang.Object
org.wikidata.wdtk.testing.MockWebResourceFetcher
All Implemented Interfaces:
WebResourceFetcher

public class MockWebResourceFetcher extends Object implements WebResourceFetcher
Mock implementation of WebResourceFetcher.
Author:
Markus Kroetzsch
  • Field Details

    • webResources

      public final HashMap<String,byte[]> 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

      public void setWebResourceContents(String url, String contents) throws IOException
      Defines the contents of a new web resource.
      Parameters:
      url - the URL string
      contents - 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 string
      contents - the string contents
      compressionType - 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 string
      resource - the Java resource name
      resourceClass - 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 string
      resource - the Java resource name
      resourceClass - 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
      compressionType - the compression to use on the mocked contents
      Throws:
      IOException - if the Java resource could not be loaded
    • getInputStreamForUrl

      public InputStream getInputStreamForUrl(String urlString) throws IOException
      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 interface WebResourceFetcher
      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