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