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