Home | History | Annotate | Download | only in logic
      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.logic;
     18 
     19 import com.github.javaparser.resolution.types.ResolvedType;
     20 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
     21 import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;
     22 import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration;
     23 import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration;
     24 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
     25 import org.junit.Test;
     26 
     27 import java.util.function.Consumer;
     28 import java.util.function.Function;
     29 
     30 import static org.junit.Assert.assertEquals;
     31 
     32 public class FunctionInterfaceLogicTest {
     33 
     34     @Test
     35     public void testGetFunctionalMethodNegativeCaseOnClass() {
     36         TypeSolver typeSolver = new ReflectionTypeSolver();
     37         ResolvedType string = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
     38         assertEquals(false, FunctionalInterfaceLogic.getFunctionalMethod(string).isPresent());
     39     }
     40 
     41     @Test
     42     public void testGetFunctionalMethodPositiveCasesOnInterfaces() {
     43         TypeSolver typeSolver = new ReflectionTypeSolver();
     44         ResolvedType function = new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Function.class, typeSolver), typeSolver);
     45         assertEquals(true, FunctionalInterfaceLogic.getFunctionalMethod(function).isPresent());
     46         assertEquals("apply", FunctionalInterfaceLogic.getFunctionalMethod(function).get().getName());
     47         ResolvedType consumer = new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Consumer.class, typeSolver), typeSolver);
     48         assertEquals(true, FunctionalInterfaceLogic.getFunctionalMethod(consumer).isPresent());
     49         assertEquals("accept", FunctionalInterfaceLogic.getFunctionalMethod(consumer).get().getName());
     50     }
     51 }
     52