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