diff --git a/plugins/com.google.cloud.tools.eclipse.appengine.deploy/src/com/google/cloud/tools/eclipse/appengine/deploy/flex/FlexMavenPackagedProjectStagingDelegate.java b/plugins/com.google.cloud.tools.eclipse.appengine.deploy/src/com/google/cloud/tools/eclipse/appengine/deploy/flex/FlexMavenPackagedProjectStagingDelegate.java index 03826393e8..5f78f2a3b6 100644 --- a/plugins/com.google.cloud.tools.eclipse.appengine.deploy/src/com/google/cloud/tools/eclipse/appengine/deploy/flex/FlexMavenPackagedProjectStagingDelegate.java +++ b/plugins/com.google.cloud.tools.eclipse.appengine.deploy/src/com/google/cloud/tools/eclipse/appengine/deploy/flex/FlexMavenPackagedProjectStagingDelegate.java @@ -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; @@ -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 diff --git a/plugins/com.google.cloud.tools.eclipse.appengine.newproject.test/src/com/google/cloud/tools/eclipse/appengine/newproject/CodeTemplatesTest.java b/plugins/com.google.cloud.tools.eclipse.appengine.newproject.test/src/com/google/cloud/tools/eclipse/appengine/newproject/CodeTemplatesTest.java index 3652d3c6ca..3c74de8304 100644 --- a/plugins/com.google.cloud.tools.eclipse.appengine.newproject.test/src/com/google/cloud/tools/eclipse/appengine/newproject/CodeTemplatesTest.java +++ b/plugins/com.google.cloud.tools.eclipse.appengine.newproject.test/src/com/google/cloud/tools/eclipse/appengine/newproject/CodeTemplatesTest.java @@ -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"); @@ -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()); @@ -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"); @@ -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()); } } diff --git a/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/CodeTemplates.java b/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/CodeTemplates.java index 3704ee3d2f..6f04f1d110 100644 --- a/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/CodeTemplates.java +++ b/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/CodeTemplates.java @@ -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); @@ -119,8 +147,8 @@ 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)); @@ -128,6 +156,58 @@ private static IFile createJavaSourceFiles(IProject project, AppEngineProjectCon 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 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 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 { @@ -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$ @@ -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 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 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 values, IProgressMonitor monitor) throws CoreException { diff --git a/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/flex/CreateAppEngineFlexJarProject.java b/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/flex/CreateAppEngineFlexJarProject.java new file mode 100644 index 0000000000..db89159782 --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/flex/CreateAppEngineFlexJarProject.java @@ -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; + } +} diff --git a/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/messages.properties b/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/messages.properties index af1b095db5..139305b08b 100644 --- a/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/messages.properties +++ b/plugins/com.google.cloud.tools.eclipse.appengine.newproject/src/com/google/cloud/tools/eclipse/appengine/newproject/messages.properties @@ -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 diff --git a/plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/Templates.java b/plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/Templates.java index 5768afc3ea..5406b0b87b 100644 --- a/plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/Templates.java +++ b/plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/Templates.java @@ -37,14 +37,30 @@ public class Templates { public static final String APPENGINE_WEB_XML_TEMPLATE = "appengine-web.xml.ftl"; - public static final String HELLO_APPENGINE_TEMPLATE = "HelloAppEngine.java.ftl"; + public static final String APP_YAML_TEMPLATE = "app.yaml.ftl"; + public static final String INDEX_HTML_TEMPLATE = "index.html.ftl"; public static final String WEB_XML_TEMPLATE = "web.xml.ftl"; + + public static final String HELLO_APPENGINE_TEMPLATE = "HelloAppEngine.java.ftl"; public static final String HELLO_APPENGINE_TEST_TEMPLATE = "HelloAppEngineTest.java.ftl"; public static final String MOCK_HTTPSERVLETRESPONSE_TEMPLATE = "MockHttpServletResponse.java.ftl"; - public static final String APP_YAML_TEMPLATE = "app.yaml.ftl"; + + public static final String HELLO_APPENGINE_MAIN_TEMPLATE = "HelloAppEngineMain.java.ftl"; + public static final String HELLO_APPENGINE_HANDLER_TEMPLATE = "HelloAppEngineHandler.java.ftl"; + public static final String HELLO_APPENGINE_HANDLER_TEST_TEMPLATE = + "HelloAppEngineHandlerTest.java.ftl"; + public static final String MOCKHTTPEXCHANGE_TEMPLATE = "MockHttpExchange.java.ftl"; + + public static final String HELLO_APPENGINE_SPRING_BOOT_TEMPLATE = + "HelloAppEngineSpringBoot.java.ftl"; + public static final String HELLO_APPENGINE_SPRING_BOOT_INTEGRATION_TEST_TEMPLATE = + "HelloAppEngineSpringBootIntegrationTest.java.ftl"; + public static final String POM_XML_STANDARD_TEMPLATE = "pom.xml.standard.ftl"; public static final String POM_XML_FLEX_TEMPLATE = "pom.xml.flex.ftl"; + public static final String POM_XML_FLEX_JAR_TEMPLATE = "pom.xml.flex.jar.ftl"; + public static final String POM_XML_FLEX_SPRING_BOOT_TEMPLATE = "pom.xml.flex.spring.boot.ftl"; private static Configuration configuration; diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineHandler.java.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineHandler.java.ftl new file mode 100644 index 0000000000..1158d384b8 --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineHandler.java.ftl @@ -0,0 +1,19 @@ +<#if package != "">package ${package}; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; + +public class HelloAppEngineHandler implements HttpHandler { + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + String response = "Hello App Engine!"; + httpExchange.sendResponseHeaders(200, response.length()); + try (OutputStream out = httpExchange.getResponseBody()) { + out.write(response.getBytes(StandardCharsets.UTF_8)); + } + } +} \ No newline at end of file diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineHandlerTest.java.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineHandlerTest.java.ftl new file mode 100644 index 0000000000..b961647065 --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineHandlerTest.java.ftl @@ -0,0 +1,16 @@ +<#if package != "">package ${package}; + +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +public class HelloAppEngineHandlerTest { + + @Test + public void test() throws IOException { + MockHttpExchange httpExchange = new MockHttpExchange(); + new HelloAppEngineHandler().handle(httpExchange); + Assert.assertEquals(200, httpExchange.getResponseCode()); + Assert.assertEquals("Hello App Engine!", httpExchange.getWrittenContents()); + } +} \ No newline at end of file diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineMain.java.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineMain.java.ftl new file mode 100644 index 0000000000..3e078a529c --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineMain.java.ftl @@ -0,0 +1,14 @@ +<#if package != "">package ${package}; + +import com.sun.net.httpserver.HttpServer; +import java.io.IOException; +import java.net.InetSocketAddress; + +public class HelloAppEngineMain { + + public static void main(String[] args) throws IOException { + HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); + server.createContext("/", new HelloAppEngineHandler()); + server.start(); + } +} \ No newline at end of file diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineSpringBoot.java.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineSpringBoot.java.ftl new file mode 100644 index 0000000000..bd4d11478f --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineSpringBoot.java.ftl @@ -0,0 +1,20 @@ +<#if package != "">package ${package}; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +@RestController +public class HelloAppEngineSpringBoot { + + @RequestMapping("/") + public String home() { + return "Hello App Engine!"; + } + + public static void main(String[] args) { + SpringApplication.run(HelloAppEngineSpringBoot.class, args); + } +} \ No newline at end of file diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineSpringBootIntegrationTest.java.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineSpringBootIntegrationTest.java.ftl new file mode 100644 index 0000000000..95e5738172 --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/HelloAppEngineSpringBootIntegrationTest.java.ftl @@ -0,0 +1,30 @@ +<#if package != "">package ${package}; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class HelloAppEngineSpringBootIntegrationTest { + + @Autowired + private TestRestTemplate template; + + @Test + public void contextLoads() throws Exception { + } + + @Test + public void testRequest() throws Exception { + ResponseEntity responseEntity = template.getForEntity("/", String.class); + assertTrue(responseEntity.toString().contains("Hello App Engine!")); + } +} \ No newline at end of file diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/MockHttpExchange.java.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/MockHttpExchange.java.ftl new file mode 100644 index 0000000000..458834443f --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/MockHttpExchange.java.ftl @@ -0,0 +1,107 @@ +<#if package != "">package ${package}; + +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpPrincipal; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.InetSocketAddress; +import java.net.URI; +import java.nio.charset.StandardCharsets; + +public class MockHttpExchange extends HttpExchange { + + private int responseCode; + private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + public String getWrittenContents() throws UnsupportedEncodingException { + return outputStream.toString("UTF-8"); + } + + @Override + public void close() { + } + + @Override + public Object getAttribute(String name) { + return null; + } + + @Override + public HttpContext getHttpContext() { + return null; + } + + @Override + public InetSocketAddress getLocalAddress() { + return null; + } + + @Override + public HttpPrincipal getPrincipal() { + return null; + } + + @Override + public String getProtocol() { + return null; + } + + @Override + public InetSocketAddress getRemoteAddress() { + return null; + } + + @Override + public InputStream getRequestBody() { + return null; + } + + @Override + public Headers getRequestHeaders() { + return null; + } + + @Override + public String getRequestMethod() { + return null; + } + + @Override + public URI getRequestURI() { + return null; + } + + @Override + public OutputStream getResponseBody() { + return outputStream; + } + + @Override + public int getResponseCode() { + return responseCode; + } + + @Override + public Headers getResponseHeaders() { + return null; + } + + @Override + public void sendResponseHeaders(int responseCode, long responseLength) throws IOException { + this.responseCode = responseCode; + } + + @Override + public void setAttribute(String name, Object value) { + } + + @Override + public void setStreams(InputStream in, OutputStream out) { + } + +} \ No newline at end of file diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.ftl index 6e78cfdb12..87e1405a97 100644 --- a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.ftl +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.ftl @@ -16,7 +16,6 @@ UTF-8 1.8 1.8 - true diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.jar.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.jar.ftl new file mode 100644 index 0000000000..d93f6e701f --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.jar.ftl @@ -0,0 +1,73 @@ + + + + 4.0.0 + jar + ${projectVersion} + + ${projectGroupId} + ${projectArtifactId} + + + UTF-8 + UTF-8 + 1.8 + 1.8 + + + + 3.3.9 + + + + + + junit + junit + 4.12 + test + + + + + + + org.codehaus.mojo + versions-maven-plugin + 2.4 + + + compile + + display-dependency-updates + display-plugin-updates + + + + + + + + com.jolira + onejar-maven-plugin + 1.4.4 + + + + <#if package != "">${package}.HelloAppEngineMain + + + one-jar + + + + + + + + diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.spring.boot.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.spring.boot.ftl new file mode 100644 index 0000000000..ab72d95269 --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.flex.spring.boot.ftl @@ -0,0 +1,72 @@ + + + + 4.0.0 + jar + ${projectVersion} + + ${projectGroupId} + ${projectArtifactId} + + + UTF-8 + UTF-8 + 1.8 + 1.8 + + + + 3.3.9 + + + + + org.springframework.boot + spring-boot-starter-web + 1.5.8.RELEASE + + + + + org.springframework.boot + spring-boot-starter-test + 1.5.8.RELEASE + test + + + + + + + org.codehaus.mojo + versions-maven-plugin + 2.4 + + + compile + + display-dependency-updates + display-plugin-updates + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 1.5.8.RELEASE + + + + repackage + + + + + + + + diff --git a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.standard.ftl b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.standard.ftl index f61677680f..3dffffc539 100644 --- a/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.standard.ftl +++ b/plugins/com.google.cloud.tools.eclipse.util/templates/appengine/pom.xml.standard.ftl @@ -16,7 +16,6 @@ UTF-8 ${compilerVersion} ${compilerVersion} - true