| /******************************************************************************* |
| * Copyright (c) 2015, 2016 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.jdt.ui.tests.core.source; |
| |
| import org.eclipse.jdt.testplugin.JavaProjectHelper; |
| |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| |
| import org.eclipse.jdt.core.ICompilationUnit; |
| import org.eclipse.jdt.core.IJavaElement; |
| import org.eclipse.jdt.core.IJavaProject; |
| import org.eclipse.jdt.core.IMethod; |
| import org.eclipse.jdt.core.IPackageFragment; |
| import org.eclipse.jdt.core.IPackageFragmentRoot; |
| import org.eclipse.jdt.core.IType; |
| import org.eclipse.jdt.core.JavaModelException; |
| import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; |
| import org.eclipse.jdt.core.dom.CompilationUnit; |
| import org.eclipse.jdt.core.dom.IMethodBinding; |
| import org.eclipse.jdt.core.dom.ITypeBinding; |
| import org.eclipse.jdt.core.dom.NodeFinder; |
| |
| import org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedMethodsOperation; |
| import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2; |
| import org.eclipse.jdt.internal.corext.dom.ASTNodes; |
| import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; |
| import org.eclipse.jdt.internal.corext.util.JavaModelUtil; |
| |
| import org.eclipse.jdt.ui.tests.core.Java18ProjectTestSetup; |
| |
| import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; |
| |
| import junit.framework.Test; |
| import junit.framework.TestCase; |
| import junit.framework.TestSuite; |
| |
| public class AddUnimplementedMethodsTest18 extends TestCase { |
| |
| private static final Class<AddUnimplementedMethodsTest18> THIS= AddUnimplementedMethodsTest18.class; |
| |
| private IJavaProject fJavaProject; |
| |
| private IPackageFragment fPackage; |
| |
| public AddUnimplementedMethodsTest18(String name) { |
| super(name); |
| } |
| |
| public static Test suite() { |
| return setUpTest(new TestSuite(THIS)); |
| } |
| |
| public static Test setUpTest(Test test) { |
| return new Java18ProjectTestSetup(test); |
| } |
| |
| @Override |
| protected void tearDown() throws Exception { |
| JavaProjectHelper.clear(fJavaProject, Java18ProjectTestSetup.getDefaultClasspath()); |
| fJavaProject= null; |
| fPackage= null; |
| } |
| |
| @Override |
| protected void setUp() throws Exception { |
| fJavaProject= Java18ProjectTestSetup.getProject(); |
| IPackageFragmentRoot root= JavaProjectHelper.addSourceContainer(fJavaProject, "src"); |
| fPackage= root.createPackageFragment("ibm.util", true, null); |
| } |
| |
| public void testBug460521() throws Exception { |
| StringBuffer buf= new StringBuffer(); |
| buf.append("interface I {\n"); |
| buf.append("}\n"); |
| |
| ICompilationUnit cu= fPackage.createCompilationUnit("I.java", buf.toString(), true, null); |
| IType testClass= cu.createType(buf.toString(), null, true, null); |
| |
| testHelper(testClass, -1, true); |
| |
| IMethod[] methods= testClass.getMethods(); |
| checkMethods(new String[] { "hashCode", "equals", "clone", "toString", "finalize" }, methods); |
| } |
| |
| private void testHelper(IType testClass, int insertionPos, boolean implementAllOverridable) throws JavaModelException, CoreException { |
| RefactoringASTParser parser= new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); |
| CompilationUnit unit= parser.parse(testClass.getCompilationUnit(), true); |
| AbstractTypeDeclaration declaration= ASTNodes.getParent(NodeFinder.perform(unit, testClass.getNameRange()), AbstractTypeDeclaration.class); |
| assertNotNull("Could not find type declaration node", declaration); |
| ITypeBinding binding= declaration.resolveBinding(); |
| assertNotNull("Binding for type declaration could not be resolved", binding); |
| |
| IMethodBinding[] overridableMethods= implementAllOverridable ? StubUtility2.getOverridableMethods(unit.getAST(), binding, false) : null; |
| |
| AddUnimplementedMethodsOperation op= new AddUnimplementedMethodsOperation(unit, binding, overridableMethods, insertionPos, true, true, true); |
| op.run(new NullProgressMonitor()); |
| JavaModelUtil.reconcile(testClass.getCompilationUnit()); |
| } |
| |
| private void checkMethods(String[] expected, IMethod[] methods) { |
| int nMethods= methods.length; |
| int nExpected= expected.length; |
| assertTrue("" + nExpected + " methods expected, is " + nMethods, nMethods == nExpected); |
| for (int i= 0; i < nExpected; i++) { |
| String methName= expected[i]; |
| assertTrue("method " + methName + " expected", nameContained(methName, methods)); |
| } |
| } |
| |
| private boolean nameContained(String methName, IJavaElement[] methods) { |
| for (int i= 0; i < methods.length; i++) { |
| if (methods[i].getElementName().equals(methName)) { |
| return true; |
| } |
| } |
| return false; |
| } |
| } |