View Javadoc
1   package org.apache.maven.plugins.javadoc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.assertj.core.api.Assertions.assertThat;
23  import static org.mockito.ArgumentMatchers.anyString;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.never;
26  import static org.mockito.Mockito.times;
27  import static org.mockito.Mockito.verify;
28  import static org.mockito.Mockito.when;
29  
30  import java.io.File;
31  
32  import org.apache.maven.plugin.logging.Log;
33  
34  import org.junit.Before;
35  import org.junit.Test;
36  
37  public class AbstractJavadocMojoTest
38  {
39      AbstractJavadocMojo mojo;
40  
41      @Before
42      public void setUp()
43      {
44          mojo = new AbstractJavadocMojo()
45          {
46              @Override
47              public void doExecute()
48              {
49              }
50          };
51      }
52  
53      @Test
54      public void testMJAVADOC432_DetectLinksMessages()
55      {
56          Log log = mock( Log.class );
57          when( log.isErrorEnabled() ).thenReturn( true );
58          mojo.setLog( log );
59          mojo.outputDirectory = new File( "target/test-classes" );
60  
61          // first continues after warning, next exits with warning
62          assertThat( mojo.isValidJavadocLink( new File( "pom.xml" ).getPath(), true ) ).isFalse();
63          assertThat( mojo.isValidJavadocLink( "file://%%", true ) ).isFalse();
64          assertThat( mojo.isValidJavadocLink( new File( "pom.xml" ).toURI().toString(), true ) ).isFalse();
65          verify( log, times( 4 ) ).warn( anyString() );
66          verify( log, never() ).error( anyString() );
67  
68          // first continues after error, next exits with error
69          assertThat( mojo.isValidJavadocLink( new File( "pom.xml" ).getPath(), false ) ).isFalse();
70          assertThat( mojo.isValidJavadocLink( "file://%%", false ) ).isFalse();
71          assertThat( mojo.isValidJavadocLink( new File( "pom.xml" ).toURI().toString(), false ) ).isFalse();
72          verify( log, times( 4 ) ).error( anyString() );
73          verify( log, times( 4 ) ).warn( anyString() ); // no extra warnings
74      }
75  
76      @Test
77      public void testMJAVADOC527_DetectLinksRecursion()
78      {
79          Log log = mock( Log.class );
80          when( log.isErrorEnabled() ).thenReturn( true );
81          mojo.setLog( log );
82          mojo.outputDirectory = new File( "target/test-classes" );
83  
84          assertThat( mojo.isValidJavadocLink( "http://javamail.java.net/mailapi/apidocs", false ) ).isFalse();
85          assertThat(
86                  mojo.isValidJavadocLink( "http://commons.apache.org/proper/commons-lang/apidocs", false ) ).isTrue();
87      }
88  }