Skip to content

Commit

Permalink
Remove .appengine.facets dependency from ui.util (#1183)
Browse files Browse the repository at this point in the history
* Remove .appengine.facets dependency from ui.util
* Make getProject static method
* Don't call empty super method
* Remove Preconditions
* Throw NullPointerException
  • Loading branch information
chanseokoh authored Jan 5, 2017
1 parent dbe4b7c commit 514cf8a
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@

import static org.mockito.Mockito.mock;

import org.eclipse.core.runtime.CoreException;
import com.google.cloud.tools.eclipse.util.FacetedProjectHelper;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.junit.Test;

import com.google.cloud.tools.eclipse.util.FacetedProjectHelper;

public class FacetedProjectHelperTest {

@Test(expected = NullPointerException.class)
public void testGetFacetedProject_nullArgument() throws CoreException {
new FacetedProjectHelper().getFacetedProject(null);
}

@Test(expected = NullPointerException.class)
public void testProjectHasFacet_projectNull() {
new FacetedProjectHelper().projectHasFacet(null, null);
Expand All @@ -40,7 +33,7 @@ public void testProjectHasFacet_projectNull() {
public void testProjectHasFacet_facetIdNull() {
new FacetedProjectHelper().projectHasFacet(mock(IFacetedProject.class), null);
}

@Test(expected = IllegalArgumentException.class)
public void testProjectHasFacet_facetIdEmpty() {
new FacetedProjectHelper().projectHasFacet(mock(IFacetedProject.class), "");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,29 @@

package com.google.cloud.tools.eclipse.appengine.deploy.ui.standard;

import com.google.api.client.auth.oauth2.Credential;
import com.google.cloud.tools.appengine.api.deploy.DefaultDeployConfiguration;
import com.google.cloud.tools.eclipse.appengine.deploy.CleanupOldDeploysJob;
import com.google.cloud.tools.eclipse.appengine.deploy.standard.StandardDeployJob;
import com.google.cloud.tools.eclipse.appengine.deploy.standard.StandardDeployJobConfig;
import com.google.cloud.tools.eclipse.appengine.deploy.standard.StandardDeployPreferences;
import com.google.cloud.tools.eclipse.appengine.deploy.standard.StandardDeployPreferencesConverter;
import com.google.cloud.tools.eclipse.appengine.deploy.ui.DeployConsole;
import com.google.cloud.tools.eclipse.appengine.deploy.ui.DeployPreferencesDialog;
import com.google.cloud.tools.eclipse.appengine.deploy.ui.Messages;
import com.google.cloud.tools.eclipse.appengine.login.IGoogleLoginService;
import com.google.cloud.tools.eclipse.sdk.ui.MessageConsoleWriterOutputLineListener;
import com.google.cloud.tools.eclipse.ui.util.MessageConsoleUtilities;
import com.google.cloud.tools.eclipse.ui.util.ProjectFromSelectionHelper;
import com.google.cloud.tools.eclipse.ui.util.ServiceUtils;
import com.google.cloud.tools.eclipse.usagetracker.AnalyticsEvents;
import com.google.cloud.tools.eclipse.usagetracker.AnalyticsPingManager;
import java.io.IOException;
import java.nio.file.Files;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
Expand All @@ -40,26 +56,8 @@
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.handlers.HandlerUtil;

import com.google.api.client.auth.oauth2.Credential;
import com.google.cloud.tools.appengine.api.deploy.DefaultDeployConfiguration;
import com.google.cloud.tools.eclipse.appengine.deploy.CleanupOldDeploysJob;
import com.google.cloud.tools.eclipse.appengine.deploy.standard.StandardDeployJob;
import com.google.cloud.tools.eclipse.appengine.deploy.standard.StandardDeployJobConfig;
import com.google.cloud.tools.eclipse.appengine.deploy.standard.StandardDeployPreferences;
import com.google.cloud.tools.eclipse.appengine.deploy.standard.StandardDeployPreferencesConverter;
import com.google.cloud.tools.eclipse.appengine.deploy.ui.DeployConsole;
import com.google.cloud.tools.eclipse.appengine.deploy.ui.DeployPreferencesDialog;
import com.google.cloud.tools.eclipse.appengine.deploy.ui.Messages;
import com.google.cloud.tools.eclipse.appengine.login.IGoogleLoginService;
import com.google.cloud.tools.eclipse.sdk.ui.MessageConsoleWriterOutputLineListener;
import com.google.cloud.tools.eclipse.ui.util.MessageConsoleUtilities;
import com.google.cloud.tools.eclipse.ui.util.ProjectFromSelectionHelper;
import com.google.cloud.tools.eclipse.ui.util.ServiceUtils;
import com.google.cloud.tools.eclipse.usagetracker.AnalyticsEvents;
import com.google.cloud.tools.eclipse.usagetracker.AnalyticsPingManager;
import com.google.cloud.tools.eclipse.util.FacetedProjectHelper;
import com.google.common.annotations.VisibleForTesting;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;

/**
* Command handler to deploy a web application project to App Engine Standard.
Expand All @@ -71,40 +69,36 @@ public class StandardDeployCommandHandler extends AbstractHandler {

private static final String CONSOLE_NAME = "App Engine Deploy";

private ProjectFromSelectionHelper helper;

public StandardDeployCommandHandler() {
this(new FacetedProjectHelper());
}

@VisibleForTesting
StandardDeployCommandHandler(FacetedProjectHelper facetedProjectHelper) {
this.helper = new ProjectFromSelectionHelper(facetedProjectHelper);
}

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
IProject project = helper.getAppEngineStandardProject(event);
if (project != null) {
if (!checkProjectErrors(project)) {
MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
Messages.getString("build.error.dialog.title"),
Messages.getString("build.error.dialog.message"));
return null;
}

IGoogleLoginService loginService = ServiceUtils.getService(event, IGoogleLoginService.class);
DeployPreferencesDialog dialog =
new DeployPreferencesDialog(HandlerUtil.getActiveShell(event), project, loginService);
if (dialog.open() == Window.OK) {
launchDeployJob(project, dialog.getCredential());
}
IProject project = ProjectFromSelectionHelper.getProject(event);
if (project == null) {
throw new NullPointerException("Deploy menu enabled for non-project resources");
}
IFacetedProject facetedProject = ProjectFacetsManager.create(project);
if (facetedProject == null) {
throw new NullPointerException("Deploy menu enabled for non-faceted projects");
}

if (!checkProjectErrors(project)) {
MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
Messages.getString("build.error.dialog.title"),
Messages.getString("build.error.dialog.message"));
return null;
}

IGoogleLoginService loginService = ServiceUtils.getService(event, IGoogleLoginService.class);
DeployPreferencesDialog dialog =
new DeployPreferencesDialog(HandlerUtil.getActiveShell(event), project, loginService);
if (dialog.open() == Window.OK) {
launchDeployJob(project, dialog.getCredential());
}
// return value must be null, reserved for future use
return null;
} catch (CoreException | IOException exception) {
throw new ExecutionException(Messages.getString("deploy.failed.error.message"), exception); //$NON-NLS-1$
throw new ExecutionException(
Messages.getString("deploy.failed.error.message"), exception); //$NON-NLS-1$
}
}

Expand Down Expand Up @@ -137,14 +131,13 @@ private void launchDeployJob(IProject project, Credential credential)

@Override
public void done(IJobChangeEvent event) {
super.done(event);
AnalyticsPingManager.getInstance().sendPing(AnalyticsEvents.APP_ENGINE_DEPLOY_SUCCESS,
AnalyticsEvents.APP_ENGINE_DEPLOY_STANDARD, null);
launchCleanupJob();
}
});
deploy.schedule();

IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
consoleManager.showConsoleView(messageConsole);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import static org.junit.Assert.assertTrue;

import com.google.cloud.tools.eclipse.test.util.project.ProjectUtils;
import com.google.cloud.tools.eclipse.util.FacetedProjectHelper;
import java.io.IOException;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntime;
import org.junit.After;
import org.junit.Test;
Expand All @@ -54,7 +54,7 @@ public void testStandardFacetInstallation() throws IOException, CoreException {
ProjectUtils.importProjects(getClass(), "projects/test-dynamic-web-project.zip", null);
assertEquals(1, projects.size());
IProject project = projects.get(0);
IFacetedProject facetedProject = new FacetedProjectHelper().getFacetedProject(project);
IFacetedProject facetedProject = ProjectFacetsManager.create(project);
// verify that the appengine-web.xml is installed in the dynamic web root folder
AppEngineStandardFacet.installAppEngineFacet(facetedProject, true, null);
IFile correctAppEngineWebXml = project.getFile(new Path("war/WEB-INF/appengine-web.xml"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@

import org.eclipse.core.runtime.CoreException;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.junit.Test;

public class DeployPropertyPageTest extends AbstractProjectTests {
@Test
public void testPropertyPageTitle_standardProject() throws CoreException {
String projectName = "foo";
project = SwtBotAppEngineActions.createNativeWebAppProject(bot, projectName, null, null);
IFacetedProject facetedProject = new FacetedProjectHelper().getFacetedProject(project);
IFacetedProject facetedProject = ProjectFacetsManager.create(project);
assertNotNull("Native App Engine projects should be faceted", facetedProject);
assertTrue(
new FacetedProjectHelper().projectHasFacet(facetedProject, AppEngineStandardFacet.ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.core.runtime.Path;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -96,7 +97,7 @@ private void createAndCheck(String artifactId, String location,
project.getLocation().toFile().getParentFile().getCanonicalPath());
}

IFacetedProject facetedProject = new FacetedProjectHelper().getFacetedProject(project);
IFacetedProject facetedProject = ProjectFacetsManager.create(project);
assertNotNull("m2e-wtp should create a faceted project", facetedProject);
assertTrue("Project does not have standard facet",
new FacetedProjectHelper().projectHasFacet(facetedProject, AppEngineStandardFacet.ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.core.runtime.Path;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -61,7 +62,7 @@ private void createAndCheck(String projectName, String packageName, String[] pro
packageName);
assertTrue(project.exists());

IFacetedProject facetedProject = new FacetedProjectHelper().getFacetedProject(project);
IFacetedProject facetedProject = ProjectFacetsManager.create(project);
assertNotNull("Native App Engine projects should be faceted", facetedProject);
assertTrue("Project does not have standard facet",
new FacetedProjectHelper().projectHasFacet(facetedProject, AppEngineStandardFacet.ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Require-Bundle: org.eclipse.ui.workbench
Import-Package: com.google.cloud.tools.eclipse.appengine.facets,
com.google.cloud.tools.eclipse.ui.util,
Import-Package: com.google.cloud.tools.eclipse.ui.util,
com.google.cloud.tools.eclipse.util,
com.google.cloud.tools.eclipse.util.status,
com.google.cloud.tools.project;version="0.1.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,22 @@

package com.google.cloud.tools.eclipse.ui.util;

import com.google.cloud.tools.eclipse.appengine.facets.AppEngineStandardFacet;
import com.google.cloud.tools.eclipse.util.AdapterUtil;
import com.google.cloud.tools.eclipse.util.FacetedProjectHelper;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;

public class ProjectFromSelectionHelper {

private FacetedProjectHelper facetedProjectHelper;

public ProjectFromSelectionHelper(FacetedProjectHelper facetedProjectHelper) {
this.facetedProjectHelper = facetedProjectHelper;
}

public IProject getAppEngineStandardProject(ExecutionEvent event)
throws CoreException, ExecutionException {
public static IProject getProject(ExecutionEvent event) throws ExecutionException {
ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
if (structuredSelection.size() == 1) {
IProject project = AdapterUtil.adapt(structuredSelection.getFirstElement(), IProject.class);
if (project == null) {
return null;
}

IFacetedProject facetedProject = facetedProjectHelper.getFacetedProject(project);
if (AppEngineStandardFacet.hasAppEngineFacet(facetedProject)) {
return project;
}
return AdapterUtil.adapt(structuredSelection.getFirstElement(), IProject.class);
}
}
return null;
Expand Down
Loading

0 comments on commit 514cf8a

Please sign in to comment.