View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.api.plugin.testing.stubs;
20  
21  import java.util.Objects;
22  
23  import org.apache.maven.api.Artifact;
24  import org.apache.maven.api.ArtifactCoordinates;
25  import org.apache.maven.api.Version;
26  import org.apache.maven.api.VersionConstraint;
27  import org.apache.maven.api.annotations.Nonnull;
28  import org.apache.maven.internal.impl.DefaultModelVersionParser;
29  import org.apache.maven.internal.impl.DefaultVersionParser;
30  import org.eclipse.aether.util.version.GenericVersionScheme;
31  
32  /**
33   *
34   */
35  public class ArtifactStub implements Artifact {
36      private String groupId;
37      private String artifactId;
38      private String classifier;
39      private String version;
40      private String baseVersion;
41      private String extension;
42  
43      public ArtifactStub() {
44          groupId = "";
45          artifactId = "";
46          version = "";
47          classifier = "";
48          extension = "";
49      }
50  
51      public ArtifactStub(String groupId, String artifactId, String classifier, String version, String extension) {
52          this.groupId = groupId;
53          this.artifactId = artifactId;
54          this.classifier = classifier;
55          this.version = version;
56          this.extension = extension;
57      }
58  
59      @Nonnull
60      @Override
61      public String getGroupId() {
62          return groupId;
63      }
64  
65      public void setGroupId(String groupId) {
66          this.groupId = groupId;
67      }
68  
69      @Nonnull
70      @Override
71      public String getArtifactId() {
72          return artifactId;
73      }
74  
75      public void setArtifactId(String artifactId) {
76          this.artifactId = artifactId;
77      }
78  
79      @Nonnull
80      @Override
81      public String getClassifier() {
82          return classifier;
83      }
84  
85      public void setClassifier(String classifier) {
86          this.classifier = classifier;
87      }
88  
89      @Nonnull
90      @Override
91      public Version getVersion() {
92          return getParser().parseVersion(version);
93      }
94  
95      public void setVersion(String version) {
96          this.version = version;
97      }
98  
99      public Version getBaseVersion() {
100         return getParser().parseVersion(baseVersion != null ? baseVersion : version);
101     }
102 
103     public void setBaseVersion(String baseVersion) {
104         this.baseVersion = baseVersion;
105     }
106 
107     @Nonnull
108     @Override
109     public String getExtension() {
110         return extension;
111     }
112 
113     public void setExtension(String extension) {
114         this.extension = extension;
115     }
116 
117     @Override
118     public boolean isSnapshot() {
119         return false;
120     }
121 
122     @Override
123     public ArtifactCoordinates toCoordinates() {
124         return new ArtifactCoordinates() {
125             @Override
126             public String getGroupId() {
127                 return groupId;
128             }
129 
130             @Override
131             public String getArtifactId() {
132                 return artifactId;
133             }
134 
135             @Override
136             public String getClassifier() {
137                 return classifier;
138             }
139 
140             @Override
141             public VersionConstraint getVersionConstraint() {
142                 return getParser().parseVersionConstraint(version);
143             }
144 
145             @Override
146             public String getExtension() {
147                 return extension;
148             }
149         };
150     }
151 
152     @Override
153     public String toString() {
154         return "ArtifactStub["
155                 + "groupId='" + groupId + '\''
156                 + ", artifactId='" + artifactId + '\''
157                 + ", classifier='" + classifier + '\''
158                 + ", version='" + version + '\''
159                 + ", extension='" + extension + '\''
160                 + ']';
161     }
162 
163     @Override
164     public boolean equals(Object o) {
165         if (this == o) {
166             return true;
167         }
168         if (!(o instanceof ArtifactStub)) {
169             return false;
170         }
171         ArtifactStub that = (ArtifactStub) o;
172         return Objects.equals(groupId, that.groupId)
173                 && Objects.equals(artifactId, that.artifactId)
174                 && Objects.equals(classifier, that.classifier)
175                 && Objects.equals(version, that.version)
176                 && Objects.equals(extension, that.extension);
177     }
178 
179     @Override
180     public int hashCode() {
181         return Objects.hash(groupId, artifactId, classifier, version, extension);
182     }
183 
184     private static DefaultVersionParser getParser() {
185         return new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme()));
186     }
187 }