Coverage Report - org.jbehave.core.io.rest.filesystem.ExportFromFilesystem
 
Classes in this File Line Coverage Branch Coverage Complexity
ExportFromFilesystem
93%
27/29
75%
6/8
2.4
 
 1  
 package org.jbehave.core.io.rest.filesystem;
 2  
 
 3  
 import static org.jbehave.core.io.rest.filesystem.FilesystemUtils.asFile;
 4  
 
 5  
 import java.io.File;
 6  
 import java.io.IOException;
 7  
 import java.util.Map;
 8  
 
 9  
 import org.apache.commons.io.FileUtils;
 10  
 import org.jbehave.core.io.rest.Resource;
 11  
 import org.jbehave.core.io.rest.ResourceExporter;
 12  
 import org.jbehave.core.io.rest.ResourceIndexer;
 13  
 import org.jbehave.core.io.rest.ResourceUploader;
 14  
 
 15  
 /**
 16  
  * Implementation that reads from filesystem the resources and uploads them.
 17  
  * 
 18  
  * An include pattern of the resources may be provided.
 19  
  * 
 20  
  * The exporter requires an instance of a {@link ResourceIndexer} and of a
 21  
  * {@link ResourceUploader}.
 22  
  */
 23  
 public class ExportFromFilesystem implements ResourceExporter {
 24  
 
 25  
         private final ResourceIndexer indexer;
 26  
         private final ResourceUploader uploader;
 27  
         private final String sourcePath;
 28  
         private final String sourceExt;
 29  
         private String syntax;
 30  
         private String includes;
 31  
 
 32  
         public ExportFromFilesystem(ResourceIndexer indexer,
 33  
                         ResourceUploader uploader, String sourcePath, String sourceExt,
 34  1
                         String syntax, String includes) {
 35  1
                 this.indexer = indexer;
 36  1
                 this.uploader = uploader;
 37  1
                 this.sourcePath = sourcePath;
 38  1
                 this.sourceExt = sourceExt;
 39  1
                 this.syntax = syntax;
 40  1
                 this.includes = includes;
 41  1
         }
 42  
 
 43  
         public void exportResources(String rootURI) {
 44  1
                 Map<String, Resource> index = indexer.indexResources(rootURI,
 45  
                                 sourcePath, syntax, includes);
 46  1
                 readResources(index, sourcePath, sourceExt);
 47  1
                 uploadResources(index);
 48  1
         }
 49  
 
 50  
         private void uploadResources(Map<String, Resource> index) {
 51  1
                 for (String name : index.keySet()) {
 52  2
                         Resource resource = index.get(name);
 53  2
                         uploader.uploadResource(resource);
 54  2
                 }
 55  1
         }
 56  
 
 57  
         private void readResources(Map<String, Resource> index, String sourcePath,
 58  
                         String sourceExt) {
 59  1
                 for (String name : index.keySet()) {
 60  2
                         Resource resource = index.get(name);
 61  2
                         readResource(resource, asFile(resource, sourcePath, sourceExt));
 62  2
                 }
 63  1
         }
 64  
 
 65  
         private void readResource(Resource resource, File file) {
 66  2
                 if ( file.isDirectory() || !file.exists() ) return;
 67  
                 try {
 68  2
                         String text = FileUtils.readFileToString(file);
 69  2
                         resource.setContent(text);
 70  0
                 } catch (IOException e) {
 71  0
                         throw new RuntimeException("Failed to read resource " + resource
 72  
                                         + " from file " + file, e);
 73  2
                 }
 74  2
         }
 75  
 
 76  
 }