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