Skip to content

Commit

Permalink
[fix] dboissier#244 Label is now mandatory to prevent NPE during Serv…
Browse files Browse the repository at this point in the history
…er tree building + (add DumbAware on all actions)
  • Loading branch information
dboissier committed Jul 26, 2018
1 parent 3ebf6dc commit 012f2ac
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 39 deletions.
Binary file modified snapshot/mongo4idea-0.12.1-SNAPSHOT.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import org.codinjutsu.tools.mongo.ServerConfiguration;
import org.codinjutsu.tools.mongo.logic.ConfigurationException;
import org.codinjutsu.tools.mongo.logic.MongoManager;
Expand Down Expand Up @@ -54,9 +55,9 @@ protected JComponent createCenterPanel() {
protected void doOKAction() {
try {
serverConfigurationPanel.applyConfigurationData(configuration);
super.doOKAction();
} catch (ConfigurationException confEx) {
serverConfigurationPanel.setErrorMessage(confEx.getMessage());
}
super.doOKAction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,28 +234,8 @@ private ServerConfiguration createServerConfigurationForTesting() {
return configuration;
}

private void validateUrls() {
List<String> serverUrls = getServerUrls();
if (serverUrls == null) {
throw new ConfigurationException("URL(s) should be set");
}
for (String serverUrl : serverUrls) {
String[] host_port = serverUrl.split(":");
if (host_port.length < 2) {
throw new ConfigurationException(String.format("URL '%s' format is incorrect. It should be 'host:port'", serverUrl));
}

try {
Integer.valueOf(host_port[1]);
} catch (NumberFormatException e) {
throw new ConfigurationException(String.format("Port in the URL '%s' is incorrect. It should be a number", serverUrl));
}
}

}


public void applyConfigurationData(ServerConfiguration configuration) {
validateLabel();
validateUrls();

configuration.setLabel(getLabel());
Expand All @@ -278,6 +258,34 @@ public void applyConfigurationData(ServerConfiguration configuration) {
configuration.setSshTunnelingConfiguration(isSshTunneling() ? createSshTunnelingSettings() : SshTunnelingConfiguration.EMPTY);
}

private void validateLabel() {
String label = getLabel();
if (StringUtils.isBlank(label)) {
throw new ConfigurationException("Label should be set");
}
}

private void validateUrls() {
List<String> serverUrls = getServerUrls();
if (serverUrls == null) {
throw new ConfigurationException("URL(s) should be set");
}
for (String serverUrl : serverUrls) {
String[] host_port = serverUrl.split(":");
if (host_port.length < 2) {
throw new ConfigurationException(String.format("URL '%s' format is incorrect. It should be 'host:port'", serverUrl));
}

try {
Integer.valueOf(host_port[1]);
} catch (NumberFormatException e) {
throw new ConfigurationException(String.format("Port in the URL '%s' is incorrect. It should be a number", serverUrl));
}
}

}


private SshTunnelingConfiguration createSshTunnelingSettings() {
return new SshTunnelingConfiguration(
getSshProxyHost(), getSshProxyUser(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.view.edition.AddKeyDialog;
import org.codinjutsu.tools.mongo.view.edition.MongoEditionPanel;

import java.awt.event.KeyEvent;

public class AddKeyAction extends AnAction {
public class AddKeyAction extends AnAction implements DumbAware {

private final MongoEditionPanel mongoEditionPanel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.view.edition.AddValueDialog;
import org.codinjutsu.tools.mongo.view.edition.MongoEditionPanel;

public class AddValueAction extends AnAction {
public class AddValueAction extends AnAction implements DumbAware {

private final MongoEditionPanel mongoEditionPanel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.view.edition.MongoEditionPanel;

import java.awt.event.KeyEvent;

public class DeleteKeyAction extends AnAction {
public class DeleteKeyAction extends AnAction implements DumbAware {

private final MongoEditionPanel mongoEditionPanel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.MongoConfiguration;
import org.codinjutsu.tools.mongo.ServerConfiguration;
import org.codinjutsu.tools.mongo.view.ConfigurationDialog;
import org.codinjutsu.tools.mongo.view.MongoExplorerPanel;

public class AddServerAction extends AnAction {
public class AddServerAction extends AnAction implements DumbAware {
private final MongoExplorerPanel mongoExplorerPanel;

public AddServerAction(MongoExplorerPanel mongoExplorerPanel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.SystemInfo;
import org.codinjutsu.tools.mongo.model.MongoCollection;
import org.codinjutsu.tools.mongo.model.MongoDatabase;
Expand All @@ -27,7 +28,7 @@
import javax.swing.*;
import java.awt.event.KeyEvent;

public class DeleteAction extends AnAction {
public class DeleteAction extends AnAction implements DumbAware {
private final MongoExplorerPanel mongoExplorerPanel;

public DeleteAction(MongoExplorerPanel mongoExplorerPanel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.ServerConfiguration;
import org.codinjutsu.tools.mongo.model.MongoServer;
import org.codinjutsu.tools.mongo.view.ConfigurationDialog;
Expand All @@ -27,7 +28,7 @@
import java.awt.*;
import java.awt.event.KeyEvent;

public class EditServerAction extends AnAction {
public class EditServerAction extends AnAction implements DumbAware {
private final MongoExplorerPanel mongoExplorerPanel;

public EditServerAction(MongoExplorerPanel mongoExplorerPanel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.ToggleAction;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.utils.GuiUtils;
import org.codinjutsu.tools.mongo.view.QueryPanel;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class EnableAggregateAction extends ToggleAction {
public class EnableAggregateAction extends ToggleAction implements DumbAware {

private static final String ENABLE_FIND_MODE = "Toggle to Find Mode";
private static final String ENABLE_AGGREGATION_MODE = "Toggle to Aggregation Mode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.view.MongoPanel;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class ExecuteQuery extends AnAction {
public class ExecuteQuery extends AnAction implements DumbAware {
private final MongoPanel mongoPanel;

public ExecuteQuery(MongoPanel mongoPanel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.view.MongoPanel;

public class NavigateBackwardAction extends AnAction {
public class NavigateBackwardAction extends AnAction implements DumbAware {
private final MongoPanel mongoPanel;

public NavigateBackwardAction(MongoPanel mongoPanel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.PopupChooserBuilder;
import com.intellij.ui.components.JBList;
Expand All @@ -36,7 +37,7 @@
import java.util.LinkedList;
import java.util.List;

public class OperatorCompletionAction extends AnAction implements Disposable {
public class OperatorCompletionAction extends AnAction implements Disposable, DumbAware {

private static final String MONGO_OPERATOR_COMPLETION = "MONGO_OPERATOR_COMPLETION";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.view.MongoPanel;
import org.codinjutsu.tools.mongo.view.MongoResultPanel;

public class ViewAsTableAction extends AnAction {
public class ViewAsTableAction extends AnAction implements DumbAware {
private final MongoPanel mongoPanel;

public ViewAsTableAction(MongoPanel mongoPanel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import org.codinjutsu.tools.mongo.view.MongoPanel;
import org.codinjutsu.tools.mongo.view.MongoResultPanel;

public class ViewAsTreeAction extends AnAction {
public class ViewAsTreeAction extends AnAction implements DumbAware {
private final MongoPanel mongoPanel;

public ViewAsTreeAction(MongoPanel mongoPanel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ public class MongoTreeBuilder extends AbstractTreeBuilder {

public MongoTreeBuilder(@NotNull Tree tree) {
init(tree, new DefaultTreeModel(new DefaultMutableTreeNode()), new MyTreeStructure(), (descriptorLeft, descriptorRight) -> {
if (descriptorLeft.getElement() == null || descriptorRight.getElement() == null) {
return 0;
}

if (descriptorLeft instanceof ServerDescriptor && descriptorRight instanceof ServerDescriptor) {
MongoServer mongoServerLeft = (MongoServer) descriptorLeft.getElement();
MongoServer mongoServerRight = (MongoServer) descriptorRight.getElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,18 @@ public void loadFormWithSSHTunneling() {
frameFixture.textBox("sshProxyPasswordField").requireText("mySecuredPassword");
}

@Test
public void validateFormWithMissingLabelShouldThrowAConfigurationException() {
thrown.expect(ConfigurationException.class);
thrown.expectMessage("Label should be set");

configurationPanel.applyConfigurationData(new ServerConfiguration());
}
@Test
public void validateFormWithMissingMongoUrlShouldThrowAConfigurationException() {
thrown.expect(ConfigurationException.class);
thrown.expectMessage("URL(s) should be set");
frameFixture.textBox("labelField").setText("MyServer");

frameFixture.textBox("serverUrlsField").setText(null);

Expand All @@ -248,6 +256,7 @@ public void validateFormWithMissingMongoUrlShouldThrowAConfigurationException()
public void validateFormWithEmptyMongoUrlShouldThrowAConfigurationException() {
thrown.expect(ConfigurationException.class);
thrown.expectMessage("URL(s) should be set");
frameFixture.textBox("labelField").setText("MyServer");

frameFixture.textBox("serverUrlsField").setText("");

Expand All @@ -258,6 +267,7 @@ public void validateFormWithEmptyMongoUrlShouldThrowAConfigurationException() {
public void validateFormWithBadMongoUrlShouldThrowAConfigurationException() {
thrown.expect(ConfigurationException.class);
thrown.expectMessage("URL 'host' format is incorrect. It should be 'host:port'");
frameFixture.textBox("labelField").setText("MyServer");

frameFixture.textBox("serverUrlsField").setText("host");

Expand All @@ -268,6 +278,8 @@ public void validateFormWithBadMongoUrlShouldThrowAConfigurationException() {
public void validateFormWithBadMongoPortShouldThrowAConfigurationException() {
thrown.expect(ConfigurationException.class);
thrown.expectMessage("Port in the URL 'host:port' is incorrect. It should be a number");
frameFixture.textBox("labelField").setText("MyServer");


frameFixture.textBox("serverUrlsField").setText("host:port");

Expand All @@ -276,7 +288,7 @@ public void validateFormWithBadMongoPortShouldThrowAConfigurationException() {

@Test
public void validateFormWithReplicatSet() {

frameFixture.textBox("labelField").setText("MyServer");
frameFixture.textBox("serverUrlsField").setText(" localhost:25, localhost:26 ");

ServerConfiguration configuration = new ServerConfiguration();
Expand Down

0 comments on commit 012f2ac

Please sign in to comment.