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