use of org.junit.Assert.assertNotNull in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testResolveCodeAction_QuickAssists.
@Test
public void testResolveCodeAction_QuickAssists() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
StringBuilder buf = new StringBuilder();
buf.append("public class E {\n");
buf.append(" public E(int count) {\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit unit = defaultPackage.createCompilationUnit("E.java", buf.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
Range range = CodeActionUtil.getRange(unit, "count");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(JavaCodeActionKind.QUICK_ASSIST));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
Assert.assertFalse("No quickassist actions were found", codeActions.isEmpty());
for (Either<Command, CodeAction> codeAction : codeActions) {
Assert.assertNull("Should defer the edit property to the resolveCodeAction request", codeAction.getRight().getEdit());
Assert.assertNotNull("Should preserve the data property for the unresolved code action", codeAction.getRight().getData());
}
Optional<Either<Command, CodeAction>> assignToNewFieldResponse = codeActions.stream().filter(codeAction -> {
return "Assign parameter to new field".equals(codeAction.getRight().getTitle());
}).findFirst();
Assert.assertTrue("Should return the quick assist 'Convert to method reference'", assignToNewFieldResponse.isPresent());
CodeAction unresolvedCodeAction = assignToNewFieldResponse.get().getRight();
CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
buf = new StringBuilder();
buf.append("public class E {\n");
buf.append(" private int count;\n");
buf.append("\n");
buf.append(" public E(int count) {\n");
buf.append(" this.count = count;\n");
buf.append(" }\n");
buf.append("}\n");
Assert.assertEquals(buf.toString(), actual);
}
use of org.junit.Assert.assertNotNull in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testResolveCodeAction_Refactors.
@Test
public void testResolveCodeAction_Refactors() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
StringBuilder buf = new StringBuilder();
buf.append("interface I {\n");
buf.append(" void method();\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" public void bar(I i) {}\n");
buf.append(" public void foo() {\n");
buf.append(" bar(new I() {\n");
buf.append(" @Override\n");
buf.append(" /*[*/public void method() {\n");
buf.append(" System.out.println();\n");
buf.append(" }/*]*/\n");
buf.append(" });\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit unit = defaultPackage.createCompilationUnit("E.java", buf.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit);
params.setRange(range);
CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(CodeActionKind.Refactor));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
Assert.assertFalse("No refactor actions were found", codeActions.isEmpty());
for (Either<Command, CodeAction> codeAction : codeActions) {
Assert.assertNull("Should defer the edit property to the resolveCodeAction request", codeAction.getRight().getEdit());
Assert.assertNotNull("Should preserve the data property for the unresolved code action", codeAction.getRight().getData());
}
Optional<Either<Command, CodeAction>> convertToLambdaResponse = codeActions.stream().filter(codeAction -> {
return "Convert to lambda expression".equals(codeAction.getRight().getTitle());
}).findFirst();
Assert.assertTrue("Should return the refactor action 'Convert to lambda expression'", convertToLambdaResponse.isPresent());
CodeAction unresolvedCodeAction = convertToLambdaResponse.get().getRight();
CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
buf = new StringBuilder();
buf.append("interface I {\n");
buf.append(" void method();\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" public void bar(I i) {}\n");
buf.append(" public void foo() {\n");
buf.append(" bar(() -> System.out.println());\n");
buf.append(" }\n");
buf.append("}\n");
Assert.assertEquals(buf.toString(), actual);
}
use of org.junit.Assert.assertNotNull in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testResolveCodeAction_QuickFixes.
@Test
public void testResolveCodeAction_QuickFixes() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
StringBuilder buf = new StringBuilder();
buf.append("public class Foo {\n");
buf.append(" void foo() {\n");
buf.append(" String bar = \"astring\";");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit unit = defaultPackage.createCompilationUnit("Foo.java", buf.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "bar");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range)), Collections.singletonList(CodeActionKind.QuickFix));
params.setContext(context);
List<Either<Command, CodeAction>> quickfixActions = server.codeAction(params).join();
Assert.assertNotNull(quickfixActions);
Assert.assertFalse("No quickfix actions were found", quickfixActions.isEmpty());
for (Either<Command, CodeAction> codeAction : quickfixActions) {
Assert.assertNull("Should defer the edit property to the resolveCodeAction request", codeAction.getRight().getEdit());
Assert.assertNotNull("Should preserve the data property for the unresolved code action", codeAction.getRight().getData());
}
Optional<Either<Command, CodeAction>> removeUnusedResponse = quickfixActions.stream().filter(codeAction -> {
return "Remove 'bar' and all assignments".equals(codeAction.getRight().getTitle());
}).findFirst();
Assert.assertTrue("Should return the quickfix \"Remove 'bar' and all assignments\"", removeUnusedResponse.isPresent());
CodeAction unresolvedCodeAction = removeUnusedResponse.get().getRight();
CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
buf = new StringBuilder();
buf.append("public class Foo {\n");
buf.append(" void foo() { }\n");
buf.append("}\n");
Assert.assertEquals(buf.toString(), actual);
}
Aggregations