1 package org.apache.maven.plugins.source;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
48
49
50
51
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
60 mojo.getProject().setFile( testPom );
61
62 setVariableValueToObject( mojo, "classifier", classifier );
63
64 mojo.execute();
65 }
66
67
68
69
70
71
72
73
74
75
76
77
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
104
105
106
107
108
109
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
120
121
122
123
124
125
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 }