Search in sources :

Example 6 with IChooseImportQuery

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery in project che by eclipse.

the class ImportOrganizeTest method test4.

@Test
public void test4() throws Exception {
    File junitSrcArchive = new File(JUnitSourceSetup.class.getClassLoader().getResource(JavaProjectHelper.JUNIT_SRC_381.toOSString()).getFile());
    assertTrue("junit src not found", junitSrcArchive != null && junitSrcArchive.exists());
    JavaProjectHelper.addSourceContainerWithImport(fJProject1, "src", junitSrcArchive, JavaProjectHelper.JUNIT_SRC_ENCODING);
    ICompilationUnit cu = (ICompilationUnit) fJProject1.findElement(new Path("junit/textui/TestRunner.java"));
    assertNotNull("TestRunner.java", cu);
    String[] order = new String[0];
    IChooseImportQuery query = createQuery("TestRunner", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    assertImports(cu, new String[] { "java.io.PrintStream", "junit.framework.Test", "junit.framework.TestResult", "junit.framework.TestSuite", "junit.runner.BaseTestRunner", "junit.runner.StandardTestSuiteLoader", "junit.runner.TestSuiteLoader", "junit.runner.Version" });
}
Also used : Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) JUnitSourceSetup(org.eclipse.che.plugin.java.server.jdt.search.JUnitSourceSetup) File(java.io.File) Test(org.junit.Test)

Example 7 with IChooseImportQuery

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery in project che by eclipse.

the class ImportOrganizeTest method test_bug78716.

@Test
public void test_bug78716() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack0 = sourceFolder.createPackageFragment("pack0", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack0;\n");
    buf.append("public enum MyEnum {\n");
    buf.append("	A, B, C\n");
    buf.append("}\n");
    pack0.createCompilationUnit("MyEnum.java", buf.toString(), false, null);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("pack1", false, null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import pack0.MyEnum;\n");
    buf.append("import static pack0.MyEnum.*;\n");
    buf.append("\n");
    buf.append("public class Test2 {\n");
    buf.append("	MyEnum e= A;\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("Test2.java", buf.toString(), false, null);
    String[] order = new String[] { "", "#" };
    IChooseImportQuery query = createQuery("MyClass", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import pack0.MyEnum;\n");
    buf.append("\n");
    buf.append("import static pack0.MyEnum.A;\n");
    buf.append("\n");
    buf.append("public class Test2 {\n");
    buf.append("	MyEnum e= A;\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Example 8 with IChooseImportQuery

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery in project che by eclipse.

the class ImportOrganizeTest method testGroups1.

@Test
public void testGroups1() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack2 = sourceFolder.createPackageFragment("pack0", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack0;\n");
    buf.append("public class List1 {\n");
    buf.append("}\n");
    pack2.createCompilationUnit("List1.java", buf.toString(), false, null);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("pack1", false, null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    File f;\n");
    buf.append("    IOException f1;\n");
    buf.append("    RandomAccessFile f2;\n");
    buf.append("    ArrayList f3;\n");
    buf.append("    List1 f4;\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    String[] order = new String[] { "java.io", "java.util" };
    IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.io.File;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.io.RandomAccessFile;\n");
    buf.append("\n");
    buf.append("import java.util.ArrayList;\n");
    buf.append("\n");
    buf.append("import pack0.List1;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    File f;\n");
    buf.append("    IOException f1;\n");
    buf.append("    RandomAccessFile f2;\n");
    buf.append("    ArrayList f3;\n");
    buf.append("    List1 f4;\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Example 9 with IChooseImportQuery

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery in project che by eclipse.

the class ImportOrganizeTest method testImportFromDefaultWithStar.

@Test
public void testImportFromDefaultWithStar() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack2 = sourceFolder.createPackageFragment("", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("public class List1 {\n");
    buf.append("}\n");
    pack2.createCompilationUnit("List1.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("public class List2 {\n");
    buf.append("}\n");
    pack2.createCompilationUnit("List2.java", buf.toString(), false, null);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("pack1", false, null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    // no imports from default in compatibility >= 1.4
    buf.append("import java.util.Set;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("import java.util.Map;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    Vector v;\n");
    buf.append("    Set v2;\n");
    buf.append("    Map v3;\n");
    buf.append("    List1 v4;\n");
    buf.append("    List2 v5;\n");
    buf.append("    String v6;\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    String[] order = new String[] { "java", "pack" };
    IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 2, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.util.*;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    Vector v;\n");
    buf.append("    Set v2;\n");
    buf.append("    Map v3;\n");
    buf.append("    List1 v4;\n");
    buf.append("    List2 v5;\n");
    buf.append("    String v6;\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Example 10 with IChooseImportQuery

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery in project che by eclipse.

the class ImportOrganizeTest method testImportCountAddandRemove.

@Test
public void testImportCountAddandRemove() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack1 = sourceFolder.createPackageFragment("pack1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.util.*;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public void foo() {\n");
    buf.append("        HashMap m;\n");
    buf.append("        ArrayList l;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    String[] order = new String[] { "java", "pack" };
    IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.util.ArrayList;\n");
    buf.append("import java.util.HashMap;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public void foo() {\n");
    buf.append("        HashMap m;\n");
    buf.append("        ArrayList l;\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
    assertEquals(2, op.getNumberOfImportsAdded());
    assertEquals(1, op.getNumberOfImportsRemoved());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Aggregations

IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)71 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)70 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)70 Test (org.junit.Test)69 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)67 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)67 File (java.io.File)5 JUnitSourceSetup (org.eclipse.che.plugin.java.server.jdt.search.JUnitSourceSetup)5 Path (org.eclipse.core.runtime.Path)5 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)1