Class InjectResources


  • public final class InjectResources
    extends java.lang.Object
    InjectResources is a small fluid Java DSL that simplifies loading and parsing of resource files.

    This library takes care of opening/closing of streams and handles exceptions.

    Example:

     String text = resource()
          .withPath("/com/adelean/junit/jupiter", "resource.txt")
          .asText()
          .text();
     

    Or with resources content parsing:

     // Assuming there is object 'reader' able to parse properties content in variable of type DbConnection
     DbConnection dbConnection = resource()
          .withPath("/com/adelean/junit/jupiter", "db.properties")
          .asInputStream()
          .parseChecked(reader::readValue);
     
    • Method Detail

      • resource

        public static InjectResources resource()
        Entry point of InjectResourcess fluid Java DSL.
        Returns:
        InjectResources resource factory
      • onClassLoaderOf

        public Resource.ResourceOnClassloader onClassLoaderOf​(java.lang.Class<?> clazz)
        Returns builder for resource that is present on a classpath of ClassLoader of clazz argument.
        Parameters:
        clazz - class on a classpath where requested resource is present
        Returns:
        Resource.ResourceOnClassloader resource factory
      • onClassLoader

        public Resource.ResourceOnClassloader onClassLoader​(java.lang.ClassLoader classLoader)
        Returns builder for resource that is present on a classpath of classLoader argument.
        Parameters:
        classLoader - classloader from a classpath where requested resource is present
        Returns:
        Resource.ResourceOnClassloader resource factory
      • withPath

        public Resource withPath​(java.lang.String firstPathToken,
                                 java.lang.String... otherTokens)
        Returns builder for resource with defined path on a classpath of the threads context ClassLoader.

        Those two operations are equivalent:

             withPath("/com/adelean/junit/jupiter/resource.txt")
        
             // equivalent to
        
             withPath("/com/adelean/junit/jupiter", "resource.txt")
         
        Parameters:
        firstPathToken - path of resource, it least one token
        otherTokens - path of resource, allows multiple tokens
        Returns:
        Resource resource builder