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