Home | History | Annotate | Download | only in contexts
      1 /*
      2  * Copyright 2016 Federico Tomassetti
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.github.javaparser.symbolsolver.resolution.javaparser.contexts;
     18 
     19 import com.github.javaparser.ParseException;
     20 import com.github.javaparser.ast.CompilationUnit;
     21 import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
     22 import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
     23 import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
     24 import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
     25 import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
     26 import com.github.javaparser.resolution.types.ResolvedType;
     27 import com.github.javaparser.symbolsolver.core.resolution.Context;
     28 import com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext;
     29 import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
     30 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
     31 import com.github.javaparser.symbolsolver.model.resolution.Value;
     32 import com.github.javaparser.symbolsolver.model.typesystem.NullType;
     33 import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
     34 import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
     35 import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
     36 import com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver;
     37 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
     38 import com.google.common.collect.ImmutableList;
     39 import org.junit.Before;
     40 import org.junit.Test;
     41 
     42 import java.io.IOException;
     43 import java.util.Optional;
     44 
     45 import static org.junit.Assert.assertEquals;
     46 import static org.junit.Assert.assertTrue;
     47 import static org.mockito.Mockito.mock;
     48 import static org.mockito.Mockito.when;
     49 
     50 /**
     51  * @author Federico Tomassetti
     52  */
     53 public class CompilationUnitContextResolutionTest extends AbstractResolutionTest {
     54 
     55     private TypeSolver typeSolver;
     56 
     57     @Before
     58     public void setup() {
     59         typeSolver = new ReflectionTypeSolver();
     60     }
     61 
     62     @Test
     63     public void getParent() {
     64         CompilationUnit cu = parseSample("ClassWithTypeVariables");
     65         Context context = new CompilationUnitContext(cu, typeSolver);
     66 
     67         assertTrue(null == context.getParent());
     68     }
     69 
     70     @Test
     71     public void solveExistingGenericType() {
     72         CompilationUnit cu = parseSample("ClassWithTypeVariables");
     73         Context context = new CompilationUnitContext(cu, typeSolver);
     74 
     75         Optional<ResolvedType> a = context.solveGenericType("A", new MemoryTypeSolver());
     76         Optional<ResolvedType> b = context.solveGenericType("B", new MemoryTypeSolver());
     77         Optional<ResolvedType> c = context.solveGenericType("C", new MemoryTypeSolver());
     78 
     79         assertEquals(false, a.isPresent());
     80         assertEquals(false, b.isPresent());
     81         assertEquals(false, c.isPresent());
     82     }
     83 
     84     @Test
     85     public void solveUnexistingGenericType() {
     86         CompilationUnit cu = parseSample("ClassWithTypeVariables");
     87         Context context = new CompilationUnitContext(cu, typeSolver);
     88 
     89         Optional<ResolvedType> d = context.solveGenericType("D", new MemoryTypeSolver());
     90 
     91         assertEquals(false, d.isPresent());
     92     }
     93 
     94     @Test
     95     public void solveSymbolReferringToStaticallyImportedValue() throws ParseException, IOException {
     96         CompilationUnit cu = parseSample("CompilationUnitSymbols");
     97         Context context = new CompilationUnitContext(cu, typeSolver);
     98 
     99         CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    100         typeSolver.add(new ReflectionTypeSolver());
    101         typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    102         SymbolReference<? extends ResolvedValueDeclaration> ref = context.solveSymbol("out", typeSolver);
    103         assertEquals(true, ref.isSolved());
    104         assertEquals("java.io.PrintStream", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
    105     }
    106 
    107     @Test
    108     public void solveSymbolReferringToStaticallyImportedUsingAsteriskValue() throws ParseException, IOException {
    109         CompilationUnit cu = parseSample("CompilationUnitSymbols");
    110         Context context = new CompilationUnitContext(cu, typeSolver);
    111 
    112         CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    113         typeSolver.add(new ReflectionTypeSolver());
    114         typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    115         SymbolReference<? extends ResolvedValueDeclaration> ref = context.solveSymbol("err", typeSolver);
    116         assertEquals(true, ref.isSolved());
    117         assertEquals("java.io.PrintStream", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
    118     }
    119 
    120     @Test
    121     public void solveSymbolReferringToStaticField() throws ParseException, IOException {
    122         CompilationUnit cu = parseSample("CompilationUnitSymbols");
    123         Context context = new CompilationUnitContext(cu, typeSolver);
    124 
    125         SymbolReference<? extends ResolvedValueDeclaration> ref = context.solveSymbol("java.lang.System.out", new ReflectionTypeSolver());
    126         assertEquals(true, ref.isSolved());
    127         assertEquals("java.io.PrintStream", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
    128     }
    129 
    130     @Test
    131     public void solveSymbolAsValueReferringToStaticallyImportedValue() throws ParseException, IOException {
    132         CompilationUnit cu = parseSample("CompilationUnitSymbols");
    133         Context context = new CompilationUnitContext(cu, typeSolver);
    134 
    135         CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    136         typeSolver.add(new ReflectionTypeSolver());
    137         typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    138         Optional<Value> ref = context.solveSymbolAsValue("out", typeSolver);
    139         assertEquals(true, ref.isPresent());
    140         assertEquals("java.io.PrintStream", ref.get().getType().describe());
    141     }
    142 
    143     @Test
    144     public void solveSymbolAsValueReferringToStaticallyImportedUsingAsteriskValue() throws ParseException, IOException {
    145         CompilationUnit cu = parseSample("CompilationUnitSymbols");
    146         Context context = new CompilationUnitContext(cu, typeSolver);
    147 
    148         CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    149         typeSolver.add(new ReflectionTypeSolver());
    150         typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    151         Optional<Value> ref = context.solveSymbolAsValue("err", typeSolver);
    152         assertEquals(true, ref.isPresent());
    153         assertEquals("java.io.PrintStream", ref.get().getType().describe());
    154     }
    155 
    156     @Test
    157     public void solveSymbolAsValueReferringToStaticField() throws ParseException, IOException {
    158         CompilationUnit cu = parseSample("CompilationUnitSymbols");
    159         Context context = new CompilationUnitContext(cu, typeSolver);
    160 
    161         Optional<Value> ref = context.solveSymbolAsValue("java.lang.System.out", new ReflectionTypeSolver());
    162         assertEquals(true, ref.isPresent());
    163         assertEquals("java.io.PrintStream", ref.get().getType().describe());
    164     }
    165 
    166     @Test
    167     public void solveTypeInSamePackage() {
    168         CompilationUnit cu = parseSample("CompilationUnitWithImports");
    169         Context context = new CompilationUnitContext(cu, typeSolver);
    170 
    171         ResolvedReferenceTypeDeclaration otherClass = mock(ResolvedReferenceTypeDeclaration.class);
    172         when(otherClass.getQualifiedName()).thenReturn("com.foo.OtherClassInSamePackage");
    173         MemoryTypeSolver memoryTypeSolver = new MemoryTypeSolver();
    174         memoryTypeSolver.addDeclaration("com.foo.OtherClassInSamePackage", otherClass);
    175 
    176         SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("OtherClassInSamePackage", memoryTypeSolver);
    177         assertEquals(true, ref.isSolved());
    178         assertEquals("com.foo.OtherClassInSamePackage", ref.getCorrespondingDeclaration().getQualifiedName());
    179     }
    180 
    181     @Test
    182     public void solveTypeImported() throws ParseException, IOException {
    183         CompilationUnit cu = parseSample("CompilationUnitWithImports");
    184         Context context = new CompilationUnitContext(cu, typeSolver);
    185 
    186         SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("Assert", new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    187         assertEquals(true, ref.isSolved());
    188         assertEquals("org.junit.Assert", ref.getCorrespondingDeclaration().getQualifiedName());
    189     }
    190 
    191     @Test
    192     public void solveTypeNotImported() throws ParseException, IOException {
    193         CompilationUnit cu = parseSample("CompilationUnitWithImports");
    194         Context context = new CompilationUnitContext(cu, typeSolver);
    195 
    196         SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("org.junit.Assume", new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    197         assertEquals(true, ref.isSolved());
    198         assertEquals("org.junit.Assume", ref.getCorrespondingDeclaration().getQualifiedName());
    199     }
    200 
    201     @Test
    202     public void solveMethodStaticallyImportedWithAsterisk() throws ParseException, IOException {
    203         CompilationUnit cu = parseSample("CompilationUnitWithImports");
    204         Context context = new CompilationUnitContext(cu, typeSolver);
    205 
    206         CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    207         typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    208         typeSolver.add(new ReflectionTypeSolver());
    209 
    210         SymbolReference<ResolvedMethodDeclaration> ref = context.solveMethod("assertFalse", ImmutableList.of(ResolvedPrimitiveType.BOOLEAN), false, typeSolver);
    211         assertEquals(true, ref.isSolved());
    212         assertEquals("assertFalse", ref.getCorrespondingDeclaration().getName());
    213         assertEquals(1, ref.getCorrespondingDeclaration().getNumberOfParams());
    214         assertEquals("boolean", ref.getCorrespondingDeclaration().getParam(0).getType().describe());
    215         assertEquals(true, ref.getCorrespondingDeclaration().getParam(0).getType().isPrimitive());
    216     }
    217 
    218     @Test
    219     public void solveMethodStaticallyImportedWithoutAsterisk() throws ParseException, IOException {
    220         CompilationUnit cu = parseSample("CompilationUnitSymbols");
    221         Context context = new CompilationUnitContext(cu, typeSolver);
    222 
    223         CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    224         typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    225         typeSolver.add(new ReflectionTypeSolver());
    226 
    227         SymbolReference<ResolvedMethodDeclaration> ref = context.solveMethod("assertEquals", ImmutableList.of(NullType.INSTANCE, NullType.INSTANCE), false, typeSolver);
    228         assertEquals(true, ref.isSolved());
    229         assertEquals("assertEquals", ref.getCorrespondingDeclaration().getName());
    230         assertEquals(2, ref.getCorrespondingDeclaration().getNumberOfParams());
    231         assertEquals("java.lang.Object", ref.getCorrespondingDeclaration().getParam(0).getType().asReferenceType().getQualifiedName());
    232         assertEquals("java.lang.Object", ref.getCorrespondingDeclaration().getParam(1).getType().asReferenceType().getQualifiedName());
    233 
    234     }
    235 
    236 }
    237