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   *
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      @Override
100     public Version getBaseVersion() {
101         return getParser().parseVersion(baseVersion != null ? baseVersion : version);
102     }
103 
104     public void setBaseVersion(String baseVersion) {
105         this.baseVersion = baseVersion;
106     }
107 
108     @Nonnull
109     @Override
110     public String getExtension() {
111         return extension;
112     }
113 
114     public void setExtension(String extension) {
115         this.extension = extension;
116     }
117 
118     @Override
119     public boolean isSnapshot() {
120         return false;
121     }
122 
123     @Override
124     public ArtifactCoordinates toCoordinates() {
125         return new ArtifactCoordinates() {
126             @Override
127             public String getGroupId() {
128                 return groupId;
129             }
130 
131             @Override
132             public String getArtifactId() {
133                 return artifactId;
134             }
135 
136             @Override
137             public String getClassifier() {
138                 return classifier;
139             }
140 
141             @Override
142             public VersionConstraint getVersionConstraint() {
143                 return getParser().parseVersionConstraint(version);
144             }
145 
146             @Override
147             public String getExtension() {
148                 return extension;
149             }
150         };
151     }
152 
153     @Override
154     public String toString() {
155         return "ArtifactStub["
156                 + "groupId='" + groupId + '\''
157                 + ", artifactId='" + artifactId + '\''
158                 + ", classifier='" + classifier + '\''
159                 + ", version='" + version + '\''
160                 + ", extension='" + extension + '\''
161                 + ']';
162     }
163 
164     @Override
165     public boolean equals(Object o) {
166         if (this == o) {
167             return true;
168         }
169         if (!(o instanceof ArtifactStub)) {
170             return false;
171         }
172         ArtifactStub that = (ArtifactStub) o;
173         return Objects.equals(groupId, that.groupId)
174                 && Objects.equals(artifactId, that.artifactId)
175                 && Objects.equals(classifier, that.classifier)
176                 && Objects.equals(version, that.version)
177                 && Objects.equals(extension, that.extension);
178     }
179 
180     @Override
181     public int hashCode() {
182         return Objects.hash(groupId, artifactId, classifier, version, extension);
183     }
184 
185     private static DefaultVersionParser getParser() {
186         return new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme()));
187     }
188 }