Skip to content

Commit

Permalink
Add WebListener to init() Objectify 6 (#3213)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanseokoh authored Jul 6, 2018
1 parent d1f635f commit 98bb7e7
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,36 @@ public void testMaterializeAppEngineStandardFiles_objectifyWithJava8()
validateObjectifyFilterConfigInWebXml(false);
}

@Test
public void testMaterializeAppEngineStandardFiles_noObjectifyListenerWithObjectify5()
throws CoreException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setRuntimeId(AppEngineRuntime.STANDARD_JAVA_8.getId());
config.setAppEngineLibraries(Collections.singleton(new Library("objectify")));

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

@Test
public void testMaterializeAppEnginFlexFiles_noObjectifyListener()
throws CoreException {
AppEngineProjectConfig config = new AppEngineProjectConfig();

CodeTemplates.materializeAppEngineFlexFiles(project, config, monitor);
assertFalse(objectifyListenerClassExists());
}

@Test
public void testMaterializeAppEngineFlexFiles_objectifyListenerWithObjectify6()
throws CoreException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setAppEngineLibraries(Collections.singleton(new Library("objectify6")));

CodeTemplates.materializeAppEngineFlexFiles(project, config, monitor);
assertTrue(objectifyListenerClassExists());
}

@Test
public void testMaterializeAppEngineStandardFiles_noPomXml() throws CoreException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
Expand Down Expand Up @@ -212,6 +242,32 @@ public void testIsObjectifySelected_objectify6() {
assertTrue(CodeTemplates.isObjectifySelected(config));
}

@Test
public void testIsObjectify6Selected_notSelected() {
AppEngineProjectConfig config = new AppEngineProjectConfig();
assertFalse(CodeTemplates.isObjectify6Selected(config));
}

@Test
public void testIsObjectify6Selected_objectify5() {
List<Library> libraries = Arrays.asList(new Library("a-library"), new Library("objectify"));

AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setAppEngineLibraries(libraries);

assertFalse(CodeTemplates.isObjectify6Selected(config));
}

@Test
public void testIsObjectify6Selected_objectify6() {
List<Library> libraries = Arrays.asList(new Library("objectify6"), new Library("a-library"));

AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setAppEngineLibraries(libraries);

assertTrue(CodeTemplates.isObjectify6Selected(config));
}

@Test
public void testIsStandardJava7RuntimeSelected_java7() {
AppEngineProjectConfig config = new AppEngineProjectConfig();
Expand All @@ -226,6 +282,10 @@ public void testIsStandardJava7RuntimeSelected_java8() {
assertFalse(CodeTemplates.isStandardJava7RuntimeSelected(config));
}

private boolean objectifyListenerClassExists() {
return project.getFile("src/main/java/ObjectifyWebListener.java").exists();
}

private boolean objectifyFilterClassExists() {
return project.getFile("src/main/java/ObjectifyWebFilter.java").exists();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ private static IFile createJavaSourceFiles(IProject project, AppEngineProjectCon
createChildFile("ObjectifyWebFilter.java", //$NON-NLS-1$
Templates.OBJECTIFY_WEB_FILTER_TEMPLATE,
mainPackageFolder, properties, subMonitor.split(5));

if (isObjectify6Selected(config)) {
createChildFile("ObjectifyWebListener.java", //$NON-NLS-1$
Templates.OBJECTIFY_WEB_LISTENER_TEMPLATE,
mainPackageFolder, properties, subMonitor.split(5));
}
}

return hello;
Expand Down Expand Up @@ -210,6 +216,13 @@ static boolean isObjectifySelected(AppEngineProjectConfig config) {
return selectedLibraries.stream().anyMatch(isObjectify);
}

@VisibleForTesting
static boolean isObjectify6Selected(AppEngineProjectConfig config) {
Predicate<Library> isObjectify6 = library -> "objectify6".equals(library.getId()); //$NON-NLS-1$
List<Library> selectedLibraries = config.getAppEngineLibraries();
return selectedLibraries.stream().anyMatch(isObjectify6);
}

private static void createWebContents(IProject project, IProgressMonitor monitor)
throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* Description of the App Engine runtime environments.
*/
public enum AppEngineRuntime {
STANDARD_JAVA_7(Messages.getString("appengine.runtimes.java7"), null), // $NON-NLS-1$
STANDARD_JAVA_8(Messages.getString("appengine.runtimes.java8"), "java8"); // $NON-NLS-1$ //$NON-NLS-2$
STANDARD_JAVA_7(Messages.getString("appengine.runtimes.java7"), null), //$NON-NLS-1$
STANDARD_JAVA_8(Messages.getString("appengine.runtimes.java8"), "java8"); //$NON-NLS-1$ //$NON-NLS-2$

public static final EnumSet<AppEngineRuntime> STANDARD_RUNTIMES =
EnumSet.of(STANDARD_JAVA_7, STANDARD_JAVA_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ public void testCreateFileContent_objectifyWebFilterWithoutPackage()
compareToFile("objectifyWebFilterWithoutPackage.txt");
}

@Test
public void testCreateFileContent_objectifyWebListenerWithPackage()
throws CoreException, IOException {
dataMap.put("package", "com.example");
Templates.createFileContent(fileLocation, Templates.OBJECTIFY_WEB_LISTENER_TEMPLATE, dataMap);

compareToFile("objectifyWebListenerWithPackage.txt");
}

@Test
public void testCreateFileContent_objectifyWebListenerWithoutPackage()
throws CoreException, IOException {
dataMap.put("package", "");
Templates.createFileContent(fileLocation, Templates.OBJECTIFY_WEB_LISTENER_TEMPLATE, dataMap);

compareToFile("objectifyWebListenerWithoutPackage.txt");
}

private static InputStream getDataFile(String fileName) throws IOException {
Bundle bundle = FrameworkUtil.getBundle(TemplatesTest.class);
URL expectedFileUrl = bundle.getResource("/testData/templates/appengine/" + fileName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.googlecode.objectify.ObjectifyService;

@WebListener
public class ObjectifyWebListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent event) {
ObjectifyService.init();
// Here is also a great place to register your POJO entity classes.
// ObjectifyService.register(YourEntity.class);
}

@Override
public void contextDestroyed(ServletContextEvent event) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.googlecode.objectify.ObjectifyService;

@WebListener
public class ObjectifyWebListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent event) {
ObjectifyService.init();
// Here is also a great place to register your POJO entity classes.
// ObjectifyService.register(YourEntity.class);
}

@Override
public void contextDestroyed(ServletContextEvent event) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Templates {
public static final String POM_XML_FLEX_TEMPLATE = "pom.xml.flex.ftl";
public static final String LOGGING_PROPERTIES_TEMPLATE = "logging.properties.ftl";
public static final String OBJECTIFY_WEB_FILTER_TEMPLATE = "ObjectifyWebFilter.java.ftl";
public static final String OBJECTIFY_WEB_LISTENER_TEMPLATE = "ObjectifyWebListener.java.ftl";

private static Configuration configuration;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<#if package != "">package ${package};

</#if>import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.googlecode.objectify.ObjectifyService;

@WebListener
public class ObjectifyWebListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent event) {
ObjectifyService.init();
// Here is also a great place to register your POJO entity classes.
// ObjectifyService.register(YourEntity.class);
}

@Override
public void contextDestroyed(ServletContextEvent event) {
}
}

0 comments on commit 98bb7e7

Please sign in to comment.