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 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
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 )
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
64
65
66
67
68
69
70
71
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
98
99
100
101
102
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
112
113
114
115
116
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 }