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.codehaus.plexus.util.FileUtils;
26  import org.codehaus.plexus.util.IOUtil;
27  import org.codehaus.plexus.util.ReaderFactory;
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(), LOCAL_REPO + legacyGroupId + "/" + "jars" + "/" + artifactId + "-"
107             + version + "." + packaging );
108 
109         assertTrue( installedArtifact.exists() );
110     }
111     
112     public void testInstallFileWithClassifier()
113         throws Exception
114     {
115         File testPom =
116             new File( getBasedir(), "target/test-classes/unit/install-file-with-classifier/plugin-config.xml" );
117 
118         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
119 
120         assertNotNull( mojo );
121 
122         assignValuesForParameter( mojo );
123 
124         assertNotNull( classifier );
125 
126         mojo.execute();
127 
128         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
129             artifactId + "-" + version + "-" + classifier + "." + packaging );
130 
131         assertTrue( installedArtifact.exists() );
132     }
133 
134     public void testInstallFileWithGeneratePom()
135         throws Exception
136     {
137         File testPom =
138             new File( getBasedir(), "target/test-classes/unit/install-file-test-generatePom/plugin-config.xml" );
139 
140         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
141 
142         assertNotNull( mojo );
143 
144         assignValuesForParameter( mojo );
145 
146         mojo.execute();
147 
148         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
149             artifactId + "-" + version + "." + packaging );
150 
151         assertTrue( ( (Boolean) getVariableValueFromObject( mojo, "generatePom" ) ).booleanValue() );
152 
153         assertTrue( installedArtifact.exists() );
154 
155         File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
156             artifactId + "-" + version + "." + "pom" );
157 
158         Model model;
159 
160         Reader reader = null;
161         try
162         {
163             reader = ReaderFactory.newXmlReader( installedPom );
164             model = new MavenXpp3Reader().read( reader );
165         }
166         finally
167         {
168             IOUtil.close( reader );
169         }
170 
171         assertEquals( "4.0.0", model.getModelVersion() );
172 
173         assertEquals( (String) getVariableValueFromObject( mojo, "groupId" ), model.getGroupId() );
174 
175         assertEquals( artifactId, model.getArtifactId() );
176 
177         assertEquals( version, model.getVersion() );
178     }
179 
180     public void testInstallFileWithPomFile()
181         throws Exception
182     {
183         File testPom =
184             new File( getBasedir(), "target/test-classes/unit/install-file-with-pomFile-test/plugin-config.xml" );
185 
186         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
187 
188         assertNotNull( mojo );
189 
190         assignValuesForParameter( mojo );
191 
192         mojo.execute();
193 
194         File pomFile = (File) getVariableValueFromObject( mojo, "pomFile" );
195 
196         assertTrue( pomFile.exists() );
197 
198         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
199             artifactId + "-" + version + "." + packaging );
200 
201         assertTrue( installedArtifact.exists() );
202 
203         File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
204             artifactId + "-" + version + "." + "pom" );
205 
206         assertTrue( installedPom.exists() );
207     }
208 
209     public void testInstallFileWithPomAsPackaging()
210         throws Exception
211     {
212         File testPom = new File( getBasedir(),
213                                  "target/test-classes/unit/install-file-with-pom-as-packaging/" + "plugin-config.xml" );
214 
215         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
216 
217         assertNotNull( mojo );
218 
219         assignValuesForParameter( mojo );
220 
221         assertTrue( file.exists() );
222 
223         assertEquals( "pom", packaging );
224 
225         mojo.execute();
226 
227         File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
228             artifactId + "-" + version + "." + "pom" );
229 
230         assertTrue( installedPom.exists() );
231     }
232 
233     public void testInstallFileWithChecksum()
234         throws Exception
235     {
236         File testPom =
237             new File( getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml" );
238 
239         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
240 
241         assertNotNull( mojo );
242 
243         assignValuesForParameter( mojo );
244 
245         boolean createChecksum = ( (Boolean) getVariableValueFromObject( mojo, "createChecksum" ) ).booleanValue();
246 
247         assertTrue( createChecksum );
248 
249         mojo.execute();
250 
251         //get the actual checksum of the artifact
252         String actualMd5Sum = mojo.md5Digester.calc( file );
253         String actualSha1Sum = mojo.sha1Digester.calc( file );
254 
255         String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
256             artifactId + "-" + version;
257 
258         File installedArtifact = new File( localPath + "." + "jar" );
259 
260         File md5 = new File( localPath + ".jar.md5" );
261         File sha1 = new File( localPath + ".jar.sha1" );
262 
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 
269         assertEquals( actualMd5Sum, generatedMd5 );
270         assertEquals( actualSha1Sum, generatedSha1 );
271 
272         assertTrue( installedArtifact.exists() );
273     }
274 
275     private void assignValuesForParameter( Object obj )
276         throws Exception
277     {
278         this.groupId = dotToSlashReplacer( (String) getVariableValueFromObject( obj, "groupId" ) );
279 
280         this.legacyGroupId = (String) getVariableValueFromObject( obj, "groupId" );
281         
282         this.artifactId = (String) getVariableValueFromObject( obj, "artifactId" );
283 
284         this.version = (String) getVariableValueFromObject( obj, "version" );
285 
286         this.packaging = (String) getVariableValueFromObject( obj, "packaging" );
287 
288         this.classifier = (String) getVariableValueFromObject( obj, "classifier" );
289 
290         this.file = (File) getVariableValueFromObject( obj, "file" );
291     }
292 
293     private String dotToSlashReplacer( String parameter )
294     {
295         return parameter.replace( '.', '/' );
296     }
297 }