Coverage Report - org.apache.maven.artifact.DefaultArtifact
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArtifact
0%
0/186
0%
0/88
1.807
 
 1  
 package org.apache.maven.artifact;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.artifact.handler.ArtifactHandler;
 23  
 import org.apache.maven.artifact.metadata.ArtifactMetadata;
 24  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 25  
 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
 26  
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 27  
 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
 28  
 import org.apache.maven.artifact.versioning.VersionRange;
 29  
 import org.codehaus.plexus.util.StringUtils;
 30  
 
 31  
 import java.io.File;
 32  
 import java.util.Collection;
 33  
 import java.util.Collections;
 34  
 import java.util.HashMap;
 35  
 import java.util.List;
 36  
 import java.util.Map;
 37  
 import java.util.regex.Matcher;
 38  
 
 39  
 /**
 40  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
 41  
  * @version $Id: DefaultArtifact.java 634058 2008-03-05 22:18:13Z brett $
 42  
  * @todo this should possibly be replaced by type handler
 43  
  */
 44  
 public class DefaultArtifact
 45  
     implements Artifact
 46  
 {
 47  
     private String groupId;
 48  
 
 49  
     private String artifactId;
 50  
 
 51  
     /**
 52  
      * The resolved version for the artifact after conflict resolution, that has not been transformed.
 53  
      *
 54  
      * @todo should be final
 55  
      */
 56  
     private String baseVersion;
 57  
 
 58  
     private final String type;
 59  
 
 60  
     private final String classifier;
 61  
 
 62  
     private String scope;
 63  
 
 64  
     private File file;
 65  
 
 66  
     private ArtifactRepository repository;
 67  
 
 68  
     private String downloadUrl;
 69  
 
 70  
     private ArtifactFilter dependencyFilter;
 71  
 
 72  
     private ArtifactHandler artifactHandler;
 73  
 
 74  
     private List dependencyTrail;
 75  
 
 76  
     private String version;
 77  
 
 78  
     private VersionRange versionRange;
 79  
 
 80  
     private boolean resolved;
 81  
 
 82  
     private boolean release;
 83  
 
 84  
     private List availableVersions;
 85  
 
 86  
     private Map metadataMap;
 87  
 
 88  
     private boolean optional;
 89  
 
 90  
     public DefaultArtifact( String groupId, String artifactId, VersionRange versionRange, String scope, String type,
 91  
                             String classifier, ArtifactHandler artifactHandler )
 92  
     {
 93  0
         this( groupId, artifactId, versionRange, scope, type, classifier, artifactHandler, false );
 94  0
     }
 95  
 
 96  
     public DefaultArtifact( String groupId, String artifactId, VersionRange versionRange, String scope, String type,
 97  
                             String classifier, ArtifactHandler artifactHandler, boolean optional )
 98  0
     {
 99  0
         this.groupId = groupId;
 100  
 
 101  0
         this.artifactId = artifactId;
 102  
 
 103  0
         this.versionRange = versionRange;
 104  
 
 105  0
         selectVersionFromNewRangeIfAvailable();
 106  
 
 107  0
         this.artifactHandler = artifactHandler;
 108  
 
 109  0
         this.scope = scope;
 110  
 
 111  0
         this.type = type;
 112  
 
 113  0
         if ( classifier == null )
 114  
         {
 115  0
             classifier = artifactHandler.getClassifier();
 116  
         }
 117  
 
 118  0
         this.classifier = classifier;
 119  
 
 120  0
         this.optional = optional;
 121  
 
 122  0
         validateIdentity();
 123  0
     }
 124  
 
 125  
     private void validateIdentity()
 126  
     {
 127  0
         if ( empty( groupId ) )
 128  
         {
 129  0
             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
 130  
                                                   "The groupId cannot be empty." );
 131  
         }
 132  
 
 133  0
         if ( artifactId == null )
 134  
         {
 135  0
             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
 136  
                                                   "The artifactId cannot be empty." );
 137  
         }
 138  
 
 139  0
         if ( type == null )
 140  
         {
 141  0
             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
 142  
                                                   "The type cannot be empty." );
 143  
         }
 144  
 
 145  0
         if ( ( version == null ) && ( versionRange == null ) )
 146  
         {
 147  0
             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
 148  
                                                   "The version cannot be empty." );
 149  
         }
 150  0
     }
 151  
 
 152  
     private boolean empty( String value )
 153  
     {
 154  0
         return ( value == null ) || ( value.trim().length() < 1 );
 155  
     }
 156  
 
 157  
     public String getClassifier()
 158  
     {
 159  0
         return classifier;
 160  
     }
 161  
 
 162  
     public boolean hasClassifier()
 163  
     {
 164  0
         return StringUtils.isNotEmpty( classifier );
 165  
     }
 166  
 
 167  
     public String getScope()
 168  
     {
 169  0
         return scope;
 170  
     }
 171  
 
 172  
     public String getGroupId()
 173  
     {
 174  0
         return groupId;
 175  
     }
 176  
 
 177  
     public String getArtifactId()
 178  
     {
 179  0
         return artifactId;
 180  
     }
 181  
 
 182  
     public String getVersion()
 183  
     {
 184  0
         return version;
 185  
     }
 186  
 
 187  
     public void setVersion( String version )
 188  
     {
 189  0
         this.version = version;
 190  0
         setBaseVersionInternal( version );
 191  0
         versionRange = null;
 192  0
     }
 193  
 
 194  
     public String getType()
 195  
     {
 196  0
         return type;
 197  
     }
 198  
 
 199  
     public void setFile( File file )
 200  
     {
 201  0
         this.file = file;
 202  0
     }
 203  
 
 204  
     public File getFile()
 205  
     {
 206  0
         return file;
 207  
     }
 208  
 
 209  
     public ArtifactRepository getRepository()
 210  
     {
 211  0
         return repository;
 212  
     }
 213  
 
 214  
     public void setRepository( ArtifactRepository repository )
 215  
     {
 216  0
         this.repository = repository;
 217  0
     }
 218  
 
 219  
     // ----------------------------------------------------------------------
 220  
     //
 221  
     // ----------------------------------------------------------------------
 222  
 
 223  
     public String getId()
 224  
     {
 225  0
         return getDependencyConflictId() + ":" + getBaseVersion();
 226  
     }
 227  
 
 228  
     public String getDependencyConflictId()
 229  
     {
 230  0
         StringBuffer sb = new StringBuffer();
 231  0
         sb.append( getGroupId() );
 232  0
         sb.append( ":" );
 233  0
         appendArtifactTypeClassifierString( sb );
 234  0
         return sb.toString();
 235  
     }
 236  
 
 237  
     private void appendArtifactTypeClassifierString( StringBuffer sb )
 238  
     {
 239  0
         sb.append( getArtifactId() );
 240  0
         sb.append( ":" );
 241  0
         sb.append( getType() );
 242  0
         if ( hasClassifier() )
 243  
         {
 244  0
             sb.append( ":" );
 245  0
             sb.append( getClassifier() );
 246  
         }
 247  0
     }
 248  
 
 249  
     public void addMetadata( ArtifactMetadata metadata )
 250  
     {
 251  0
         if ( metadataMap == null )
 252  
         {
 253  0
             metadataMap = new HashMap();
 254  
         }
 255  
 
 256  0
         ArtifactMetadata m = (ArtifactMetadata) metadataMap.get( metadata.getKey() );
 257  0
         if ( m != null )
 258  
         {
 259  0
             m.merge( metadata );
 260  
         }
 261  
         else
 262  
         {
 263  0
             metadataMap.put( metadata.getKey(), metadata );
 264  
         }
 265  0
     }
 266  
 
 267  
     public Collection getMetadataList()
 268  
     {
 269  0
         return metadataMap == null ? Collections.EMPTY_LIST : metadataMap.values();
 270  
     }
 271  
 
 272  
     // ----------------------------------------------------------------------
 273  
     // Object overrides
 274  
     // ----------------------------------------------------------------------
 275  
 
 276  
     public String toString()
 277  
     {
 278  0
         StringBuffer sb = new StringBuffer();
 279  0
         if ( getGroupId() != null )
 280  
         {
 281  0
             sb.append( getGroupId() );
 282  0
             sb.append( ":" );
 283  
         }
 284  0
         appendArtifactTypeClassifierString( sb );
 285  0
         sb.append( ":" );
 286  0
         if ( getBaseVersionInternal() != null )
 287  
         {
 288  0
             sb.append( getBaseVersionInternal() );
 289  
         }
 290  
         else
 291  
         {
 292  0
             sb.append( versionRange.toString() );
 293  
         }
 294  0
         if ( scope != null )
 295  
         {
 296  0
             sb.append( ":" );
 297  0
             sb.append( scope );
 298  
         }
 299  0
         return sb.toString();
 300  
     }
 301  
 
 302  
     public int hashCode()
 303  
     {
 304  0
         int result = 17;
 305  0
         result = 37 * result + groupId.hashCode();
 306  0
         result = 37 * result + artifactId.hashCode();
 307  0
         result = 37 * result + type.hashCode();
 308  0
         if ( version != null )
 309  
         {
 310  0
             result = 37 * result + version.hashCode();
 311  
         }
 312  0
         result = 37 * result + ( classifier != null ? classifier.hashCode() : 0 );
 313  0
         return result;
 314  
     }
 315  
 
 316  
     public boolean equals( Object o )
 317  
     {
 318  0
         if ( o == this )
 319  
         {
 320  0
             return true;
 321  
         }
 322  
 
 323  0
         if ( !( o instanceof Artifact ) )
 324  
         {
 325  0
             return false;
 326  
         }
 327  
 
 328  0
         Artifact a = (Artifact) o;
 329  
 
 330  0
         if ( !a.getGroupId().equals( groupId ) )
 331  
         {
 332  0
             return false;
 333  
         }
 334  0
         else if ( !a.getArtifactId().equals( artifactId ) )
 335  
         {
 336  0
             return false;
 337  
         }
 338  0
         else if ( !a.getVersion().equals( version ) )
 339  
         {
 340  0
             return false;
 341  
         }
 342  0
         else if ( !a.getType().equals( type ) )
 343  
         {
 344  0
             return false;
 345  
         }
 346  0
         else if ( a.getClassifier() == null ? classifier != null : !a.getClassifier().equals( classifier ) )
 347  
         {
 348  0
             return false;
 349  
         }
 350  
 
 351  
         // We don't consider the version range in the comparison, just the resolved version
 352  
 
 353  0
         return true;
 354  
     }
 355  
 
 356  
     public String getBaseVersion()
 357  
     {
 358  0
         if ( baseVersion == null )
 359  
         {
 360  0
             if ( version == null )
 361  
             {
 362  0
                 throw new NullPointerException( "version was null for " + groupId + ":" + artifactId );
 363  
             }
 364  0
             setBaseVersionInternal( version );
 365  
         }
 366  0
         return baseVersion;
 367  
     }
 368  
 
 369  
     protected String getBaseVersionInternal()
 370  
     {
 371  0
         if ( ( baseVersion == null ) && ( version != null ) )
 372  
         {
 373  0
             setBaseVersionInternal( version );
 374  
         }
 375  
 
 376  0
         return baseVersion;
 377  
     }
 378  
 
 379  
     public void setBaseVersion( String baseVersion )
 380  
     {
 381  0
         setBaseVersionInternal( baseVersion );
 382  0
     }
 383  
 
 384  
     protected void setBaseVersionInternal( String baseVersion )
 385  
     {
 386  0
         Matcher m = VERSION_FILE_PATTERN.matcher( baseVersion );
 387  0
         if ( m.matches() )
 388  
         {
 389  0
             this.baseVersion = m.group( 1 ) + "-" + SNAPSHOT_VERSION;
 390  
         }
 391  
         else
 392  
         {
 393  0
             this.baseVersion = baseVersion;
 394  
         }
 395  0
     }
 396  
 
 397  
     public int compareTo( Object o )
 398  
     {
 399  0
         Artifact a = (Artifact) o;
 400  
 
 401  0
         int result = groupId.compareTo( a.getGroupId() );
 402  0
         if ( result == 0 )
 403  
         {
 404  0
             result = artifactId.compareTo( a.getArtifactId() );
 405  0
             if ( result == 0 )
 406  
             {
 407  0
                 result = type.compareTo( a.getType() );
 408  0
                 if ( result == 0 )
 409  
                 {
 410  0
                     if ( classifier == null )
 411  
                     {
 412  0
                         if ( a.getClassifier() != null )
 413  
                         {
 414  0
                             result = 1;
 415  
                         }
 416  
                     }
 417  
                     else
 418  
                     {
 419  0
                         if ( a.getClassifier() != null )
 420  
                         {
 421  0
                             result = classifier.compareTo( a.getClassifier() );
 422  
                         }
 423  
                         else
 424  
                         {
 425  0
                             result = -1;
 426  
                         }
 427  
                     }
 428  0
                     if ( result == 0 )
 429  
                     {
 430  
                         // We don't consider the version range in the comparison, just the resolved version
 431  0
                         result = version.compareTo( a.getVersion() );
 432  
                     }
 433  
                 }
 434  
             }
 435  
         }
 436  0
         return result;
 437  
     }
 438  
 
 439  
     public void updateVersion( String version, ArtifactRepository localRepository )
 440  
     {
 441  0
         setResolvedVersion( version );
 442  0
         setFile( new File( localRepository.getBasedir(), localRepository.pathOf( this ) ) );
 443  0
     }
 444  
 
 445  
     public String getDownloadUrl()
 446  
     {
 447  0
         return downloadUrl;
 448  
     }
 449  
 
 450  
     public void setDownloadUrl( String downloadUrl )
 451  
     {
 452  0
         this.downloadUrl = downloadUrl;
 453  0
     }
 454  
 
 455  
     public ArtifactFilter getDependencyFilter()
 456  
     {
 457  0
         return dependencyFilter;
 458  
     }
 459  
 
 460  
     public void setDependencyFilter( ArtifactFilter artifactFilter )
 461  
     {
 462  0
         dependencyFilter = artifactFilter;
 463  0
     }
 464  
 
 465  
     public ArtifactHandler getArtifactHandler()
 466  
     {
 467  0
         return artifactHandler;
 468  
     }
 469  
 
 470  
     public List getDependencyTrail()
 471  
     {
 472  0
         return dependencyTrail;
 473  
     }
 474  
 
 475  
     public void setDependencyTrail( List dependencyTrail )
 476  
     {
 477  0
         this.dependencyTrail = dependencyTrail;
 478  0
     }
 479  
 
 480  
     public void setScope( String scope )
 481  
     {
 482  0
         this.scope = scope;
 483  0
     }
 484  
 
 485  
     public VersionRange getVersionRange()
 486  
     {
 487  
         // I am assuming this is happening as a result of the MNG-1577 work, but somehow the value
 488  
         // of versionRange just goes null or is not set. But this is happeningin Yoko and the value is
 489  
         // set when attaching the JAR and not set when attaching the test JAR.
 490  0
         if ( versionRange == null )
 491  
         {
 492  0
             versionRange = VersionRange.createFromVersion( version );
 493  
         }
 494  
 
 495  0
         return versionRange;
 496  
     }
 497  
 
 498  
     public void setVersionRange( VersionRange versionRange )
 499  
     {
 500  0
         this.versionRange = versionRange;
 501  
 
 502  0
         selectVersionFromNewRangeIfAvailable();
 503  0
     }
 504  
 
 505  
     private void selectVersionFromNewRangeIfAvailable()
 506  
     {
 507  0
         if ( ( versionRange != null ) && ( versionRange.getRecommendedVersion() != null ) )
 508  
         {
 509  0
             selectVersion( versionRange.getRecommendedVersion().toString() );
 510  
         }
 511  
         else
 512  
         {
 513  0
             version = null;
 514  0
             baseVersion = null;
 515  
         }
 516  0
     }
 517  
 
 518  
     public void selectVersion( String version )
 519  
     {
 520  0
         this.version = version;
 521  0
         setBaseVersionInternal( version );
 522  0
     }
 523  
 
 524  
     public void setGroupId( String groupId )
 525  
     {
 526  0
         this.groupId = groupId;
 527  0
     }
 528  
 
 529  
     public void setArtifactId( String artifactId )
 530  
     {
 531  0
         this.artifactId = artifactId;
 532  0
     }
 533  
 
 534  
     public boolean isSnapshot()
 535  
     {
 536  0
         if ( getBaseVersion() != null )
 537  
         {
 538  0
             return getBaseVersion().endsWith( SNAPSHOT_VERSION ) || getBaseVersion().equals( LATEST_VERSION );
 539  
         }
 540  
         else
 541  
         {
 542  0
             return false;
 543  
         }
 544  
     }
 545  
 
 546  
     public void setResolved( boolean resolved )
 547  
     {
 548  0
         this.resolved = resolved;
 549  0
     }
 550  
 
 551  
     public boolean isResolved()
 552  
     {
 553  0
         return resolved;
 554  
     }
 555  
 
 556  
     public void setResolvedVersion( String version )
 557  
     {
 558  0
         this.version = version;
 559  
         // retain baseVersion
 560  0
     }
 561  
 
 562  
     public void setArtifactHandler( ArtifactHandler artifactHandler )
 563  
     {
 564  0
         this.artifactHandler = artifactHandler;
 565  0
     }
 566  
 
 567  
     public void setRelease( boolean release )
 568  
     {
 569  0
         this.release = release;
 570  0
     }
 571  
 
 572  
     public boolean isRelease()
 573  
     {
 574  0
         return release;
 575  
     }
 576  
 
 577  
     public List getAvailableVersions()
 578  
     {
 579  0
         return availableVersions;
 580  
     }
 581  
 
 582  
     public void setAvailableVersions( List availableVersions )
 583  
     {
 584  0
         this.availableVersions = availableVersions;
 585  0
     }
 586  
 
 587  
     public boolean isOptional()
 588  
     {
 589  0
         return optional;
 590  
     }
 591  
 
 592  
     public ArtifactVersion getSelectedVersion()
 593  
         throws OverConstrainedVersionException
 594  
     {
 595  0
         return versionRange.getSelectedVersion( this );
 596  
     }
 597  
 
 598  
     public boolean isSelectedVersionKnown()
 599  
         throws OverConstrainedVersionException
 600  
     {
 601  0
         return versionRange.isSelectedVersionKnown( this );
 602  
     }
 603  
 
 604  
     public void setOptional( boolean optional )
 605  
     {
 606  0
         this.optional = optional;
 607  0
     }
 608  
 }