Search in sources :

Example 16 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class AssistQuickFixTest method testSplitDeclaration7.

@Test
public void testSplitDeclaration7() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package e;\n");
    buf.append("public class Test {\n");
    buf.append("    public void test() {\n");
    buf.append("        String[] test = x;\n");
    buf.append("    }\n");
    buf.append("}");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String str = "=";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str), 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 1);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package e;\n");
    buf.append("public class Test {\n");
    buf.append("    public void test() {\n");
    buf.append("        String[] test;\n");
    buf.append("        test = x;\n");
    buf.append("    }\n");
    buf.append("}");
    assertEqualString(preview, buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 17 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class AssistQuickFixTest method testAssignToLocal12.

@Test
public void testAssignToLocal12() throws Exception {
    // assign to recovered statement in if body with no brackets
    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("        if (i == 0)\n");
    buf.append("           i++\n");
    buf.append("        else\n");
    buf.append("            System.getProperties()\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("System.getProperties()");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("import java.util.Properties;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(int i) {\n");
    buf.append("        if (i == 0)\n");
    buf.append("           i++\n");
    buf.append("        else {\n");
    buf.append("            Properties properties = System.getProperties();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(1);
    String preview2 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("import java.util.Properties;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    private Properties properties;\n");
    buf.append("\n");
    buf.append("    public void foo(int i) {\n");
    buf.append("        if (i == 0)\n");
    buf.append("           i++\n");
    buf.append("        else\n");
    buf.append("            properties = System.getProperties();\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 18 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class AssistQuickFixTest method testInvertEquals3.

@Test
public void testInvertEquals3() 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("    private String a= \"a\";\n");
    buf.append("    private String b= \"b\";\n");
    buf.append("    public void foo() {\n");
    buf.append("        a.equals(b);\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String str = "equals";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str), str.length());
    List proposals = collectAssists(context, FILTER_EQ);
    assertNumberOfProposals(proposals, 1);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private String a= \"a\";\n");
    buf.append("    private String b= \"b\";\n");
    buf.append("    public void foo() {\n");
    buf.append("        b.equals(a);\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(preview, buf.toString());
    cu = pack1.createCompilationUnit("E.java", buf.toString(), true, null);
    context = getCorrectionContext(cu, buf.toString().indexOf(str), str.length());
    proposals = collectAssists(context, FILTER_EQ);
    assertNumberOfProposals(proposals, 1);
    assertCorrectLabels(proposals);
    proposal = (CUCorrectionProposal) proposals.get(0);
    preview = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private String a= \"a\";\n");
    buf.append("    private String b= \"b\";\n");
    buf.append("    public void foo() {\n");
    buf.append("        a.equals(b);\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(preview, buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 19 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class AssistQuickFixTest method testCreateInSuper.

@Test
public void testCreateInSuper() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer 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);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public interface IB {\n");
    buf.append("}\n");
    pack1.createCompilationUnit("IB.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("public class E extends A implements IB {\n");
    buf.append("    public Vector foo(int count) throws IOException {\n");
    buf.append("        return null;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String str = "foo";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("\n");
    buf.append("public interface IB {\n");
    buf.append("\n");
    buf.append("    Vector foo(int count) throws IOException;\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(1);
    String preview2 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("\n");
    buf.append("public class A {\n");
    buf.append("\n");
    buf.append("    public Vector foo(int count) throws IOException {\n");
    buf.append("        //TODO\n");
    buf.append("        return null;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 20 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class AssistQuickFixTest method testUnwrapTryStatement.

@Test
public void testUnwrapTryStatement() 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() {\n");
    buf.append("        try {\n");
    buf.append("            StringBuffer buf= new StringBuffer();\n");
    buf.append("            buf.append(1);\n");
    buf.append("            buf.append(2);\n");
    buf.append("            buf.append(3);\n");
    buf.append("        } finally {\n");
    buf.append("            return;\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String str = "try";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 1);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        StringBuffer buf= new StringBuffer();\n");
    buf.append("        buf.append(1);\n");
    buf.append("        buf.append(2);\n");
    buf.append("        buf.append(3);\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(preview, buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)290 ArrayList (java.util.ArrayList)285 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)285 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)284 Test (org.junit.Test)284 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)228 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)57 List (java.util.List)48 Hashtable (java.util.Hashtable)47 ProblemLocation (org.eclipse.jdt.internal.ui.text.correction.ProblemLocation)7 Ignore (org.junit.Ignore)7 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)6 ChangeCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)6 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)5 CorrectPackageDeclarationProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 Iterator (java.util.Iterator)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 IDocument (org.eclipse.jface.text.IDocument)2