Skip to content

Commit

Permalink
Merge pull request #2347 from eclipse/CHE-2336
Browse files Browse the repository at this point in the history
CHE-2336: add an action for downloading all projects from the ws
  • Loading branch information
Valeriy Svydenko authored Sep 7, 2016
2 parents 84a7af7 + 7f2fa61 commit d67e004
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import javax.validation.constraints.NotNull;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.api.resources.Resource.PROJECT;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;
Expand Down Expand Up @@ -60,9 +59,12 @@ public DownloadProjectAction(AppContext appContext,
/** {@inheritDoc} */
@Override
public void actionPerformed(ActionEvent e) {
final Project project = appContext.getRootProject();
final Resource resource = appContext.getResource();

checkState(project != null, "Null project occurred");
if (resource == null || resource.getResourceType() != PROJECT) {
return;
}
final Project project = (Project)resource;

downloadContainer.setUrl(project.getURL());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.actions;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.Resources;
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.resources.Project;
import org.eclipse.che.ide.download.DownloadContainer;

import javax.validation.constraints.NotNull;

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

/**
* Download all projects from the workspace.
*
* @author Valeriy Svydenko
*/
@Singleton
public class DownloadWsAction extends AbstractPerspectiveAction {

private final AppContext appContext;
private final DownloadContainer downloadContainer;

@Inject
public DownloadWsAction(AppContext appContext,
CoreLocalizationConstant locale,
Resources resources,
DownloadContainer downloadContainer) {
super(singletonList(PROJECT_PERSPECTIVE_ID),
locale.downloadProjectAsZipName(),
locale.downloadProjectAsZipDescription(),
null,
resources.downloadZip());
this.appContext = appContext;
this.downloadContainer = downloadContainer;
}

/** {@inheritDoc} */
@Override
public void actionPerformed(ActionEvent e) {
downloadContainer.setUrl(appContext.getDevMachine().getWsAgentBaseUrl() + "/project/export/");
}

/** {@inheritDoc} */
@Override
public void updateInPerspective(@NotNull ActionEvent e) {
final Project[] projects = appContext.getProjects();
e.getPresentation().setVisible(true);
e.getPresentation().setEnabled(projects != null && projects.length > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.che.ide.actions.DeleteResourceAction;
import org.eclipse.che.ide.actions.DownloadProjectAction;
import org.eclipse.che.ide.actions.DownloadResourceAction;
import org.eclipse.che.ide.actions.DownloadWsAction;
import org.eclipse.che.ide.actions.EditFileAction;
import org.eclipse.che.ide.actions.ExpandEditorAction;
import org.eclipse.che.ide.actions.FormatterAction;
Expand Down Expand Up @@ -218,6 +219,9 @@ public interface ParserResource extends ClientBundle {
@Inject
private DownloadProjectAction downloadProjectAction;

@Inject
private DownloadWsAction downloadWsAction;

@Inject
private DownloadResourceAction downloadResourceAction;

Expand Down Expand Up @@ -411,8 +415,8 @@ public void initialize() {
actionManager.registerAction("createProject", createProjectAction);
workspaceGroup.add(createProjectAction);

actionManager.registerAction("downloadAsZipAction", downloadProjectAction);
workspaceGroup.add(downloadProjectAction);
actionManager.registerAction("downloadWsAsZipAction", downloadWsAction);
workspaceGroup.add(downloadWsAction);

workspaceGroup.addSeparator();

Expand Down Expand Up @@ -448,6 +452,7 @@ public void initialize() {
actionManager.registerAction("convertFolderToProject", convertFolderToProjectAction);
projectGroup.add(convertFolderToProjectAction);

actionManager.registerAction("downloadAsZipAction", downloadProjectAction);
projectGroup.add(downloadProjectAction);

actionManager.registerAction("showHideHiddenFiles", showHiddenFilesAction);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.actions;

import com.google.gwtmockito.GwtMockitoTestRunner;

import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.Resources;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.api.action.Presentation;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.machine.DevMachine;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.download.DownloadContainer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* @author Valeriy Svydenko
*/
@RunWith(GwtMockitoTestRunner.class)
public class DownloadWsActionTest {
@Mock
private AppContext appContext;
@Mock
private DownloadContainer downloadContainer;
@Mock
private CoreLocalizationConstant locale;
@Mock
private Resources resources;
@Mock
private ActionEvent actionEvent;
@Mock
private Presentation presentation;

@InjectMocks
private DownloadWsAction action;

@Test
public void actionShouldBeInitialized() throws Exception {
verify(locale).downloadProjectAsZipName();
verify(locale).downloadProjectAsZipDescription();
verify(resources).downloadZip();
}

@Test
public void actionShouldBePerformed() throws Exception {
String baseUrl = "baseUrl";

DevMachine devMachine = mock(DevMachine.class);
when(appContext.getDevMachine()).thenReturn(devMachine);
when(devMachine.getWsAgentBaseUrl()).thenReturn(baseUrl);

action.actionPerformed(actionEvent);

verify(downloadContainer).setUrl(eq("baseUrl/project/export/"));
}

@Test
public void actionShouldBeEnable() throws Exception {
Project project = mock(Project.class);

when(actionEvent.getPresentation()).thenReturn(presentation);
Project[] projects = new Project[1];
projects[0] = project;
when(appContext.getProjects()).thenReturn(projects);

action.updateInPerspective(actionEvent);

verify(presentation).setVisible(true);
verify(presentation).setEnabled(true);
}

@Test
public void actionShouldNotBeEnabledIfWsIsEmpty() throws Exception {
when(actionEvent.getPresentation()).thenReturn(presentation);
Project[] projects = new Project[0];
when(appContext.getProjects()).thenReturn(projects);

action.updateInPerspective(actionEvent);

verify(presentation).setVisible(true);
verify(presentation).setEnabled(false);
}

@Test
public void actionShouldNotBeEnabledIfListOfProjectsIsNull() throws Exception {
when(actionEvent.getPresentation()).thenReturn(presentation);
when(appContext.getProjects()).thenReturn(null);

action.updateInPerspective(actionEvent);

verify(presentation).setVisible(true);
verify(presentation).setEnabled(false);
}
}

0 comments on commit d67e004

Please sign in to comment.