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.inline.CodeTagConverter;
26  import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.inline.DocRootTagConverter;
27  import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.inline.JavadocInlineTagToHtmlConverter;
28  import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.inline.LinkPlainTagToHtmlConverter;
29  import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.inline.LinkTagToHtmlConverter;
30  import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.inline.LiteralTagToHtmlConverter;
31  import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.inline.ValueTagConverter;
32  import org.apache.maven.tools.plugin.javadoc.FullyQualifiedJavadocReference;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  
37  class JavadocInlineTagsToXhtmlConverterTest {
38      private final ConverterContext context;
39      private final JavadocInlineTagsToXhtmlConverter converter;
40  
41      public JavadocInlineTagsToXhtmlConverterTest() {
42          URI baseUrl = URI.create("https://javadoc.example.com/");
43          context = new SimpleConverterContext("my.package", baseUrl);
44          converter = createInlineTagConverter();
45      }
46  
47      public static JavadocInlineTagsToXhtmlConverter createInlineTagConverter() {
48          Map<String, JavadocInlineTagToHtmlConverter> converters = new HashMap<>();
49          converters.put("link", new LinkTagToHtmlConverter());
50          converters.put("linkplain", new LinkPlainTagToHtmlConverter());
51          converters.put("literal", new LiteralTagToHtmlConverter());
52          converters.put("code", new CodeTagConverter());
53          converters.put("value", new ValueTagConverter());
54          converters.put("docRoot", new DocRootTagConverter());
55          return new JavadocInlineTagsToXhtmlConverter(converters);
56      }
57  
58      @Test
59      void testComplexJavadoc() {
60          String test = "This is a {@code <>code} and {@link package.Class#member} test {@code code2}\nsome other text";
61          assertEquals(
62                  "This is a <code>&lt;&gt;code</code> and <a href=\"https://javadoc.example.com/package/Class.html#member\"><code>package.Class.member</code></a> test <code>code2</code> some other text",
63                  converter.convert(test, context));
64      }
65  
66      @Test
67      void testCode() {
68          String test = "{@code text}";
69          assertEquals("<code>text</code>", converter.convert(test, context));
70  
71          test = "{@code <A&B>}";
72          assertEquals("<code>&lt;A&amp;B&gt;</code>", converter.convert(test, context));
73  
74          test = "Something{@code \n<A&B>\n   }";
75          assertEquals("Something<code> &lt;A&amp;B&gt; </code>", converter.convert(test, context));
76  
77          test = "{@code\ntest}";
78          assertEquals("<code>test</code>", converter.convert(test, context));
79      }
80  
81      @Test
82      void testLiteral() {
83          String test = "{@literal text}";
84          assertEquals("text", converter.convert(test, context));
85  
86          test = "{@literal text}  {@literal text}";
87          assertEquals("text text", converter.convert(test, context));
88  
89          test = "{@literal <A&B>}";
90          assertEquals("&lt;A&amp;B&gt;", converter.convert(test, context));
91      }
92  
93      @Test
94      void testLink() {
95          String test = "{@link Class}";
96          assertEquals(
97                  "<a href=\"https://javadoc.example.com/my/package/Class.html\"><code>Class</code></a>",
98                  converter.convert(test, context));
99  
100         test = "{@link MyClass#field1}";
101         assertEquals(
102                 "<a href=\"https://javadoc.example.com/my/package/MyClass.html#field1\"><code>MyClass.field1</code></a>",
103                 converter.convert(test, context));
104     }
105 
106     @Test
107     void testLinkplain() {
108         String test = "{@linkplain Class}";
109         assertEquals(
110                 "<a href=\"https://javadoc.example.com/my/package/Class.html\">Class</a>",
111                 converter.convert(test, context));
112 
113         test = "{@linkplain #field}";
114         assertEquals(
115                 "<a href=\"https://javadoc.example.com/my/package/package-summary.html#field\">field</a>",
116                 converter.convert(test, context));
117 
118         test = "{@linkplain Class#field}";
119         assertEquals(
120                 "<a href=\"https://javadoc.example.com/my/package/Class.html#field\">Class.field</a>",
121                 converter.convert(test, context));
122 
123         test = "{@linkplain #method()}";
124         assertEquals(
125                 "<a href=\"https://javadoc.example.com/my/package/package-summary.html#method()\">method()</a>",
126                 converter.convert(test, context));
127 
128         test = "{@linkplain #method(Object arg)}";
129         assertEquals(
130                 "<a href=\"https://javadoc.example.com/my/package/package-summary.html#method(Object)\">method(Object)</a>",
131                 converter.convert(test, context));
132 
133         test = "{@linkplain #method(Object, String)}";
134         assertEquals(
135                 "<a href=\"https://javadoc.example.com/my/package/package-summary.html#method(Object,String)\">method(Object,String)</a>",
136                 converter.convert(test, context));
137 
138         test = "{@linkplain #method(Object, String) label}";
139         assertEquals(
140                 "<a href=\"https://javadoc.example.com/my/package/package-summary.html#method(Object,String)\">label</a>",
141                 converter.convert(test, context));
142 
143         test = "{@linkplain Class#method(Object, String)}";
144         assertEquals(
145                 "<a href=\"https://javadoc.example.com/my/package/Class.html#method(Object,String)\">Class.method(Object,String)</a>",
146                 converter.convert(test, context));
147 
148         test = "{@linkplain Class#method(Object, String) label}";
149         assertEquals(
150                 "<a href=\"https://javadoc.example.com/my/package/Class.html#method(Object,String)\">label</a>",
151                 converter.convert(test, context));
152     }
153 
154     @Test
155     void testValue() {
156         String test = "{@value Class#STATIC_FIELD}";
157         assertEquals("some field value", converter.convert(test, context));
158     }
159 
160     @Test
161     void testDocroot() {
162         String test = "Some <a href=\"{@docRoot}/test.html\">link</a>";
163         assertEquals(
164                 "Some <a href=\"https://javadoc.example.com/test.html\">link</a>", converter.convert(test, context));
165     }
166 
167     @Test
168     void testMultipleTags() {
169         String test = "Some code {@code myCode} and link {@linkplain Class}. Something";
170         assertEquals(
171                 "Some code <code>myCode</code> and link <a href=\"https://javadoc.example.com/my/package/Class.html\">Class</a>. Something",
172                 converter.convert(test, context));
173     }
174 
175     @Test
176     void testExceptionInTag() {
177         URI baseUrl = URI.create("https://javadoc.example.com/");
178         ConverterContext context = new SimpleConverterContext("my.package", baseUrl) {
179 
180             @Override
181             public String getStaticFieldValue(FullyQualifiedJavadocReference reference) {
182                 throw new IllegalArgumentException("Some exception");
183             }
184         };
185         String test = "{@value Class#STATIC_FIELD}";
186         assertEquals(
187                 "{@value Class#STATIC_FIELD}<!-- error processing javadoc tag 'value': Some exception -->",
188                 converter.convert(test, context));
189     }
190 
191     @Test
192     void testUnknownTag() {
193         String test = "{@unknown text}";
194         assertEquals("{@unknown text}<!-- unsupported tag 'unknown' -->", converter.convert(test, context));
195     }
196 }