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 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.Reader;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.apache.commons.lang3.SystemUtils;
35 import org.apache.maven.execution.MavenSession;
36 import org.apache.maven.model.Plugin;
37 import org.apache.maven.plugin.LegacySupport;
38 import org.apache.maven.plugin.MojoExecution;
39 import org.apache.maven.plugin.MojoExecutionException;
40 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
41 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
42 import org.apache.maven.plugins.javadoc.ProxyServer.AuthAsyncProxyServlet;
43 import org.apache.maven.project.MavenProject;
44 import org.apache.maven.project.ProjectBuildingRequest;
45 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
46 import org.apache.maven.settings.Proxy;
47 import org.apache.maven.settings.Settings;
48 import org.codehaus.plexus.util.FileUtils;
49 import org.codehaus.plexus.util.IOUtil;
50 import org.codehaus.plexus.util.ReaderFactory;
51 import org.codehaus.plexus.util.StringUtils;
52 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
53
54
55
56
57
58
59
60 public class JavadocReportTest
61 extends AbstractMojoTestCase
62 {
63 private static final char LINE_SEPARATOR = ' ';
64
65
66 private static boolean TEST_REPO_CREATED = false;
67
68 private File unit;
69
70 private File localRepo;
71
72
73 @Override
74 protected void setUp()
75 throws Exception
76 {
77 super.setUp();
78
79 unit = new File( getBasedir(), "src/test/resources/unit" );
80
81 localRepo = new File( getBasedir(), "target/local-repo/" );
82
83 createTestRepo();
84 }
85
86
87 private JavadocReport lookupMojo( File testPom )
88 throws Exception
89 {
90 JavadocReport mojo = (JavadocReport) lookupMojo( "javadoc", testPom );
91
92 MojoExecution mojoExec = new MojoExecution( new Plugin(), "javadoc", null );
93
94 setVariableValueToObject( mojo, "mojo", mojoExec );
95
96 MavenProject currentProject = new MavenProjectStub();
97 currentProject.setGroupId( "GROUPID" );
98 currentProject.setArtifactId( "ARTIFACTID" );
99
100 setVariableValueToObject( mojo, "session", newMavenSession( currentProject ) );
101
102 return mojo;
103 }
104
105
106
107
108
109
110 private void createTestRepo()
111 throws IOException
112 {
113 if ( TEST_REPO_CREATED )
114 {
115 return;
116 }
117
118 localRepo.mkdirs();
119
120
121
122
123
124 File sourceDir = new File( unit, "doclet-test/artifact-doclet" );
125 assertTrue( sourceDir.exists() );
126 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
127
128
129
130
131
132 sourceDir = new File( unit, "doclet-path-test/artifact-doclet" );
133 assertTrue( sourceDir.exists() );
134 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
135
136
137
138
139
140
141 sourceDir = new File( unit, "taglet-test/artifact-taglet" );
142 assertTrue( sourceDir.exists() );
143 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
144
145
146
147
148
149 sourceDir = new File( unit, "stylesheetfile-test/artifact-stylesheetfile" );
150 assertTrue( sourceDir.exists() );
151 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
152
153
154
155
156
157 sourceDir = new File( unit, "helpfile-test/artifact-helpfile" );
158 assertTrue( sourceDir.exists() );
159 FileUtils.copyDirectoryStructure( sourceDir, localRepo );
160
161
162 List<String> files =
163 FileUtils.getFileAndDirectoryNames( localRepo, FileUtils.getDefaultExcludesAsString(), null, true,
164 true, true, true );
165 for ( String filename : files )
166 {
167 File file = new File( filename );
168
169 if ( file.isDirectory() )
170 {
171 FileUtils.deleteDirectory( file );
172 }
173 else
174 {
175 file.delete();
176 }
177 }
178
179 TEST_REPO_CREATED = true;
180 }
181
182
183
184
185
186
187
188
189
190
191 private static String readFile( File file )
192 throws IOException
193 {
194 String strTmp;
195 StringBuilder str = new StringBuilder( (int) file.length() );
196 BufferedReader in = new BufferedReader( new FileReader( file ) );
197
198 try
199 {
200 while ( ( strTmp = in.readLine() ) != null )
201 {
202 str.append( LINE_SEPARATOR );
203 str.append( strTmp );
204 }
205 }
206 finally
207 {
208 in.close();
209 }
210
211 return str.toString();
212 }
213
214
215
216
217
218
219 public void testDefaultConfiguration()
220 throws Exception
221 {
222 File testPom = new File( unit, "default-configuration/default-configuration-plugin-config.xml" );
223 JavadocReport mojo = lookupMojo( testPom );
224 mojo.execute();
225
226
227 File apidocs = new File( getBasedir(), "target/test/unit/default-configuration/target/site/apidocs" );
228
229 File generatedFile = new File( apidocs, "def/configuration/App.html" );
230 assertTrue( generatedFile.exists() );
231 assertTrue( FileUtils.fileRead( generatedFile, "UTF-8" ).contains( "/docs/api/java/lang/Object.html" ) );
232
233 assertTrue( new File( apidocs, "def/configuration/AppSample.html" ).exists() );
234 assertTrue( new File( apidocs, "def/configuration/package-frame.html" ).exists() );
235 assertTrue( new File( apidocs, "def/configuration/package-summary.html" ).exists() );
236 assertTrue( new File( apidocs, "def/configuration/package-tree.html" ).exists() );
237 assertTrue( new File( apidocs, "def/configuration/package-use.html" ).exists() );
238
239
240 assertTrue( new File( apidocs, "def/configuration/class-use/App.html" ).exists() );
241 assertTrue( new File( apidocs, "def/configuration/class-use/AppSample.html" ).exists() );
242
243
244 assertTrue( new File( apidocs, "allclasses-frame.html" ).exists() );
245 assertTrue( new File( apidocs, "allclasses-noframe.html" ).exists() );
246 assertTrue( new File( apidocs, "constant-values.html" ).exists() );
247 assertTrue( new File( apidocs, "deprecated-list.html" ).exists() );
248 assertTrue( new File( apidocs, "help-doc.html" ).exists() );
249 assertTrue( new File( apidocs, "index-all.html" ).exists() );
250 assertTrue( new File( apidocs, "index.html" ).exists() );
251 assertTrue( new File( apidocs, "overview-tree.html" ).exists() );
252 assertTrue( new File( apidocs, "package-list" ).exists() );
253 assertTrue( new File( apidocs, "stylesheet.css" ).exists() );
254 }
255
256
257
258
259
260
261 public void testSubpackages()
262 throws Exception
263 {
264 File testPom = new File( unit, "subpackages-test/subpackages-test-plugin-config.xml" );
265 JavadocReport mojo = lookupMojo( testPom );
266 mojo.execute();
267
268 File apidocs = new File( getBasedir(), "target/test/unit/subpackages-test/target/site/apidocs" );
269
270
271 assertFalse( new File( apidocs, "subpackages/test/excluded" ).exists() );
272 assertFalse( new File( apidocs, "subpackages/test/included/exclude" ).exists() );
273
274
275 assertTrue( new File( apidocs, "subpackages/test/App.html" ).exists() );
276 assertTrue( new File( apidocs, "subpackages/test/AppSample.html" ).exists() );
277 assertTrue( new File( apidocs, "subpackages/test/included/IncludedApp.html" ).exists() );
278 assertTrue( new File( apidocs, "subpackages/test/included/IncludedAppSample.html" ).exists() );
279 }
280
281 public void testIncludesExcludes()
282 throws Exception
283 {
284 File testPom = new File( unit, "file-include-exclude-test/file-include-exclude-plugin-config.xml" );
285 JavadocReport mojo = lookupMojo( testPom );
286 mojo.execute();
287
288 File apidocs = new File( getBasedir(), "target/test/unit/file-include-exclude-test/target/site/apidocs" );
289
290
291 assertTrue( new File( apidocs, "subpackages/test/App.html" ).exists() );
292 assertTrue( new File( apidocs, "subpackages/test/AppSample.html" ).exists() );
293 assertTrue( new File( apidocs, "subpackages/test/included/IncludedApp.html" ).exists() );
294 assertTrue( new File( apidocs, "subpackages/test/included/IncludedAppSample.html" ).exists() );
295 assertFalse( new File( apidocs, "subpackages/test/PariahApp.html" ).exists() );
296 }
297
298
299
300
301
302
303 public void testDocfiles()
304 throws Exception
305 {
306
307
308 if ( JavadocVersion.parse( SystemUtils.JAVA_SPECIFICATION_VERSION ).compareTo( JavadocVersion.parse( "9" ) ) == 0 )
309 {
310 return;
311 }
312
313 File testPom = new File( unit, "docfiles-test/docfiles-test-plugin-config.xml" );
314 JavadocReport mojo = lookupMojo( testPom );
315 mojo.execute();
316
317 File apidocs = new File( getBasedir(), "target/test/unit/docfiles-test/target/site/apidocs/" );
318
319
320 assertTrue( new File( apidocs, "doc-files" ).exists() );
321 assertTrue( new File( apidocs, "doc-files/included-dir1/sample-included1.gif" ).exists() );
322 assertTrue( new File( apidocs, "doc-files/included-dir2/sample-included2.gif" ).exists() );
323 assertFalse( new File( apidocs, "doc-files/excluded-dir1" ).exists() );
324 assertFalse( new File( apidocs, "doc-files/excluded-dir2" ).exists() );
325
326 testPom = new File( unit, "docfiles-with-java-test/docfiles-with-java-test-plugin-config.xml" );
327 mojo = lookupMojo( testPom );
328 mojo.execute();
329 }
330
331
332
333
334
335
336
337 public void testCustomConfiguration()
338 throws Exception
339 {
340 File testPom = new File( unit, "custom-configuration/custom-configuration-plugin-config.xml" );
341 JavadocReport mojo = lookupMojo( testPom );
342 mojo.execute();
343
344 File apidocs = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/apidocs" );
345
346
347 assertFalse( new File( apidocs, "overview-tree.html" ).exists() );
348 assertFalse( new File( apidocs, "custom/configuration/package-tree.html" ).exists() );
349
350
351 assertFalse( new File( apidocs, "index-all.html" ).exists() );
352
353
354
355
356 assertFalse( new File( apidocs, "deprecated-list.html" ).exists() );
357 assertFalse( new File( apidocs, "custom/configuration/App.html" ).exists() );
358
359
360
361 String str = readFile( new File( apidocs, "custom/configuration/AppSample.html" ) );
362 assertFalse( str.toLowerCase().contains( "author" ) );
363
364
365 assertTrue( str.toUpperCase().contains( "SAMPLE BOTTOM CONTENT" ) );
366
367
368 assertTrue( str.toLowerCase().contains( "href=\"http://java.sun.com/j2se/1.4.2/docs/api/java/lang/string.html" ) );
369
370
371 assertTrue( str.toUpperCase().contains( "MAVEN JAVADOC PLUGIN TEST" ) );
372
373
374 assertTrue( str.toUpperCase().contains( "MAVEN JAVADOC PLUGIN TEST FOOTER" ) );
375
376
377 assertFalse( str.toUpperCase().contains( "/HELP-DOC.HTML" ) );
378
379
380 assertTrue( new File( apidocs, "custom/configuration/exclude1/Exclude1App.html" ).exists() );
381 assertFalse( new File( apidocs, "custom/configuration/exclude1/subexclude/SubexcludeApp.html" ).exists() );
382 assertFalse( new File( apidocs, "custom/configuration/exclude2/Exclude2App.html" ).exists() );
383
384 File options = new File( apidocs, "options" );
385 assertTrue( options.isFile() );
386 String contentOptions = null;
387 Reader reader = null;
388 try
389 {
390 reader = ReaderFactory.newPlatformReader( options );
391 contentOptions = IOUtil.toString( reader );
392 reader.close();
393 reader = null;
394 }
395 finally
396 {
397 IOUtil.close( reader );
398 }
399
400 assertTrue( contentOptions != null );
401 assertTrue( contentOptions.contains( "-link" ) );
402 assertTrue( contentOptions.contains( "http://java.sun.com/j2se/" ) );
403 }
404
405
406
407
408
409
410 public void testDoclets()
411 throws Exception
412 {
413
414
415
416
417
418 File testPom = new File( unit, "doclet-test/doclet-test-plugin-config.xml" );
419 JavadocReport mojo = lookupMojo( testPom );
420
421 MavenSession session = spy( newMavenSession( mojo.project ) );
422 ProjectBuildingRequest buildingRequest = mock( ProjectBuildingRequest.class );
423 when( buildingRequest.getRemoteRepositories() ).thenReturn( mojo.project.getRemoteArtifactRepositories() );
424 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
425 MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
426 repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
427 when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
428 when( session.getRepositorySession() ).thenReturn( repositorySession );
429 LegacySupport legacySupport = lookup( LegacySupport.class );
430 legacySupport.setSession( session );
431
432 setVariableValueToObject( mojo, "session", session );
433
434 mojo.execute();
435
436 File generatedFile = new File( getBasedir(), "target/test/unit/doclet-test/target/site/apidocs/graph.dot" );
437 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
438
439 File optionsFile = new File( mojo.getOutputDirectory(), "options" );
440 assertTrue( optionsFile.exists() );
441 String options = readFile( optionsFile );
442 assertTrue( options.contains( "/target/local-repo/umlgraph/UMLGraph/2.1/UMLGraph-2.1.jar" ) );
443
444
445
446
447
448
449 testPom = new File( unit, "doclet-path-test/doclet-path-test-plugin-config.xml" );
450 mojo = lookupMojo( testPom );
451 setVariableValueToObject( mojo, "session", session );
452 mojo.execute();
453
454 generatedFile = new File( getBasedir(), "target/test/unit/doclet-test/target/site/apidocs/graph.dot" );
455 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
456
457 optionsFile = new File( mojo.getOutputDirectory(), "options" );
458 assertTrue( optionsFile.exists() );
459 options = readFile( optionsFile );
460 assertTrue( options.contains( "/target/local-repo/umlgraph/UMLGraph/2.1/UMLGraph-2.1.jar" ) );
461 assertTrue( options.contains( "/target/local-repo/umlgraph/UMLGraph-bis/2.1/UMLGraph-bis-2.1.jar" ) );
462 }
463
464
465
466
467
468
469
470
471 public void testQuotedPath()
472 throws Exception
473 {
474 File testPom = new File( unit, "quotedpath'test/quotedpath-test-plugin-config.xml" );
475 JavadocReport mojo = lookupMojo( testPom );
476 mojo.execute();
477
478 File apidocs = new File( getBasedir(), "target/test/unit/quotedpath'test/target/site/apidocs" );
479
480
481 assertTrue( new File( apidocs, "quotedpath/test/App.html" ).exists() );
482 assertTrue( new File( apidocs, "quotedpath/test/AppSample.html" ).exists() );
483
484
485 assertTrue( new File( apidocs, "index-all.html" ).exists() );
486 assertTrue( new File( apidocs, "index.html" ).exists() );
487 assertTrue( new File( apidocs, "overview-tree.html" ).exists() );
488 assertTrue( new File( apidocs, "package-list" ).exists() );
489 assertTrue( new File( apidocs, "stylesheet.css" ).exists() );
490 }
491
492
493
494
495 public void testExceptions()
496 throws Exception
497 {
498 try
499 {
500 File testPom = new File( unit, "default-configuration/exception-test-plugin-config.xml" );
501 JavadocReport mojo = lookupMojo( testPom );
502 mojo.execute();
503
504 fail( "Must throw exception." );
505 }
506 catch ( Exception e )
507 {
508 assertTrue( true );
509
510 try
511 {
512 FileUtils.deleteDirectory( new File( getBasedir(), "exception" ) );
513 }
514 catch ( IOException ie )
515 {
516
517 }
518 }
519 }
520
521
522
523
524
525
526 public void testTaglets()
527 throws Exception
528 {
529
530
531
532
533 File testPom = new File( unit, "taglet-test/taglet-test-plugin-config.xml" );
534 JavadocReport mojo = lookupMojo( testPom );
535
536 MavenSession session = spy( newMavenSession( mojo.project ) );
537 ProjectBuildingRequest buildingRequest = mock( ProjectBuildingRequest.class );
538 when( buildingRequest.getRemoteRepositories() ).thenReturn( mojo.project.getRemoteArtifactRepositories() );
539 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
540 MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
541 repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
542 when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
543 when( session.getRepositorySession() ).thenReturn( repositorySession );
544 LegacySupport legacySupport = lookup( LegacySupport.class );
545 legacySupport.setSession( session );
546
547 setVariableValueToObject( mojo, "session", session );
548
549 mojo.execute();
550
551 File apidocs = new File( getBasedir(), "target/test/unit/taglet-test/target/site/apidocs" );
552
553 assertTrue( new File( apidocs, "index.html" ).exists() );
554
555 File appFile = new File( apidocs, "taglet/test/App.html" );
556 assertTrue( appFile.exists() );
557 String appString = readFile( appFile );
558 assertTrue( appString.contains( "<b>To Do:</b>" ) );
559 }
560
561
562
563
564
565
566 public void testJdk5()
567 throws Exception
568 {
569
570
571 if ( JavadocVersion.parse( SystemUtils.JAVA_SPECIFICATION_VERSION ).compareTo( JavadocVersion.parse( "9" ) ) >= 0 )
572 {
573 return;
574 }
575
576 File testPom = new File( unit, "jdk5-test/jdk5-test-plugin-config.xml" );
577 JavadocReport mojo = lookupMojo( testPom );
578 mojo.execute();
579
580 File apidocs = new File( getBasedir(), "target/test/unit/jdk5-test/target/site/apidocs" );
581
582 File index = new File( apidocs, "index.html" );
583 assertTrue( FileUtils.fileExists( index.getAbsolutePath() ) );
584
585 File overviewSummary = new File( apidocs, "overview-summary.html" );
586 assertTrue( overviewSummary.exists() );
587 String content = readFile( overviewSummary );
588 assertTrue( content.contains( "<b>Test the package-info</b>" ) );
589
590 File packageSummary = new File( apidocs, "jdk5/test/package-summary.html" );
591 assertTrue( packageSummary.exists() );
592 content = readFile( packageSummary );
593 assertTrue( content.contains( "<b>Test the package-info</b>" ) );
594 }
595
596
597
598
599
600
601
602 public void testToFindJavadoc()
603 throws Exception
604 {
605 String oldJreHome = System.getProperty( "java.home" );
606 System.setProperty( "java.home", "foo/bar" );
607
608 File testPom = new File( unit, "javaHome-test/javaHome-test-plugin-config.xml" );
609 JavadocReport mojo = lookupMojo( testPom );
610 mojo.execute();
611
612 System.setProperty( "java.home", oldJreHome );
613 }
614
615
616
617
618
619
620 public void testJavadocResources()
621 throws Exception
622 {
623 File testPom = new File( unit, "resources-test/resources-test-plugin-config.xml" );
624 JavadocReport mojo = lookupMojo( testPom );
625 mojo.execute();
626
627 File apidocs = new File( getBasedir(), "target/test/unit/resources-test/target/site/apidocs/" );
628
629 File app = new File( apidocs, "resources/test/App.html" );
630 assertTrue( app.exists() );
631 String content = readFile( app );
632 assertTrue( content.contains( "<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">" ) );
633 assertTrue( new File( apidocs, "resources/test/doc-files/maven-feather.png" ).exists() );
634
635 File app2 = new File( apidocs, "resources/test2/App2.html" );
636 assertTrue( app2.exists() );
637 content = readFile( app2 );
638 assertTrue( content.contains( "<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">" ) );
639 assertFalse( new File( apidocs, "resources/test2/doc-files/maven-feather.png" ).exists() );
640
641
642 testPom = new File( unit, "resources-with-excludes-test/resources-with-excludes-test-plugin-config.xml" );
643 mojo = lookupMojo( testPom );
644 mojo.execute();
645
646 apidocs = new File( getBasedir(), "target/test/unit/resources-with-excludes-test/target/site/apidocs" );
647
648 app = new File( apidocs, "resources/test/App.html" );
649 assertTrue( app.exists() );
650 content = readFile( app );
651 assertTrue( content.contains( "<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">" ) );
652
653 JavadocVersion javadocVersion = (JavadocVersion) getVariableValueFromObject( mojo, "javadocRuntimeVersion" );
654 if( javadocVersion.compareTo( JavadocVersion.parse( "1.8" ) ) >= 0 && javadocVersion.compareTo( JavadocVersion.parse( "10" ) ) < 0)
655 {
656
657 assertTrue( "This bug appeared in JDK8 and was planned to be fixed in JDK9, see JDK-8032205",
658 new File( apidocs, "resources/test/doc-files/maven-feather.png" ).exists() );
659 }
660 else
661 {
662 assertFalse( new File( apidocs, "resources/test/doc-files/maven-feather.png" ).exists() );
663 }
664 assertTrue( new File( apidocs, "resources/test2/doc-files/maven-feather.png" ).exists() );
665
666 app2 = new File( apidocs, "resources/test2/App2.html" );
667 assertTrue( app2.exists() );
668 content = readFile( app2 );
669 assertTrue( content.contains( "<img src=\"doc-files/maven-feather.png\" alt=\"Maven\">" ) );
670 assertTrue( new File( apidocs, "resources/test2/doc-files/maven-feather.png" ).exists() );
671 }
672
673
674
675
676
677
678 public void testPom()
679 throws Exception
680 {
681 File testPom = new File( unit, "pom-test/pom-test-plugin-config.xml" );
682 JavadocReport mojo = lookupMojo( testPom );
683 mojo.execute();
684
685 assertFalse( new File( getBasedir(), "target/test/unit/pom-test/target/site" ).exists() );
686 }
687
688
689
690
691
692
693 public void testTag()
694 throws Exception
695 {
696 File testPom = new File( unit, "tag-test/tag-test-plugin-config.xml" );
697 JavadocReport mojo = lookupMojo( testPom );
698 mojo.execute();
699
700 File app = new File( getBasedir(), "target/test/unit/tag-test/target/site/apidocs/tag/test/App.html" );
701 assertTrue( FileUtils.fileExists( app.getAbsolutePath() ) );
702 String readed = readFile( app );
703 assertTrue( readed.contains( ">To do something:</" ) );
704 assertTrue( readed.contains( ">Generator Class:</" ) );
705 assertTrue( readed.contains( ">Version:</" ) );
706 assertTrue( readed.toLowerCase().contains( "</dt>" + LINE_SEPARATOR + " <dd>1.0</dd>" )
707 || readed.toLowerCase().contains( "</dt>" + LINE_SEPARATOR + "<dd>1.0</dd>" ) );
708 }
709
710
711
712
713
714
715 public void testHeaderFooter()
716 throws Exception
717 {
718 File testPom = new File( unit, "header-footer-test/header-footer-test-plugin-config.xml" );
719 JavadocReport mojo = lookupMojo( testPom );
720 try
721 {
722 mojo.execute();
723 }
724 catch ( MojoExecutionException e )
725 {
726 assertTrue( "Doesnt handle correctly newline for header or footer parameter", false );
727 }
728
729 assertTrue( true );
730 }
731
732
733
734
735
736
737 public void testNewline()
738 throws Exception
739 {
740 File testPom = new File( unit, "newline-test/newline-test-plugin-config.xml" );
741 JavadocReport mojo = lookupMojo( testPom );
742 try
743 {
744 mojo.execute();
745 }
746 catch ( MojoExecutionException e )
747 {
748 fail( "Doesn't handle correctly newline for string parameters. See options and packages files." );
749 }
750
751 assertTrue( true );
752 }
753
754
755
756
757
758
759 public void testJdk6()
760 throws Exception
761 {
762 File testPom = new File( unit, "jdk6-test/jdk6-test-plugin-config.xml" );
763 JavadocReport mojo = lookupMojo( testPom );
764 mojo.execute();
765
766 File apidocs = new File( getBasedir(), "target/test/unit/jdk6-test/target/site/apidocs" );
767
768 assertTrue( new File( apidocs, "index.html" ).exists() );
769
770 File overviewSummary = new File( apidocs, "overview-summary.html" );
771 assertTrue( overviewSummary.exists() );
772 String content = readFile( overviewSummary );
773 assertTrue( content.contains( "Top - Copyright © All rights reserved." ) );
774 assertTrue( content.contains( "Header - Copyright © All rights reserved." ) );
775 assertTrue( content.contains( "Footer - Copyright © All rights reserved." ) );
776
777 File packageSummary = new File( apidocs, "jdk6/test/package-summary.html" );
778 assertTrue( packageSummary.exists() );
779 content = readFile( packageSummary );
780 assertTrue( content.contains( "Top - Copyright © All rights reserved." ) );
781 assertTrue( content.contains( "Header - Copyright © All rights reserved." ) );
782 assertTrue( content.contains( "Footer - Copyright © All rights reserved." ) );
783 }
784
785
786
787
788
789
790 public void testProxy()
791 throws Exception
792 {
793 Settings settings = new Settings();
794 Proxy proxy = new Proxy();
795
796
797 proxy.setActive( true );
798 proxy.setHost( "127.0.0.1" );
799 proxy.setPort( 80 );
800 proxy.setProtocol( "http" );
801 proxy.setUsername( "toto" );
802 proxy.setPassword( "toto" );
803 proxy.setNonProxyHosts( "www.google.com|*.somewhere.com" );
804 settings.addProxy( proxy );
805
806 File testPom = new File( getBasedir(), "src/test/resources/unit/proxy-test/proxy-test-plugin-config.xml" );
807 JavadocReport mojo = lookupMojo( testPom );
808
809 MavenSession session = spy( newMavenSession( mojo.project ) );
810 ProjectBuildingRequest buildingRequest = mock( ProjectBuildingRequest.class );
811 when( buildingRequest.getRemoteRepositories() ).thenReturn( mojo.project.getRemoteArtifactRepositories() );
812 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
813 MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
814 repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
815 when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
816 when( session.getRepositorySession() ).thenReturn( repositorySession );
817 LegacySupport legacySupport = lookup( LegacySupport.class );
818 legacySupport.setSession( session );
819
820 setVariableValueToObject( mojo, "settings", settings );
821 setVariableValueToObject( mojo, "session", session );
822 mojo.execute();
823
824 File commandLine = new File( getBasedir(), "target/test/unit/proxy-test/target/site/apidocs/javadoc." + ( SystemUtils.IS_OS_WINDOWS ? "bat" : "sh" ) );
825 assertTrue( FileUtils.fileExists( commandLine.getAbsolutePath() ) );
826 String readed = readFile( commandLine );
827 assertTrue( readed.contains( "-J-Dhttp.proxySet=true" ) );
828 assertTrue( readed.contains( "-J-Dhttp.proxyHost=127.0.0.1" ) );
829 assertTrue( readed.contains( "-J-Dhttp.proxyPort=80" ) );
830 assertTrue( readed.contains( "-J-Dhttp.proxyUser=\\\"toto\\\"" ) );
831 assertTrue( readed.contains( "-J-Dhttp.proxyPassword=\\\"toto\\\"" ) );
832 assertTrue( readed.contains( "-J-Dhttp.nonProxyHosts=\\\"www.google.com|*.somewhere.com\\\"" ) );
833
834 File options = new File( getBasedir(), "target/test/unit/proxy-test/target/site/apidocs/options" );
835 assertTrue( FileUtils.fileExists( options.getAbsolutePath() ) );
836 String optionsContent = readFile( options );
837
838 assertFalse( optionsContent.contains( "-link" ) );
839
840
841 ProxyServer proxyServer = null;
842 AuthAsyncProxyServlet proxyServlet;
843 try
844 {
845 proxyServlet = new AuthAsyncProxyServlet();
846 proxyServer = new ProxyServer( proxyServlet );
847 proxyServer.start();
848
849 settings = new Settings();
850 proxy = new Proxy();
851 proxy.setActive( true );
852 proxy.setHost( proxyServer.getHostName() );
853 proxy.setPort( proxyServer.getPort() );
854 proxy.setProtocol( "http" );
855 settings.addProxy( proxy );
856
857 mojo = lookupMojo( testPom );
858 setVariableValueToObject( mojo, "settings", settings );
859 setVariableValueToObject( mojo, "session", session );
860 mojo.execute();
861 readed = readFile( commandLine );
862 assertTrue( readed.contains( "-J-Dhttp.proxySet=true" ) );
863 assertTrue( readed.contains( "-J-Dhttp.proxyHost=" + proxyServer.getHostName() ) );
864 assertTrue( readed.contains( "-J-Dhttp.proxyPort=" + proxyServer.getPort() ) );
865
866 optionsContent = readFile( options );
867
868
869
870
871
872
873
874
875
876
877 }
878 finally
879 {
880 if ( proxyServer != null )
881 {
882 proxyServer.stop();
883 }
884 }
885
886
887 Map<String, String> authentications = new HashMap<>();
888 authentications.put( "foo", "bar" );
889 try
890 {
891 proxyServlet = new AuthAsyncProxyServlet( authentications );
892 proxyServer = new ProxyServer( proxyServlet );
893 proxyServer.start();
894
895 settings = new Settings();
896 proxy = new Proxy();
897 proxy.setActive( true );
898 proxy.setHost( proxyServer.getHostName() );
899 proxy.setPort( proxyServer.getPort() );
900 proxy.setProtocol( "http" );
901 proxy.setUsername( "foo" );
902 proxy.setPassword( "bar" );
903 settings.addProxy( proxy );
904
905 mojo = lookupMojo( testPom );
906 setVariableValueToObject( mojo, "settings", settings );
907 setVariableValueToObject( mojo, "session", session );
908 mojo.execute();
909 readed = readFile( commandLine );
910 assertTrue( readed.contains( "-J-Dhttp.proxySet=true" ) );
911 assertTrue( readed.contains( "-J-Dhttp.proxyHost=" + proxyServer.getHostName() ) );
912 assertTrue( readed.contains( "-J-Dhttp.proxyPort=" + proxyServer.getPort() ) );
913 assertTrue( readed.contains( "-J-Dhttp.proxyUser=\\\"foo\\\"" ) );
914 assertTrue( readed.contains( "-J-Dhttp.proxyPassword=\\\"bar\\\"" ) );
915
916 optionsContent = readFile( options );
917
918
919
920 }
921 finally
922 {
923 if ( proxyServer != null )
924 {
925 proxyServer.stop();
926 }
927 }
928 }
929
930
931
932
933
934
935 public void testValidateOptions()
936 throws Exception
937 {
938
939 File testPom = new File( unit, "validate-options-test/wrong-encoding-test-plugin-config.xml" );
940 JavadocReport mojo = lookupMojo( testPom );
941 try
942 {
943 mojo.execute();
944 assertTrue( "No wrong encoding catch", false );
945 }
946 catch ( MojoExecutionException e )
947 {
948 assertTrue( "No wrong encoding catch", e.getMessage().contains( "Unsupported option <encoding/>" ) );
949 }
950 testPom = new File( unit, "validate-options-test/wrong-docencoding-test-plugin-config.xml" );
951 mojo = lookupMojo( testPom );
952 try
953 {
954 mojo.execute();
955 assertTrue( "No wrong docencoding catch", false );
956 }
957 catch ( MojoExecutionException e )
958 {
959 assertTrue( "No wrong docencoding catch", e.getMessage().contains( "Unsupported option <docencoding/>" ) );
960 }
961 testPom = new File( unit, "validate-options-test/wrong-charset-test-plugin-config.xml" );
962 mojo = lookupMojo( testPom );
963 try
964 {
965 mojo.execute();
966 assertTrue( "No wrong charset catch", false );
967 }
968 catch ( MojoExecutionException e )
969 {
970 assertTrue( "No wrong charset catch", e.getMessage().contains( "Unsupported option <charset/>" ) );
971 }
972
973
974 testPom = new File( unit, "validate-options-test/wrong-locale-test-plugin-config.xml" );
975 mojo = lookupMojo( testPom );
976 try
977 {
978 mojo.execute();
979 assertTrue( "No wrong locale catch", false );
980 }
981 catch ( MojoExecutionException e )
982 {
983 assertTrue( "No wrong locale catch", e.getMessage().contains( "Unsupported option <locale/>" ) );
984 }
985 testPom = new File( unit, "validate-options-test/wrong-locale-with-variant-test-plugin-config.xml" );
986 mojo = lookupMojo( testPom );
987 mojo.execute();
988 assertTrue( "No wrong locale catch", true );
989
990
991 testPom = new File( unit, "validate-options-test/conflict-options-test-plugin-config.xml" );
992 mojo = lookupMojo( testPom );
993 try
994 {
995 mojo.execute();
996 assertTrue( "No conflict catch", false );
997 }
998 catch ( MojoExecutionException e )
999 {
1000 assertTrue( "No conflict catch", e.getMessage().contains( "Option <nohelp/> conflicts with <helpfile/>" ) );
1001 }
1002 }
1003
1004
1005
1006
1007
1008
1009 public void testTagletArtifacts()
1010 throws Exception
1011 {
1012 File testPom = new File( unit, "tagletArtifacts-test/tagletArtifacts-test-plugin-config.xml" );
1013 JavadocReport mojo = lookupMojo( testPom );
1014
1015 MavenSession session = spy( newMavenSession( mojo.project ) );
1016 ProjectBuildingRequest buildingRequest = mock( ProjectBuildingRequest.class );
1017 when( buildingRequest.getRemoteRepositories() ).thenReturn( mojo.project.getRemoteArtifactRepositories() );
1018 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
1019 MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
1020 repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
1021 when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
1022 when( session.getRepositorySession() ).thenReturn( repositorySession );
1023 LegacySupport legacySupport = lookup( LegacySupport.class );
1024 legacySupport.setSession( session );
1025 setVariableValueToObject( mojo, "session", session );
1026
1027 mojo.execute();
1028
1029 File optionsFile = new File( mojo.getOutputDirectory(), "options" );
1030 assertTrue( optionsFile.exists() );
1031 String options = readFile( optionsFile );
1032
1033 assertEquals( 20, StringUtils.countMatches( options, LINE_SEPARATOR + "-taglet" + LINE_SEPARATOR ) );
1034 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoAggregatorTypeTaglet" ) );
1035 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoComponentFieldTaglet" ) );
1036 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoConfiguratorTypeTaglet" ) );
1037 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoExecuteTypeTaglet" ) );
1038 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoExecutionStrategyTypeTaglet" ) );
1039 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoGoalTypeTaglet" ) );
1040 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoInheritByDefaultTypeTaglet" ) );
1041 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoInstantiationStrategyTypeTaglet" ) );
1042 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoParameterFieldTaglet" ) );
1043 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoPhaseTypeTaglet" ) );
1044 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoReadOnlyFieldTaglet" ) );
1045 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoRequiredFieldTaglet" ) );
1046 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoRequiresDependencyResolutionTypeTaglet" ) );
1047 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoRequiresDirectInvocationTypeTaglet" ) );
1048 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoRequiresOnLineTypeTaglet" ) );
1049 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoRequiresProjectTypeTaglet" ) );
1050 assertTrue( options.contains( "org.apache.maven.tools.plugin.javadoc.MojoRequiresReportsTypeTaglet" ) );
1051 assertTrue( options.contains( "org.codehaus.plexus.javadoc.PlexusConfigurationTaglet" ) );
1052 assertTrue( options.contains( "org.codehaus.plexus.javadoc.PlexusRequirementTaglet" ) );
1053 assertTrue( options.contains( "org.codehaus.plexus.javadoc.PlexusComponentTaglet" ) );
1054 }
1055
1056
1057
1058
1059
1060
1061 public void testStylesheetfile()
1062 throws Exception
1063 {
1064 File testPom = new File( unit, "stylesheetfile-test/pom.xml" );
1065
1066 JavadocReport mojo = lookupMojo( testPom );
1067 assertNotNull( mojo );
1068
1069 MavenSession session = spy( newMavenSession( mojo.project ) );
1070 ProjectBuildingRequest buildingRequest = mock( ProjectBuildingRequest.class );
1071 when( buildingRequest.getRemoteRepositories() ).thenReturn( mojo.project.getRemoteArtifactRepositories() );
1072 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
1073 MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
1074 repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
1075 when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
1076 when( session.getRepositorySession() ).thenReturn( repositorySession );
1077 LegacySupport legacySupport = lookup( LegacySupport.class );
1078 legacySupport.setSession( session );
1079 setVariableValueToObject( mojo, "session", session );
1080
1081 File apidocs = new File( getBasedir(), "target/test/unit/stylesheetfile-test/target/site/apidocs" );
1082
1083 File stylesheetfile = new File( apidocs, "stylesheet.css" );
1084 File options = new File( apidocs, "options" );
1085
1086
1087 setVariableValueToObject( mojo, "stylesheet", "javamaven" );
1088
1089 try
1090 {
1091 mojo.execute();
1092 assertTrue( false );
1093 }
1094 catch ( Exception e )
1095 {
1096 assertTrue( true );
1097 }
1098
1099
1100 setVariableValueToObject( mojo, "stylesheet", "java" );
1101 mojo.execute();
1102
1103 String content = readFile( stylesheetfile );
1104 assertTrue( content.contains( "/* Javadoc style sheet */" ) );
1105
1106 String optionsContent = readFile( options );
1107 assertFalse( optionsContent.contains( "-stylesheetfile" ) );
1108
1109
1110 setVariableValueToObject( mojo, "stylesheet", "maven" );
1111 mojo.execute();
1112
1113 content = readFile( stylesheetfile );
1114 assertTrue( content.contains( "/* Javadoc style sheet */" )
1115 && content.contains( "Licensed to the Apache Software Foundation (ASF) under one" ) );
1116
1117 optionsContent = readFile( options );
1118 assertTrue( optionsContent.contains( "-stylesheetfile" ) );
1119 assertTrue( optionsContent.contains( "'" + stylesheetfile.getAbsolutePath().replaceAll( "\\\\", "/" ) + "'" ) );
1120
1121
1122 setVariableValueToObject( mojo, "stylesheet", null );
1123 setVariableValueToObject( mojo, "stylesheetfile", "com/mycompany/app/javadoc/css/stylesheet.css" );
1124 mojo.execute();
1125
1126 content = readFile( stylesheetfile );
1127 assertTrue( content.contains( "/* Custom Javadoc style sheet in project */" ) );
1128
1129 optionsContent = readFile( options );
1130 assertTrue( optionsContent.contains( "-stylesheetfile" ) );
1131 File stylesheetResource =
1132 new File( unit, "stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css/stylesheet.css" );
1133 assertTrue( optionsContent.contains( "'" + stylesheetResource.getAbsolutePath().replaceAll( "\\\\", "/" )
1134 + "'" ) );
1135
1136
1137 setVariableValueToObject( mojo, "stylesheetfile", "com/mycompany/app/javadoc/css2/stylesheet.css" );
1138 mojo.execute();
1139
1140 content = readFile( stylesheetfile );
1141 assertTrue( content.contains( "/* Custom Javadoc style sheet in artefact */" ) );
1142
1143 optionsContent = readFile( options );
1144 assertTrue( optionsContent.contains( "-stylesheetfile" ) );
1145 assertTrue( optionsContent.contains( "'" + stylesheetfile.getAbsolutePath().replaceAll( "\\\\", "/" ) + "'" ) );
1146
1147
1148 File css =
1149 new File( unit, "stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css3/stylesheet.css" );
1150 setVariableValueToObject( mojo, "stylesheetfile", css.getAbsolutePath() );
1151 mojo.execute();
1152
1153 content = readFile( stylesheetfile );
1154 assertTrue( content.contains( "/* Custom Javadoc style sheet as file */" ) );
1155
1156 optionsContent = readFile( options );
1157 assertTrue( optionsContent.contains( "-stylesheetfile" ) );
1158 stylesheetResource =
1159 new File( unit, "stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css3/stylesheet.css" );
1160 assertTrue( optionsContent.contains( "'" + stylesheetResource.getAbsolutePath().replaceAll( "\\\\", "/" ) + "'" ) );
1161 }
1162
1163
1164
1165
1166
1167
1168 public void testHelpfile()
1169 throws Exception
1170 {
1171 File testPom = new File( unit, "helpfile-test/pom.xml" );
1172
1173 JavadocReport mojo = lookupMojo( testPom );
1174 assertNotNull( mojo );
1175
1176 MavenSession session = spy( newMavenSession( mojo.project ) );
1177 ProjectBuildingRequest buildingRequest = mock( ProjectBuildingRequest.class );
1178 when( buildingRequest.getRemoteRepositories() ).thenReturn( mojo.project.getRemoteArtifactRepositories() );
1179 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
1180 MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
1181 repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
1182 when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
1183 when( session.getRepositorySession() ).thenReturn( repositorySession );
1184 LegacySupport legacySupport = lookup( LegacySupport.class );
1185 legacySupport.setSession( session );
1186 setVariableValueToObject( mojo, "session", session );
1187
1188 File apidocs = new File( getBasedir(), "target/test/unit/helpfile-test/target/site/apidocs" );
1189
1190 File helpfile = new File( apidocs, "help-doc.html" );
1191 File options = new File( apidocs, "options" );
1192
1193
1194 mojo.execute();
1195
1196 String content = readFile( helpfile );
1197 assertTrue( content.contains( "<!-- Generated by javadoc" ) );
1198
1199 String optionsContent = readFile( options );
1200 assertFalse( optionsContent.contains( "-helpfile" ) );
1201
1202
1203 setVariableValueToObject( mojo, "helpfile", "com/mycompany/app/javadoc/helpfile/help-doc.html" );
1204
1205 setVariableValueToObject( mojo, "session", session );
1206
1207 mojo.execute();
1208
1209 content = readFile( helpfile );
1210 assertTrue( content.contains( "<!-- Help file from artefact -->" ) );
1211
1212 optionsContent = readFile( options );
1213 assertTrue( optionsContent.contains( "-helpfile" ) );
1214 File help = new File( apidocs, "help-doc.html" );
1215 assertTrue( optionsContent.contains( "'" + help.getAbsolutePath().replaceAll( "\\\\", "/" ) + "'" ) );
1216
1217
1218 setVariableValueToObject( mojo, "helpfile", "com/mycompany/app/javadoc/helpfile2/help-doc.html" );
1219 mojo.execute();
1220
1221 content = readFile( helpfile );
1222 assertTrue( content.contains( "<!-- Help file from file -->" ) );
1223
1224 optionsContent = readFile( options );
1225 assertTrue( optionsContent.contains( "-helpfile" ) );
1226 help = new File( unit, "helpfile-test/src/main/resources/com/mycompany/app/javadoc/helpfile2/help-doc.html" );
1227 assertTrue( optionsContent.contains( "'" + help.getAbsolutePath().replaceAll( "\\\\", "/" ) + "'" ) );
1228
1229
1230 help = new File( unit, "helpfile-test/src/main/resources/com/mycompany/app/javadoc/helpfile2/help-doc.html" );
1231 setVariableValueToObject( mojo, "helpfile", help.getAbsolutePath() );
1232 mojo.execute();
1233
1234 content = readFile( helpfile );
1235 assertTrue( content.contains( "<!-- Help file from file -->" ) );
1236
1237 optionsContent = readFile( options );
1238 assertTrue( optionsContent.contains( "-helpfile" ) );
1239 assertTrue( optionsContent.contains( "'" + help.getAbsolutePath().replaceAll( "\\\\", "/" ) + "'" ) );
1240 }
1241 }