Skip to content

Commit

Permalink
Fix NPE for junit and testng test actions. (#5308)
Browse files Browse the repository at this point in the history
Detail:
Fix NPE in case if user open context menu in the jar nodes in the ProjectExplorer.
Fix NPE when user click to RUN menu in the main toolbar in the workspace without projects.
Fix NPE when user launch step into action in the java debugger which should open java files from some jar.
Clean up unused injections and imports.

Main goal of this pull request: fix NPE and nothing to break :)

Signed-off-by: Aleksandr Andrienko <aandrienko@codenvy.com>
  • Loading branch information
AndrienkoAleksandr authored Jun 13, 2017
1 parent fe60e9f commit 8720ded
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*******************************************************************************/
package org.eclipse.che.plugin.testing.junit.ide.action;

import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -22,16 +22,8 @@
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.resources.Container;
import org.eclipse.che.ide.api.resources.File;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.api.resources.Resource;
import org.eclipse.che.ide.api.resources.VirtualFile;
import org.eclipse.che.ide.api.selection.Selection;
import org.eclipse.che.ide.api.selection.SelectionAgent;
import org.eclipse.che.ide.ext.java.client.util.JavaUtil;
import org.eclipse.che.ide.resources.tree.ContainerNode;
import org.eclipse.che.ide.resources.tree.FileNode;
import org.eclipse.che.plugin.testing.ide.TestServiceClient;
import org.eclipse.che.plugin.testing.ide.action.RunTestActionDelegate;
import org.eclipse.che.plugin.testing.ide.view.TestResultPresenter;
Expand All @@ -51,24 +43,21 @@ public class RunAllTestAction extends AbstractPerspectiveAction
private final TestResultPresenter presenter;
private final TestServiceClient service;
private final AppContext appContext;
private final SelectionAgent selectionAgent;
private final RunTestActionDelegate delegate;

@Inject
public RunAllTestAction(JUnitTestResources resources,
NotificationManager notificationManager,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
SelectionAgent selectionAgent,
JUnitTestLocalizationConstant localization) {
super(Arrays.asList(PROJECT_PERSPECTIVE_ID), localization.actionRunAllTitle(),
NotificationManager notificationManager,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
JUnitTestLocalizationConstant localization) {
super(singletonList(PROJECT_PERSPECTIVE_ID), localization.actionRunAllTitle(),
localization.actionRunAllDescription(), null, resources.testAllIcon());
this.notificationManager = notificationManager;
this.presenter = presenter;
this.service = service;
this.appContext = appContext;
this.selectionAgent = selectionAgent;
this.delegate = new RunTestActionDelegate(this);
}

Expand All @@ -87,18 +76,14 @@ public void actionPerformed(ActionEvent e) {
@Override
public void updateInPerspective(@NotNull ActionEvent e) {
Resource resource = appContext.getResource();
if (resource == null) {
if (resource == null || resource.getProject() == null) {
e.getPresentation().setEnabledAndVisible(false);
return;
}

Project project = resource.getProject();
if (project == null) {
e.getPresentation().setEnabledAndVisible(false);
}


e.getPresentation().setVisible(true);

String projectType = project.getType();
String projectType = resource.getProject().getType();
boolean enable = "maven".equals(projectType);
e.getPresentation().setEnabled(enable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*******************************************************************************/
package org.eclipse.che.plugin.testing.junit.ide.action;

import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -23,7 +23,6 @@
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.resources.File;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.api.resources.Resource;
import org.eclipse.che.ide.api.resources.VirtualFile;
import org.eclipse.che.ide.ext.java.client.util.JavaUtil;
Expand All @@ -50,12 +49,12 @@ public class RunClassTestAction extends AbstractPerspectiveAction

@Inject
public RunClassTestAction(JUnitTestResources resources,
NotificationManager notificationManager,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
JUnitTestLocalizationConstant localization) {
super(Arrays.asList(PROJECT_PERSPECTIVE_ID), localization.actionRunClassTitle(),
NotificationManager notificationManager,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
JUnitTestLocalizationConstant localization) {
super(singletonList(PROJECT_PERSPECTIVE_ID), localization.actionRunClassTitle(),
localization.actionRunClassDescription(), null, resources.testIcon());
this.notificationManager = notificationManager;
this.presenter = presenter;
Expand All @@ -80,20 +79,15 @@ public void actionPerformed(ActionEvent e) {
@Override
public void updateInPerspective(@NotNull ActionEvent e) {
Resource resource = appContext.getResource();
if (! (resource instanceof File)) {
if (!(resource instanceof File) || resource.getProject() == null) {
e.getPresentation().setEnabledAndVisible(false);
return;
}

Project project = resource.getProject();
if (project == null) {
e.getPresentation().setEnabledAndVisible(false);
}


e.getPresentation().setVisible(true);

String projectType = project.getType();
if (! "maven".equals(projectType)) {
String projectType = resource.getProject().getType();
if (!"maven".equals(projectType)) {
e.getPresentation().setEnabled(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*******************************************************************************/
package org.eclipse.che.plugin.testing.testng.ide.action;

import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -47,12 +47,12 @@ public class RunAllTestAction extends AbstractPerspectiveAction

@Inject
public RunAllTestAction(TestNGResources resources,
NotificationManager notificationManager,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
TestNGLocalizationConstant localization) {
super(Arrays.asList(PROJECT_PERSPECTIVE_ID), localization.actionRunAllTitle(),
NotificationManager notificationManager,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
TestNGLocalizationConstant localization) {
super(singletonList(PROJECT_PERSPECTIVE_ID), localization.actionRunAllTitle(),
localization.actionRunAllDescription(), null, resources.testAllIcon());
this.notificationManager = notificationManager;
this.presenter = presenter;
Expand All @@ -76,22 +76,18 @@ public void actionPerformed(ActionEvent e) {
@Override
public void updateInPerspective(@NotNull ActionEvent e) {
Resource resource = appContext.getResource();
if (resource == null) {
if (resource == null || resource.getProject() == null) {
e.getPresentation().setEnabledAndVisible(false);
return;
}

Project project = resource.getProject();
if (project == null) {
e.getPresentation().setEnabledAndVisible(false);
}


e.getPresentation().setVisible(true);

String projectType = project.getType();
String projectType = resource.getProject().getType();
boolean enable = "maven".equals(projectType);
e.getPresentation().setEnabled(enable);
}

@Override
public NotificationManager getNotificationManager() {
return notificationManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*******************************************************************************/
package org.eclipse.che.plugin.testing.testng.ide.action;

import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -21,14 +21,11 @@
import org.eclipse.che.ide.api.action.AbstractPerspectiveAction;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.resources.File;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.api.resources.Resource;
import org.eclipse.che.ide.api.resources.VirtualFile;
import org.eclipse.che.ide.ext.java.client.util.JavaUtil;
import org.eclipse.che.ide.rest.DtoUnmarshallerFactory;
import org.eclipse.che.plugin.testing.ide.TestServiceClient;
import org.eclipse.che.plugin.testing.ide.action.RunTestActionDelegate;
import org.eclipse.che.plugin.testing.ide.view.TestResultPresenter;
Expand All @@ -51,14 +48,12 @@ public class RunClassTestAction extends AbstractPerspectiveAction

@Inject
public RunClassTestAction(TestNGResources resources,
NotificationManager notificationManager,
EditorAgent editorAgent,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
DtoUnmarshallerFactory dtoUnmarshallerFactory,
TestNGLocalizationConstant localization) {
super(Arrays.asList(PROJECT_PERSPECTIVE_ID), localization.actionRunClassTitle(),
NotificationManager notificationManager,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
TestNGLocalizationConstant localization) {
super(singletonList(PROJECT_PERSPECTIVE_ID), localization.actionRunClassTitle(),
localization.actionRunClassDescription(), null, resources.testIcon());
this.notificationManager = notificationManager;
this.presenter = presenter;
Expand All @@ -83,20 +78,15 @@ public void actionPerformed(ActionEvent e) {
@Override
public void updateInPerspective(@NotNull ActionEvent e) {
Resource resource = appContext.getResource();
if (! (resource instanceof File)) {
if (!(resource instanceof File) || resource.getProject() == null) {
e.getPresentation().setEnabledAndVisible(false);
return;
}

Project project = resource.getProject();
if (project == null) {
e.getPresentation().setEnabledAndVisible(false);
}


e.getPresentation().setVisible(true);

String projectType = project.getType();
if (! "maven".equals(projectType)) {
String projectType = resource.getProject().getType();
if (!"maven".equals(projectType)) {
e.getPresentation().setEnabled(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*******************************************************************************/
package org.eclipse.che.plugin.testing.testng.ide.action;

import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -49,12 +49,12 @@ public class RunTestXMLAction extends AbstractPerspectiveAction

@Inject
public RunTestXMLAction(TestNGResources resources,
NotificationManager notificationManager,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
TestNGLocalizationConstant localization) {
super(Arrays.asList(PROJECT_PERSPECTIVE_ID), localization.actionRunXMLTitle(),
NotificationManager notificationManager,
AppContext appContext,
TestResultPresenter presenter,
TestServiceClient service,
TestNGLocalizationConstant localization) {
super(singletonList(PROJECT_PERSPECTIVE_ID), localization.actionRunXMLTitle(),
localization.actionRunXMLDescription(), null, resources.testAllIcon());
this.notificationManager = notificationManager;
this.presenter = presenter;
Expand All @@ -80,32 +80,27 @@ public void actionPerformed(ActionEvent e) {
@Override
public void updateInPerspective(@NotNull ActionEvent e) {
Resource resource = appContext.getResource();
if (resource == null) {
if (resource == null || resource.getProject() == null) {
e.getPresentation().setEnabledAndVisible(false);
return;
}

Project project = resource.getProject();
if (project == null) {
e.getPresentation().setEnabledAndVisible(false);
}


e.getPresentation().setVisible(true);

String projectType = project.getType();
if (! "maven".equals(projectType)) {
String projectType = resource.getProject().getType();
if (!"maven".equals(projectType)) {
e.getPresentation().setEnabled(false);
}

String expectedXmlFile = MavenAttributes.DEFAULT_TEST_RESOURCES_FOLDER + "/testng.xml";
if (resource instanceof File) {
File file = (File)resource;
String expectedXmlFile = MavenAttributes.DEFAULT_TEST_RESOURCES_FOLDER + "/testng.xml";
e.getPresentation().setEnabled(file.getLocation().toString().endsWith(expectedXmlFile));
} else {
e.getPresentation().setEnabled(true);
}
}

@Override
public NotificationManager getNotificationManager() {
return notificationManager;
Expand Down

0 comments on commit 8720ded

Please sign in to comment.