use of org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveDestinationsResponse in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testMoveInstanceMethod.
@Test
public void testMoveInstanceMethod() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
// @formatter:off
ICompilationUnit cuSecond = pack1.createCompilationUnit("Second.java", "package test1;\n" + "\n" + "public class Second {\n" + " public void foo() {\n" + " }\n" + "}", false, null);
// @formatter:on
// @formatter:off
pack1.createCompilationUnit("Third.java", "package test1;\n" + "\n" + "public class Third {\n" + " public void bar() {\n" + " }\n" + "}", false, null);
// @formatter:on
// @formatter:off
ICompilationUnit cu = pack1.createCompilationUnit("E.java", "package test1;\n" + "\n" + "public class E {\n" + " Second s;\n" + " String name;\n" + " int id;\n" + " public void print(Third t) {\n" + " System.out.println(name);\n" + " s.foo();\n" + " t.bar();\n" + " }\n" + "\n" + " public void log(Third t) {\n" + " print(t);\n" + " }\n" + "}", false, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "s.foo");
MoveParams moveParams = new MoveParams("moveInstanceMethod", new String[] { JDTUtils.toURI(cu) }, params);
MoveDestinationsResponse response = MoveHandler.getMoveDestinations(moveParams);
assertNotNull(response);
assertNull(response.errorMessage);
assertNotNull(response.destinations);
assertEquals(2, response.destinations.length);
RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveInstanceMethod", params, response.destinations[1], true), new NullProgressMonitor());
assertNotNull(refactorEdit);
assertNotNull(refactorEdit.edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
assertEquals(2, changes.size());
// @formatter:off
String expected = "package test1;\n" + "\n" + "public class E {\n" + " Second s;\n" + " String name;\n" + " int id;\n" + " public void log(Third t) {\n" + " s.print(this, t);\n" + " }\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(cu.getSource(), textEdit.getEdits()));
// @formatter:off
expected = "package test1;\n" + "\n" + "public class Second {\n" + " public void foo() {\n" + " }\n" + "\n" + " public void print(E e, Third t) {\n" + " System.out.println(e.name);\n" + " foo();\n" + " t.bar();\n" + " }\n" + "}";
// @formatter:on
textEdit = changes.get(1).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(cuSecond.getSource(), textEdit.getEdits()));
}
use of org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveDestinationsResponse in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testGetInstanceMethodDestinations.
@Test
public void testGetInstanceMethodDestinations() throws Exception {
when(preferenceManager.getClientPreferences().isMoveRefactoringSupported()).thenReturn(true);
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
// @formatter:off
pack1.createCompilationUnit("Second.java", "package test1;\n" + "\n" + "public class Second {\n" + " public void foo() {\n" + " }\n" + "}", false, null);
// @formatter:on
// @formatter:off
pack1.createCompilationUnit("Third.java", "package test1;\n" + "\n" + "public class Third {\n" + " public void bar() {\n" + " }\n" + "}", false, null);
// @formatter:on
// @formatter:off
ICompilationUnit cu = pack1.createCompilationUnit("E.java", "package test1;\n" + "\n" + "public class E {\n" + " Second s;\n" + " String name;\n" + " int id;\n" + " public void print(Third t) {\n" + " s.foo();\n" + " t.bar();\n" + " }\n" + "}", false, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "s.foo");
MoveParams moveParams = new MoveParams("moveInstanceMethod", new String[] { JDTUtils.toURI(cu) }, params);
MoveDestinationsResponse response = MoveHandler.getMoveDestinations(moveParams);
assertNotNull(response);
assertNull(response.errorMessage);
assertNotNull(response.destinations);
assertEquals(2, response.destinations.length);
assertEquals("t", ((LspVariableBinding) response.destinations[0]).name);
assertFalse(((LspVariableBinding) response.destinations[0]).isField);
assertEquals("Third", ((LspVariableBinding) response.destinations[0]).type);
assertEquals("s", ((LspVariableBinding) response.destinations[1]).name);
assertTrue(((LspVariableBinding) response.destinations[1]).isField);
assertEquals("Second", ((LspVariableBinding) response.destinations[1]).type);
}
use of org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveDestinationsResponse in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testGetPackageDestinations.
@Test
public void testGetPackageDestinations() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", false, null);
// @formatter:off
ICompilationUnit unit = pack1.createCompilationUnit("A.java", "package jdtls.test1;\r\n" + "\r\n" + "public class A {\r\n" + "}", true, null);
// @formatter:on
MoveParams params = new MoveParams("moveResource", new String[] { JDTUtils.toURI(unit) });
MoveDestinationsResponse response = MoveHandler.getMoveDestinations(params);
assertNotNull(response);
assertNotNull(response.destinations);
assertEquals(3, response.destinations.length);
assertTrue(((PackageNode) response.destinations[0]).isDefaultPackage);
assertEquals("jdtls", ((PackageNode) response.destinations[1]).displayName);
assertEquals("jdtls.test1", ((PackageNode) response.destinations[2]).displayName);
assertTrue(((PackageNode) response.destinations[2]).isParentOfSelectedFile);
}
Aggregations