Coverage Report - org.jbehave.mojo.UnpackViewResources
 
Classes in this File Line Coverage Branch Coverage Complexity
UnpackViewResources
97%
42/43
77%
14/18
2.714
UnpackViewResources$1
100%
3/3
75%
3/4
2.714
 
 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  12
 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  
     public void execute() throws MojoExecutionException {
 62  3
         File destination = viewDirectory();
 63  3
         for (Artifact artifact : resourceArtifacts()) {
 64  3
             unpack(artifact.getFile(), destination, resourceIncludes, resourcesExcludes);
 65  
         }
 66  2
     }
 67  
 
 68  
     private File viewDirectory() {
 69  3
         StoryReporterBuilder storyReporterBuilder = newEmbedder().configuration().storyReporterBuilder();
 70  3
         String build = project.getBuild().getDirectory();
 71  3
         String output = storyReporterBuilder.outputDirectory().getName();
 72  3
         String view = storyReporterBuilder.viewResources().getProperty("viewDirectory");
 73  3
         return new File(build + "/" + output + "/" + view);
 74  
     }
 75  
 
 76  
     private Set<Artifact> resourceArtifacts() {
 77  3
         Set<Artifact> artifacts = allArtifacts();
 78  3
         CollectionUtils.filter(artifacts, new Predicate() {
 79  
             public boolean evaluate(Object object) {
 80  5
                 Artifact artifact = (Artifact) object;
 81  5
                 return allowedBy("artifactId", artifact.getArtifactId(), resourceArtifactIds)
 82  
                         && allowedBy("type", artifact.getType(), resourceTypes);
 83  
             }
 84  
         });
 85  3
         return artifacts;
 86  
     }
 87  
 
 88  
     private boolean allowedBy(String name, String property, String[] values) {
 89  9
         boolean allowed = false;
 90  9
         if (values.length > 0) {
 91  13
             for (String value : values) {
 92  12
                 if (property.equals(value)) {
 93  8
                     allowed = true;
 94  8
                     break;
 95  
                 }
 96  
             }
 97  
         } else {
 98  0
             allowed = true;
 99  
         }
 100  9
         if (!allowed) {
 101  1
             getLog().debug("Artifact property " + name + " not allowed by values " + Arrays.asList(values));
 102  
         }
 103  9
         return allowed;
 104  
     }
 105  
 
 106  
     @SuppressWarnings("unchecked")
 107  
     private Set<Artifact> allArtifacts() {
 108  3
         return new HashSet<Artifact>(project.getArtifacts());
 109  
     }
 110  
 
 111  
     private void unpack(File file, File destination, String includes, String excludes) throws MojoExecutionException {
 112  
         try {
 113  3
             destination.mkdirs();
 114  
 
 115  3
             UnArchiver unArchiver = archiverManager.getUnArchiver(file);
 116  3
             unArchiver.setSourceFile(file);
 117  3
             unArchiver.setDestDirectory(destination);
 118  
 
 119  3
             if (StringUtils.isNotEmpty(excludes) || StringUtils.isNotEmpty(includes)) {
 120  2
                 IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
 121  2
                 if (StringUtils.isNotEmpty(excludes)) {
 122  2
                     selectors[0].setExcludes(excludes.split(","));
 123  
                 }
 124  2
                 if (StringUtils.isNotEmpty(includes)) {
 125  2
                     selectors[0].setIncludes(includes.split(","));
 126  
                 }
 127  2
                 unArchiver.setFileSelectors(selectors);
 128  
             }
 129  
 
 130  3
             unArchiver.extract();
 131  
 
 132  2
             getLog().info("Unpacked " + file + " to " + destination);
 133  1
         } catch (Exception e) {
 134  1
             throw new MojoExecutionException("Failed unpacking " + file + " to " + destination, e);
 135  2
         }
 136  2
     }
 137  
 
 138  
 }