Skip to content

Commit

Permalink
Allow creating Servlet 2.5 project with App Engine Java 8 runtime (#3324
Browse files Browse the repository at this point in the history
)

* Add new AppEngineRuntime enum value
* Add Java 8 Servlet 2.5 runtime support
  • Loading branch information
chanseokoh authored Dec 21, 2018
1 parent d127f70 commit 5c43fce
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ public class CloudSdkStagingHelperTest {

@Mock private IProgressMonitor monitor;

private IPath sourceDirectory;
private IPath stagingDirectory;
private IProject project;

@Before
public void setUp() {
project = projectCreator.getProject();
stagingDirectory = new Path(tempFolder.getRoot().toString());
sourceDirectory = new Path(tempFolder.getRoot().toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ public static boolean isAppEngineStandardRuntime(
* @param monitor the progress monitor
* @throws CoreException if anything goes wrong during install
*/
public static void installAppEngineFacet(final IFacetedProject facetedProject,
final boolean installDependentFacets, final IProgressMonitor monitor) throws CoreException {
public static void installAppEngineFacet(IFacetedProject facetedProject,
boolean installDependentFacets, IProgressMonitor monitor) throws CoreException {
ILock lock = acquireLock(facetedProject.getProject());
try {
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.mockito.Mockito.mock;

import com.google.cloud.tools.eclipse.appengine.libraries.model.Library;
import com.google.cloud.tools.eclipse.appengine.ui.AppEngineRuntime;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
Expand Down Expand Up @@ -58,9 +59,12 @@ public void testServiceName() {

@Test
public void testRuntime() {
Assert.assertNull(config.getRuntimeId());
config.setRuntimeId("foobar");
Assert.assertEquals("foobar", config.getRuntimeId());
Assert.assertNull(config.getRuntime());
config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8);
Assert.assertEquals(AppEngineRuntime.STANDARD_JAVA_8, config.getRuntime());

config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8_SERVLET_25);
Assert.assertEquals(AppEngineRuntime.STANDARD_JAVA_8_SERVLET_25, config.getRuntime());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,46 +82,74 @@ public void setUp() throws CoreException {
public void testMaterializeAppEngineStandardFiles_java8()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setRuntimeId(AppEngineRuntime.STANDARD_JAVA_8.getId());
config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8);
IFile mostImportant = CodeTemplates.materializeAppEngineStandardFiles(project, config, monitor);
validateNonConfigFiles(mostImportant, "http://xmlns.jcp.org/xml/ns/javaee",
"http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd", "3.1");
validateAppEngineWebXml(AppEngineRuntime.STANDARD_JAVA_8);
validateAppEngineWebXml(config.getRuntime());
validateLoggingProperties();
}

@Test
public void testMaterializeAppEngineStandardFiles_noObjectifyWithJava8()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setRuntimeId(AppEngineRuntime.STANDARD_JAVA_8.getId());
config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8);

CodeTemplates.materializeAppEngineStandardFiles(project, config, monitor);
assertFalse(objectifyFilterClassExists());
assertFalse(objectifyListenerClassExists());
validateObjectifyFilterConfigInWebXml(false);
}

@Test
public void testMaterializeAppEngineStandardFiles_objectifyWithJava8()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setRuntimeId(AppEngineRuntime.STANDARD_JAVA_8.getId());
config.setAppEngineLibraries(Collections.singleton(new Library("objectify")));
config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8);
config.setAppEngineLibraries(Collections.singleton(new Library("objectify"))); // Objectify 5

CodeTemplates.materializeAppEngineStandardFiles(project, config, monitor);
assertTrue(objectifyFilterClassExists());
assertFalse(objectifyListenerClassExists()); // listener added only for Objectify 6
validateObjectifyFilterConfigInWebXml(false);
}

@Test
public void testMaterializeAppEngineStandardFiles_noObjectifyListenerWithObjectify5()
throws CoreException {
public void testMaterializeAppEngineStandardFiles_java8Servlet25()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8_SERVLET_25);
IFile mostImportant = CodeTemplates.materializeAppEngineStandardFiles(project, config, monitor);
validateNonConfigFiles(mostImportant, "http://java.sun.com/xml/ns/javaee",
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd", "2.5");
validateAppEngineWebXml(config.getRuntime());
validateLoggingProperties();
}

@Test
public void testMaterializeAppEngineStandardFiles_noObjectifyWithJava8Servlet25()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8_SERVLET_25);

CodeTemplates.materializeAppEngineStandardFiles(project, config, monitor);
assertFalse(objectifyFilterClassExists());
assertFalse(objectifyListenerClassExists());
validateObjectifyFilterConfigInWebXml(false);
}

@Test
public void testMaterializeAppEngineStandardFiles_objectifyWithJava8Servlet25()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setRuntimeId(AppEngineRuntime.STANDARD_JAVA_8.getId());
config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8_SERVLET_25);
config.setAppEngineLibraries(Collections.singleton(new Library("objectify")));

CodeTemplates.materializeAppEngineStandardFiles(project, config, monitor);
assertFalse(objectifyFilterClassExists());
assertFalse(objectifyListenerClassExists());
validateObjectifyFilterConfigInWebXml(true);
}

@Test
Expand Down Expand Up @@ -238,11 +266,24 @@ public void testIsObjectify6Selected_objectify6() {
}

@Test
public void testIsServlet25Selected() {
assertFalse(CodeTemplates.isServlet25Selected(null));
public void testIsServlet25Selected_nullRuntime() {
assertFalse(CodeTemplates.isServlet25Selected(new AppEngineProjectConfig()));
}

@Test
public void testIsServlet25Selected_java8Runtime() {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8);
assertFalse(CodeTemplates.isServlet25Selected(config));
}

@Test
public void testIsServlet25Selected_java8Servlet25Runtime() {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setRuntime(AppEngineRuntime.STANDARD_JAVA_8_SERVLET_25);
assertTrue(CodeTemplates.isServlet25Selected(config));
}

private boolean objectifyListenerClassExists() {
return project.getFile("src/main/java/ObjectifyWebListener.java").exists();
}
Expand Down Expand Up @@ -330,12 +371,12 @@ private void validateAppEngineWebXml(AppEngineRuntime runtime)

NodeList runtimeElements = doc.getDocumentElement().getElementsByTagNameNS(
"http://appengine.google.com/ns/1.0", "runtime");
if (runtime.getId() == null) {
if (runtime.getRuntimeId() == null) {
Assert.assertEquals("should not have a <runtime> element", 0, runtimeElements.getLength());
} else {
Assert.assertEquals("should have exactly 1 <runtime> element", 1,
runtimeElements.getLength());
Assert.assertEquals(runtime.getId(), runtimeElements.item(0).getTextContent());
Assert.assertEquals(runtime.getRuntimeId(), runtimeElements.item(0).getTextContent());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testDescription() {
public void testDefaultRuntime() {
Assert.assertEquals(AppEngineRuntime.STANDARD_JAVA_8,
AppEngineStandardWizardPage.DEFAULT_RUNTIME);
Assert.assertEquals(AppEngineStandardWizardPage.DEFAULT_RUNTIME.getId(), page.getRuntimeId());
Assert.assertEquals(AppEngineStandardWizardPage.DEFAULT_RUNTIME, page.getRuntime());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.tools.eclipse.appengine.newproject;

import com.google.cloud.tools.eclipse.appengine.libraries.model.Library;
import com.google.cloud.tools.eclipse.appengine.ui.AppEngineRuntime;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -33,7 +34,7 @@ public class AppEngineProjectConfig {
private IProject project;
private List<Library> appEngineLibraries = Collections.emptyList();
private String serviceName;
private String runtimeId;
private AppEngineRuntime runtime;

private boolean useMaven;
private String mavenGroupId;
Expand Down Expand Up @@ -83,12 +84,12 @@ void setServiceName(String serviceName) {
this.serviceName = serviceName;
}

String getRuntimeId() {
return runtimeId;
AppEngineRuntime getRuntime() {
return runtime;
}

void setRuntimeId(String runtimeId) {
this.runtimeId = runtimeId;
void setRuntime(AppEngineRuntime runtime) {
this.runtime = runtime;
}

void setUseMaven(String mavenGroupId, String mavenArtifactId, String mavenVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public boolean performFinish() {
private void retrieveConfigurationValues() {
config.setServiceName(appEnginePage.getServiceName());
config.setPackageName(appEnginePage.getPackageName());
config.setRuntimeId(appEnginePage.getRuntimeId());
config.setRuntime(appEnginePage.getRuntime());
config.setProject(appEnginePage.getProjectHandle());
if (!appEnginePage.useDefaults()) {
config.setEclipseProjectLocationUri(appEnginePage.getLocationURI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.cloud.tools.eclipse.appengine.libraries.ui.LibrarySelectorGroup;
import com.google.cloud.tools.eclipse.appengine.newproject.maven.MavenCoordinatesWizardUi;
import com.google.cloud.tools.eclipse.appengine.ui.AppEngineImages;
import com.google.cloud.tools.eclipse.appengine.ui.AppEngineRuntime;
import com.google.cloud.tools.eclipse.util.JavaPackageValidator;
import com.google.cloud.tools.eclipse.util.MavenCoordinatesValidator;
import com.google.cloud.tools.project.ServiceNameValidator;
Expand Down Expand Up @@ -112,7 +113,7 @@ protected void createRuntimeField(@SuppressWarnings("unused") Composite composit
// default: do nothing; used by subclasses
}

public String getRuntimeId() {
public AppEngineRuntime getRuntime() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.tools.eclipse.appengine.newproject;

import com.google.cloud.tools.eclipse.appengine.libraries.model.Library;
import com.google.cloud.tools.eclipse.appengine.ui.AppEngineRuntime;
import com.google.cloud.tools.eclipse.util.ArtifactRetriever;
import com.google.cloud.tools.eclipse.util.Templates;
import com.google.cloud.tools.eclipse.util.io.ResourceUtils;
Expand Down Expand Up @@ -154,11 +155,11 @@ private static void createAppEngineConfigFiles(IProject project,
if (!Strings.isNullOrEmpty(service)) {
properties.put("service", service); //$NON-NLS-1$
}
String runtime = config.getRuntimeId();
if (Strings.isNullOrEmpty(runtime)) {
AppEngineRuntime runtime = config.getRuntime();
if (runtime == null) {
properties.put("runtime", "java8"); //$NON-NLS-1$ //$NON-NLS-2$
} else {
properties.put("runtime", runtime); //$NON-NLS-1$
properties.put("runtime", runtime.getRuntimeId()); //$NON-NLS-1$
}

IFolder webInf = project.getFolder("src/main/webapp/WEB-INF"); //$NON-NLS-1$
Expand Down Expand Up @@ -205,7 +206,7 @@ private static void createWebXml(IProject project, AppEngineProjectConfig config

@VisibleForTesting
static boolean isServlet25Selected(AppEngineProjectConfig config) {
return false;
return config.getRuntime() == AppEngineRuntime.STANDARD_JAVA_8_SERVLET_25;
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,13 @@ public String getText(Object element) {
}

@Override
public String getRuntimeId() {
AppEngineRuntime selected = DEFAULT_RUNTIME;
public AppEngineRuntime getRuntime() {
if (runtimeField != null && !runtimeField.getSelection().isEmpty()) {
Preconditions.checkState(runtimeField.getSelection() instanceof IStructuredSelection,
"ComboViewer should return an IStructuredSelection");
IStructuredSelection selection = (IStructuredSelection) runtimeField.getSelection();
selected = (AppEngineRuntime) selection.getFirstElement();
return (AppEngineRuntime) selection.getFirstElement();
}
return selected.getId();
return DEFAULT_RUNTIME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.tools.eclipse.appengine.standard.java8;

import static org.junit.Assert.assertEquals;

import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator;
import com.google.cloud.tools.eclipse.util.io.ResourceUtils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jst.j2ee.web.project.facet.WebFacetUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class AppEngineStandardJre8ProjectFacetDetectorTest {

@Rule public final TestProjectCreator projectCreator = new TestProjectCreator();

private IProject project;

@Before
public void setUp() {
project = projectCreator.getProject();
}

@Test
public void testGetWebFacetVersionToInstall_noWebXml() {
assertEquals(WebFacetUtils.WEB_31,
AppEngineStandardJre8ProjectFacetDetector.getWebFacetVersionToInstall(project));
}

@Test
public void testGetWebFacetVersionToInstall_servlet25WebXml() throws CoreException {
createFile("src/main/webapp/WEB-INF/web.xml", "<web-app version='2.5'/>");
assertEquals(WebFacetUtils.WEB_25,
AppEngineStandardJre8ProjectFacetDetector.getWebFacetVersionToInstall(project));
}

@Test
public void testGetWebFacetVersionToInstall_servlet31WebXml() throws CoreException {
createFile("src/main/webapp/WEB-INF/web.xml", "<web-app version='3.1'/>");
assertEquals(WebFacetUtils.WEB_31,
AppEngineStandardJre8ProjectFacetDetector.getWebFacetVersionToInstall(project));
}

@Test
public void testGetWebFacetVersionToInstall_noVersionWebXml() throws CoreException {
createFile("src/main/webapp/WEB-INF/web.xml", "<web-app/>");
assertEquals(WebFacetUtils.WEB_31,
AppEngineStandardJre8ProjectFacetDetector.getWebFacetVersionToInstall(project));
}

@Test
public void testGetWebFacetVersionToInstall_invalidWebXml() throws CoreException {
createFile("src/main/webapp/WEB-INF/web.xml", "not XML");
assertEquals(WebFacetUtils.WEB_31,
AppEngineStandardJre8ProjectFacetDetector.getWebFacetVersionToInstall(project));
}

@Test
public void testGetWebFacetVersionToInstall_webContentFolder() throws CoreException {
createFile("WebContent/WEB-INF/web.xml", "<web-app version='2.5'/>");
assertEquals(WebFacetUtils.WEB_25,
AppEngineStandardJre8ProjectFacetDetector.getWebFacetVersionToInstall(project));
}

private IFile createFile(String path, String content) throws CoreException {
InputStream in = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
IFile file = project.getFile(path);
ResourceUtils.createFolders(file.getParent(), null);
file.create(in, true, null);
return file;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Import-Package: com.google.cloud.tools.appengine;version="[0.6.8,0.7.0)",
com.google.cloud.tools.eclipse.ui.util,
com.google.cloud.tools.eclipse.util,
com.google.cloud.tools.eclipse.util.status,
com.google.common.annotations;version="[26.0.0,27.0.0)",
com.google.common.base;version="[26.0.0,27.0.0)",
com.google.common.io;version="[26.0.0,27.0.0)",
org.eclipse.core.commands,
Expand Down
Loading

0 comments on commit 5c43fce

Please sign in to comment.