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