blob: d96f3db7269a43788178c6d83f8605f820fcdff7 [file] [log] [blame]
package org.eclipse.jdt.ui.tests.quickfix;
import java.util.ArrayList;
import java.util.Hashtable;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.testplugin.JavaProjectHelper;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.text.correction.CorrectionContext;
import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor;
import org.eclipse.jdt.internal.ui.text.correction.NewMethodCompletionProposal;
public class UnresolvedMethodsQuickFixTest extends QuickFixTest {
private static final Class THIS= UnresolvedMethodsQuickFixTest.class;
private IJavaProject fJProject1;
private IPackageFragmentRoot fSourceFolder;
public UnresolvedMethodsQuickFixTest(String name) {
super(name);
}
public static Test suite() {
if (false) {
return new TestSuite(THIS);
} else {
TestSuite suite= new TestSuite();
suite.addTest(new UnresolvedMethodsQuickFixTest("testSuperMethodInvocation"));
return suite;
}
}
protected void setUp() throws Exception {
Hashtable options= JavaCore.getDefaultOptions();
options.put(JavaCore.FORMATTER_TAB_CHAR, JavaCore.SPACE);
options.put(JavaCore.FORMATTER_TAB_SIZE, "4");
JavaCore.setOptions(options);
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
store.setValue(PreferenceConstants.CODEGEN__FILE_COMMENTS, false);
store.setValue(PreferenceConstants.CODEGEN__JAVADOC_STUBS, false);
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");
assertTrue("rt not found", JavaProjectHelper.addRTJar(fJProject1) != null);
fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
}
protected void tearDown() throws Exception {
JavaProjectHelper.delete(fJProject1);
}
public void testMethodInSameType() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E {\n");
buf.append(" void foo(Vector vec) {\n");
buf.append(" int i= goo(vec, true);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
CompilationUnit astRoot= AST.parseCompilationUnit(cu, true);
IProblem[] problems= astRoot.getProblems();
assertNumberOf("problems", problems.length, 1);
CorrectionContext context= getCorrectionContext(cu, problems[0]);
assertCorrectContext(context);
ArrayList proposals= new ArrayList();
JavaCorrectionProcessor.collectCorrections(context, proposals);
assertNumberOf("proposals", proposals.size(), 1);
assertCorrectLabels(proposals);
NewMethodCompletionProposal proposal= (NewMethodCompletionProposal) proposals.get(0);
String preview= proposal.getCompilationUnitChange().getPreviewContent();
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E {\n");
buf.append(" void foo(Vector vec) {\n");
buf.append(" int i= goo(vec, true);\n");
buf.append(" }\n");
buf.append(" private int goo(Vector vec, boolean b) {\n");
buf.append(" return 0;\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
public void testMethodInSameTypeUsingThis() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E {\n");
buf.append(" void foo(Vector vec) {\n");
buf.append(" int i= this.goo(vec, true);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
CompilationUnit astRoot= AST.parseCompilationUnit(cu, true);
IProblem[] problems= astRoot.getProblems();
assertNumberOf("problems", problems.length, 1);
CorrectionContext context= getCorrectionContext(cu, problems[0]);
assertCorrectContext(context);
ArrayList proposals= new ArrayList();
JavaCorrectionProcessor.collectCorrections(context, proposals);
assertNumberOf("proposals", proposals.size(), 1);
assertCorrectLabels(proposals);
NewMethodCompletionProposal proposal= (NewMethodCompletionProposal) proposals.get(0);
String preview= proposal.getCompilationUnitChange().getPreviewContent();
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E {\n");
buf.append(" void foo(Vector vec) {\n");
buf.append(" int i= this.goo(vec, true);\n");
buf.append(" }\n");
buf.append(" private int goo(Vector vec, boolean b) {\n");
buf.append(" return 0;\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
public void testMethodInDifferentClass() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo(X x) {\n");
buf.append(" boolean i= x.goo(1, 2.1);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class X {\n");
buf.append("}\n");
pack1.createCompilationUnit("X.java", buf.toString(), false, null);
CompilationUnit astRoot= AST.parseCompilationUnit(cu, true);
IProblem[] problems= astRoot.getProblems();
assertNumberOf("problems", problems.length, 1);
CorrectionContext context= getCorrectionContext(cu, problems[0]);
assertCorrectContext(context);
ArrayList proposals= new ArrayList();
JavaCorrectionProcessor.collectCorrections(context, proposals);
assertNumberOf("proposals", proposals.size(), 1);
assertCorrectLabels(proposals);
NewMethodCompletionProposal proposal= (NewMethodCompletionProposal) proposals.get(0);
String preview= proposal.getCompilationUnitChange().getPreviewContent();
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class X {\n");
buf.append(" public boolean goo(int i, double d) {\n");
buf.append(" return false;\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
public void testMethodInDifferentInterface() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo(X x) {\n");
buf.append(" boolean i= x.goo(getClass());\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public interface X {\n");
buf.append("}\n");
pack1.createCompilationUnit("X.java", buf.toString(), false, null);
CompilationUnit astRoot= AST.parseCompilationUnit(cu, true);
IProblem[] problems= astRoot.getProblems();
assertNumberOf("problems", problems.length, 1);
CorrectionContext context= getCorrectionContext(cu, problems[0]);
assertCorrectContext(context);
ArrayList proposals= new ArrayList();
JavaCorrectionProcessor.collectCorrections(context, proposals);
assertNumberOf("proposals", proposals.size(), 1);
assertCorrectLabels(proposals);
NewMethodCompletionProposal proposal= (NewMethodCompletionProposal) proposals.get(0);
String preview= proposal.getCompilationUnitChange().getPreviewContent();
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public interface X {\n");
buf.append(" boolean goo(Class c);\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
public void testSuperConstructor() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E extends A {\n");
buf.append(" public E(int i) {\n");
buf.append(" super(i);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu1=pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append("}\n");
pack1.createCompilationUnit("A.java", buf.toString(), false, null);
CompilationUnit astRoot= AST.parseCompilationUnit(cu1, true);
IProblem[] problems= astRoot.getProblems();
assertNumberOf("problems", problems.length, 1);
CorrectionContext context= getCorrectionContext(cu1, problems[0]);
ArrayList proposals= new ArrayList();
JavaCorrectionProcessor.collectCorrections(context, proposals);
assertNumberOf("proposals", proposals.size(), 1);
assertCorrectLabels(proposals);
NewMethodCompletionProposal proposal= (NewMethodCompletionProposal) proposals.get(0);
String preview= proposal.getCompilationUnitChange().getPreviewContent();
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append(" public A(int i) {\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
public void testClassInstanceCreation() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" A a= new A(i);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu1=pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append("}\n");
pack1.createCompilationUnit("A.java", buf.toString(), false, null);
CompilationUnit astRoot= AST.parseCompilationUnit(cu1, true);
IProblem[] problems= astRoot.getProblems();
assertNumberOf("problems", problems.length, 1);
CorrectionContext context= getCorrectionContext(cu1, problems[0]);
ArrayList proposals= new ArrayList();
JavaCorrectionProcessor.collectCorrections(context, proposals);
assertNumberOf("proposals", proposals.size(), 1);
assertCorrectLabels(proposals);
NewMethodCompletionProposal proposal= (NewMethodCompletionProposal) proposals.get(0);
String preview= proposal.getCompilationUnitChange().getPreviewContent();
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append(" public A(int i) {\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
public void testConstructorInvocation() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public E(int i) {\n");
buf.append(" this(i, true);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu1=pack1.createCompilationUnit("E.java", buf.toString(), false, null);
CompilationUnit astRoot= AST.parseCompilationUnit(cu1, true);
IProblem[] problems= astRoot.getProblems();
assertNumberOf("problems", problems.length, 1);
CorrectionContext context= getCorrectionContext(cu1, problems[0]);
ArrayList proposals= new ArrayList();
JavaCorrectionProcessor.collectCorrections(context, proposals);
assertNumberOf("proposals", proposals.size(), 1);
assertCorrectLabels(proposals);
NewMethodCompletionProposal proposal= (NewMethodCompletionProposal) proposals.get(0);
String preview= proposal.getCompilationUnitChange().getPreviewContent();
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public E(int i) {\n");
buf.append(" this(i, true);\n");
buf.append(" }\n");
buf.append(" public E(int i, boolean b) {\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
public void testSuperMethodInvocation() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E extends A {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" super.foo(i);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu1=pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append("}\n");
pack1.createCompilationUnit("A.java", buf.toString(), false, null);
CompilationUnit astRoot= AST.parseCompilationUnit(cu1, true);
IProblem[] problems= astRoot.getProblems();
assertNumberOf("problems", problems.length, 1);
CorrectionContext context= getCorrectionContext(cu1, problems[0]);
ArrayList proposals= new ArrayList();
JavaCorrectionProcessor.collectCorrections(context, proposals);
assertNumberOf("proposals", proposals.size(), 1);
assertCorrectLabels(proposals);
NewMethodCompletionProposal proposal= (NewMethodCompletionProposal) proposals.get(0);
String preview= proposal.getCompilationUnitChange().getPreviewContent();
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
}