Skip to content

Commit

Permalink
Move servlet filter init failure test to open source.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 447853756
Change-Id: I9d6ba66c23c72c53c68b5d22a1f8147ecb1d35d2
  • Loading branch information
ludoch authored and gae-java-bot committed May 10, 2022
1 parent 3cc3566 commit 421d1cc
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 3 deletions.
83 changes: 83 additions & 0 deletions runtime/failinitfilterwebapp/pom.xml
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2021 Google LLC
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
https://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0</version>

<groupId>com.google.appengine.demos</groupId>
<artifactId>failinitfilterwebapp</artifactId>
<name>AppEngine :: failinitfilterwebapp</name>

<properties>
<appengine.app.version>1</appengine.app.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<outputDirectory>target/${project.artifactId}-${project.version}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<archiveClasses>true</archiveClasses>
<webResources>
<!-- in order to interpolate version from pom into appengine-web.xml -->
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<projectId>ludo-in-in</projectId>
<version>failinitfilter</version>
<promote>false</promote>
<automaticRestart>true</automaticRestart>
</configuration>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,38 @@
/*
* Copyright 2021 Google LLC
*
* 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
*
* https://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.
*/

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class FailInitializationFilter implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
throw new ServletException("Intentionally failing to initialize.");
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws ServletException {
throw new ServletException("Unexpectedly got a request.");
}

@Override
public void destroy() {}
}
@@ -0,0 +1,25 @@
# Copyright 2021 Google LLC
#
# 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
#
# https://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.
#
runtime: java8
inbound_services:
- warmup
threadsafe: True
auto_id_policy: default
api_version: 'user_defined'
handlers:
- url: /.*
script: unused
login: optional
secure: optional
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2021 Google LLC
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
https://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.
-->
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
</appengine-web-app>
30 changes: 30 additions & 0 deletions runtime/failinitfilterwebapp/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2021 Google LLC
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
https://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.
-->
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>FailInitializationFilter</filter-name>
<filter-class>FailInitializationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>FailInitializationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
9 changes: 6 additions & 3 deletions runtime/nogaeapiswebapp/src/main/webapp/WEB-INF/web.xml
Expand Up @@ -14,9 +14,12 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>NoGaeApisServlet</servlet-name>
<servlet-class>NoGaeApisServlet</servlet-class>
</servlet>
Expand Down
1 change: 1 addition & 0 deletions runtime/pom.xml
Expand Up @@ -35,6 +35,7 @@
<module>deployment</module>
<module>local</module>
<module>nogaeapiswebapp</module>
<module>failinitfilterwebapp</module>
<module>test</module>
<module>testapps</module>
</modules>
Expand Down
@@ -0,0 +1,52 @@
/*
* Copyright 2021 Google LLC
*
* 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
*
* https://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.apphosting.runtime.jetty9;

import static com.google.common.truth.Truth.assertThat;

import java.io.File;
import java.io.IOException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class FailureFilterTest extends JavaRuntimeViaHttpBase {

private static File appRoot;

@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
File currentDirectory = new File("").getAbsoluteFile();
appRoot = new File(currentDirectory, "../failinitfilterwebapp/target/failinitfilterwebapp-1.0");
assertThat(appRoot.isDirectory()).isTrue();
}

private RuntimeContext<DummyApiServer> runtimeContext() throws IOException, InterruptedException {
RuntimeContext.Config<DummyApiServer> config =
RuntimeContext.Config.builder().setApplicationPath(appRoot.toString()).build();
return RuntimeContext.create(config);
}

@Test
public void testFilterInitFailed() throws Exception {
try (RuntimeContext<DummyApiServer> runtime = runtimeContext()) {
assertThat(runtime.executeHttpGet("/", 500))
.contains("javax.servlet.ServletException: Intentionally failing to initialize.");
}
}
}

0 comments on commit 421d1cc

Please sign in to comment.