Coverage Report - org.jbehave.mojo.UnpackViewResources
 
Classes in this File Line Coverage Branch Coverage Complexity
UnpackViewResources
97%
44/45
80%
16/20
3
UnpackViewResources$1
100%
3/3
75%
3/4
3
 
 1  
 package org.jbehave.mojo;
 2  
 
 3  
 import java.io.File;
 4  
 import java.util.Arrays;
 5  
 import java.util.HashSet;
 6  
 import java.util.Set;
 7  
 
 8  
 import org.apache.commons.collections.CollectionUtils;
 9  
 import org.apache.commons.collections.Predicate;
 10  
 import org.apache.maven.artifact.Artifact;
 11  
 import org.apache.maven.plugin.MojoExecutionException;
 12  
 import org.apache.maven.project.MavenProject;
 13  
 import org.codehaus.plexus.archiver.UnArchiver;
 14  
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 15  
 import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
 16  
 import org.codehaus.plexus.util.StringUtils;
 17  
 import org.jbehave.core.reporters.StoryReporterBuilder;
 18  
 
 19  
 /**
 20  
  * Mojo to unpack resources to view directory, whose location is derived from
 21  
  * the configured StoryReporterBuilder accessible from the Embedder.
 22  
  * 
 23  
  * @goal unpack-view-resources
 24  
  * @phase process-resources
 25  
  * @requiresDependencyResolution test
 26  
  */
 27  16
 public class UnpackViewResources extends AbstractEmbedderMojo {
 28  
 
 29  
     /**
 30  
      * @parameter expression="${project}"
 31  
      * @readonly
 32  
      * @required
 33  
      */
 34  
     MavenProject project;
 35  
 
 36  
     /**
 37  
      * @component
 38  
      */
 39  
     ArchiverManager archiverManager;
 40  
 
 41  
     /**
 42  
      * @parameter
 43  
      */
 44  3
     String[] resourceArtifactIds = new String[] { "jbehave-site-resources", "jbehave-core" };
 45  
 
 46  
     /**
 47  
      * @parameter
 48  
      */
 49  3
     String[] resourceTypes = new String[] { "zip" };
 50  
 
 51  
     /**
 52  
      * @parameter
 53  
      */
 54  
     String resourceIncludes;
 55  
 
 56  
     /**
 57  
      * @parameter
 58  
      */
 59  
     String resourcesExcludes;
 60  
 
 61  
     /**
 62  
      * @parameter
 63  
      */
 64  
     File viewDirectory;
 65  
 
 66  
     public void execute() throws MojoExecutionException {
 67  4
         File destination = viewDirectory();
 68  4
         for (Artifact artifact : resourceArtifacts()) {
 69  6
             unpack(artifact.getFile(), destination, resourceIncludes, resourcesExcludes);
 70  
         }
 71  3
     }
 72  
 
 73  
     private File viewDirectory() {
 74  4
         if ( viewDirectory != null ){
 75  1
             return viewDirectory;
 76  
         }
 77  3
         StoryReporterBuilder storyReporterBuilder = newEmbedder().configuration().storyReporterBuilder();
 78  3
         String build = project.getBuild().getDirectory();
 79  3
         String output = storyReporterBuilder.outputDirectory().getName();
 80  3
         String view = storyReporterBuilder.viewResources().getProperty("viewDirectory");
 81  3
         return new File(build + "/" + output + "/" + view);
 82  
     }
 83  
 
 84  
     private Set<Artifact> resourceArtifacts() {
 85  4
         Set<Artifact> artifacts = allArtifacts();
 86  4
         CollectionUtils.filter(artifacts, new Predicate() {
 87  
             public boolean evaluate(Object object) {
 88  7
                 Artifact artifact = (Artifact) object;
 89  7
                 return allowedBy("artifactId", artifact.getArtifactId(), resourceArtifactIds)
 90  
                         && allowedBy("type", artifact.getType(), resourceTypes);
 91  
             }
 92  
         });
 93  4
         return artifacts;
 94  
     }
 95  
 
 96  
     private boolean allowedBy(String name, String property, String[] values) {
 97  13
         boolean allowed = false;
 98  13
         if (values.length > 0) {
 99  18
             for (String value : values) {
 100  17
                 if (property.equals(value)) {
 101  12
                     allowed = true;
 102  12
                     break;
 103  
                 }
 104  
             }
 105  
         } else {
 106  0
             allowed = true;
 107  
         }
 108  13
         if (!allowed) {
 109  1
             getLog().debug("Artifact property " + name + " not allowed by values " + Arrays.asList(values));
 110  
         }
 111  13
         return allowed;
 112  
     }
 113  
 
 114  
     @SuppressWarnings("unchecked")
 115  
     private Set<Artifact> allArtifacts() {
 116  4
         return new HashSet<Artifact>(project.getArtifacts());
 117  
     }
 118  
 
 119  
     private void unpack(File file, File destination, String includes, String excludes) throws MojoExecutionException {
 120  
         try {
 121  6
             destination.mkdirs();
 122  
 
 123  6
             UnArchiver unArchiver = archiverManager.getUnArchiver(file);
 124  6
             unArchiver.setSourceFile(file);
 125  6
             unArchiver.setDestDirectory(destination);
 126  
 
 127  6
             if (StringUtils.isNotEmpty(excludes) || StringUtils.isNotEmpty(includes)) {
 128  4
                 IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
 129  4
                 if (StringUtils.isNotEmpty(excludes)) {
 130  4
                     selectors[0].setExcludes(excludes.split(","));
 131  
                 }
 132  4
                 if (StringUtils.isNotEmpty(includes)) {
 133  4
                     selectors[0].setIncludes(includes.split(","));
 134  
                 }
 135  4
                 unArchiver.setFileSelectors(selectors);
 136  
             }
 137  
 
 138  6
             unArchiver.extract();
 139  
 
 140  5
             getLog().info("Unpacked " + file + " to " + destination);
 141  1
         } catch (Exception e) {
 142  1
             throw new MojoExecutionException("Failed unpacking " + file + " to " + destination, e);
 143  5
         }
 144  5
     }
 145  
 
 146  
 }