use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.
the class AssistQuickFixTest method testExtractToMethod2.
@Test
public void testExtractToMethod2() throws Exception {
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=41302
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() {\n");
buf.append(" int a = 1;\n");
buf.append(" int b = 1;\n");
buf.append(" int d = a + b;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset1 = buf.toString().indexOf("int b = 1;");
int offset2 = buf.toString().indexOf("a + b;") + 6;
AssistContext context = getCorrectionContext(cu, offset1, offset2 - offset1);
List proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 2);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" int a = 1;\n");
buf.append(" extracted(a);\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" private void extracted(int a) {\n");
buf.append(" int b = 1;\n");
buf.append(" int d = a + b;\n");
buf.append(" }\n");
buf.append("}\n");
String ex1 = buf.toString();
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" int a = 1;\n");
buf.append(" final int b = 1;\n");
buf.append(" final int d = a + b;\n");
buf.append(" }\n");
buf.append("}\n");
String ex2 = buf.toString();
buf = new StringBuffer();
// Wrap in buf.append() (to clipboard)
String ex3 = null;
assertExpectedExistInProposals(proposals, new String[] { ex1, ex2, ex3 });
}
use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.
the class AssistQuickFixTest method testInvertEquals6.
@Test
public void testInvertEquals6() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("class A {\n");
buf.append(" static String get() {\n");
buf.append(" return \"a\";\n");
buf.append(" }\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" \"a\".equals(A.get());\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), 0);
List proposals = collectAssists(context, FILTER_EQ);
assertNumberOfProposals(proposals, 1);
assertCorrectLabels(proposals);
cu = pack1.createCompilationUnit("E.java", buf.toString(), true, null);
CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
String preview = getPreviewContent(proposal);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("class A {\n");
buf.append(" static String get() {\n");
buf.append(" return \"a\";\n");
buf.append(" }\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" A.get().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), 0);
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("class A {\n");
buf.append(" static String get() {\n");
buf.append(" return \"a\";\n");
buf.append(" }\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" \"a\".equals(A.get());\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.
the class AssistQuickFixTest17 method testConvertToMultiCatch2.
@Test
public void testConvertToMultiCatch2() 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() {\n");
buf.append(" try {\n");
buf.append(" System.out.println(\"foo\");\n");
buf.append(" } catch (IllegalArgumentException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (NullPointerException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("catch");
AssistContext context = getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
List proposals = collectAssists(context, false);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" try {\n");
buf.append(" System.out.println(\"foo\");\n");
buf.append(" } catch (IllegalArgumentException | NullPointerException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\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 AssistQuickFixTest17 method testConvertToMultiCatch4.
@Test
public void testConvertToMultiCatch4() 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() {\n");
buf.append(" try {\n");
buf.append(" System.out.println(\"foo\");\n");
buf.append(" } catch (IllegalArgumentException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (NullPointerException e) {\n");
buf.append(" \n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("catch");
AssistContext context = getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
List proposals = collectAssists(context, false);
assertProposalDoesNotExist(proposals, CONVERT_TO_A_SINGLE_MULTI_CATCH_BLOCK);
}
use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.
the class AssistQuickFixTest17 method testUnrollMultiCatch4.
@Test
public void testUnrollMultiCatch4() 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() {\n");
buf.append(" try {\n");
buf.append(" System.out.println(\"foo\");\n");
buf.append(" } catch (IllegalArgumentException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (NullPointerException | ClassCastException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (ArrayIndexOutOfBoundsException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("catch (NullPointerException");
AssistContext context = getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
List proposals = collectAssists(context, false);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" try {\n");
buf.append(" System.out.println(\"foo\");\n");
buf.append(" } catch (IllegalArgumentException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (NullPointerException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (ClassCastException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (ArrayIndexOutOfBoundsException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Aggregations