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.tools.plugin.extractor.annotations.converter;
20  
21  import java.net.URI;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.block.JavadocBlockTagToHtmlConverter;
26  import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.block.SeeTagConverter;
27  import org.apache.maven.tools.plugin.javadoc.FullyQualifiedJavadocReference;
28  import org.apache.maven.tools.plugin.javadoc.JavadocReference;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  
33  class JavadocBlockTagsToXhtmlConverterTest {
34      private final ConverterContext context;
35      private final JavadocBlockTagsToXhtmlConverter converter;
36      private final JavadocInlineTagsToXhtmlConverter inlineTagsConverter;
37  
38      public JavadocBlockTagsToXhtmlConverterTest() {
39          URI baseUrl = URI.create("https://javadoc.example.com/");
40          context = new SimpleConverterContext("my.package", baseUrl);
41          inlineTagsConverter = JavadocInlineTagsToXhtmlConverterTest.createInlineTagConverter();
42          Map<String, JavadocBlockTagToHtmlConverter> blockTagConverters = new HashMap<>();
43          blockTagConverters.put("see", new SeeTagConverter());
44          converter = new JavadocBlockTagsToXhtmlConverter(inlineTagsConverter, blockTagConverters);
45      }
46  
47      @Test
48      void testSee() {
49          assertEquals(
50                  "<br /><strong>See also:</strong> \"Some reference\"",
51                  converter.convert("see", "\"Some reference\"", context));
52          assertEquals(
53                  ", <a href=\"example.com\">Example</a>",
54                  converter.convert("see", "<a href=\"example.com\">Example</a>", context));
55  
56          // new context should start again with "See also:"
57          ConverterContext context2 =
58                  new SimpleConverterContext("my.package", URI.create("https://javadoc.example.com/"));
59          assertEquals(
60                  "<br /><strong>See also:</strong> <a href=\"example.com\">Example</a>",
61                  converter.convert("see", "<a href=\"example.com\">Example</a>", context2));
62      }
63  
64      @Test
65      void testExceptionInTag() {
66          URI baseUrl = URI.create("https://javadoc.example.com/");
67          ConverterContext context = new SimpleConverterContext("my.package", baseUrl) {
68              @Override
69              public FullyQualifiedJavadocReference resolveReference(JavadocReference reference) {
70                  throw new IllegalArgumentException("Some exception");
71              }
72          };
73          Map<String, JavadocBlockTagToHtmlConverter> blockTagConverters = new HashMap<>();
74          blockTagConverters.put("example", new JavadocBlockTagToHtmlConverter() {
75              @Override
76              public String convert(String text, ConverterContext context) {
77                  throw new IllegalArgumentException("Some exception");
78              }
79          });
80          JavadocBlockTagsToXhtmlConverter converter =
81                  new JavadocBlockTagsToXhtmlConverter(inlineTagsConverter, blockTagConverters);
82          String test = "Class#field";
83          assertEquals(
84                  "@example Class#field<!-- error processing javadoc tag 'example': Some exception-->",
85                  converter.convert("example", test, context));
86      }
87  }