Class MockStringContentFactory

java.lang.Object
org.wikidata.wdtk.testing.MockStringContentFactory

public class MockStringContentFactory extends Object
Helper class to create BufferedReaders and InputStreams with predefined contents.
Author:
Markus Kroetzsch
  • Constructor Details

    • MockStringContentFactory

      public MockStringContentFactory()
  • Method Details

    • newMockInputStream

      public static InputStream newMockInputStream(String contents)
      Returns a new InputStream that gives access to the contents as given in the input string, encoded in UTF-8.
      Parameters:
      contents -
      Returns:
      an input stream for the given string
    • newMockInputStream

      public static InputStream newMockInputStream(byte[] contents)
      Returns a new InputStream that gives access to the contents as given in the input bytes.
      Parameters:
      contents -
      Returns:
      an input stream for the given bytes
    • getStringFromUrl

      public static String getStringFromUrl(URL url) throws IOException
      Loads a string from the file at the given URL. This should only be used for relatively small files, obviously. The file contents is interpreted as UTF-8.
      Parameters:
      url -
      Returns:
      string contents of the file at the given URL
      Throws:
      IOException - if the URL could not be resolved or the file could not be read
    • getStringFromBufferedReader

      public static String getStringFromBufferedReader(BufferedReader bufferedReader) throws IOException
      Loads a string from the given buffered reader. Newline will be appended after each line but the last.
      Parameters:
      bufferedReader -
      Returns:
      string contents of the buffered reader
      Throws:
      IOException - if it was not possible to read from the buffered reader
    • getStringFromInputStream

      public static String getStringFromInputStream(InputStream inputStream) throws IOException
      Loads a string from the given input stream. UTF-8 encoding will be assumed. Newline will be appended after each line but the last.
      Parameters:
      inputStream -
      Returns:
      string contents of the input stream
      Throws:
      IOException - if it was not possible to read from the buffered reader
    • getFailingInputStream

      public static InputStream getFailingInputStream()
      Returns an input stream that will throw IOExceptions on common reading operations.
      Returns:
      input stream that fails on reading
    • getBytesFromString

      public static byte[] getBytesFromString(String string, CompressionType compressionType) throws IOException
      Turns a string into a sequence of bytes, possibly compressed. In any case, the character encoding used for converting the string into bytes is UTF8.
      Parameters:
      string -
      compressionType -
      Returns:
      Throws:
      IOException