View Javadoc
1   package org.apache.maven.plugin.assembly.archive.task;
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 junit.framework.TestCase;
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.model.Model;
25  import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
26  import org.apache.maven.plugin.assembly.archive.task.testutils.ArtifactMock;
27  import org.apache.maven.plugin.assembly.archive.task.testutils.MockAndControlForAddArtifactTask;
28  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
29  import org.apache.maven.plugin.assembly.model.DependencySet;
30  import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.archiver.ArchivedFileSet;
33  import org.codehaus.plexus.archiver.ArchiverException;
34  import org.codehaus.plexus.logging.Logger;
35  import org.codehaus.plexus.logging.console.ConsoleLogger;
36  import org.easymock.classextension.EasyMockSupport;
37  
38  import java.io.File;
39  import java.io.IOException;
40  import java.nio.charset.Charset;
41  import java.util.Arrays;
42  
43  import static org.easymock.EasyMock.anyObject;
44  
45  @SuppressWarnings( "OctalInteger" )
46  public class AddArtifactTaskTest
47      extends TestCase
48  {
49  
50      private EasyMockSupport mockManager;
51  
52      private MockAndControlForAddArtifactTask mac;
53  
54      public void setUp()
55          throws IOException
56      {
57          mockManager = new EasyMockSupport();
58  
59          Model model = new Model();
60          model.setGroupId( "group" );
61          model.setArtifactId( "main" );
62          model.setVersion( "1000" );
63  
64          MavenProject mainProject = new MavenProject( model );
65  
66          mac = new MockAndControlForAddArtifactTask( mockManager, mainProject );
67          mac.expectGetFinalName( "final-name" );
68      }
69  
70      public void testShouldAddArchiveFileWithoutUnpacking()
71          throws ArchiveCreationException, AssemblyFormattingException, IOException
72      {
73          String outputLocation = "artifact";
74  
75          ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
76          File artifactFile = artifactMock.setNewFile();
77  
78          mac.expectGetMode( 0222, 0222 );
79          mac.expectGetDestFile( new File( "junk" ) );
80          mac.expectAddFile( artifactFile, outputLocation );
81          mac.expectInterpolators();
82  
83          mockManager.replayAll();
84  
85          AddArtifactTask task = createTask( artifactMock.getArtifact() );
86  
87          task.execute( mac.archiver, mac.configSource );
88  
89          mockManager.verifyAll();
90      }
91  
92      public void testShouldAddArchiveFileWithDefaultOutputLocation()
93          throws ArchiveCreationException, AssemblyFormattingException, IOException
94      {
95          String artifactId = "myArtifact";
96          String version = "1";
97          String ext = "jar";
98          String outputDir = "tmp/";
99  
100         ArtifactMock mock = new ArtifactMock( mockManager, "group", artifactId, version, ext, false );
101 
102         File file = mock.setNewFile();
103         mock.setExtension( ext );
104 
105         mac.expectGetMode( 0222, 0222 );
106 
107         mac.expectGetDestFile( new File( "junk" ) );
108         mac.expectAddFile( file, outputDir + artifactId + "-" + version + "." + ext );
109         mac.expectInterpolators();
110         mockManager.replayAll();
111 
112         AddArtifactTask task =
113             new AddArtifactTask( mock.getArtifact(), new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null );
114         task.setOutputDirectory( outputDir );
115         task.setFileNameMapping( new DependencySet().getOutputFileNameMapping() );
116 
117         Model model = new Model();
118         model.setArtifactId( artifactId );
119         model.setVersion( version );
120 
121         MavenProject project = new MavenProject( model );
122         task.setProject( project );
123 
124         task.execute( mac.archiver, mac.configSource );
125 
126         mockManager.verifyAll();
127     }
128 
129     private AddArtifactTask createTask( Artifact artifact )
130     {
131         AddArtifactTask task = new AddArtifactTask( artifact, new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ), null );
132 
133         task.setFileNameMapping( "artifact" );
134 
135         return task;
136     }
137 
138     public void testShouldAddArchiveFileWithUnpack()
139         throws ArchiveCreationException, AssemblyFormattingException, IOException
140     {
141         mac.expectModeChange( -1, -1, -1, -1, 1 );
142         mac.expectInterpolators();
143 
144         ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
145         File artifactFile = artifactMock.setNewFile();
146 
147         String outputLocation = "";
148 
149         mac.expectGetDestFile( new File( "junk" ) );
150         try
151         {
152 //            mac.archiver.addArchivedFileSet( artifactFile, outputLocation, AddArtifactTask.DEFAULT_INCLUDES_ARRAY,
153 // null );
154             mac.archiver.addArchivedFileSet( (ArchivedFileSet) anyObject(), (Charset) anyObject() );
155         }
156         catch ( ArchiverException e )
157         {
158             fail( "Should never happen." );
159         }
160 
161         mockManager.replayAll();
162 
163         AddArtifactTask task = createTask( artifactMock.getArtifact() );
164 
165         task.setUnpack( true );
166 
167         task.execute( mac.archiver, mac.configSource );
168 
169         mockManager.verifyAll();
170     }
171 
172     public void testShouldAddArchiveFileWithUnpackAndModes()
173         throws ArchiveCreationException, AssemblyFormattingException, IOException
174     {
175         int directoryMode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
176         int fileMode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
177 
178         mac.expectModeChange( -1, -1, directoryMode, fileMode, 2 );
179         mac.expectInterpolators();
180 
181 //        mac.expectIsSnapshot( false );
182 
183         String outputLocation = "";
184 
185         ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
186         File artifactFile = artifactMock.setNewFile();
187 
188         mac.expectGetDestFile( new File( "junk" ) );
189         try
190         {
191             mac.archiver.addArchivedFileSet( (ArchivedFileSet) anyObject(), (Charset) anyObject()  );
192         }
193         catch ( ArchiverException e )
194         {
195             fail( "Should never happen." );
196         }
197 
198         mockManager.replayAll();
199 
200         AddArtifactTask task = createTask( artifactMock.getArtifact() );
201 
202         task.setUnpack( true );
203 
204         task.setDirectoryMode( directoryMode );
205         task.setFileMode( fileMode );
206 
207         task.execute( mac.archiver, mac.configSource );
208 
209         mockManager.verifyAll();
210     }
211 
212     public void testShouldAddArchiveFileWithUnpackIncludesAndExcludes()
213         throws ArchiveCreationException, AssemblyFormattingException, IOException
214     {
215         mac.expectModeChange( -1, -1, -1, -1, 1 );
216 
217         String[] includes = { "**/*.txt" };
218         String[] excludes = { "**/README.txt" };
219 
220         ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
221         artifactMock.setNewFile();
222 
223         mac.expectGetDestFile( new File( "junk" ) );
224         mac.expectAddArchivedFileSet();
225         mac.expectInterpolators();
226 
227         mockManager.replayAll();
228 
229         AddArtifactTask task = createTask( artifactMock.getArtifact() );
230 
231         task.setUnpack( true );
232         task.setIncludes( Arrays.asList( includes ) );
233         task.setExcludes( Arrays.asList( excludes ) );
234 
235         task.execute( mac.archiver, mac.configSource );
236 
237         mockManager.verifyAll();
238     }
239 
240 }