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.impl.DefaultModelVersionParser;
29  import org.apache.maven.impl.DefaultVersionParser;
30  import org.eclipse.aether.util.version.GenericVersionScheme;
31  
32  /**
33   * A stub implementation of {@link Artifact} for testing purposes.
34   * Provides basic artifact information without actual artifact resolution.
35   *
36   * @since 4.0.0
37   */
38  public class ArtifactStub implements Artifact {
39      private String groupId;
40      private String artifactId;
41      private String classifier;
42      private String version;
43      private String baseVersion;
44      private String extension;
45  
46      public ArtifactStub() {
47          groupId = "";
48          artifactId = "";
49          version = "";
50          classifier = "";
51          extension = "";
52      }
53  
54      public ArtifactStub(String groupId, String artifactId, String classifier, String version, String extension) {
55          this.groupId = groupId;
56          this.artifactId = artifactId;
57          this.classifier = classifier;
58          this.version = version;
59          this.extension = extension;
60      }
61  
62      @Nonnull
63      @Override
64      public String getGroupId() {
65          return groupId;
66      }
67  
68      public void setGroupId(String groupId) {
69          this.groupId = groupId;
70      }
71  
72      @Nonnull
73      @Override
74      public String getArtifactId() {
75          return artifactId;
76      }
77  
78      public void setArtifactId(String artifactId) {
79          this.artifactId = artifactId;
80      }
81  
82      @Nonnull
83      @Override
84      public String getClassifier() {
85          return classifier;
86      }
87  
88      public void setClassifier(String classifier) {
89          this.classifier = classifier;
90      }
91  
92      @Nonnull
93      @Override
94      public Version getVersion() {
95          return getParser().parseVersion(version);
96      }
97  
98      public void setVersion(String version) {
99          this.version = version;
100     }
101 
102     @Override
103     public Version getBaseVersion() {
104         return getParser().parseVersion(baseVersion != null ? baseVersion : version);
105     }
106 
107     public void setBaseVersion(String baseVersion) {
108         this.baseVersion = baseVersion;
109     }
110 
111     @Nonnull
112     @Override
113     public String getExtension() {
114         return extension;
115     }
116 
117     public void setExtension(String extension) {
118         this.extension = extension;
119     }
120 
121     @Override
122     public boolean isSnapshot() {
123         return false;
124     }
125 
126     @Override
127     public ArtifactCoordinates toCoordinates() {
128         return new ArtifactCoordinates() {
129             @Override
130             public String getGroupId() {
131                 return groupId;
132             }
133 
134             @Override
135             public String getArtifactId() {
136                 return artifactId;
137             }
138 
139             @Override
140             public String getClassifier() {
141                 return classifier;
142             }
143 
144             @Override
145             public VersionConstraint getVersionConstraint() {
146                 return getParser().parseVersionConstraint(version);
147             }
148 
149             @Override
150             public String getExtension() {
151                 return extension;
152             }
153         };
154     }
155 
156     @Override
157     public String toString() {
158         return "ArtifactStub["
159                 + "groupId='" + groupId + '\''
160                 + ", artifactId='" + artifactId + '\''
161                 + ", classifier='" + classifier + '\''
162                 + ", version='" + version + '\''
163                 + ", extension='" + extension + '\''
164                 + ']';
165     }
166 
167     @Override
168     public boolean equals(Object o) {
169         if (this == o) {
170             return true;
171         }
172         if (!(o instanceof ArtifactStub)) {
173             return false;
174         }
175         ArtifactStub that = (ArtifactStub) o;
176         return Objects.equals(groupId, that.groupId)
177                 && Objects.equals(artifactId, that.artifactId)
178                 && Objects.equals(classifier, that.classifier)
179                 && Objects.equals(version, that.version)
180                 && Objects.equals(extension, that.extension);
181     }
182 
183     @Override
184     public int hashCode() {
185         return Objects.hash(groupId, artifactId, classifier, version, extension);
186     }
187 
188     private static DefaultVersionParser getParser() {
189         return new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme()));
190     }
191 }