Coverage Report - org.jbehave.core.io.LoadFromClasspath
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadFromClasspath
100%
17/17
100%
2/2
1.714
 
 1  
 package org.jbehave.core.io;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.InputStream;
 5  
 
 6  
 import org.apache.commons.lang.builder.ToStringBuilder;
 7  
 import org.apache.commons.lang.builder.ToStringStyle;
 8  
 
 9  
 /**
 10  
  * Loads story resources from classpath
 11  
  */
 12  
 public class LoadFromClasspath implements ResourceLoader, StoryLoader {
 13  
 
 14  
     protected final ClassLoader classLoader;
 15  
 
 16  
     public LoadFromClasspath() {
 17  2142
         this(Thread.currentThread().getContextClassLoader());
 18  2142
     }
 19  
 
 20  
     public LoadFromClasspath(Class<?> loadFromClass) {
 21  3
         this(loadFromClass.getClassLoader());
 22  3
     }
 23  
 
 24  2158
     public LoadFromClasspath(ClassLoader classLoader) {
 25  2158
         this.classLoader = classLoader;
 26  2158
     }
 27  
 
 28  
     public String loadResourceAsText(String resourcePath) {
 29  5
         InputStream stream = resourceAsStream(resourcePath);
 30  
         try {
 31  4
             return IOUtils.toString(stream, true);
 32  1
         } catch (IOException e) {
 33  1
             throw new InvalidStoryResource(resourcePath, stream, e);
 34  
         }
 35  
     }
 36  
 
 37  
     public String loadStoryAsText(String storyPath) {
 38  5
         return loadResourceAsText(storyPath);
 39  
     }
 40  
 
 41  
     protected InputStream resourceAsStream(String resourcePath) {
 42  5
         InputStream stream = classLoader.getResourceAsStream(resourcePath);
 43  5
         if (stream == null) {
 44  1
             throw new StoryResourceNotFound(resourcePath, classLoader);
 45  
         }
 46  4
         return stream;
 47  
     }
 48  
 
 49  
     @Override
 50  
     public String toString() {
 51  1
         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
 52  
     }
 53  
 
 54  
 }