Coverage Report - org.apache.maven.artifact.DefaultArtifact
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArtifact
58 %
114/195
40 %
38/94
0
 
 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 767322 2009-04-21 22:52:54Z jdcasey $
 42  
  * @todo this should possibly be replaced by type handler
 43  
  */
 44  0
 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<String> 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<ArtifactVersion> availableVersions;
 85  
 
 86  
     private Map<Object, ArtifactMetadata> 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  30
         this( groupId, artifactId, versionRange, scope, type, classifier, artifactHandler, false );
 94  30
     }
 95  
 
 96  
     public DefaultArtifact( String groupId, String artifactId, VersionRange versionRange, String scope, String type,
 97  
                             String classifier, ArtifactHandler artifactHandler, boolean optional )
 98  801
     {
 99  801
         this.groupId = groupId;
 100  
 
 101  801
         this.artifactId = artifactId;
 102  
 
 103  801
         this.versionRange = versionRange;
 104  
 
 105  801
         selectVersionFromNewRangeIfAvailable();
 106  
 
 107  801
         this.artifactHandler = artifactHandler;
 108  
 
 109  801
         this.scope = scope;
 110  
 
 111  801
         this.type = type;
 112  
 
 113  801
         if ( classifier == null )
 114  
         {
 115  773
             classifier = artifactHandler.getClassifier();
 116  
         }
 117  
 
 118  801
         this.classifier = classifier;
 119  
 
 120  801
         this.optional = optional;
 121  
 
 122  801
         validateIdentity();
 123  801
     }
 124  
 
 125  
     private void validateIdentity()
 126  
     {
 127  801
         if ( empty( groupId ) )
 128  
         {
 129  0
             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
 130  
                                                   "The groupId cannot be empty." );
 131  
         }
 132  
 
 133  801
         if ( artifactId == null )
 134  
         {
 135  0
             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
 136  
                                                   "The artifactId cannot be empty." );
 137  
         }
 138  
 
 139  801
         if ( type == null )
 140  
         {
 141  0
             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
 142  
                                                   "The type cannot be empty." );
 143  
         }
 144  
 
 145  801
         if ( ( version == null ) && ( versionRange == null ) )
 146  
         {
 147  0
             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
 148  
                                                   "The version cannot be empty." );
 149  
         }
 150  801
     }
 151  
 
 152  
     private boolean empty( String value )
 153  
     {
 154  801
         return ( value == null ) || ( value.trim().length() < 1 );
 155  
     }
 156  
 
 157  
     public String getClassifier()
 158  
     {
 159  306
         return classifier;
 160  
     }
 161  
 
 162  
     public boolean hasClassifier()
 163  
     {
 164  6422
         return StringUtils.isNotEmpty( classifier );
 165  
     }
 166  
 
 167  
     public String getScope()
 168  
     {
 169  4816
         return scope;
 170  
     }
 171  
 
 172  
     public String getGroupId()
 173  
     {
 174  6879
         return groupId;
 175  
     }
 176  
 
 177  
     public String getArtifactId()
 178  
     {
 179  7076
         return artifactId;
 180  
     }
 181  
 
 182  
     public String getVersion()
 183  
     {
 184  4096
         return version;
 185  
     }
 186  
 
 187  
     public void setVersion( String version )
 188  
     {
 189  23
         this.version = version;
 190  23
         setBaseVersionInternal( version );
 191  23
         versionRange = null;
 192  23
     }
 193  
 
 194  
     public String getType()
 195  
     {
 196  6769
         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  1917
         return getDependencyConflictId() + ":" + getBaseVersion();
 226  
     }
 227  
 
 228  
     public String getDependencyConflictId()
 229  
     {
 230  6418
         StringBuffer sb = new StringBuffer();
 231  6418
         sb.append( getGroupId() );
 232  6418
         sb.append( ":" );
 233  6418
         appendArtifactTypeClassifierString( sb );
 234  6418
         return sb.toString();
 235  
     }
 236  
 
 237  
     private void appendArtifactTypeClassifierString( StringBuffer sb )
 238  
     {
 239  6422
         sb.append( getArtifactId() );
 240  6422
         sb.append( ":" );
 241  6422
         sb.append( getType() );
 242  6422
         if ( hasClassifier() )
 243  
         {
 244  11
             sb.append( ":" );
 245  11
             sb.append( getClassifier() );
 246  
         }
 247  6422
     }
 248  
 
 249  
     public void addMetadata( ArtifactMetadata metadata )
 250  
     {
 251  0
         if ( metadataMap == null )
 252  
         {
 253  0
             metadataMap = new HashMap<Object, ArtifactMetadata>();
 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 ArtifactMetadata getMetadata( Class<?> metadataClass )
 268  
     {
 269  0
         Collection<ArtifactMetadata> metadata = getMetadataList();
 270  
         
 271  0
         if ( metadata != null )
 272  
         {
 273  0
             for ( ArtifactMetadata m : metadata )
 274  
             {
 275  0
                 if ( metadataClass.isAssignableFrom( m.getClass() ) )
 276  
                 {
 277  0
                     return m;
 278  
                 }
 279  
             }
 280  
         }
 281  
         
 282  0
         return null;
 283  
     }
 284  
     
 285  
     public Collection<ArtifactMetadata> getMetadataList()
 286  
     {
 287  
         Collection<ArtifactMetadata> result;
 288  0
         if ( metadataMap == null )
 289  
         {
 290  0
             result = Collections.emptyList();
 291  
         }
 292  
         else
 293  
         {
 294  0
             result = metadataMap.values();
 295  
         }
 296  
         
 297  0
         return result;
 298  
     }
 299  
 
 300  
     // ----------------------------------------------------------------------
 301  
     // Object overrides
 302  
     // ----------------------------------------------------------------------
 303  
 
 304  
     public String toString()
 305  
     {
 306  4
         StringBuffer sb = new StringBuffer();
 307  4
         if ( getGroupId() != null )
 308  
         {
 309  3
             sb.append( getGroupId() );
 310  3
             sb.append( ":" );
 311  
         }
 312  4
         appendArtifactTypeClassifierString( sb );
 313  4
         sb.append( ":" );
 314  4
         if ( getBaseVersionInternal() != null )
 315  
         {
 316  4
             sb.append( getBaseVersionInternal() );
 317  
         }
 318  
         else
 319  
         {
 320  0
             sb.append( versionRange.toString() );
 321  
         }
 322  4
         if ( scope != null )
 323  
         {
 324  3
             sb.append( ":" );
 325  3
             sb.append( scope );
 326  
         }
 327  4
         return sb.toString();
 328  
     }
 329  
 
 330  
     public int hashCode()
 331  
     {
 332  1290
         int result = 17;
 333  1290
         result = 37 * result + groupId.hashCode();
 334  1290
         result = 37 * result + artifactId.hashCode();
 335  1290
         result = 37 * result + type.hashCode();
 336  1290
         if ( version != null )
 337  
         {
 338  1260
             result = 37 * result + version.hashCode();
 339  
         }
 340  1290
         result = 37 * result + ( classifier != null ? classifier.hashCode() : 0 );
 341  1290
         return result;
 342  
     }
 343  
 
 344  
     public boolean equals( Object o )
 345  
     {
 346  598
         if ( o == this )
 347  
         {
 348  561
             return true;
 349  
         }
 350  
 
 351  37
         if ( !( o instanceof Artifact ) )
 352  
         {
 353  0
             return false;
 354  
         }
 355  
 
 356  37
         Artifact a = (Artifact) o;
 357  
 
 358  37
         if ( !a.getGroupId().equals( groupId ) )
 359  
         {
 360  0
             return false;
 361  
         }
 362  37
         else if ( !a.getArtifactId().equals( artifactId ) )
 363  
         {
 364  0
             return false;
 365  
         }
 366  37
         else if ( !a.getVersion().equals( version ) )
 367  
         {
 368  0
             return false;
 369  
         }
 370  37
         else if ( !a.getType().equals( type ) )
 371  
         {
 372  0
             return false;
 373  
         }
 374  37
         else if ( a.getClassifier() == null ? classifier != null : !a.getClassifier().equals( classifier ) )
 375  
         {
 376  0
             return false;
 377  
         }
 378  
 
 379  
         // We don't consider the version range in the comparison, just the resolved version
 380  
 
 381  37
         return true;
 382  
     }
 383  
 
 384  
     public String getBaseVersion()
 385  
     {
 386  1918
         if ( baseVersion == null )
 387  
         {
 388  0
             if ( version == null )
 389  
             {
 390  0
                 throw new NullPointerException( "version was null for " + groupId + ":" + artifactId );
 391  
             }
 392  0
             setBaseVersionInternal( version );
 393  
         }
 394  1918
         return baseVersion;
 395  
     }
 396  
 
 397  
     protected String getBaseVersionInternal()
 398  
     {
 399  8
         if ( ( baseVersion == null ) && ( version != null ) )
 400  
         {
 401  0
             setBaseVersionInternal( version );
 402  
         }
 403  
 
 404  8
         return baseVersion;
 405  
     }
 406  
 
 407  
     public void setBaseVersion( String baseVersion )
 408  
     {
 409  0
         setBaseVersionInternal( baseVersion );
 410  0
     }
 411  
 
 412  
     protected void setBaseVersionInternal( String baseVersion )
 413  
     {
 414  1083
         Matcher m = VERSION_FILE_PATTERN.matcher( baseVersion );
 415  1083
         if ( m.matches() )
 416  
         {
 417  9
             this.baseVersion = m.group( 1 ) + "-" + SNAPSHOT_VERSION;
 418  
         }
 419  
         else
 420  
         {
 421  1074
             this.baseVersion = baseVersion;
 422  
         }
 423  1083
     }
 424  
 
 425  
     public int compareTo( Artifact a )
 426  
     {
 427  0
         int result = groupId.compareTo( a.getGroupId() );
 428  0
         if ( result == 0 )
 429  
         {
 430  0
             result = artifactId.compareTo( a.getArtifactId() );
 431  0
             if ( result == 0 )
 432  
             {
 433  0
                 result = type.compareTo( a.getType() );
 434  0
                 if ( result == 0 )
 435  
                 {
 436  0
                     if ( classifier == null )
 437  
                     {
 438  0
                         if ( a.getClassifier() != null )
 439  
                         {
 440  0
                             result = 1;
 441  
                         }
 442  
                     }
 443  
                     else
 444  
                     {
 445  0
                         if ( a.getClassifier() != null )
 446  
                         {
 447  0
                             result = classifier.compareTo( a.getClassifier() );
 448  
                         }
 449  
                         else
 450  
                         {
 451  0
                             result = -1;
 452  
                         }
 453  
                     }
 454  0
                     if ( result == 0 )
 455  
                     {
 456  
                         // We don't consider the version range in the comparison, just the resolved version
 457  0
                         result = version.compareTo( a.getVersion() );
 458  
                     }
 459  
                 }
 460  
             }
 461  
         }
 462  0
         return result;
 463  
     }
 464  
 
 465  
     public void updateVersion( String version, ArtifactRepository localRepository )
 466  
     {
 467  0
         setResolvedVersion( version );
 468  0
         setFile( new File( localRepository.getBasedir(), localRepository.pathOf( this ) ) );
 469  0
     }
 470  
 
 471  
     public String getDownloadUrl()
 472  
     {
 473  0
         return downloadUrl;
 474  
     }
 475  
 
 476  
     public void setDownloadUrl( String downloadUrl )
 477  
     {
 478  0
         this.downloadUrl = downloadUrl;
 479  0
     }
 480  
 
 481  
     public ArtifactFilter getDependencyFilter()
 482  
     {
 483  1096
         return dependencyFilter;
 484  
     }
 485  
 
 486  
     public void setDependencyFilter( ArtifactFilter artifactFilter )
 487  
     {
 488  306
         dependencyFilter = artifactFilter;
 489  306
     }
 490  
 
 491  
     public ArtifactHandler getArtifactHandler()
 492  
     {
 493  0
         return artifactHandler;
 494  
     }
 495  
 
 496  
     public List<String> getDependencyTrail()
 497  
     {
 498  4
         return dependencyTrail;
 499  
     }
 500  
 
 501  
     public void setDependencyTrail( List<String> dependencyTrail )
 502  
     {
 503  943
         this.dependencyTrail = dependencyTrail;
 504  943
     }
 505  
 
 506  
     public void setScope( String scope )
 507  
     {
 508  25
         this.scope = scope;
 509  25
     }
 510  
 
 511  
     public VersionRange getVersionRange()
 512  
     {
 513  
         // I am assuming this is happening as a result of the MNG-1577 work, but somehow the value
 514  
         // of versionRange just goes null or is not set. But this is happeningin Yoko and the value is
 515  
         // set when attaching the JAR and not set when attaching the test JAR.
 516  925
         if ( versionRange == null )
 517  
         {
 518  9
             versionRange = VersionRange.createFromVersion( version );
 519  
         }
 520  
 
 521  925
         return versionRange;
 522  
     }
 523  
 
 524  
     public void setVersionRange( VersionRange versionRange )
 525  
     {
 526  280
         this.versionRange = versionRange;
 527  
 
 528  280
         selectVersionFromNewRangeIfAvailable();
 529  280
     }
 530  
 
 531  
     private void selectVersionFromNewRangeIfAvailable()
 532  
     {
 533  1081
         if ( ( versionRange != null ) && ( versionRange.getRecommendedVersion() != null ) )
 534  
         {
 535  1041
             selectVersion( versionRange.getRecommendedVersion().toString() );
 536  
         }
 537  
         else
 538  
         {
 539  40
             version = null;
 540  40
             baseVersion = null;
 541  
         }
 542  1081
     }
 543  
 
 544  
     public void selectVersion( String version )
 545  
     {
 546  1060
         this.version = version;
 547  1060
         setBaseVersionInternal( version );
 548  1060
     }
 549  
 
 550  
     public void setGroupId( String groupId )
 551  
     {
 552  2
         this.groupId = groupId;
 553  2
     }
 554  
 
 555  
     public void setArtifactId( String artifactId )
 556  
     {
 557  0
         this.artifactId = artifactId;
 558  0
     }
 559  
 
 560  
     public boolean isSnapshot()
 561  
     {
 562  0
         if ( getBaseVersion() != null )
 563  
         {
 564  0
             return getBaseVersion().endsWith( SNAPSHOT_VERSION ) || getBaseVersion().equals( LATEST_VERSION );
 565  
         }
 566  
         else
 567  
         {
 568  0
             return false;
 569  
         }
 570  
     }
 571  
 
 572  
     public void setResolved( boolean resolved )
 573  
     {
 574  0
         this.resolved = resolved;
 575  0
     }
 576  
 
 577  
     public boolean isResolved()
 578  
     {
 579  0
         return resolved;
 580  
     }
 581  
 
 582  
     public void setResolvedVersion( String version )
 583  
     {
 584  0
         this.version = version;
 585  
         // retain baseVersion
 586  0
     }
 587  
 
 588  
     public void setArtifactHandler( ArtifactHandler artifactHandler )
 589  
     {
 590  0
         this.artifactHandler = artifactHandler;
 591  0
     }
 592  
 
 593  
     public void setRelease( boolean release )
 594  
     {
 595  0
         this.release = release;
 596  0
     }
 597  
 
 598  
     public boolean isRelease()
 599  
     {
 600  0
         return release;
 601  
     }
 602  
 
 603  
     public List<ArtifactVersion> getAvailableVersions()
 604  
     {
 605  33
         return availableVersions;
 606  
     }
 607  
 
 608  
     public void setAvailableVersions( List<ArtifactVersion> availableVersions )
 609  
     {
 610  17
         this.availableVersions = availableVersions;
 611  17
     }
 612  
 
 613  
     public boolean isOptional()
 614  
     {
 615  959
         return optional;
 616  
     }
 617  
 
 618  
     public ArtifactVersion getSelectedVersion()
 619  
         throws OverConstrainedVersionException
 620  
     {
 621  0
         return versionRange.getSelectedVersion( this );
 622  
     }
 623  
 
 624  
     public boolean isSelectedVersionKnown()
 625  
         throws OverConstrainedVersionException
 626  
     {
 627  13
         return versionRange.isSelectedVersionKnown( this );
 628  
     }
 629  
 
 630  
     public void setOptional( boolean optional )
 631  
     {
 632  0
         this.optional = optional;
 633  0
     }
 634  
 }