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 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
59 mojo.getProject().setFile( testPom );
60
61 setVariableValueToObject( mojo, "classifier", classifier );
62
63 mojo.execute();
64 }
65
66
67
68
69
70
71
72
73
74
75
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
102
103
104
105
106
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
117
118
119
120
121
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 }