View Javadoc

1   package org.apache.maven.plugin.install;
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.metadata.ArtifactMetadata;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.install.stubs.AttachedArtifactStub0;
26  import org.apache.maven.plugin.install.stubs.InstallArtifactStub;
27  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  import java.io.File;
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
37   */
38  
39  public class InstallMojoTest
40      extends AbstractMojoTestCase
41  {
42  
43      InstallArtifactStub artifact;
44  
45      private final String LOCAL_REPO = "target/local-repo/";
46  
47      public void setUp()
48          throws Exception
49      {
50          super.setUp();
51  
52          System.out.println( ">>>Cleaning local repo " + getBasedir() + "/" + LOCAL_REPO + "..." );
53  
54          FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) );
55      }
56  
57      public void testInstallTestEnvironment()
58          throws Exception
59      {
60          File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
61  
62          InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
63  
64          assertNotNull( mojo );
65      }
66  
67      public void testBasicInstall()
68          throws Exception
69      {
70          File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
71  
72          InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
73  
74          assertNotNull( mojo );
75  
76          File file = new File( getBasedir(), "target/test-classes/unit/basic-install-test/target/" +
77              "maven-install-test-1.0-SNAPSHOT.jar" );
78  
79          artifact = (InstallArtifactStub) getVariableValueFromObject( mojo, "artifact" );
80  
81          artifact.setFile( file );
82  
83          mojo.execute();
84  
85          String groupId = dotToSlashReplacer( artifact.getGroupId() );
86  
87          String packaging = getVariableValueFromObject( mojo, "packaging" ).toString();
88  
89          File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
90              artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + packaging );
91  
92          assertTrue( installedArtifact.exists() );
93      }
94  
95      public void testBasicInstallWithAttachedArtifacts()
96          throws Exception
97      {
98          File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test-with-attached-artifacts/" +
99              "plugin-config.xml" );
100 
101         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
102 
103         assertNotNull( mojo );
104 
105         List attachedArtifacts = (ArrayList) getVariableValueFromObject( mojo, "attachedArtifacts" );
106 
107         mojo.execute();
108 
109         String packaging = getVariableValueFromObject( mojo, "packaging" ).toString();
110 
111         String groupId = "";
112 
113         for ( Iterator iter = attachedArtifacts.iterator(); iter.hasNext(); )
114         {
115             AttachedArtifactStub0 attachedArtifact = (AttachedArtifactStub0) iter.next();
116 
117             groupId = dotToSlashReplacer( attachedArtifact.getGroupId() );
118 
119             File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" +
120                 attachedArtifact.getArtifactId() + "/" + attachedArtifact.getVersion() + "/" +
121                 attachedArtifact.getArtifactId() + "-" + attachedArtifact.getVersion() + "." + packaging );
122 
123             assertTrue( installedArtifact.exists() );
124         }
125     }
126 
127     public void testUpdateReleaseParamSetToTrue()
128         throws Exception
129     {
130         File testPom = new File( getBasedir(), "target/test-classes/unit/configured-install-test/plugin-config.xml" );
131 
132         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
133 
134         assertNotNull( mojo );
135 
136         File file = new File( getBasedir(), "target/test-classes/unit/configured-install-test/target/" +
137             "maven-install-test-1.0-SNAPSHOT.jar" );
138 
139         artifact = (InstallArtifactStub) getVariableValueFromObject( mojo, "artifact" );
140 
141         artifact.setFile( file );
142 
143         mojo.execute();
144 
145         assertTrue( artifact.isRelease() );
146     }
147 
148     public void testInstallIfArtifactFileIsNull()
149         throws Exception
150     {
151         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
152 
153         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
154 
155         assertNotNull( mojo );
156 
157         artifact = (InstallArtifactStub) getVariableValueFromObject( mojo, "artifact" );
158 
159         artifact.setFile( null );
160 
161         assertNull( artifact.getFile() );
162 
163         try
164         {
165             mojo.execute();
166 
167             fail( "Did not throw mojo execution exception" );
168         }
169         catch ( MojoExecutionException e )
170         {
171             //expected
172         }
173     }
174 
175     public void testInstallIfPackagingIsPom()
176         throws Exception
177     {
178         File testPom = new File( getBasedir(),
179                                  "target/test-classes/unit/basic-install-test-packaging-pom/" + "plugin-config.xml" );
180 
181         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
182 
183         assertNotNull( mojo );
184 
185         String packaging = (String) getVariableValueFromObject( mojo, "packaging" );
186 
187         assertEquals( "pom", packaging );
188 
189         artifact = (InstallArtifactStub) getVariableValueFromObject( mojo, "artifact" );
190 
191         mojo.execute();
192 
193         String groupId = dotToSlashReplacer( artifact.getGroupId() );
194 
195         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
196             artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + "jar" );
197 
198         assertTrue( installedArtifact.exists() );
199     }
200 
201     public void testBasicInstallAndCreateChecksumIsTrue()
202         throws Exception
203     {
204         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-checksum/plugin-config.xml" );
205 
206         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
207 
208         assertNotNull( mojo );
209 
210         File file = new File( getBasedir(), "target/test-classes/unit/basic-install-checksum/" + "maven-test-jar.jar" );
211 
212         artifact = (InstallArtifactStub) getVariableValueFromObject( mojo, "artifact" );
213 
214         boolean createChecksum = ( (Boolean) getVariableValueFromObject( mojo, "createChecksum" ) ).booleanValue();
215 
216         assertTrue( createChecksum );
217 
218         artifact.setFile( file );
219 
220         mojo.execute();
221 
222         ArtifactMetadata metadata = null;
223         for ( Iterator iter = artifact.getMetadataList().iterator(); iter.hasNext(); )
224         {
225             metadata = (ArtifactMetadata) iter.next();
226             if ( metadata.getRemoteFilename().endsWith( "pom" ) )
227             {
228                 break;
229             }
230         }
231 
232         ArtifactRepository localRepo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
233 
234         File pom = new File( localRepo.getBasedir(), localRepo.pathOfLocalRepositoryMetadata( metadata, localRepo ) );
235 
236         assertTrue( pom.exists() );
237 
238         //get the actual checksum of the pom
239         String actualPomMd5Sum = mojo.md5Digester.calc( pom );
240         String actualPomSha1Sum = mojo.sha1Digester.calc( pom );
241 
242         //get the actual checksum of the artifact
243         String actualMd5Sum = mojo.md5Digester.calc( file );
244         String actualSha1Sum = mojo.sha1Digester.calc( file );
245 
246         String groupId = dotToSlashReplacer( artifact.getGroupId() );
247 
248         String packaging = getVariableValueFromObject( mojo, "packaging" ).toString();
249 
250         String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
251             artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion();
252 
253         File installedArtifact = new File( localPath + "." + packaging );
254 
255         File pomMd5 = new File( localPath + ".pom.md5" );
256         File pomSha1 = new File( localPath + ".pom.sha1" );
257 
258         File md5 = new File( localPath + "." + packaging + ".md5" );
259         File sha1 = new File( localPath + "." + packaging + ".sha1" );
260 
261         assertTrue( pomMd5.exists() );
262         assertTrue( pomSha1.exists() );
263         assertTrue( md5.exists() );
264         assertTrue( sha1.exists() );
265 
266         String generatedMd5 = FileUtils.fileRead( md5, "UTF-8" );
267         String generatedSha1 = FileUtils.fileRead( sha1, "UTF-8" );
268         String generatedPomMd5 = FileUtils.fileRead( pomMd5, "UTF-8" );
269         String generatedPomSha1 = FileUtils.fileRead( pomSha1, "UTF-8" );
270 
271         assertEquals( actualMd5Sum, generatedMd5 );
272         assertEquals( actualSha1Sum, generatedSha1 );
273         assertEquals( actualPomMd5Sum, generatedPomMd5 );
274         assertEquals( actualPomSha1Sum, generatedPomSha1 );
275 
276         assertTrue( installedArtifact.exists() );
277     }
278     
279     public void testSkip()
280     throws Exception
281     {	
282         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
283 
284         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
285 
286         assertNotNull( mojo );
287 
288         File file = new File( getBasedir(), "target/test-classes/unit/basic-install-test/target/" +
289             "maven-install-test-1.0-SNAPSHOT.jar" );
290 
291         artifact = (InstallArtifactStub) getVariableValueFromObject( mojo, "artifact" );
292 
293         artifact.setFile( file );
294 
295         mojo.setSkip(true);
296             
297         mojo.execute();
298 
299         String groupId = dotToSlashReplacer( artifact.getGroupId() );
300 
301         String packaging = getVariableValueFromObject( mojo, "packaging" ).toString();
302 
303         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
304            artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + packaging );
305 
306         assertFalse( installedArtifact.exists() );
307     }
308     
309 
310     private String dotToSlashReplacer( String parameter )
311     {
312         return parameter.replace( '.', '/' );
313     }
314 }