Package io.github.cjstehno.testthings
Class Resources
java.lang.Object
io.github.cjstehno.testthings.Resources
Some useful utilities for working with classpath resources.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
copyResourceTo
(String resourcePath, File file) Copies the classpath resource at the specified path to the provided destination file.static void
copyResourceTo
(String resourcePath, Path path) Copies the classpath resource at the specified path to the provided destination path.static <T> T
resourceDeserialized
(SerdesProvider provider, String path, Class<? extends T> type) Loads the resource at the specified path as bytes and then deserializes it to the specified object type.static <T> T
resourceDeserialized
(String path, Class<? extends T> type) Loads the resource at the specified path as bytes and then deserializes as JSON it to the specified object type.static File
resourceFile
(String path) Retrieves theFile
reference to the classpath resource.static Path
resourcePath
(String path) Retrieves thePath
reference to the classpath resource.static Reader
resourceReader
(String path) Loads the specified classpath resource as aReader
, specifically aBufferedReader
.static InputStream
resourceStream
(String path) Loads the specified classpath resource as an InputStream.static byte[]
resourceToBytes
(String path) Loads the specified classpath resource as a byte array.static String
resourceToString
(String path) Loads the specified resource path as a UTF-8 string.static String
resourceToString
(String path, Charset charset) Loads the specified resource path as a String with the specified charset.static URI
resourceUri
(String path) Retrieves the URI for the specified resource.static URL
resourceUrl
(String path) Retrieves the URL for the specified resource.static String
Loads the content at the specified classpath location as a string template.
-
Constructor Details
-
Resources
public Resources()
-
-
Method Details
-
resourceToString
Loads the specified resource path as a String with the specified charset.- Parameters:
path
- the classpath resource pathcharset
- the charset to be used- Returns:
- the string version of the resource
-
resourceToString
Loads the specified resource path as a UTF-8 string.- Parameters:
path
- the classpath resource path- Returns:
- the string version of the resource
-
resourceToBytes
Loads the specified classpath resource as a byte array.- Parameters:
path
- the resource path- Returns:
- a byte array containing the resource data
-
resourceStream
Loads the specified classpath resource as an InputStream.- Parameters:
path
- the resource path- Returns:
- an InputStream containing the resource data
-
resourceReader
Loads the specified classpath resource as aReader
, specifically aBufferedReader
.- Parameters:
path
- the resource path- Returns:
- a Reader containing the resource data
-
resourceUrl
Retrieves the URL for the specified resource.- Parameters:
path
- the resource path- Returns:
- the resource URL
-
resourcePath
Retrieves thePath
reference to the classpath resource.- Parameters:
path
- the resource path- Returns:
- the Path reference to the classpath resource
-
resourceFile
Retrieves theFile
reference to the classpath resource.- Parameters:
path
- the resource path- Returns:
- the File reference to the classpath resource
-
resourceDeserialized
public static <T> T resourceDeserialized(SerdesProvider provider, String path, Class<? extends T> type) Loads the resource at the specified path as bytes and then deserializes it to the specified object type.- Type Parameters:
T
- the type of the deserialized object- Parameters:
provider
- the serdes providerpath
- the resource pathtype
- the object type- Returns:
- the deserialized object loaded from the resource path
-
resourceDeserialized
Loads the resource at the specified path as bytes and then deserializes as JSON it to the specified object type.- Type Parameters:
T
- the type of the deserialized object- Parameters:
path
- the resource pathtype
- the object type- Returns:
- the deserialized object from the resource path content
-
resourceUri
Retrieves the URI for the specified resource.- Parameters:
path
- the resource path- Returns:
- the resource URI
-
copyResourceTo
Copies the classpath resource at the specified path to the provided destination path.- Parameters:
resourcePath
- the classpath resource pathpath
- the destination path
-
copyResourceTo
Copies the classpath resource at the specified path to the provided destination file.- Parameters:
resourcePath
- the classpath resource pathfile
- the destination file
-
template
Loads the content at the specified classpath location as a string template. The replacement values are surrounded by curly braces, as{tag}
. The provided map defines the replacement values.- Parameters:
path
- the resource pathreplacements
- the replacement values- Returns:
- the template with all defined values replaced
-