Skip to content
Permalink
Browse files
GROOVY-10593: add new test cases and bugfix for static imports
  • Loading branch information
stalb authored and paulk-asert committed May 3, 2022
1 parent 7926dd8 commit 937a5bf4a6fc5d7a21cc8f56a1daf534bf073f57
Showing 5 changed files with 99 additions and 4 deletions.
@@ -18,6 +18,9 @@
*/
package org.codehaus.groovy.tools.groovydoc;

import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.JavaParser;

import org.codehaus.groovy.groovydoc.GroovyAnnotationRef;
import org.codehaus.groovy.groovydoc.GroovyClassDoc;
import org.codehaus.groovy.groovydoc.GroovyConstructorDoc;
@@ -38,6 +41,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
@@ -682,11 +686,24 @@ private static boolean isPrimitiveType(String name) {
private GroovyClassDoc resolveInternalClassDocFromImport(GroovyRootDoc rootDoc, String baseName) {
if (isPrimitiveType(baseName)) return null;
for (String importName : importedClassesAndPackages) {
if (importName.endsWith("/" + baseName)) {
GroovyClassDoc doc = ((SimpleGroovyRootDoc)rootDoc).classNamedExact(importName);
if (doc != null) return doc;
String targetClassName = null;
if (aliases.containsKey(baseName)) {
targetClassName = aliases.get(baseName);
} else if (importName.endsWith("/" + baseName)) {
targetClassName = importName;
} else if (importName.endsWith("/*")) {
GroovyClassDoc doc = ((SimpleGroovyRootDoc)rootDoc).classNamedExact(importName.substring(0, importName.length() - 1) + baseName);
targetClassName = importName.substring(0, importName.length() - 1) + baseName;
}
// need this for correct resolution of static imports
if (targetClassName != null){
GroovyClassDoc doc = null;
Optional<Name> name = new JavaParser().parseName(targetClassName.replace('/','.')).getResult();
String staticPart = "";
while (doc == null && name.isPresent()) {
doc = ((SimpleGroovyRootDoc)rootDoc).classNamedExact(name.get().asString().replace('.','/')+staticPart);
staticPart = "."+name.get().getIdentifier()+staticPart;
name = name.get().getQualifier();
}
if (doc != null) return doc;
}
}
@@ -877,6 +877,69 @@ public void testJavaExtendsStaticImportedClassWithNameWhichExistInDefaultPackage
assertEquals("Classes from imported packages should shadow classes from default packages", "StaticList.List", extendedClass.group(5));
}

public void testGroovyExtendsStaticImportedAliasesClassWithNameWhichExistInDefaultPackages() throws Exception {
// Groovy interface b.TestStatic imports a.StaticList.ListAlias as List and extends List.
// List should be recognized as a.StaticList.List and not java.util.List
htmlTool.add(Arrays.asList(
"org/codehaus/groovy/tools/groovydoc/testfiles/groovy_10593/a/StaticList.java",
"org/codehaus/groovy/tools/groovydoc/testfiles/groovy_10593/b/TestStaticAlias.groovy"
));

final MockOutputTool output = new MockOutputTool();
htmlTool.renderToOutput(output, MOCK_DIR);
final String testAdapterDoc = output.getText(MOCK_DIR + "/org/codehaus/groovy/tools/groovydoc/testfiles/groovy_10593/b/TestStaticAlias.html");

// TestStatic should etends a.StaticList.List
final Matcher extendedClass = Pattern.compile("extends\\s+<a[^>]*href='[^']*(((java/util/)|(a/StaticList\\.))List(Alias)?)\\.html'[^>]*>((StaticList\\.)?List(Alias)?)</a>").matcher(testAdapterDoc);

assertTrue("TestStatic interface should extends List", extendedClass.find());

assertEquals("Classes from imported packages should shadow classes from default packages", "a/StaticList.ListAlias", extendedClass.group(1));
assertEquals("Classes from imported packages should shadow classes from default packages", "StaticList.ListAlias", extendedClass.group(6));
}

public void testGroovyExtendsStaticStarImportedClassWithNameWhichExistInDefaultPackages() throws Exception {
// Groovy interface b.TestStaticStar imports a.StaticList.* and extends List.
// List should be recognized as a.StaticList.List and not java.util.List
htmlTool.add(Arrays.asList(
"org/codehaus/groovy/tools/groovydoc/testfiles/groovy_10593/a/StaticList.java",
"org/codehaus/groovy/tools/groovydoc/testfiles/groovy_10593/b/TestStaticStar.groovy"
));

final MockOutputTool output = new MockOutputTool();
htmlTool.renderToOutput(output, MOCK_DIR);
final String testAdapterDoc = output.getText(MOCK_DIR + "/org/codehaus/groovy/tools/groovydoc/testfiles/groovy_10593/b/TestStaticStar.html");

// TestStatic should etends a.StaticList.List
final Matcher extendedClass = Pattern.compile("extends\\s+<a[^>]*href='[^']*(((java/util/)|(a/StaticList\\.))List)\\.html'[^>]*>((StaticList\\.)?List)</a>").matcher(testAdapterDoc);

assertTrue("TestStaticStar interface should extends List", extendedClass.find());

assertEquals("Classes from imported packages should shadow classes from default packages", "a/StaticList.List", extendedClass.group(1));
assertEquals("Classes from imported packages should shadow classes from default packages", "StaticList.List", extendedClass.group(5));
}

public void testJavaExtendsStaticStarImportedClassWithNameWhichExistInDefaultPackages() throws Exception {
// Java interface b.TestStaticStar imports a.StaticList.* and extends List.
// List should be recognized as a.StaticList.List and not java.util.List
htmlTool.add(Arrays.asList(
"org/codehaus/groovy/tools/groovydoc/testfiles/groovy_10593/a/StaticList.java",
"org/codehaus/groovy/tools/groovydoc/testfiles/groovy_10593/b/TestStaticStar.groovy"
));

final MockOutputTool output = new MockOutputTool();
htmlTool.renderToOutput(output, MOCK_DIR);
final String testAdapterDoc = output.getText(MOCK_DIR + "/org/codehaus/groovy/tools/groovydoc/testfiles/groovy_10593/b/TestStaticStar.html");

// TestStatic should etends a.StaticList.List
final Matcher extendedClass = Pattern.compile("extends\\s+<a[^>]*href='[^']*(((java/util/)|(a/StaticList\\.))List)\\.html'[^>]*>((StaticList\\.)?List)</a>").matcher(testAdapterDoc);

assertTrue("TestStaticStar interface should extends List", extendedClass.find());

assertEquals("Classes from imported packages should shadow classes from default packages", "a/StaticList.List", extendedClass.group(1));
assertEquals("Classes from imported packages should shadow classes from default packages", "StaticList.List", extendedClass.group(5));
}

public void testClassDeclarationHeader() throws Exception {
final String base = "org/codehaus/groovy/tools/groovydoc/testfiles";
htmlTool.add(Arrays.asList(
@@ -0,0 +1,5 @@
package org.codehaus.groovy.tools.groovydoc.testfiles.groovy_10593.b;

import org.codehaus.groovy.tools.groovydoc.testfiles.groovy_10593.a.StaticList.ListAlias as List;

public interface TestStaticAlias extends List {}
@@ -0,0 +1,5 @@
package org.codehaus.groovy.tools.groovydoc.testfiles.groovy_10593.b;

import org.codehaus.groovy.tools.groovydoc.testfiles.groovy_10593.a.StaticList.*;

public interface TestStaticStar extends List {}
@@ -0,0 +1,5 @@
package org.codehaus.groovy.tools.groovydoc.testfiles.groovy_10593.b;

import org.codehaus.groovy.tools.groovydoc.testfiles.groovy_10593.a.StaticList.*;

public interface TestStaticStar extends List {}

0 comments on commit 937a5bf

Please sign in to comment.