Coverage Report - org.jbehave.core.io.rest.filesystem.ExportFromFilesystem
 
Classes in this File Line Coverage Branch Coverage Complexity
ExportFromFilesystem
81%
26/32
83%
5/6
2.143
 
 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  
  * A local updated copy of the index may be provided, via a filesystem path. If
 19  
  * the index path is <code>null</code>, the index will be retrieved by the
 20  
  * {@link ResourceIndexer} directly from the root URI.
 21  
  * 
 22  
  * The exporter requires an instance of a {@link ResourceIndexer} and of a
 23  
  * {@link ResourceUploader}.
 24  
  */
 25  
 public class ExportFromFilesystem implements ResourceExporter {
 26  
 
 27  
     private final ResourceIndexer indexer;
 28  
     private final ResourceUploader uploader;
 29  
     private final String sourcePath;
 30  
     private final String sourceExt;
 31  
     private String indexPath;
 32  
 
 33  
     public ExportFromFilesystem(ResourceIndexer indexer, ResourceUploader uploader, String sourcePath, String sourceExt) {
 34  0
         this(indexer, uploader, sourcePath, sourceExt, null);
 35  0
     }
 36  
 
 37  
     public ExportFromFilesystem(ResourceIndexer indexer, ResourceUploader uploader, String sourcePath,
 38  1
             String sourceExt, String indexPath) {
 39  1
         this.indexer = indexer;
 40  1
         this.uploader = uploader;
 41  1
         this.sourcePath = sourcePath;
 42  1
         this.sourceExt = sourceExt;
 43  1
         this.indexPath = indexPath;
 44  1
     }
 45  
 
 46  
     public void exportResources(String rootURI) {
 47  1
         Map<String, Resource> index = (indexPath == null ? indexer.indexResources(rootURI) : indexer.indexResources(
 48  
                 rootURI, indexEntity(indexPath)));
 49  1
         readResources(index, sourcePath, sourceExt);
 50  1
         uploadResources(index);
 51  1
     }
 52  
 
 53  
     private String indexEntity(String indexPath) {
 54  
         try {
 55  1
             return FileUtils.readFileToString(new File(indexPath));
 56  0
         } catch (IOException e) {
 57  0
             throw new RuntimeException("Failed to read index from " + indexPath, e);
 58  
         }
 59  
     }
 60  
 
 61  
     private void uploadResources(Map<String, Resource> index) {
 62  1
         for (String name : index.keySet()) {
 63  2
             Resource resource = index.get(name);
 64  2
             uploader.uploadResourceAsText(resource.getURI(), resource.getText());
 65  2
         }
 66  1
     }
 67  
 
 68  
     private void readResources(Map<String, Resource> index, String sourcePath, String sourceExt) {
 69  1
         for (String name : index.keySet()) {
 70  2
             Resource resource = index.get(name);
 71  2
             readResource(resource, asFile(resource, sourcePath, sourceExt));
 72  2
         }
 73  1
     }
 74  
 
 75  
     private void readResource(Resource resource, File file) {
 76  
         try {
 77  2
             String text = FileUtils.readFileToString(file);
 78  2
             resource.setText(text);
 79  0
         } catch (IOException e) {
 80  0
             throw new RuntimeException("Failed to read resource " + resource + " from file " + file, e);
 81  2
         }
 82  2
     }
 83  
 
 84  
 }