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.plugins.war.stub;
20  
21  import java.util.Objects;
22  
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.artifact.handler.ArtifactHandler;
25  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
26  import org.apache.maven.artifact.versioning.VersionRange;
27  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
28  
29  public abstract class AbstractArtifactStub extends ArtifactStub {
30      protected String basedir;
31  
32      public AbstractArtifactStub(String basedir) {
33          this.basedir = basedir;
34      }
35  
36      public String getVersion() {
37          return "0.0-Test";
38      }
39  
40      @Override
41      public String getBaseVersion() {
42          return getVersion();
43      }
44  
45      public String getScope() {
46          return Artifact.SCOPE_RUNTIME;
47      }
48  
49      public VersionRange getVersionRange() {
50          return VersionRange.createFromVersion(getVersion());
51      }
52  
53      public boolean isOptional() {
54          return false;
55      }
56  
57      public ArtifactHandler getArtifactHandler() {
58          return new DefaultArtifactHandler(getType());
59      }
60  
61      /*
62       * TODO: Copied from org/apache/maven/artifact/DefaultArtifact.java; Consider merging...
63       */
64      public int compareTo(Artifact a) {
65          /* -- We need to support groupId=null (it is missing in DefaultArtifact.java) */
66          int result;
67          if (a.getGroupId() != null) {
68              result = getGroupId().compareTo(a.getGroupId());
69          } else {
70              result = (getGroupId() == null ? 0 : -1);
71          }
72          /* -- */
73  
74          if (result == 0) {
75              result = getArtifactId().compareTo(a.getArtifactId());
76              if (result == 0) {
77                  result = getType().compareTo(a.getType());
78                  if (result == 0) {
79                      if (getClassifier() == null) {
80                          if (a.getClassifier() != null) {
81                              result = 1;
82                          }
83                      } else {
84                          if (a.getClassifier() != null) {
85                              result = getClassifier().compareTo(a.getClassifier());
86                          } else {
87                              result = -1;
88                          }
89                      }
90                      if (result == 0) {
91                          // We don't consider the version range in the comparison, just the resolved version
92                          result = getVersion().compareTo(a.getVersion());
93                      }
94                  }
95              }
96          }
97          return result;
98      }
99  
100     /*
101      * TODO: Copied from org/apache/maven/artifact/DefaultArtifact.java; Consider merging...
102      */
103     public boolean equals(Object o) {
104         if (o == this) {
105             return true;
106         }
107 
108         if (!(o instanceof Artifact)) {
109             return false;
110         }
111 
112         Artifact a = (Artifact) o;
113 
114         /* -- We need to support groupId=null (it is missing in DefaultArtifact.java) */
115         if (a.getGroupId() == null ? (getGroupId() != null) : a.getGroupId().equals(getGroupId())) {
116             return false;
117         } else if (!a.getArtifactId().equals(getArtifactId())) {
118             return false;
119         } else if (!a.getVersion().equals(getVersion())) {
120             return false;
121         } else if (!a.getType().equals(getType())) {
122             return false;
123         } else if (a.getClassifier() == null
124                 ? getClassifier() != null
125                 : !a.getClassifier().equals(getClassifier())) {
126             return false;
127         }
128 
129         // We don't consider the version range in the comparison, just the resolved version
130 
131         return true;
132     }
133 
134     @Override
135     public int hashCode() {
136         return Objects.hashCode(basedir);
137     }
138 }