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