View Javadoc
1   package org.apache.maven.plugins.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 static org.easymock.EasyMock.anyObject;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.nio.charset.Charset;
27  import java.util.Arrays;
28  
29  import junit.framework.TestCase;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
34  import org.apache.maven.plugins.assembly.archive.task.testutils.ArtifactMock;
35  import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddArtifactTask;
36  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
37  import org.apache.maven.plugins.assembly.model.DependencySet;
38  import org.apache.maven.plugins.assembly.utils.TypeConversionUtils;
39  import org.apache.maven.project.MavenProject;
40  import org.codehaus.plexus.archiver.ArchivedFileSet;
41  import org.codehaus.plexus.archiver.ArchiverException;
42  import org.codehaus.plexus.logging.Logger;
43  import org.codehaus.plexus.logging.console.ConsoleLogger;
44  import org.easymock.classextension.EasyMockSupport;
45  
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         artifactMock.setNewFile();
146 
147         mac.expectGetDestFile( new File( "junk" ) );
148         try
149         {
150 //            mac.archiver.addArchivedFileSet( artifactFile, outputLocation, AddArtifactTask.DEFAULT_INCLUDES_ARRAY,
151 // null );
152             mac.archiver.addArchivedFileSet( (ArchivedFileSet) anyObject(), (Charset) anyObject() );
153         }
154         catch ( ArchiverException e )
155         {
156             fail( "Should never happen." );
157         }
158 
159         mockManager.replayAll();
160 
161         AddArtifactTask task = createTask( artifactMock.getArtifact() );
162 
163         task.setUnpack( true );
164 
165         task.execute( mac.archiver, mac.configSource );
166 
167         mockManager.verifyAll();
168     }
169 
170     public void testShouldAddArchiveFileWithUnpackAndModes()
171         throws ArchiveCreationException, AssemblyFormattingException, IOException
172     {
173         int directoryMode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
174         int fileMode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
175 
176         mac.expectModeChange( -1, -1, directoryMode, fileMode, 2 );
177         mac.expectInterpolators();
178 
179         ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
180         artifactMock.setNewFile();
181 
182         mac.expectGetDestFile( new File( "junk" ) );
183         try
184         {
185             mac.archiver.addArchivedFileSet( (ArchivedFileSet) anyObject(), (Charset) anyObject() );
186         }
187         catch ( ArchiverException e )
188         {
189             fail( "Should never happen." );
190         }
191 
192         mockManager.replayAll();
193 
194         AddArtifactTask task = createTask( artifactMock.getArtifact() );
195 
196         task.setUnpack( true );
197 
198         task.setDirectoryMode( directoryMode );
199         task.setFileMode( fileMode );
200 
201         task.execute( mac.archiver, mac.configSource );
202 
203         mockManager.verifyAll();
204     }
205 
206     public void testShouldAddArchiveFileWithUnpackIncludesAndExcludes()
207         throws ArchiveCreationException, AssemblyFormattingException, IOException
208     {
209         mac.expectModeChange( -1, -1, -1, -1, 1 );
210 
211         String[] includes = { "**/*.txt" };
212         String[] excludes = { "**/README.txt" };
213 
214         ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
215         artifactMock.setNewFile();
216 
217         mac.expectGetDestFile( new File( "junk" ) );
218         mac.expectAddArchivedFileSet();
219         mac.expectInterpolators();
220 
221         mockManager.replayAll();
222 
223         AddArtifactTask task = createTask( artifactMock.getArtifact() );
224 
225         task.setUnpack( true );
226         task.setIncludes( Arrays.asList( includes ) );
227         task.setExcludes( Arrays.asList( excludes ) );
228 
229         task.execute( mac.archiver, mac.configSource );
230 
231         mockManager.verifyAll();
232     }
233 
234 }