|
| 1 | +/* |
| 2 | + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. |
| 3 | + * Copyright (C) 2011, 2013-2021 The JavaParser Team. |
| 4 | + * |
| 5 | + * This file is part of JavaParser. |
| 6 | + * |
| 7 | + * JavaParser can be used either under the terms of |
| 8 | + * a) the GNU Lesser General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * b) the terms of the Apache License |
| 12 | + * |
| 13 | + * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | + * LICENCE.APACHE. Please refer to those files for details. |
| 15 | + * |
| 16 | + * JavaParser is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.github.javaparser.symbolsolver.javassistmodel; |
| 23 | + |
| 24 | +import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration; |
| 25 | +import com.github.javaparser.resolution.types.ResolvedReferenceType; |
| 26 | +import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; |
| 27 | +import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest; |
| 28 | +import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; |
| 29 | +import javassist.ClassPool; |
| 30 | +import javassist.CtClass; |
| 31 | +import javassist.NotFoundException; |
| 32 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.Arguments; |
| 35 | +import org.junit.jupiter.params.provider.ArgumentsProvider; |
| 36 | +import org.junit.jupiter.params.provider.ArgumentsSource; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.List; |
| 40 | +import java.util.stream.Collectors; |
| 41 | +import java.util.stream.Stream; |
| 42 | + |
| 43 | +import static java.util.Arrays.asList; |
| 44 | +import static java.util.Collections.singletonList; |
| 45 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 46 | + |
| 47 | +class JavassistTypeDeclarationAdapterTest extends AbstractResolutionTest { |
| 48 | + |
| 49 | + @ParameterizedTest(name = "Given {0} is expected {1}") |
| 50 | + @ArgumentsSource(GetAncestorsProvider.class) |
| 51 | + void testGetAncestors(String ctClass, List<String> expectedAncestors) throws NotFoundException, IOException { |
| 52 | + TypeSolver typeSolver = new ReflectionTypeSolver(false); |
| 53 | + CtClass clazz = new ClassPool(true).getCtClass(ctClass); |
| 54 | + |
| 55 | + ResolvedReferenceTypeDeclaration declaration = JavassistFactory.toTypeDeclaration(clazz, typeSolver); |
| 56 | + JavassistTypeDeclarationAdapter adapter = new JavassistTypeDeclarationAdapter(clazz, typeSolver, declaration); |
| 57 | + |
| 58 | + List<ResolvedReferenceType> resultAncestors = adapter.getAncestors(false); |
| 59 | + assertEquals(expectedAncestors, |
| 60 | + resultAncestors.stream().map(ResolvedReferenceType::getQualifiedName).collect(Collectors.toList())); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Class which provider arguments to be tested in {@link JavassistTypeDeclarationAdapterTest#testGetAncestors} |
| 65 | + */ |
| 66 | + static class GetAncestorsProvider implements ArgumentsProvider { |
| 67 | + @Override |
| 68 | + public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) { |
| 69 | + return Stream.of( |
| 70 | + // Node |
| 71 | + Arguments.of( |
| 72 | + "com.github.javaparser.ast.Node", |
| 73 | + asList( |
| 74 | + "java.lang.Object", |
| 75 | + "java.lang.Cloneable", |
| 76 | + "com.github.javaparser.HasParentNode", |
| 77 | + "com.github.javaparser.ast.visitor.Visitable", |
| 78 | + "com.github.javaparser.ast.nodeTypes.NodeWithRange", |
| 79 | + "com.github.javaparser.ast.nodeTypes.NodeWithTokenRange" |
| 80 | + ) |
| 81 | + ), |
| 82 | + // Expression |
| 83 | + Arguments.of( |
| 84 | + "com.github.javaparser.ast.expr.Expression", |
| 85 | + singletonList( |
| 86 | + "com.github.javaparser.ast.Node" |
| 87 | + ) |
| 88 | + ), |
| 89 | + // Annotation.class |
| 90 | + Arguments.of( |
| 91 | + "com.github.javaparser.ParseStart", |
| 92 | + singletonList( |
| 93 | + "java.lang.Object" |
| 94 | + ) |
| 95 | + ), |
| 96 | + // SlowTest Annotation |
| 97 | + Arguments.of( |
| 98 | + "com.github.javaparser.SlowTest", |
| 99 | + asList( |
| 100 | + "java.lang.Object", |
| 101 | + "java.lang.annotation.Annotation" |
| 102 | + ) |
| 103 | + ) |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments