View Javadoc

1   package org.apache.maven.plugin.source;
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.plugin.testing.AbstractMojoTestCase;
23  import org.codehaus.plexus.archiver.zip.ZipFile;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.Arrays;
28  import java.util.Enumeration;
29  import java.util.Set;
30  import java.util.TreeSet;
31  import java.util.zip.ZipEntry;
32  
33  /**
34   * @author Stephane Nicoll
35   */
36  public abstract class AbstractSourcePluginTestCase
37      extends AbstractMojoTestCase
38  {
39  
40      protected static final String FINAL_NAME_PREFIX = "maven-source-plugin-test-";
41  
42      protected static final String FINAL_NAME_SUFFIX = "-99.0";
43  
44      protected abstract String getGoal();
45  
46      /**
47       * Execute the source plugin for the specified project.
48       *
49       * @param projectName the name of the project
50       * @return the base directory of the project
51       * @throws Exception if an error occurred
52       */
53      protected void executeMojo( final String projectName, String classifier )
54          throws Exception
55      {
56          File testPom = new File( getBasedir(), getTestDir( projectName ) + "/pom.xml" );
57          AbstractSourceJarMojo mojo = (AbstractSourceJarMojo) lookupMojo( getGoal(), testPom );
58  
59          setVariableValueToObject( mojo, "classifier", classifier );
60  
61          mojo.execute();
62      }
63  
64      /**
65       * Executes the specified projects and asserts the given artifacts.
66       *
67       * @param projectName             the project to test
68       * @param expectSourceArchive     if a source archive is expected
69       * @param expectTestSourceArchive if a test source archive is expected
70       * @param expectedSourceFiles     the expected files in the source archive, if any
71       * @param expectedTestSourceFiles the expected files in the test source archive, if any
72       * @return the base directory of the project
73       * @throws Exception if any error occurs
74       */
75      protected File doTestProject( final String projectName, boolean expectSourceArchive,
76                                    boolean expectTestSourceArchive, final String[] expectedSourceFiles,
77                                    final String[] expectedTestSourceFiles, String classifier )
78          throws Exception
79      {
80          executeMojo( projectName, classifier );
81          final File testTargetDir = getTestTargetDir( projectName );
82  
83          if ( expectSourceArchive )
84          {
85              assertSourceArchive( testTargetDir, projectName );
86              assertJarContent( getSourceArchive( testTargetDir, projectName ), expectedSourceFiles );
87          }
88  
89          if ( expectTestSourceArchive )
90          {
91              assertTestSourceArchive( testTargetDir, projectName );
92              assertJarContent( getTestSourceArchive( testTargetDir, projectName ), expectedTestSourceFiles );
93          }
94  
95          return testTargetDir;
96      }
97  
98      /**
99       * Executes the specified projects and asserts the given artifacts for a source archive.
100      *
101      * @param projectName         the project to test
102      * @param expectedSourceFiles the expected files in the source archive, if any
103      * @return the base directory of the project
104      * @throws Exception if any error occurs
105      */
106     protected File doTestProjectWithSourceArchive( final String projectName, final String[] expectedSourceFiles,
107                                                    String classifier )
108         throws Exception
109     {
110         return doTestProject( projectName, true, false, expectedSourceFiles, null, classifier );
111     }
112 
113     /**
114      * Executes the specified projects and asserts the given artifacts for a test source archive.
115      *
116      * @param projectName             the project to test
117      * @param expectedTestSourceFiles the expected files in the test source archive, if any
118      * @return the base directory of the project
119      * @throws Exception if any error occurs
120      */
121     protected File doTestProjectWithTestSourceArchive( final String projectName, final String[] expectedTestSourceFiles,
122                                                        String classifier )
123         throws Exception
124     {
125         return doTestProject( projectName, false, true, null, expectedTestSourceFiles, classifier );
126     }
127 
128 
129     protected void assertSourceArchive( final File testTargetDir, final String projectName )
130     {
131         final File expectedFile = getSourceArchive( testTargetDir, projectName );
132         assertTrue( "Source archive does not exist[" + expectedFile.getAbsolutePath() + "]", expectedFile.exists() );
133     }
134 
135     protected void assertTestSourceArchive( final File testTargetDir, final String projectName )
136     {
137         final File expectedFile = getTestSourceArchive( testTargetDir, projectName );
138         assertTrue( "Test source archive does not exist[" + expectedFile.getAbsolutePath() + "]",
139                     expectedFile.exists() );
140     }
141 
142     protected File getSourceArchive( final File testTargetDir, final String projectName )
143     {
144         return new File( testTargetDir, buildFinalSourceName( projectName ) + ".jar" );
145     }
146 
147     protected File getTestSourceArchive( final File testTargetDir, final String projectName )
148     {
149         return new File( testTargetDir, buildFinalTestSourceName( projectName ) + ".jar" );
150     }
151 
152     protected String buildFinalSourceName( final String projectName )
153     {
154         return FINAL_NAME_PREFIX + projectName + FINAL_NAME_SUFFIX + "-sources";
155     }
156 
157     protected String buildFinalTestSourceName( final String projectName )
158     {
159         return FINAL_NAME_PREFIX + projectName + FINAL_NAME_SUFFIX + "-test-sources";
160     }
161 
162     protected File getTestDir( String projectName )
163         throws IOException
164     {
165         File f = new File( "target/test-classes/unit/" + projectName );
166         if ( !new File( f, "pom.xml" ).exists() )
167         {
168             throw new IllegalStateException( "No pom file found in " + f.getPath() );
169         }
170         return f;
171     }
172 
173     protected void assertJarContent( final File jarFile, final String[] expectedFiles )
174         throws IOException
175     {
176         ZipFile jar = new ZipFile( jarFile );
177         Enumeration entries = jar.getEntries();
178 
179         if ( expectedFiles.length == 0 )
180         {
181             assertFalse( "Jar file should not contain any entry", entries.hasMoreElements() );
182         }
183         else
184         {
185             assertTrue( entries.hasMoreElements() );
186 
187             Set expected = new TreeSet( Arrays.asList( expectedFiles ) );
188 
189             while ( entries.hasMoreElements() )
190             {
191                 ZipEntry entry = (ZipEntry) entries.nextElement();
192 
193                 assertTrue( "Not expecting " + entry.getName() + " in " + jarFile, expected.remove( entry.getName() ) );
194             }
195 
196             assertTrue( "Missing entries " + expected.toString() + " in " + jarFile, expected.isEmpty() );
197         }
198     }
199 
200     protected File getTestTargetDir( String projectName )
201     {
202         return new File( getBasedir(), "target/test/unit/" + projectName + "/target" );
203     }
204 }