Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.cloud.tools.eclipse.util.status.StatusUtil;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import java.io.File;
import org.apache.maven.project.MavenProject;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -113,8 +114,15 @@ private static IPath getFinalArtifactPath(IProject project) throws CoreException

String buildDirectory = mavenProject.getBuild().getDirectory();
String finalName = mavenProject.getBuild().getFinalName();
String finalArtifactPath = buildDirectory + "/" + finalName + "." + mavenProject.getPackaging();
return new Path(finalArtifactPath);
String finalNamePath = buildDirectory + "/" + finalName;

String oneJarArtifactPath = finalNamePath + ".one-jar.jar";
if (new File(oneJarArtifactPath).exists()) {
return new Path(oneJarArtifactPath);
} else {
String finalArtifactPath = finalNamePath + "." + mavenProject.getPackaging();
return new Path(finalArtifactPath);
}
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void testMaterializeAppEngineStandardFiles_pomXmlIfEnablingMaven()
public void testMaterializeAppEngineFlexFiles()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setServiceName("database-service");
IFile mostImportant = CodeTemplates.materializeAppEngineFlexFiles(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");
Expand All @@ -124,19 +125,77 @@ public void testMaterializeAppEngineFlexFiles_pomXmlIfEnablingMaven()
validatePomXml();
}

@Test
public void testMaterializeAppEngineFlexJarFiles()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setUseMaven("my.project.group.id", "my-project-artifact-id", "98.76.54");
config.setServiceName("database-service");

IFile mostImportant = CodeTemplates.materializeFlexJar(project, config, monitor);

validateFlexJarNonConfigFiles(mostImportant);
validateAppYaml();
validatePomXml();
}

@Test
public void testMaterializeFlexSpringBoot()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setUseMaven("my.project.group.id", "my-project-artifact-id", "98.76.54");
config.setServiceName("database-service");

IFile mostImportant = CodeTemplates.materializeFlexSpringBoot(project, config, monitor);

validateFlexSpringBootNonConfigFiles(mostImportant);
validateAppYaml();
validatePomXml();
}

@Test
public void testMaterializeAppEngineFlexJarFiles_mainClassSet()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setUseMaven("my.project.group.id", "my-project-artifact-id", "98.76.54");
config.setPackageName("com.example");

IFile mostImportant = CodeTemplates.materializeFlexJar(project, config, monitor);
validateMainClassInPomXml("com.example", mostImportant);
}

@Test
public void testMaterializeAppEngineFlexJarFiles_defaultPackageMainClassSet()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setUseMaven("my.project.group.id", "my-project-artifact-id", "98.76.54");

IFile mostImportant = CodeTemplates.materializeFlexJar(project, config, monitor);
validateMainClassInPomXml(null /* expectedPackage */, mostImportant);
}

private void validateMainClassInPomXml(String expectedPackage, IFile mostImportant)
throws ParserConfigurationException, SAXException, IOException, CoreException {
String mainClassName = mostImportant.getFullPath().removeFileExtension().lastSegment();

IFile pomXml = project.getFile("pom.xml");
Element root = buildDocument(pomXml).getDocumentElement();
Element mainClass = (Element) root.getElementsByTagName("mainClass").item(0);
if (expectedPackage == null) {
Assert.assertEquals(mainClassName, mainClass.getTextContent());
} else {
Assert.assertEquals(expectedPackage + "." + mainClassName, mainClass.getTextContent());
}
}

private void validateNonConfigFiles(IFile mostImportant,
String webXmlNamespace, String webXmlSchemaUrl, String servletVersion)
throws ParserConfigurationException, SAXException, IOException, CoreException {
IFolder src = project.getFolder("src");
IFolder main = src.getFolder("main");
IFolder java = main.getFolder("java");
IFile servlet = java.getFile("HelloAppEngine.java");
IFile servlet = project.getFile("src/main/java/HelloAppEngine.java");
Assert.assertTrue(servlet.exists());
Assert.assertEquals(servlet, mostImportant);

IFolder webapp = main.getFolder("webapp");
IFolder webinf = webapp.getFolder("WEB-INF");
IFile webXml = webinf.getFile("web.xml");
IFile webXml = project.getFile("src/main/webapp/WEB-INF/web.xml");
Element root = buildDocument(webXml).getDocumentElement();
Assert.assertEquals("web-app", root.getNodeName());
Assert.assertEquals(webXmlNamespace, root.getNamespaceURI());
Expand All @@ -148,18 +207,37 @@ private void validateNonConfigFiles(IFile mostImportant,
Assert.assertEquals("HelloAppEngine", servletClass.getTextContent());
}

IFile htmlFile = webapp.getFile("index.html");
IFile htmlFile = project.getFile("src/main/webapp/index.html");
Element html = buildDocument(htmlFile).getDocumentElement();
Assert.assertEquals("html", html.getNodeName());

IFolder test = src.getFolder("test");
IFolder testJava = test.getFolder("java");
IFile servletTest = testJava.getFile("HelloAppEngineTest.java");
IFile servletTest = project.getFile("src/test/java/HelloAppEngineTest.java");
Assert.assertTrue(servletTest.exists());
IFile mockServletResponse = testJava.getFile("MockHttpServletResponse.java");
IFile mockServletResponse = project.getFile("src/test/java/MockHttpServletResponse.java");
Assert.assertTrue(mockServletResponse.exists());
}

private void validateFlexJarNonConfigFiles(IFile mostImportant) {
IFile main = project.getFile("src/main/java/HelloAppEngineMain.java");
Assert.assertTrue(main.exists());
Assert.assertEquals(main, mostImportant);
IFile handler = project.getFile("src/main/java/HelloAppEngineHandler.java");
Assert.assertTrue(handler.exists());

IFile handlerTest = project.getFile("src/test/java/HelloAppEngineHandlerTest.java");
Assert.assertTrue(handlerTest.exists());
IFile mockHttpExchange = project.getFile("src/test/java/MockHttpExchange.java");
Assert.assertTrue(mockHttpExchange.exists());
}

private void validateFlexSpringBootNonConfigFiles(IFile mostImportant) {
IFile main = project.getFile("src/main/java/HelloAppEngineSpringBoot.java");
Assert.assertTrue(main.exists());
Assert.assertEquals(main, mostImportant);
IFile handler = project.getFile("src/test/java/HelloAppEngineSpringBootIntegrationTest.java");
Assert.assertTrue(handler.exists());
}

private void validateAppEngineWebXml(AppEngineRuntime runtime)
throws ParserConfigurationException, SAXException, IOException, CoreException {
IFolder webinf = project.getFolder("src/main/webapp/WEB-INF");
Expand Down Expand Up @@ -188,15 +266,14 @@ private void validateAppEngineWebXml(AppEngineRuntime runtime)
}

private void validateAppYaml() throws IOException, CoreException {
IFolder appengineFolder = project.getFolder("src/main/appengine");
Assert.assertTrue(appengineFolder.exists());
IFile appYaml = appengineFolder.getFile("app.yaml");
IFile appYaml = project.getFile("src/main/appengine/app.yaml");
Assert.assertTrue(appYaml.exists());

try (BufferedReader reader = new BufferedReader(
new InputStreamReader(appYaml.getContents(), StandardCharsets.UTF_8))) {
Assert.assertEquals("runtime: java", reader.readLine());
Assert.assertEquals("env: flex", reader.readLine());
Assert.assertEquals("service: database-service", reader.readLine());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ private static IFile materialize(IProject project, AppEngineProjectConfig config
return hello;
}

public static IFile materializeFlexJar(IProject project, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, "Generating code", 30);

IFile hello = createFlexJarJavaSourceFiles(project, config, subMonitor.newChild(20));

boolean isStandardProject = false;
createAppEngineWebXmlOrAppYaml(project, config, isStandardProject, subMonitor.newChild(5));

createFlexJarPomXml(project, config, subMonitor.newChild(5));

return hello;
}

public static IFile materializeFlexSpringBoot(IProject project, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, "Generating code", 10);

IFile hello = createFlexSpringBootJavaSourceFiles(project, config, subMonitor.newChild(20));

boolean isStandardProject = false;
createAppEngineWebXmlOrAppYaml(project, config, isStandardProject, subMonitor.newChild(5));

createFlexSpringBootPomXml(project, config, subMonitor.newChild(5));

return hello;
}

private static IFile createJavaSourceFiles(IProject project, AppEngineProjectConfig config,
boolean isStandardProject, IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 15);
Expand All @@ -119,15 +147,67 @@ private static IFile createJavaSourceFiles(IProject project, AppEngineProjectCon
mainPackageFolder, properties, subMonitor.newChild(5));

createChildFile("HelloAppEngineTest.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_TEST_TEMPLATE, testPackageFolder,
properties, subMonitor.newChild(5));
Templates.HELLO_APPENGINE_TEST_TEMPLATE,
testPackageFolder, properties, subMonitor.newChild(5));
createChildFile("MockHttpServletResponse.java", //$NON-NLS-1$
Templates.MOCK_HTTPSERVLETRESPONSE_TEMPLATE,
testPackageFolder, properties, subMonitor.newChild(5));

return hello;
}

private static IFile createFlexJarJavaSourceFiles(IProject project, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 20);

String packageName = config.getPackageName();
String packagePath = packageName.replace('.', '/');
IFolder mainPackageFolder = project.getFolder("src/main/java/" + packagePath); //$NON-NLS-1$
IFolder testPackageFolder = project.getFolder("src/test/java/" + packagePath); //$NON-NLS-1$

Map<String, String> properties = new HashMap<>();
properties.put("package", Strings.nullToEmpty(packageName)); //$NON-NLS-1$

IFile hello = createChildFile("HelloAppEngineMain.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_MAIN_TEMPLATE,
mainPackageFolder, properties, subMonitor.newChild(5));
createChildFile("HelloAppEngineHandler.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_HANDLER_TEMPLATE,
mainPackageFolder, properties, subMonitor.newChild(5));

createChildFile("HelloAppEngineHandlerTest.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_HANDLER_TEST_TEMPLATE,
testPackageFolder, properties, subMonitor.newChild(5));
createChildFile("MockHttpExchange.java", //$NON-NLS-1$
Templates.MOCKHTTPEXCHANGE_TEMPLATE,
testPackageFolder, properties, subMonitor.newChild(5));

return hello;
}

private static IFile createFlexSpringBootJavaSourceFiles(IProject project,
AppEngineProjectConfig config, IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 10);

String packageName = config.getPackageName();
String packagePath = packageName.replace('.', '/');
IFolder mainPackageFolder = project.getFolder("src/main/java/" + packagePath); //$NON-NLS-1$
IFolder testPackageFolder = project.getFolder("src/test/java/" + packagePath); //$NON-NLS-1$

Map<String, String> properties = new HashMap<>();
properties.put("package", Strings.nullToEmpty(packageName)); //$NON-NLS-1$

IFile hello = createChildFile("HelloAppEngineSpringBoot.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_SPRING_BOOT_TEMPLATE,
mainPackageFolder, properties, subMonitor.newChild(5));

createChildFile("HelloAppEngineSpringBootIntegrationTest.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_SPRING_BOOT_INTEGRATION_TEST_TEMPLATE,
testPackageFolder, properties, subMonitor.newChild(5));

return hello;
}

private static void createAppEngineWebXmlOrAppYaml(IProject project,
AppEngineProjectConfig config, boolean isStandardProject, IProgressMonitor monitor)
throws CoreException {
Expand All @@ -143,8 +223,7 @@ private static void createAppEngineWebXmlOrAppYaml(IProject project,

if (isStandardProject) {
IFolder webInf = project.getFolder("src/main/webapp/WEB-INF"); //$NON-NLS-1$
createChildFile("appengine-web.xml", //$NON-NLS-1$
Templates.APPENGINE_WEB_XML_TEMPLATE,
createChildFile("appengine-web.xml", Templates.APPENGINE_WEB_XML_TEMPLATE, //$NON-NLS-1$
webInf, properties, monitor);
} else {
IFolder appengine = project.getFolder("src/main/appengine"); //$NON-NLS-1$
Expand Down Expand Up @@ -212,6 +291,29 @@ private static void createPomXml(IProject project, AppEngineProjectConfig config
}
}

private static void createFlexJarPomXml(IProject project, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
Map<String, String> properties = new HashMap<>();
properties.put("projectGroupId", config.getMavenGroupId()); //$NON-NLS-1$
properties.put("projectArtifactId", config.getMavenArtifactId()); //$NON-NLS-1$
properties.put("projectVersion", config.getMavenVersion()); //$NON-NLS-1$
properties.put("package", config.getPackageName());

createChildFile("pom.xml", Templates.POM_XML_FLEX_JAR_TEMPLATE, //$NON-NLS-1$
project, properties, monitor);
}

private static void createFlexSpringBootPomXml(IProject project, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
Map<String, String> properties = new HashMap<>();
properties.put("projectGroupId", config.getMavenGroupId()); //$NON-NLS-1$
properties.put("projectArtifactId", config.getMavenArtifactId()); //$NON-NLS-1$
properties.put("projectVersion", config.getMavenVersion()); //$NON-NLS-1$

createChildFile("pom.xml", Templates.POM_XML_FLEX_SPRING_BOOT_TEMPLATE, //$NON-NLS-1$
project, properties, monitor);
}

@VisibleForTesting
static IFile createChildFile(String name, String template, IContainer parent,
Map<String, String> values, IProgressMonitor monitor) throws CoreException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2017 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.newproject.flex;

import com.google.cloud.tools.eclipse.appengine.facets.AppEngineFlexJarFacet;
import com.google.cloud.tools.eclipse.appengine.libraries.repository.ILibraryRepositoryService;
import com.google.cloud.tools.eclipse.appengine.newproject.AppEngineProjectConfig;
import com.google.cloud.tools.eclipse.appengine.newproject.CodeTemplates;
import com.google.cloud.tools.eclipse.appengine.newproject.CreateAppEngineWtpProject;
import com.google.cloud.tools.eclipse.appengine.newproject.Messages;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;

public class CreateAppEngineFlexJarProject extends CreateAppEngineWtpProject {

CreateAppEngineFlexJarProject(AppEngineProjectConfig config, IAdaptable uiInfoAdapter,
ILibraryRepositoryService repositoryService) {
super(config, uiInfoAdapter, repositoryService);
}

@Override
public void addAppEngineFacet(IFacetedProject newProject, IProgressMonitor monitor)
throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor,
Messages.getString("add.appengine.flex.jar.facet"), 100);

AppEngineFlexJarFacet.installAppEngineFacet(
newProject, true /* installDependentFacets */, subMonitor.newChild(100));
}

@Override
public String getDescription() {
return Messages.getString("creating.app.engine.flex.project"); //$NON-NLS-1$
}

@Override
public IFile createAndConfigureProjectContent(IProject newProject, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
IFile mostImportantFile = CodeTemplates.materializeFlexJar(newProject, config, monitor);
return mostImportantFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ project.location.exists: Project location already exists: {0}
app.engine.service=App Engine service:
add.appengine.standard.facet=Adding App Engine Standard Facet
add.appengine.flex.war.facet=Adding App Engine Flexible WAR Facet
add.appengine.flex.jar.facet=Adding App Engine Flexible JAR Facet
app.engine.standard.project.runtimetype=Java version:

app.engine.flex.project=App Engine Flexible Project
Expand Down
Loading