Skip to content

Commit

Permalink
Add a test for the @WebServlet annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
earldouglas committed Sep 27, 2024
1 parent e2e600a commit a8ffb6d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ lazy val checkWar: Def.Initialize[Task[Unit]] =
"WEB-INF/classes/drivers/mem/mem$package.class",
"WEB-INF/classes/drivers/mem/mem$package.tasty",
"WEB-INF/classes/runners/",
"WEB-INF/classes/runners/CountServlet.class",
"WEB-INF/classes/runners/CountServlet.tasty",
"WEB-INF/classes/runners/HelloServlet.class",
"WEB-INF/classes/runners/HelloServlet.tasty",
"WEB-INF/classes/runners/Main$$anon$1.class",
"WEB-INF/classes/runners/Main$tx$.class",
"WEB-INF/classes/runners/Main.class",
"WEB-INF/classes/runners/Main.tasty",
"WEB-INF/classes/runners/MainServlet.class",
"WEB-INF/classes/runners/MainServlet.tasty",
"WEB-INF/classes/usecases/",
"WEB-INF/classes/usecases/Counter.class",
"WEB-INF/classes/usecases/Counter.tasty",
Expand Down
5 changes: 5 additions & 0 deletions src/sbt-test/sbt-war/combined/scenarios/war-runner-plugin.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,9 @@ TaskKey[Unit]("check") := {
|}
|""".stripMargin
)

assertEquals(
url = s"http://localhost:${warPort.value}/hello",
expectedBody = """<h1>Hello, world!</h1>"""
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,9 @@ TaskKey[Unit]("check") := {
|}
|""".stripMargin
)

assertEquals(
url = s"http://localhost:${webappPort.value}/hello",
expectedBody = """<h1>Hello, world!</h1>"""
)
}
6 changes: 4 additions & 2 deletions src/sbt-test/sbt-war/combined/scenarios/webapp-plugin.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ val checkClasses: Def.Initialize[Task[Unit]] =
"drivers/mem/mem$package$.class",
"drivers/mem/mem$package.class",
"drivers/mem/mem$package.tasty",
"runners/CountServlet.class",
"runners/CountServlet.tasty",
"runners/HelloServlet.class",
"runners/HelloServlet.tasty",
"runners/Main$$anon$1.class",
"runners/Main$tx$.class",
"runners/Main.class",
"runners/Main.tasty",
"runners/MainServlet.class",
"runners/MainServlet.tasty",
"usecases/Counter.class",
"usecases/Counter.tasty"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package runners

import java.sql.Connection
import java.sql.DriverManager
import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class MainServlet extends HttpServlet with Main:
class CountServlet extends HttpServlet with Main:

import java.sql.Connection
import java.sql.DriverManager

override lazy val c: Connection =
Class.forName("org.h2.Driver")
Expand All @@ -27,3 +29,14 @@ class MainServlet extends HttpServlet with Main:
): Unit =
res.addHeader("Content-Type", "application/json")
res.getWriter().write(unsafeIncrementAndGetAsJson())

@WebServlet(urlPatterns = Array("/hello"))
class HelloServlet extends HttpServlet:

override def doGet(
request: HttpServletRequest,
response: HttpServletResponse
): Unit =
response.setCharacterEncoding("UTF-8")
response.setContentType("text/html")
response.getWriter.write("""<h1>Hello, world!</h1>""")
6 changes: 3 additions & 3 deletions src/sbt-test/sbt-war/combined/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<web-app>

<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>runners.MainServlet</servlet-class>
<servlet-name>CountServlet</servlet-name>
<servlet-class>runners.CountServlet</servlet-class>
<async-supported>true</async-supported>
</servlet>

<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<servlet-name>CountServlet</servlet-name>
<url-pattern>/count</url-pattern>
</servlet-mapping>

Expand Down

0 comments on commit a8ffb6d

Please sign in to comment.