Search in sources :

Example 56 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class ConvertForLoopQuickFixTest method testBug214340_3.

@Test
public void testBug214340_3() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E1 {\n");
    buf.append("    int[] array = new int[3];\n");
    buf.append("    static boolean same(E1 one, E1 two) {\n");
    buf.append("        for (int i = 0; i < one.array.length; i++) {\n");
    buf.append("            System.out.println(one.array[i]);\n");
    buf.append("        }\n");
    buf.append("        return true;\n");
    buf.append("    }\n");
    buf.append("}\n");
    buf.append("\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E1.java", buf.toString(), false, null);
    List proposals = fetchConvertingProposal(buf, cu);
    assertNotNull(fConvertLoopProposal);
    assertCorrectLabels(proposals);
    String preview1 = getPreviewContent(fConvertLoopProposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E1 {\n");
    buf.append("    int[] array = new int[3];\n");
    buf.append("    static boolean same(E1 one, E1 two) {\n");
    buf.append("        for (int element : one.array) {\n");
    buf.append("            System.out.println(element);\n");
    buf.append("        }\n");
    buf.append("        return true;\n");
    buf.append("    }\n");
    buf.append("}\n");
    buf.append("\n");
    String expected = buf.toString();
    assertEqualString(preview1, expected);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) List(java.util.List) Test(org.junit.Test)

Example 57 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class ConvertForLoopQuickFixTest method testUpdatePrecondition03.

@Test
public void testUpdatePrecondition03() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Object[] x) {\n");
    buf.append("        for (int i = 0; i < x.length; i= i + 1) {}\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    assertTrue(satisfiesPrecondition(cu));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Test(org.junit.Test)

Example 58 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class LocalCorrectionsQuickFixTest17 method testRemoveRedundantTypeArguments2.

@Test
public void testRemoveRedundantTypeArguments2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.HashMap;\n");
    buf.append("import java.util.Map;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        Map<String,String> a = new HashMap<String,String>();\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.HashMap;\n");
    buf.append("import java.util.Map;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        Map<String,String> a = new HashMap<>();\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 59 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class LocalCorrectionsQuickFixTest17 method testUncaughtExceptionTryWithResources4.

@Test
public void testUncaughtExceptionTryWithResources4() throws Exception {
    //https://bugs.eclipse.org/bugs/show_bug.cgi?id=351464
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar() throws MyException {\n");
    buf.append("        throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws IOException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar() throws MyException {\n");
    buf.append("        throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws IOException, MyException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar();\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("import java.io.FileInputStream;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar() throws MyException {\n");
    buf.append("        throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws IOException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar();\n");
    buf.append("        } catch (MyException e) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(2);
    String preview3 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar() throws MyException {\n");
    buf.append("        throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws IOException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            try {\n");
    buf.append("                bar();\n");
    buf.append("            } catch (MyException e) {\n");
    buf.append("            }\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3 }, new String[] { expected1, expected2, expected3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 60 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class LocalCorrectionsQuickFixTest17 method testUncaughtExceptionTryWithResources1.

@Test
public void testUncaughtExceptionTryWithResources1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    //quick fix on 1st problem
    ArrayList proposals = collectCorrections(cu, astRoot, 3, 0);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws FileNotFoundException, IOException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\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("import java.io.FileInputStream;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        } catch (FileNotFoundException e) {\n");
    buf.append("        } catch (IOException e) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(2);
    String preview3 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        } catch (FileNotFoundException | IOException e) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3 }, new String[] { expected1, expected2, expected3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1147 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1052 Test (org.junit.Test)1039 ArrayList (java.util.ArrayList)755 List (java.util.List)453 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)435 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)384 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)284 Hashtable (java.util.Hashtable)136 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)100 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 Ignore (org.junit.Ignore)43 IType (org.eclipse.jdt.core.IType)37 IJavaElement (org.eclipse.jdt.core.IJavaElement)32 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)24 HashMap (java.util.HashMap)23 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)23 BaseTest (org.eclipse.che.plugin.java.server.che.BaseTest)21 Map (java.util.Map)19