Coverage Report - com.jcabi.aether.RootArtifact
 
Classes in this File Line Coverage Branch Coverage Complexity
RootArtifact
77%
21/27
46%
15/32
2
RootArtifact$1
N/A
N/A
2
RootArtifact$AjcClosure1
100%
1/1
N/A
2
RootArtifact$NonOptionalFilter
100%
2/2
100%
2/2
2
 
 1  20
 /**
 2  
  * Copyright (c) 2012-2014, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.aether;
 31  
 
 32  
 import com.jcabi.aspects.Cacheable;
 33  
 import com.jcabi.log.Logger;
 34  
 import java.util.Collection;
 35  
 import java.util.List;
 36  
 import javax.validation.constraints.NotNull;
 37  
 import lombok.EqualsAndHashCode;
 38  
 import org.apache.maven.model.Exclusion;
 39  
 import org.sonatype.aether.artifact.Artifact;
 40  
 import org.sonatype.aether.graph.DependencyFilter;
 41  
 import org.sonatype.aether.graph.DependencyNode;
 42  
 import org.sonatype.aether.resolution.DependencyResolutionException;
 43  
 import org.sonatype.aether.util.artifact.JavaScopes;
 44  
 
 45  
 /**
 46  
  * One root artifact found in the project.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id$
 50  
  * @since 0.7.16
 51  
  */
 52  513
 @EqualsAndHashCode(of = { "aether", "art", "exclusions" })
 53  
 final class RootArtifact {
 54  
 
 55  
     /**
 56  
      * The aether for finding children.
 57  
      */
 58  
     @NotNull
 59  
     private final transient Aether aether;
 60  
 
 61  
     /**
 62  
      * The artifact.
 63  
      */
 64  
     @NotNull
 65  
     private final transient Artifact art;
 66  
 
 67  
     /**
 68  
      * Exclusions.
 69  
      */
 70  
     @NotNull
 71  
     private final transient Collection<Exclusion> exclusions;
 72  
 
 73  
     /**
 74  
      * Ctor.
 75  
      * @param aeth Aether for finding children
 76  
      * @param artifact The artifact
 77  
      * @param excl Exclusions
 78  
      */
 79  
     protected RootArtifact(@NotNull final Aether aeth,
 80  
         @NotNull final Artifact artifact,
 81  12
         @NotNull final Collection<Exclusion> excl) {
 82  12
         this.aether = aeth;
 83  12
         this.art = artifact;
 84  12
         this.exclusions = excl;
 85  12
     }
 86  
 
 87  
     /**
 88  
      * {@inheritDoc}
 89  
      */
 90  
     @Override
 91  
     public String toString() {
 92  3
         final StringBuilder text = new StringBuilder();
 93  3
         text.append(
 94  
             Logger.format(
 95  
                 "%s:%s:%s:%d",
 96  
                 this.art.getGroupId(),
 97  
                 this.art.getArtifactId(),
 98  
                 this.art.getVersion(),
 99  
                 this.exclusions.size()
 100  
             )
 101  
         );
 102  
         try {
 103  3
             for (final Artifact child : this.children()) {
 104  2
                 text.append("\n  ").append(child);
 105  2
                 if (this.excluded(child)) {
 106  0
                     text.append(" (excluded)");
 107  
                 }
 108  2
             }
 109  2
         } catch (final DependencyResolutionException ex) {
 110  2
             text.append(' ').append(ex);
 111  1
         }
 112  3
         return text.toString();
 113  
     }
 114  
 
 115  
     /**
 116  
      * Get artifact.
 117  
      * @return The artifact
 118  
      */
 119  
     public Artifact artifact() {
 120  0
         return this.art;
 121  
     }
 122  
 
 123  
     /**
 124  
      * Get all dependencies of this root artifact.
 125  
      * @return The list of artifacts
 126  
      * @throws DependencyResolutionException If fails to resolve
 127  
      */
 128  
     @Cacheable(forever = true)
 129  
     public Collection<Artifact> children()
 130  
         throws DependencyResolutionException {
 131  20
         return this.aether.resolve(
 132  
             this.art, JavaScopes.COMPILE, new NonOptionalFilter()
 133  
         );
 134  
     }
 135  
 
 136  
     /**
 137  
      * Is this one should be excluded?
 138  
      * @param artifact The artifact to check
 139  
      * @return TRUE if it should be excluded
 140  
      */
 141  
     public boolean excluded(@NotNull final Artifact artifact) {
 142  24
         boolean excluded = false;
 143  24
         for (final Exclusion exclusion : this.exclusions) {
 144  0
             if (exclusion.getArtifactId().equals(artifact.getArtifactId())
 145  
                 && exclusion.getGroupId().equals(artifact.getGroupId())) {
 146  0
                 excluded = true;
 147  0
                 break;
 148  
             }
 149  0
         }
 150  24
         return excluded;
 151  
     }
 152  
 
 153  
     /**
 154  
      * Filter that rejects optional dependencies.
 155  
      */
 156  14
     private static class NonOptionalFilter implements DependencyFilter {
 157  
         @Override
 158  
         public boolean accept(final DependencyNode node,
 159  
             final List<DependencyNode> parents) {
 160  15
             return !node.getDependency().isOptional();
 161  
         }
 162  
     }
 163  
 }