View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.repository;
20  
21  import static org.apache.maven.plugins.repository.testutil.Assertions.assertZipContents;
22  
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
25  import org.apache.maven.plugins.repository.testutil.Assertions;
26  import org.apache.maven.plugins.repository.testutil.TestInputHandler;
27  import org.codehaus.plexus.components.interactivity.InputHandler;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.util.Collections;
33  import java.util.HashSet;
34  import java.util.Set;
35  import java.util.Stack;
36  
37  /**
38   * @author <a href="mailto:oching@exist.com">Maria Odea Ching</a>
39   */
40  public class BundleCreateMojoTest
41      extends AbstractMojoTestCase
42  {
43      protected void setUp()
44          throws Exception
45      {
46          // required for mojo lookups to work
47          super.setUp();
48  
49      }
50  
51      /**
52       * Test for repository plugin with project.packaging == pom
53       * 
54       * @throws Exception
55       */
56      public void testDefaults_PomPackaging()
57          throws Exception
58      {
59          File testPom = new File( getBasedir(), "src/test/resources/unit/pom-only/pom.xml" );
60  
61          try
62          {
63              BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
64              mojo.execute();
65          }
66          catch ( Exception e )
67          {
68              e.printStackTrace();
69          }
70  
71          File bundleSource =
72              new File( getBasedir(), "target/test/unit/pom-only/target/pom-only-bundle.jar" );
73          assertTrue( FileUtils.fileExists( bundleSource.getAbsolutePath() ) );
74  
75          Set<String> entryNames = new HashSet<String>();
76          entryNames.add( "pom.xml" );
77          entryNames.add( "META-INF/MANIFEST.MF" );
78          entryNames.add( "META-INF/" );
79  
80          assertZipContents( entryNames, Assertions.EMPTY_ENTRY_NAMES, bundleSource );
81      }
82  
83      /**
84       * Test for repository plugin default configuration
85       * 
86       * @throws Exception
87       */
88      public void testDefaultconfiguration()
89          throws Exception
90      {
91  
92          try
93          {
94              createTestJars( "default-configuration", true, true, getBasedir()
95                  + "/target/test/unit/default-configuration/target" );
96          }
97          catch ( IOException ie )
98          {
99              ie.printStackTrace();
100         }
101 
102         File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pom.xml" );
103 
104         try
105         {
106             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
107             mojo.execute();
108         }
109         catch ( Exception e )
110         {
111             e.printStackTrace();
112         }
113 
114         File bundleSource =
115             new File( getBasedir(), "target/test/unit/default-configuration/target/default-configuration-bundle.jar" );
116         assertTrue( FileUtils.fileExists( bundleSource.getAbsolutePath() ) );
117 
118         Set<String> entryNames = new HashSet<String>();
119         entryNames.add( "default-configuration-javadoc.jar" );
120         entryNames.add( "default-configuration-sources.jar" );
121         entryNames.add( "default-configuration.jar" );
122         entryNames.add( "pom.xml" );
123         entryNames.add( "META-INF/MANIFEST.MF" );
124         entryNames.add( "META-INF/" );
125 
126         assertZipContents( entryNames, Assertions.EMPTY_ENTRY_NAMES, bundleSource );
127     }
128 
129     public void testDefaultconfiguration_RemoveOne()
130         throws Exception
131     {
132         try
133         {
134             createTestJars( "default-configuration", true, true, getBasedir()
135                 + "/target/test/unit/default-configuration/target" );
136         }
137         catch ( IOException ie )
138         {
139             ie.printStackTrace();
140         }
141 
142         File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pom.xml" );
143 
144         try
145         {
146             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
147 
148             // NOTE: This is sensitive to the lookupMojo method timing...
149             TestInputHandler ih = (TestInputHandler) lookup( InputHandler.ROLE, "default" );
150 
151             Stack<String> responses = new Stack<String>();
152             responses.push( "2" );
153             ih.setLineResponses( responses );
154 
155             mojo.execute();
156         }
157         catch ( Exception e )
158         {
159             e.printStackTrace();
160         }
161 
162         File bundleSource =
163             new File( getBasedir(), "target/test/unit/default-configuration/target/default-configuration-bundle.jar" );
164         assertTrue( FileUtils.fileExists( bundleSource.getAbsolutePath() ) );
165 
166         Set<String> entryNames = new HashSet<String>();
167         entryNames.add( "default-configuration-sources.jar" );
168         entryNames.add( "default-configuration.jar" );
169         entryNames.add( "pom.xml" );
170         entryNames.add( "META-INF/MANIFEST.MF" );
171         entryNames.add( "META-INF/" );
172 
173         Set<String> bannedNames = new HashSet<String>();
174         bannedNames.add( "default-configuration-javadoc.jar" );
175 
176         assertZipContents( entryNames, bannedNames, bundleSource );
177     }
178 
179     public void testDefaultconfiguration_RemoveTwo()
180         throws Exception
181     {
182         try
183         {
184             createTestJars( "default-configuration", true, true, getBasedir()
185                 + "/target/test/unit/default-configuration/target" );
186         }
187         catch ( IOException ie )
188         {
189             ie.printStackTrace();
190         }
191 
192         File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pom.xml" );
193 
194         try
195         {
196             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
197 
198             // NOTE: This is sensitive to the lookupMojo method timing...
199             TestInputHandler ih = (TestInputHandler) lookup( InputHandler.ROLE, "default" );
200 
201             Stack<String> responses = new Stack<String>();
202             responses.push( "2,3" );
203             ih.setLineResponses( responses );
204 
205             mojo.execute();
206         }
207         catch ( Exception e )
208         {
209             e.printStackTrace();
210         }
211 
212         File bundleSource =
213             new File( getBasedir(), "target/test/unit/default-configuration/target/default-configuration-bundle.jar" );
214         assertTrue( FileUtils.fileExists( bundleSource.getAbsolutePath() ) );
215 
216         Set<String> entryNames = new HashSet<String>();
217         entryNames.add( "default-configuration.jar" );
218         entryNames.add( "pom.xml" );
219         entryNames.add( "META-INF/MANIFEST.MF" );
220         entryNames.add( "META-INF/" );
221 
222         Set<String> bannedNames = new HashSet<String>();
223         bannedNames.add( "default-configuration-javadoc.jar" );
224         bannedNames.add( "default-configuration-sources.jar" );
225 
226         assertZipContents( entryNames, bannedNames, bundleSource );
227     }
228 
229     public void testDefaultconfiguration_RemoveTwoUnordered()
230         throws Exception
231     {
232         try
233         {
234             createTestJars( "default-configuration", true, true, getBasedir()
235                 + "/target/test/unit/default-configuration/target" );
236         }
237         catch ( IOException ie )
238         {
239             ie.printStackTrace();
240         }
241 
242         File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pom.xml" );
243 
244         try
245         {
246             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
247 
248             // NOTE: This is sensitive to the lookupMojo method timing...
249             TestInputHandler ih = (TestInputHandler) lookup( InputHandler.ROLE, "default" );
250 
251             Stack<String> responses = new Stack<String>();
252             responses.push( "3,2" );
253             ih.setLineResponses( responses );
254 
255             mojo.execute();
256         }
257         catch ( Exception e )
258         {
259             e.printStackTrace();
260         }
261 
262         File bundleSource =
263             new File( getBasedir(), "target/test/unit/default-configuration/target/default-configuration-bundle.jar" );
264         assertTrue( FileUtils.fileExists( bundleSource.getAbsolutePath() ) );
265 
266         Set<String> entryNames = new HashSet<String>();
267         entryNames.add( "default-configuration.jar" );
268         entryNames.add( "pom.xml" );
269         entryNames.add( "META-INF/MANIFEST.MF" );
270         entryNames.add( "META-INF/" );
271 
272         Set<String> bannedNames = new HashSet<String>();
273         bannedNames.add( "default-configuration-javadoc.jar" );
274         bannedNames.add( "default-configuration-sources.jar" );
275 
276         assertZipContents( entryNames, bannedNames, bundleSource );
277     }
278 
279     public void testDefaultconfiguration_RemoveTwoWithSpace()
280         throws Exception
281     {
282         try
283         {
284             createTestJars( "default-configuration", true, true, getBasedir()
285                 + "/target/test/unit/default-configuration/target" );
286         }
287         catch ( IOException ie )
288         {
289             ie.printStackTrace();
290         }
291 
292         File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pom.xml" );
293 
294         try
295         {
296             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
297 
298             // NOTE: This is sensitive to the lookupMojo method timing...
299             TestInputHandler ih = (TestInputHandler) lookup( InputHandler.ROLE, "default" );
300 
301             Stack<String> responses = new Stack<String>();
302             responses.push( "2, 3" );
303             ih.setLineResponses( responses );
304 
305             mojo.execute();
306         }
307         catch ( Exception e )
308         {
309             e.printStackTrace();
310         }
311 
312         File bundleSource =
313             new File( getBasedir(), "target/test/unit/default-configuration/target/default-configuration-bundle.jar" );
314         assertTrue( FileUtils.fileExists( bundleSource.getAbsolutePath() ) );
315 
316         Set<String> entryNames = new HashSet<String>();
317         entryNames.add( "default-configuration.jar" );
318         entryNames.add( "pom.xml" );
319         entryNames.add( "META-INF/MANIFEST.MF" );
320         entryNames.add( "META-INF/" );
321 
322         Set<String> bannedNames = new HashSet<String>();
323         bannedNames.add( "default-configuration-javadoc.jar" );
324         bannedNames.add( "default-configuration-sources.jar" );
325 
326         assertZipContents( entryNames, bannedNames, bundleSource );
327     }
328 
329     /**
330      * Test repository plugin when there is no javadoc jar to be included in the bundle
331      * 
332      * @throws Exception
333      */
334     public void testNoJavadocJar()
335         throws Exception
336     {
337 
338         try
339         {
340             createTestJars( "no-javadocjar", false, true, getBasedir() + "/target/test/unit/no-javadocjar/target" );
341         }
342         catch ( IOException ie )
343         {
344             ie.printStackTrace();
345         }
346 
347         File testPom = new File( getBasedir(), "src/test/resources/unit/no-javadocjar/pom.xml" );
348 
349         try
350         {
351             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
352             mojo.execute();
353         }
354         catch ( Exception e )
355         {
356             e.printStackTrace();
357         }
358 
359         File bundleSource = new File( getBasedir(), "target/test/unit/no-javadocjar/target/no-javadocjar-bundle.jar" );
360         assertTrue( FileUtils.fileExists( bundleSource.getAbsolutePath() ) );
361 
362         Set<String> entryNames = new HashSet<String>();
363         entryNames.add( "no-javadocjar-sources.jar" );
364         entryNames.add( "no-javadocjar.jar" );
365         entryNames.add( "pom.xml" );
366         entryNames.add( "META-INF/MANIFEST.MF" );
367         entryNames.add( "META-INF/" );
368 
369         assertZipContents( entryNames, Collections.singleton( "no-javadocjar-javadoc.jar" ), bundleSource );
370     }
371 
372     /**
373      * Test repository plugin when there is no sources jar to be included in the bundle
374      * 
375      * @throws Exception
376      */
377     public void testNoSourcesJar()
378         throws Exception
379     {
380 
381         try
382         {
383             createTestJars( "no-sourcesjar", true, false, getBasedir() + "/target/test/unit/no-sourcesjar/target" );
384         }
385         catch ( IOException ie )
386         {
387             ie.printStackTrace();
388         }
389 
390         File testPom = new File( getBasedir(), "src/test/resources/unit/no-sourcesjar/pom.xml" );
391 
392         try
393         {
394             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
395             mojo.execute();
396         }
397         catch ( Exception e )
398         {
399             e.printStackTrace();
400         }
401 
402         File bundleSource = new File( getBasedir(), "target/test/unit/no-sourcesjar/target/no-sourcesjar-bundle.jar" );
403         assertTrue( FileUtils.fileExists( bundleSource.getAbsolutePath() ) );
404 
405         Set<String> entryNames = new HashSet<String>();
406         entryNames.add( "no-sourcesjar-javadoc.jar" );
407         entryNames.add( "no-sourcesjar.jar" );
408         entryNames.add( "pom.xml" );
409         entryNames.add( "META-INF/MANIFEST.MF" );
410         entryNames.add( "META-INF/" );
411 
412         assertZipContents( entryNames, Collections.singleton( "no-sourcesjar-sources.jar" ), bundleSource );
413     }
414 
415     /**
416      * Test repository plugin when there are no javadoc and sources jar files to be included in the bundle
417      * 
418      * @throws Exception
419      */
420     public void testNoJavadocSourcesJars()
421         throws Exception
422     {
423 
424         try
425         {
426             createTestJars( "no-javadoc-sources", false, false, getBasedir()
427                 + "/target/test/unit/no-javadoc-sources/target" );
428         }
429         catch ( IOException ie )
430         {
431             ie.printStackTrace();
432         }
433 
434         File testPom = new File( getBasedir(), "src/test/resources/unit/no-javadoc-sources/pom.xml" );
435 
436         try
437         {
438             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
439             mojo.execute();
440         }
441         catch ( Exception e )
442         {
443             e.printStackTrace();
444         }
445 
446         File bundleSource =
447             new File( getBasedir(), "target/test/unit/no-javadoc-sources/target/no-javadoc-sources-bundle.jar" );
448         assertTrue( FileUtils.fileExists( bundleSource.getAbsolutePath() ) );
449 
450         Set<String> entryNames = new HashSet<String>();
451         entryNames.add( "no-javadoc-sources.jar" );
452         entryNames.add( "pom.xml" );
453         entryNames.add( "META-INF/MANIFEST.MF" );
454         entryNames.add( "META-INF/" );
455 
456         Set<String> bannedNames = new HashSet<String>();
457         bannedNames.add( "no-javadoc-sources-sources.jar" );
458         bannedNames.add( "no-javadoc-sources-javadoc.jar" );
459 
460         assertZipContents( entryNames, bannedNames, bundleSource );
461     }
462 
463     /**
464      * Test repository plugin when the scm element is null
465      * 
466      * @throws Exception
467      */
468     public void testNullScm()
469         throws Exception
470     {
471         try
472         {
473             createTestJars( "no-scm", false, false, getBasedir() + "/target/test/unit/no-scm/target" );
474         }
475         catch ( IOException ie )
476         {
477             ie.printStackTrace();
478         }
479 
480         File testPom = new File( getBasedir(), "src/test/resources/unit/no-scm/pom.xml" );
481 
482         BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
483         try
484         {
485             mojo.execute();
486 
487             // MREPOSITORY-2 project.scm.connection should not be required for bundle-create
488             // MREPOSITORY-19 project.scm.{url|connection} are required for project materialization...
489              fail( "Must throw an exception on a project element scm is null" );
490         }
491         catch ( MojoExecutionException e )
492         {
493             // expected; scm.connection is required, as is scm.url
494         }
495 
496     }
497 
498     /**
499      * Test repository plugin when license file does not exist
500      * 
501      * @throws Exception
502      */
503     public void testNoLicense()
504         throws Exception
505     {
506 
507         try
508         {
509             createTestJars( "no-license-file", false, false, getBasedir() + "/target/test/unit/no-license-file/target" );
510         }
511         catch ( IOException ie )
512         {
513             ie.printStackTrace();
514         }
515 
516         File testPom = new File( getBasedir(), "src/test/resources/unit/no-license-file/pom.xml" );
517 
518         try
519         {
520             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
521             mojo.execute();
522             fail( "Must throw an exception on missing license file" );
523         }
524         catch ( Exception e )
525         {
526             assertTrue( true );
527         }
528 
529     }
530 
531     /**
532      * Test repository plugin when there is no project name specified in the pom
533      * 
534      * @throws Exception
535      */
536     public void testNoProjectName()
537         throws Exception
538     {
539         try
540         {
541             createTestJars( "no-project-name", false, false, getBasedir() + "/target/test/unit/no-project-name/target" );
542         }
543         catch ( IOException ie )
544         {
545             ie.printStackTrace();
546         }
547 
548         File testPom = new File( getBasedir(), "src/test/resources/unit/no-project-name/pom.xml" );
549 
550         try
551         {
552             BundleCreateMojo mojo = (BundleCreateMojo) lookupMojo( "bundle-create", testPom );
553             mojo.execute();
554             fail( "Must throw an exception on empty expression" );
555         }
556         catch ( Exception e )
557         {
558             assertTrue( true );
559         }
560 
561     }
562 
563     /**
564      * Method for creating the jar files that will be used in testing
565      * 
566      * @param fileName
567      *            the file name of the jar file(s) to be created
568      * @param createJavadocJar
569      *            specifies whether a javadoc jar file will be created or not
570      * @param createSourcesJar
571      *            specifies whether a sources jar file will be created or not
572      * @param destDir
573      *            the destination directory where the jar file(s) are to be created
574      * @throws IOException
575      */
576     private void createTestJars( String fileName, boolean createJavadocJar, boolean createSourcesJar, String destDir )
577         throws IOException
578     {
579         File targetDir = new File( destDir );
580         boolean success = targetDir.mkdirs();
581 
582         if ( success )
583         {
584             File outputFile = new File( destDir, fileName + ".jar" );
585             success = outputFile.createNewFile();
586 
587             if ( createJavadocJar )
588             {
589                 File javadocJar = new File( destDir, fileName + "-javadoc.jar" );
590                 success = javadocJar.createNewFile();
591             }
592 
593             if ( createSourcesJar )
594             {
595                 File sourcesJar = new File( destDir, fileName + "-sources.jar" );
596                 success = sourcesJar.createNewFile();
597             }
598         }
599         else
600         {
601             System.out.println( "Target directory not created." );
602 
603         }
604 
605     }
606 
607     protected void tearDown()
608         throws Exception
609     {
610 
611     }
612 
613 }