Package org.wikidata.wdtk.testing
Class MockStringContentFactory
java.lang.Object
org.wikidata.wdtk.testing.MockStringContentFactory
Helper class to create BufferedReaders and InputStreams with predefined
contents.
- Author:
- Markus Kroetzsch
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]getBytesFromString(String string, CompressionType compressionType) Turns a string into a sequence of bytes, possibly compressed.static InputStreamReturns an input stream that will throw IOExceptions on common reading operations.static StringgetStringFromBufferedReader(BufferedReader bufferedReader) Loads a string from the given buffered reader.static StringgetStringFromInputStream(InputStream inputStream) Loads a string from the given input stream.static StringgetStringFromUrl(URL url) Loads a string from the file at the given URL.static InputStreamnewMockInputStream(byte[] contents) Returns a new InputStream that gives access to the contents as given in the input bytes.static InputStreamnewMockInputStream(String contents) Returns a new InputStream that gives access to the contents as given in the input string, encoded in UTF-8.
-
Constructor Details
-
MockStringContentFactory
public MockStringContentFactory()
-
-
Method Details
-
newMockInputStream
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
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
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
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
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
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
-