Search in sources :

Example 36 with Assert.assertNotNull

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);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) Mock(org.mockito.Mock) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) RunWith(org.junit.runner.RunWith) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) IProject(org.eclipse.core.resources.IProject) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) LanguageServerWorkingCopyOwner(org.eclipse.jdt.ls.core.internal.LanguageServerWorkingCopyOwner) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Before(org.junit.Before) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkspaceHelper(org.eclipse.jdt.ls.core.internal.WorkspaceHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) JavaCore(org.eclipse.jdt.core.JavaCore) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) IType(org.eclipse.jdt.core.IType) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Command(org.eclipse.lsp4j.Command) CodeActionUtil(org.eclipse.jdt.ls.core.internal.CodeActionUtil) Assert.assertFalse(org.junit.Assert.assertFalse) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Optional(java.util.Optional) TestOptions(org.eclipse.jdt.ls.core.internal.correction.TestOptions) IProblem(org.eclipse.jdt.core.compiler.IProblem) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Example 37 with Assert.assertNotNull

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);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) Mock(org.mockito.Mock) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) RunWith(org.junit.runner.RunWith) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) IProject(org.eclipse.core.resources.IProject) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) LanguageServerWorkingCopyOwner(org.eclipse.jdt.ls.core.internal.LanguageServerWorkingCopyOwner) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Before(org.junit.Before) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkspaceHelper(org.eclipse.jdt.ls.core.internal.WorkspaceHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) JavaCore(org.eclipse.jdt.core.JavaCore) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) IType(org.eclipse.jdt.core.IType) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Command(org.eclipse.lsp4j.Command) CodeActionUtil(org.eclipse.jdt.ls.core.internal.CodeActionUtil) Assert.assertFalse(org.junit.Assert.assertFalse) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Optional(java.util.Optional) TestOptions(org.eclipse.jdt.ls.core.internal.correction.TestOptions) IProblem(org.eclipse.jdt.core.compiler.IProblem) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Example 38 with Assert.assertNotNull

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);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) Mock(org.mockito.Mock) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) RunWith(org.junit.runner.RunWith) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) IProject(org.eclipse.core.resources.IProject) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) LanguageServerWorkingCopyOwner(org.eclipse.jdt.ls.core.internal.LanguageServerWorkingCopyOwner) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Before(org.junit.Before) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkspaceHelper(org.eclipse.jdt.ls.core.internal.WorkspaceHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) JavaCore(org.eclipse.jdt.core.JavaCore) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) IType(org.eclipse.jdt.core.IType) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Command(org.eclipse.lsp4j.Command) CodeActionUtil(org.eclipse.jdt.ls.core.internal.CodeActionUtil) Assert.assertFalse(org.junit.Assert.assertFalse) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Optional(java.util.Optional) TestOptions(org.eclipse.jdt.ls.core.internal.correction.TestOptions) IProblem(org.eclipse.jdt.core.compiler.IProblem) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Aggregations

List (java.util.List)38 Assert (org.junit.Assert)38 Assert.assertNotNull (org.junit.Assert.assertNotNull)38 Test (org.junit.Test)38 Assert.assertEquals (org.junit.Assert.assertEquals)36 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)28 Autowired (org.springframework.beans.factory.annotation.Autowired)28 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)26 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)25 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)24 FormService (eu.bcvsolutions.idm.core.eav.api.service.FormService)24 Before (org.junit.Before)24 IdmIdentityRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)23 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)23 Assert.assertTrue (org.junit.Assert.assertTrue)23 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)22 LocalDate (java.time.LocalDate)22 Assert.assertNull (org.junit.Assert.assertNull)22 Lists (com.google.common.collect.Lists)21 IdmIdentityRoleService (eu.bcvsolutions.idm.core.api.service.IdmIdentityRoleService)20