1 package org.apache.maven.plugins.javadoc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.util.List;
27
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.plugin.MojoExecution;
31 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
32 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.languages.java.version.JavaVersion;
35 import org.codehaus.plexus.util.FileUtils;
36 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
37 import org.sonatype.aether.util.DefaultRepositorySystemSession;
38
39 public class AggregatorJavadocReportTest
40 extends AbstractMojoTestCase
41 {
42 private static final char LINE_SEPARATOR = ' ';
43
44
45 private static boolean TEST_REPO_CREATED = false;
46
47 private File unit;
48
49 private File localRepo;
50
51
52 @Override
53 protected void setUp()
54 throws Exception
55 {
56 super.setUp();
57
58 unit = new File( getBasedir(), "src/test/resources/unit" );
59
60 localRepo = new File( getBasedir(), "target/local-repo/" );
61
62 createTestRepo();
63 }
64
65 private JavadocReport lookupMojo( File testPom )
66 throws Exception
67 {
68 JavadocReport mojo = (JavadocReport) lookupMojo( "aggregate", testPom );
69
70 MojoExecution mojoExec = new MojoExecution( new Plugin(), "aggregate", null );
71 setVariableValueToObject( mojo, "mojo", mojoExec );
72
73 MavenProject currentProject = new MavenProjectStub();
74 currentProject.setGroupId( "GROUPID" );
75 currentProject.setArtifactId( "ARTIFACTID" );
76
77 MavenSession session = newMavenSession( currentProject );
78 DefaultRepositorySystemSession repoSysSession = (DefaultRepositorySystemSession) session.getRepositorySession();
79 repoSysSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
80 setVariableValueToObject( mojo, "session", session );
81
82 return mojo;
83 }
84
85
86
87
88
89
90 private void createTestRepo()
91 throws IOException
92 {
93 if ( TEST_REPO_CREATED )
94 {
95 return;
96 }
97
98 localRepo.mkdirs();
99
100
101
102
103
104 File sourceDir = new File( unit, "doclet-test/artifact-doclet" );
105 assertTrue( sourceDir.exists() );
106 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
107
108
109
110
111
112 sourceDir = new File( unit, "doclet-path-test/artifact-doclet" );
113 assertTrue( sourceDir.exists() );
114 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
115
116
117
118
119
120
121 sourceDir = new File( unit, "taglet-test/artifact-taglet" );
122 assertTrue( sourceDir.exists() );
123 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
124
125
126
127
128
129 sourceDir = new File( unit, "stylesheetfile-test/artifact-stylesheetfile" );
130 assertTrue( sourceDir.exists() );
131 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
132
133
134
135
136
137 sourceDir = new File( unit, "helpfile-test/artifact-helpfile" );
138 assertTrue( sourceDir.exists() );
139 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
140
141
142 List<String> files = FileUtils.getFileAndDirectoryNames( localRepo, FileUtils.getDefaultExcludesAsString(),
143 null, true, true, true, true );
144 for ( String filename : files )
145 {
146 File file = new File( filename );
147
148 if ( file.isDirectory() )
149 {
150 FileUtils.deleteDirectory( file );
151 }
152 else
153 {
154 file.delete();
155 }
156 }
157
158 TEST_REPO_CREATED = true;
159 }
160
161
162
163
164
165
166
167
168
169
170 private static String readFile( File file )
171 throws IOException
172 {
173 String strTmp;
174 StringBuilder str = new StringBuilder( (int) file.length() );
175 BufferedReader in = new BufferedReader( new FileReader( file ) );
176
177 try
178 {
179 while ( ( strTmp = in.readLine() ) != null )
180 {
181 str.append( LINE_SEPARATOR );
182 str.append( strTmp );
183 }
184 }
185 finally
186 {
187 in.close();
188 }
189
190 return str.toString();
191 }
192
193
194
195
196
197
198 public void testAggregate()
199 throws Exception
200 {
201 File testPom = new File( unit, "aggregate-test/aggregate-test-plugin-config.xml" );
202 JavadocReport mojo = lookupMojo( testPom );
203 mojo.execute();
204
205 File apidocs = new File( getBasedir(), "target/test/unit/aggregate-test/target/site/apidocs/" );
206
207
208 assertTrue( new File( apidocs, "aggregate/test/project1/Project1App.html" ).exists() );
209 assertTrue( new File( apidocs, "aggregate/test/project1/Project1AppSample.html" ).exists() );
210 assertTrue( new File( apidocs, "aggregate/test/project1/Project1Sample.html" ).exists() );
211 assertTrue( new File( apidocs, "aggregate/test/project1/Project1Test.html" ).exists() );
212
213
214 assertTrue( new File( apidocs, "aggregate/test/project2/Project2App.html" ).exists() );
215 assertTrue( new File( apidocs, "aggregate/test/project2/Project2AppSample.html" ).exists() );
216 assertTrue( new File( apidocs, "aggregate/test/project2/Project2Sample.html" ).exists() );
217 assertTrue( new File( apidocs, "aggregate/test/project2/Project2Test.html" ).exists() );
218 }
219
220
221
222
223
224
225 public void testAggregateJavadocResources()
226 throws Exception
227 {
228 File testPom = new File( unit, "aggregate-resources-test/aggregate-resources-test-plugin-config.xml" );
229 JavadocReport mojo = lookupMojo( testPom );
230 mojo.execute();
231
232 File apidocs = new File( getBasedir(), "target/test/unit/aggregate-resources-test/target/site/apidocs" );
233
234
235 File overviewSummary;
236 if ( JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore( "11" ) )
237 {
238 overviewSummary = new File( apidocs, "overview-summary.html" );
239 }
240 else
241 {
242 overviewSummary = new File( apidocs, "index.html" );
243 }
244
245 assertTrue( overviewSummary.exists() );
246 String overview = readFile( overviewSummary ).toLowerCase();
247 assertTrue( overview.contains( "<a href=\"resources/test/package-summary.html\">resources.test</a>" ) );
248 assertTrue( overview.contains( ">blabla</" ) );
249 assertTrue( overview.contains( "<a href=\"resources/test2/package-summary.html\">resources.test2</a>" ) );
250 assertTrue( overview.contains( "<a href=\"resources2/test/package-summary.html\">resources2.test</a>" ) );
251 assertTrue( overview.contains( "<a href=\"resources2/test2/package-summary.html\">resources2.test2</a>" ) );
252
253
254 File app = new File( apidocs, "resources/test/App.html" );
255 assertTrue( app.exists() );
256 overview = readFile( app );
257 assertTrue( overview.contains( "<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">" ) );
258 assertTrue( new File( apidocs, "resources/test/doc-files/maven-feather.png" ).exists() );
259 }
260 }