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);
}
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));
}
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 });
}
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 });
}
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 });
}
Aggregations