View Javadoc
1   package org.apache.maven.plugin.assembly.archive.task.testutils;
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 java.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.handler.ArtifactHandler;
28  import org.easymock.classextension.EasyMockSupport;
29  
30  import static org.easymock.EasyMock.expect;
31  
32  public class ArtifactMock
33  {
34  
35  
36      private final Artifact artifact;
37  
38  
39      private final ArtifactHandler handler;
40  
41      private String groupId;
42  
43      private String artifactId;
44  
45      private String baseVersion;
46  
47      private File file;
48  
49      private Boolean isSnapshot;
50  
51      private String version;
52  
53      private List<String> dependencyTrail;
54  
55      private String id;
56  
57      private String dependencyConflictId;
58  
59      private String typeAndExt;
60  
61      private final String classifier;
62  
63      public ArtifactMock( final EasyMockSupport mockManager, final String groupId, final String artifactId,
64                           final String version, final String type, final boolean isSnapshot )
65      {
66          this( mockManager, groupId, artifactId, version, type, null, isSnapshot, null, null );
67      }
68  
69      public ArtifactMock( final EasyMockSupport mockManager, final String groupId, final String artifactId,
70                           final String version, final String type, final boolean isSnapshot, final String baseVersion )
71      {
72          this( mockManager, groupId, artifactId, version, type, null, isSnapshot, baseVersion, null );
73      }
74  
75      public ArtifactMock( final EasyMockSupport mockManager, final String groupId, final String artifactId,
76                           final String version, final String type, final String classifier, final boolean isSnapshot )
77      {
78          this( mockManager, groupId, artifactId, version, type, classifier, isSnapshot, null, null );
79      }
80  
81      public ArtifactMock( final EasyMockSupport mockManager, final String groupId, final String artifactId,
82                           final String version, final String type, final String classifier, final boolean isSnapshot,
83                           final String baseVersion )
84      {
85          this( mockManager, groupId, artifactId, version, type, classifier, isSnapshot, baseVersion, null );
86      }
87  
88      private ArtifactMock( final EasyMockSupport mockManager, final String groupId, final String artifactId,
89                            final String version, final String type, final String classifier, final boolean isSnapshot,
90                            final String baseVersion, String scope )
91      {
92  
93          artifact = mockManager.createMock( Artifact.class );
94  
95          if ( scope == null )
96          {
97              scope = Artifact.SCOPE_COMPILE;
98          }
99  
100         expect( artifact.getScope()).andReturn( scope ).anyTimes();
101 
102         handler = mockManager.createMock( ArtifactHandler.class );
103 
104         expect(artifact.getArtifactHandler()).andReturn( handler ).anyTimes();
105 
106         this.classifier = classifier;
107         expect(artifact.getClassifier()).andReturn( classifier ).anyTimes();
108 
109         setSnapshot( isSnapshot );
110         setGroupId( groupId );
111         setArtifactId( artifactId );
112         setVersion( version );
113         setBaseVersion( baseVersion );
114         setType( type );
115 
116         setId();
117         setDependencyConflictId();
118     }
119 
120     public void setExtension( final String extension )
121     {
122         setTypeAndExt( extension );
123     }
124 
125     public Artifact getArtifact()
126     {
127         return artifact;
128     }
129 
130     void setArtifactId( final String artifactId )
131     {
132         if ( ( artifactId != null ) && ( this.artifactId == null ) )
133         {
134             expect(artifact.getArtifactId()).andReturn( artifactId ).anyTimes();
135             this.artifactId = artifactId;
136         }
137     }
138 
139     public void setBaseVersion( final String baseVersion )
140     {
141         if ( ( baseVersion != null ) && ( this.baseVersion == null ) )
142         {
143             expect(artifact.getBaseVersion()).andReturn( baseVersion ).anyTimes();
144 
145             this.baseVersion = baseVersion;
146         }
147     }
148 
149     public void setFile( final File destination )
150     {
151         if ( ( destination != null ) && ( file == null ) )
152         {
153             expect(artifact.getFile()).andReturn( destination ).anyTimes();
154             file = destination;
155         }
156     }
157 
158     void setGroupId( final String groupId )
159     {
160         if ( ( groupId != null ) && ( this.groupId == null ) )
161         {
162             expect(artifact.getGroupId()).andReturn( groupId ).anyTimes();
163 
164             this.groupId = groupId;
165         }
166     }
167 
168     void setVersion( final String version )
169     {
170         if ( ( version != null ) && ( this.version == null ) )
171         {
172             expect(artifact.getVersion()).andReturn( version ).anyTimes();
173 
174             this.version = version;
175 
176             if ( isSnapshot != Boolean.TRUE )
177             {
178                 setBaseVersion( version );
179                 setSnapshot( false );
180             }
181         }
182     }
183 
184     public void setDependencyTrail( final List<String> dependencyTrail )
185     {
186         if ( ( dependencyTrail != null ) && ( this.dependencyTrail == null ) )
187         {
188             expect(artifact.getDependencyTrail()).andReturn( dependencyTrail ).anyTimes();
189             this.dependencyTrail = dependencyTrail;
190         }
191     }
192 
193     void setId( final String id )
194     {
195         if ( ( id != null ) && ( this.id == null ) )
196         {
197             expect(artifact.getId()).andReturn( id ).anyTimes();
198             this.id = id;
199         }
200     }
201 
202     void setDependencyConflictId( final String id )
203     {
204         if ( ( id != null ) && ( dependencyConflictId == null ) )
205         {
206            expect( artifact.getDependencyConflictId()).andReturn( id ).anyTimes();
207 
208             dependencyConflictId = id;
209         }
210     }
211 
212     void setSnapshot( final boolean snapshot )
213     {
214         if ( isSnapshot == null )
215         {
216            expect(artifact.isSnapshot()).andReturn( snapshot ).anyTimes();
217             isSnapshot = snapshot;
218         }
219     }
220 
221     public File setNewFile()
222         throws IOException
223     {
224         if ( file == null )
225         {
226             final File newFile = File.createTempFile( "ArtifactMock.test.", "" );
227             newFile.deleteOnExit();
228             expect( artifact.getFile()).andReturn( newFile ).anyTimes();
229             file = newFile;
230         }
231 
232         return file;
233     }
234 
235     void setType( final String type )
236     {
237         setTypeAndExt( type );
238     }
239 
240     private void setTypeAndExt( final String type )
241     {
242         if ( ( type != null ) && ( typeAndExt == null ) )
243         {
244             expect(artifact.getType()).andReturn( type ).anyTimes();
245             expect(handler.getExtension()).andReturn( type ).anyTimes();
246             typeAndExt = type;
247         }
248     }
249 
250     private void setDependencyConflictId()
251     {
252         if ( ( groupId != null ) && ( artifactId != null ) && ( typeAndExt != null ) )
253         {
254             final String id =
255                 groupId + ":" + artifactId + ":" + typeAndExt + ( classifier == null ? "" : ":" + classifier );
256             setDependencyConflictId( id );
257         }
258     }
259 
260     private void setId()
261     {
262         if ( ( groupId != null ) && ( artifactId != null ) && ( typeAndExt != null ) && ( version != null ) )
263         {
264             final String id =
265                 groupId + ":" + artifactId + ":" + version + ":" + typeAndExt
266                                 + ( classifier == null ? "" : ":" + classifier );
267             setId( id );
268         }
269     }
270 
271     public void setNullFile()
272     {
273         expect(artifact.getFile()).andReturn( null ).anyTimes();
274 
275         file = new File( "set-to-null" );
276     }
277 
278 }