use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.
the class AssistQuickFixTest17 method testReplaceMultiCatchClauseWithThrows4.
@Test
public void testReplaceMultiCatchClauseWithThrows4() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.lang.reflect.InvocationTargetException;\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" try {\n");
buf.append(" String.class.getConstructor().newInstance();\n");
buf.append(" } catch (InstantiationException | IllegalAccessException\n");
buf.append(" | IllegalArgumentException | InvocationTargetException\n");
buf.append(" | NoSuchMethodException | SecurityException 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("IllegalArgumentException");
AssistContext context = getCorrectionContext(cu, offset, 0);
List proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 4);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.lang.reflect.InvocationTargetException;\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" try {\n");
buf.append(" String.class.getConstructor().newInstance();\n");
buf.append(" } catch (InstantiationException | IllegalAccessException\n");
buf.append(" | InvocationTargetException\n");
buf.append(" | NoSuchMethodException | SecurityException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.lang.reflect.InvocationTargetException;\n");
buf.append("public class E {\n");
buf.append(" public void foo() throws IllegalArgumentException {\n");
buf.append(" try {\n");
buf.append(" String.class.getConstructor().newInstance();\n");
buf.append(" } catch (InstantiationException | IllegalAccessException\n");
buf.append(" | InvocationTargetException\n");
buf.append(" | NoSuchMethodException | SecurityException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected2 = buf.toString();
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.lang.reflect.InvocationTargetException;\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" try {\n");
buf.append(" String.class.getConstructor().newInstance();\n");
buf.append(" } catch (InstantiationException | IllegalAccessException\n");
buf.append(" | InvocationTargetException\n");
buf.append(" | NoSuchMethodException | SecurityException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (IllegalArgumentException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected3 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1, expected2, expected3 });
}
use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.
the class ConvertForLoopQuickFixTest method testExpressionPrecondition05.
@Test
public void testExpressionPrecondition05() 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(" Object[] x;\n");
buf.append(" public void foo() {\n");
buf.append(" for (int i = 0; i < x.length; i++) {}\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.ICompilationUnit in project che by eclipse.
the class ConvertForLoopQuickFixTest method testMatrix2.
@Test
public void testMatrix2() 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(" public void foo() {\n");
buf.append(" int[][] matrix = {{1,2},{3,4}};\n");
buf.append(" for (int i = 0; i < matrix.length; i++){\n");
buf.append(" for(int j = 0; j < matrix[i].length; j++){\n");
buf.append(" System.out.println(matrix[i][j]);\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("A.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 A {\n");
buf.append(" public void foo() {\n");
buf.append(" int[][] matrix = {{1,2},{3,4}};\n");
buf.append(" for (int[] element : matrix) {\n");
buf.append(" for(int j = 0; j < element.length; j++){\n");
buf.append(" System.out.println(element[j]);\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected = buf.toString();
assertEqualString(preview1, expected);
}
use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.
the class ConvertForLoopQuickFixTest method testInferTypeElement.
@Test
public void testInferTypeElement() 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(" public void foo() {\n");
buf.append(" String[] array = {\"1\",\"2\"};\n");
buf.append(" for (int i = 0; i < array.length; i++){\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("A.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 A {\n");
buf.append(" public void foo() {\n");
buf.append(" String[] array = {\"1\",\"2\"};\n");
buf.append(" for (String element : array) {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected = buf.toString();
assertEqualString(preview1, expected);
}
use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.
the class ConvertForLoopQuickFixTest method testSimplestClean.
@Test
public void testSimplestClean() 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(" public void foo() {\n");
buf.append(" String[] array = {\"1\",\"2\"};\n");
buf.append(" for (int i = 0; i < array.length; i++){\n");
buf.append(" System.out.println(array[i]);\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("A.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 A {\n");
buf.append(" public void foo() {\n");
buf.append(" String[] array = {\"1\",\"2\"};\n");
buf.append(" for (String element : array) {\n");
buf.append(" System.out.println(element);\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected = buf.toString();
assertEqualString(preview1, expected);
}
Aggregations