View Javadoc
1   package org.apache.maven.tools.plugin.extractor.annotations.converter;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import java.io.File;
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  import java.util.Collections;
25  import java.util.Objects;
26  
27  import com.thoughtworks.qdox.JavaProjectBuilder;
28  import com.thoughtworks.qdox.model.JavaClass;
29  import org.apache.maven.tools.plugin.extractor.annotations.converter.test.CurrentClass;
30  import org.apache.maven.tools.plugin.extractor.annotations.converter.test.OtherClass;
31  import org.apache.maven.tools.plugin.extractor.annotations.datamodel.MojoAnnotationContent;
32  import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotatedClass;
33  import org.apache.maven.tools.plugin.javadoc.FullyQualifiedJavadocReference;
34  import org.apache.maven.tools.plugin.javadoc.JavadocLinkGenerator;
35  import org.apache.maven.tools.plugin.javadoc.JavadocReference;
36  import org.apache.maven.tools.plugin.javadoc.FullyQualifiedJavadocReference.MemberType;
37  import org.junit.jupiter.api.Test;
38  
39  import static org.junit.jupiter.api.Assertions.assertEquals;
40  import static org.junit.jupiter.api.Assertions.assertFalse;
41  import static org.junit.jupiter.api.Assertions.assertThrows;
42  import static org.junit.jupiter.api.Assertions.assertTrue;
43  
44  class JavaClassConverterContextTest
45  {
46  
47      private ConverterContext context;
48  
49      private final String currentPackageName;
50  
51      private final JavaProjectBuilder builder;
52  
53      private final JavaClass contextClass;
54  
55      private JavadocLinkGenerator linkGenerator;
56  
57      private URI javadocBaseUri;
58      
59      public JavaClassConverterContextTest()
60          throws URISyntaxException
61      {
62          builder = new JavaProjectBuilder();
63          builder.addSourceFolder( new File("src/test/java") );
64          
65          contextClass = builder.getClassByName( CurrentClass.class.getName() );
66          currentPackageName = contextClass.getPackageName();
67          javadocBaseUri = new URI("http://localhost/apidocs");
68          linkGenerator = new JavadocLinkGenerator( javadocBaseUri, "11" );
69          context = new JavaClassConverterContext( contextClass, builder, Collections.emptyMap(), linkGenerator, 10 );
70      }
71  
72      @Test
73      void testResolveReference()
74          throws URISyntaxException
75      {
76          // test fully qualified unresolvable reference
77          assertThrows( IllegalArgumentException.class,
78                        () -> context.resolveReference( JavadocReference.parse( "my.package.InvalidClass" ) ) );
79          
80          // test unresolvable reference
81          assertThrows( IllegalArgumentException.class,
82                        () -> context.resolveReference( JavadocReference.parse( "InvalidClass" ) ) );
83          
84          // test resolvable reference
85          assertEquals( new FullyQualifiedJavadocReference( currentPackageName,
86                                                            "OtherClass", false ),
87                        context.resolveReference( ( JavadocReference.parse( OtherClass.class.getName() ) ) ) );
88          
89          // already fully resolved class
90          assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "OtherClass", false ),
91                                                          context.resolveReference( ( JavadocReference.parse( OtherClass.class.getName() ) ) ) );
92          // already fully resolved package
93          assertEquals( new FullyQualifiedJavadocReference( currentPackageName, false ),
94                                                          context.resolveReference( ( JavadocReference.parse( currentPackageName ) ) ) );
95          
96          // Class from java's standard import "java.lang"
97          assertEquals( new FullyQualifiedJavadocReference( "java.lang", "String", true ),
98                        context.resolveReference( JavadocReference.parse( "String" ) ) );
99          
100         // nested class from import
101         assertEquals( new FullyQualifiedJavadocReference( "org.apache.maven.tools.plugin.extractor.annotations.converter.test.other",
102                                                           "OtherClassOtherPackage.EmbeddedEnum", false ),
103                       context.resolveReference( ( JavadocReference.parse( "OtherClassOtherPackage.EmbeddedEnum" ) ) ) );
104     }
105 
106     @Test
107     void testResolveReferenceWithMembers()
108     {
109         // field
110         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "CurrentClass", "field1", MemberType.FIELD, false ),
111                       context.resolveReference( ( JavadocReference.parse( "#field1" ) ) ) );
112         // field from super class
113         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "SuperClass", "superField1", MemberType.FIELD, false ),
114                       context.resolveReference( ( JavadocReference.parse( "#superField1" ) ) ) );
115         // method
116         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "CurrentClass", "noParamMethod()", MemberType.METHOD, false ),
117                                                         context.resolveReference( ( JavadocReference.parse( "#noParamMethod()" ) ) ) );
118         // method without parentheses
119         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "CurrentClass", "noParamMethod()", MemberType.METHOD, false ),
120                                                         context.resolveReference( ( JavadocReference.parse( "#noParamMethod" ) ) ) );
121        
122         // method with unresolved java.lang argument
123         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "CurrentClass", "simpleParamMethod(java.lang.Integer)", MemberType.METHOD, false ),
124                                                           context.resolveReference( ( JavadocReference.parse( "#simpleParamMethod(Integer)" ) ) ) );
125         
126         // method with unresolved java.lang argument with name
127         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "CurrentClass", "simpleParamMethod(java.lang.Integer)", MemberType.METHOD, false ),
128                                                          context.resolveReference( ( JavadocReference.parse( "#simpleParamMethod(Integer value)" ) ) ) );
129 
130         // method with primitive arguments
131         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "CurrentClass", "complexParamMethod(int,org.apache.maven.tools.plugin.extractor.annotations.converter.test.other.OtherClassOtherPackage.EmbeddedEnum)", MemberType.METHOD, false ),
132                                                          context.resolveReference( ( JavadocReference.parse( "#complexParamMethod(int value1, OtherClassOtherPackage.EmbeddedEnum value2)" ) ) ) );
133         // method with array arguments
134         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "CurrentClass", "arrayParamMethod(int[],java.lang.String[][][])", MemberType.METHOD, false ),
135                 context.resolveReference( ( JavadocReference.parse( "#arrayParamMethod(int[], String[][][])" ) ) ) );
136         
137         // method with generic arguments
138         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "CurrentClass", "genericsParamMethod(java.util.Collection,java.util.function.BiConsumer)", MemberType.METHOD, false ),
139                 context.resolveReference( ( JavadocReference.parse( "#genericsParamMethod(Collection something, java.util.function.BiConsumer function)" ) ) ) );
140         
141         // method with unresolvable type
142         assertThrows( IllegalArgumentException.class,
143                 () -> context.resolveReference( ( JavadocReference.parse( "#genericsParamMethod(Collection something, BiConsumer function)" ) ) ) );
144         // constructor
145         assertEquals( new FullyQualifiedJavadocReference( currentPackageName, "CurrentClass", "CurrentClass()", MemberType.CONSTRUCTOR, false ),
146                                                         context.resolveReference( ( JavadocReference.parse( "#CurrentClass()" ) ) ) );
147 
148     }
149 
150     @Test
151     void testGetUrl()
152         throws URISyntaxException
153     {
154         MojoAnnotationContent mojoAnnotationContent = new MojoAnnotationContent();
155         mojoAnnotationContent.name("other-goal");
156         MojoAnnotatedClass mojoAnnotatedClass = new MojoAnnotatedClass().setMojo( mojoAnnotationContent );
157         context = new JavaClassConverterContext( contextClass, builder, Collections.singletonMap( "org.apache.maven.tools.plugin.extractor.annotations.converter.test.OtherClass", mojoAnnotatedClass ), linkGenerator, 10 );
158         // TODO: link to current class without member?
159         //assertEquals( new URI( "" ),
160         //              context.getUrl( new FullyQualifiedJavadocReference( "org.apache.maven.tools.plugin.extractor.annotations.converter",
161         //                                                                  "JavaClassConverterContextTest" ) ) );
162 
163         // field reference not leaving context
164         assertEquals( new URI( null, null, "field1" ),
165                       context.getUrl( new FullyQualifiedJavadocReference( currentPackageName,
166                                                                           "CurrentClass", "field1", MemberType.FIELD, false ) ) );
167 
168         // field reference in another class
169         assertEquals( javadocBaseUri.resolve( new URI( null, "org/apache/maven/tools/plugin/extractor/annotations/converter/test/other/OtherClassOtherPackage.html", "field1" ) ),
170                       context.getUrl( new FullyQualifiedJavadocReference( "org.apache.maven.tools.plugin.extractor.annotations.converter.test.other",
171                                                                           "OtherClassOtherPackage", "field1", MemberType.FIELD, false ) ) );
172 
173         // field reference in another mojo
174         assertEquals( new URI( null, "./other-goal-mojo.html", "field1" ),
175                       context.getUrl( new FullyQualifiedJavadocReference( currentPackageName,
176                                                                           "OtherClass", "field1", MemberType.FIELD, false ) ) );
177         
178         // method reference in current context need to point to regular javadoc as mojo documentation does not include methods
179         assertEquals( javadocBaseUri.resolve( new URI( null, "org/apache/maven/tools/plugin/extractor/annotations/converter/test/CurrentClass.html", "noParamMethod()" ) ),
180                       context.getUrl( new FullyQualifiedJavadocReference( currentPackageName,
181                                                                           "CurrentClass", "noParamMethod()", MemberType.METHOD, false ) ) );
182         
183         // method reference with arguments
184 
185         
186         // constructor reference in current context need to point to regular javadoc as mojo documentation does not include constructors
187         assertEquals( javadocBaseUri.resolve( new URI( null, "org/apache/maven/tools/plugin/extractor/annotations/converter/test/CurrentClass.html", "CurrentClass()" ) ),
188                       context.getUrl( new FullyQualifiedJavadocReference( "org.apache.maven.tools.plugin.extractor.annotations.converter.test", 
189                                                                           "CurrentClass", "CurrentClass()", MemberType.METHOD, false ) ) );
190 
191         // package reference
192         assertEquals( javadocBaseUri.resolve( new URI( null, "org/apache/maven/tools/plugin/extractor/annotations/converter/test/package-summary.html", null ) ),
193                       context.getUrl( new FullyQualifiedJavadocReference( "org.apache.maven.tools.plugin.extractor.annotations.converter.test", false ) ) );
194 
195     }
196 
197     @Test
198     void testClassContext()
199     {
200         assertTrue( context.isReferencedBy( new FullyQualifiedJavadocReference( currentPackageName,  "CurrentClass", false ) ) );
201         assertTrue( context.isReferencedBy( new FullyQualifiedJavadocReference( currentPackageName,  "SuperClass", false ) ) );
202         assertTrue( context.isReferencedBy( new FullyQualifiedJavadocReference( currentPackageName,  "SuperClass", "superField1", MemberType.FIELD, false ) ) );
203         assertFalse( context.isReferencedBy( new FullyQualifiedJavadocReference( currentPackageName,  "OtherClass", false ) ) );
204         assertEquals( currentPackageName, context.getPackageName() );
205         assertEquals( "src/test/java/org/apache/maven/tools/plugin/extractor/annotations/converter/test/CurrentClass.java:10",
206                       context.getLocation() );
207     }
208     
209     @Test
210     void testGetStaticFieldValue()
211     {
212         assertEquals( "\"STATIC 1\"", context.getStaticFieldValue( new FullyQualifiedJavadocReference( currentPackageName,  "OtherClass", "STATIC_1", MemberType.FIELD, false ) ) );
213         assertEquals( "\"STATIC 2\"", context.getStaticFieldValue( new FullyQualifiedJavadocReference( currentPackageName,  "OtherClass", "STATIC_2", MemberType.FIELD, false ) ) );
214         // although not explicitly stated, never used for value javadoc tag, as this only supports string constants
215         assertEquals( "3l", context.getStaticFieldValue( new FullyQualifiedJavadocReference( currentPackageName,  "OtherClass", "STATIC_3", MemberType.FIELD, false ) ) );
216         FullyQualifiedJavadocReference reference = new FullyQualifiedJavadocReference( currentPackageName,  "OtherClass", "field1", MemberType.FIELD, false );
217         assertThrows( IllegalArgumentException.class, () -> context.getStaticFieldValue( reference ) );
218     }
219 }