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