1 /* 2 * Copyright (C) 2007-2010 Jlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 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.builders; 23 24 import com.github.javaparser.ast.CompilationUnit; 25 import com.github.javaparser.ast.Modifier; 26 import com.github.javaparser.ast.body.MethodDeclaration; 27 import com.github.javaparser.ast.body.Parameter; 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Test; 31 32 import java.util.List; 33 34 import static com.github.javaparser.utils.Utils.EOL; 35 import static org.junit.Assert.assertEquals; 36 37 public class NodeWithParametersBuildersTest { 38 private final CompilationUnit cu = new CompilationUnit(); 39 40 @Test 41 public void testAddParameter() { 42 MethodDeclaration addMethod = cu.addClass("test").addMethod("foo", Modifier.PUBLIC); 43 addMethod.addParameter(int.class, "yay"); 44 Parameter myNewParam = addMethod.addAndGetParameter(List.class, "myList"); 45 assertEquals(1, cu.getImports().size()); 46 assertEquals("import " + List.class.getName() + ";" + EOL, cu.getImport(0).toString()); 47 assertEquals(2, addMethod.getParameters().size()); 48 assertEquals("yay", addMethod.getParameter(0).getNameAsString()); 49 assertEquals("List", addMethod.getParameter(1).getType().toString()); 50 assertEquals(myNewParam, addMethod.getParameter(1)); 51 } 52 53 @Test 54 public void testGetParamByName() { 55 MethodDeclaration addMethod = cu.addClass("test").addMethod("foo", Modifier.PUBLIC); 56 Parameter addAndGetParameter = addMethod.addAndGetParameter(int.class, "yay"); 57 assertEquals(addAndGetParameter, addMethod.getParameterByName("yay").get()); 58 } 59 60 @Test 61 public void testGetParamByType() { 62 MethodDeclaration addMethod = cu.addClass("test").addMethod("foo", Modifier.PUBLIC); 63 Parameter addAndGetParameter = addMethod.addAndGetParameter(int.class, "yay"); 64 assertEquals(addAndGetParameter, addMethod.getParameterByType("int").get()); 65 assertEquals(addAndGetParameter, addMethod.getParameterByType(int.class).get()); 66 } 67 68 } 69