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.model.Model;
23  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
24  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
25  import org.apache.maven.shared.utils.ReaderFactory;
26  import org.apache.maven.shared.utils.io.FileUtils;
27  import org.apache.maven.shared.utils.io.IOUtil;
28  
29  import java.io.File;
30  import java.io.Reader;
31  
32  /**
33   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
34   */
35  public class InstallFileMojoTest
36      extends AbstractMojoTestCase
37  {
38      private String groupId;
39  
40      private String legacyGroupId;
41  
42      private String artifactId;
43  
44      private String version;
45  
46      private String packaging;
47  
48      private String classifier;
49  
50      private File file;
51  
52      private final String LOCAL_REPO = "target/local-repo/";
53  
54      public void setUp()
55          throws Exception
56      {
57          super.setUp();
58  
59          FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) );
60      }
61  
62      public void testInstallFileTestEnvironment()
63          throws Exception
64      {
65          File testPom = new File( getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml" );
66  
67          InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
68  
69          assertNotNull( mojo );
70      }
71  
72      public void testBasicInstallFile()
73          throws Exception
74      {
75          File testPom = new File( getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml" );
76  
77          InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
78  
79          assertNotNull( mojo );
80  
81          assignValuesForParameter( mojo );
82  
83          mojo.execute();
84  
85          File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
86              artifactId + "-" + version + "." + packaging );
87  
88          assertTrue( installedArtifact.exists() );
89      }
90  
91      public void testLayoutInstallFile()
92          throws Exception
93      {
94          File testPom = new File( getBasedir(), "target/test-classes/unit/install-file-layout-test/plugin-config.xml" );
95  
96          InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
97  
98          assertNotNull( mojo );
99  
100         assignValuesForParameter( mojo );
101 
102         mojo.setLocalRepositoryPath( new File( getBasedir(), LOCAL_REPO ) );
103 
104         mojo.execute();
105 
106         File installedArtifact = new File( getBasedir(),
107                                            LOCAL_REPO + legacyGroupId + "/" + "jars" + "/" + artifactId + "-" + version
108                                                + "." + packaging );
109 
110         assertTrue( installedArtifact.exists() );
111     }
112 
113     public void testInstallFileWithClassifier()
114         throws Exception
115     {
116         File testPom =
117             new File( getBasedir(), "target/test-classes/unit/install-file-with-classifier/plugin-config.xml" );
118 
119         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
120 
121         assertNotNull( mojo );
122 
123         assignValuesForParameter( mojo );
124 
125         assertNotNull( classifier );
126 
127         mojo.execute();
128 
129         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
130             artifactId + "-" + version + "-" + classifier + "." + packaging );
131 
132         assertTrue( installedArtifact.exists() );
133     }
134 
135     public void testInstallFileWithGeneratePom()
136         throws Exception
137     {
138         File testPom =
139             new File( getBasedir(), "target/test-classes/unit/install-file-test-generatePom/plugin-config.xml" );
140 
141         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
142 
143         assertNotNull( mojo );
144 
145         assignValuesForParameter( mojo );
146 
147         mojo.execute();
148 
149         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
150             artifactId + "-" + version + "." + packaging );
151 
152         assertTrue( (Boolean) getVariableValueFromObject( mojo, "generatePom" ) );
153 
154         assertTrue( installedArtifact.exists() );
155 
156         File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
157             artifactId + "-" + version + "." + "pom" );
158 
159         Model model;
160 
161         Reader reader = null;
162         try
163         {
164             reader = ReaderFactory.newXmlReader( installedPom );
165             model = new MavenXpp3Reader().read( reader );
166         }
167         finally
168         {
169             IOUtil.close( reader );
170         }
171 
172         assertEquals( "4.0.0", model.getModelVersion() );
173 
174         assertEquals( (String) getVariableValueFromObject( mojo, "groupId" ), model.getGroupId() );
175 
176         assertEquals( artifactId, model.getArtifactId() );
177 
178         assertEquals( version, model.getVersion() );
179     }
180 
181     public void testInstallFileWithPomFile()
182         throws Exception
183     {
184         File testPom =
185             new File( getBasedir(), "target/test-classes/unit/install-file-with-pomFile-test/plugin-config.xml" );
186 
187         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
188 
189         assertNotNull( mojo );
190 
191         assignValuesForParameter( mojo );
192 
193         mojo.execute();
194 
195         File pomFile = (File) getVariableValueFromObject( mojo, "pomFile" );
196 
197         assertTrue( pomFile.exists() );
198 
199         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
200             artifactId + "-" + version + "." + packaging );
201 
202         assertTrue( installedArtifact.exists() );
203 
204         File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
205             artifactId + "-" + version + "." + "pom" );
206 
207         assertTrue( installedPom.exists() );
208     }
209 
210     public void testInstallFileWithPomAsPackaging()
211         throws Exception
212     {
213         File testPom = new File( getBasedir(),
214                                  "target/test-classes/unit/install-file-with-pom-as-packaging/" + "plugin-config.xml" );
215 
216         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
217 
218         assertNotNull( mojo );
219 
220         assignValuesForParameter( mojo );
221 
222         assertTrue( file.exists() );
223 
224         assertEquals( "pom", packaging );
225 
226         mojo.execute();
227 
228         File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
229             artifactId + "-" + version + "." + "pom" );
230 
231         assertTrue( installedPom.exists() );
232     }
233 
234     public void testInstallFileWithChecksum()
235         throws Exception
236     {
237         File testPom =
238             new File( getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml" );
239 
240         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
241 
242         assertNotNull( mojo );
243 
244         assignValuesForParameter( mojo );
245 
246         boolean createChecksum = (Boolean) getVariableValueFromObject( mojo, "createChecksum" );
247 
248         assertTrue( createChecksum );
249 
250         mojo.execute();
251 
252         //get the actual checksum of the artifact
253         mojo.digester.calculate( file );
254         String actualMd5Sum = mojo.digester.getMd5();
255         String actualSha1Sum = mojo.digester.getSha1();
256 
257         String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
258             artifactId + "-" + version;
259 
260         File installedArtifact = new File( localPath + "." + "jar" );
261 
262         File md5 = new File( localPath + ".jar.md5" );
263         File sha1 = new File( localPath + ".jar.sha1" );
264 
265         assertTrue( md5.exists() );
266         assertTrue( sha1.exists() );
267 
268         String generatedMd5 = FileUtils.fileRead( md5, "UTF-8" );
269         String generatedSha1 = FileUtils.fileRead( sha1, "UTF-8" );
270 
271         assertEquals( actualMd5Sum, generatedMd5 );
272         assertEquals( actualSha1Sum, generatedSha1 );
273 
274         assertTrue( installedArtifact.exists() );
275     }
276 
277     private void assignValuesForParameter( Object obj )
278         throws Exception
279     {
280         this.groupId = dotToSlashReplacer( (String) getVariableValueFromObject( obj, "groupId" ) );
281 
282         this.legacyGroupId = (String) getVariableValueFromObject( obj, "groupId" );
283 
284         this.artifactId = (String) getVariableValueFromObject( obj, "artifactId" );
285 
286         this.version = (String) getVariableValueFromObject( obj, "version" );
287 
288         this.packaging = (String) getVariableValueFromObject( obj, "packaging" );
289 
290         this.classifier = (String) getVariableValueFromObject( obj, "classifier" );
291 
292         this.file = (File) getVariableValueFromObject( obj, "file" );
293     }
294 
295     private String dotToSlashReplacer( String parameter )
296     {
297         return parameter.replace( '.', '/' );
298     }
299 }