Skip to content

Commit

Permalink
#22 reproduced
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Nov 28, 2023
1 parent c33f5b0 commit 0895844
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
25 changes: 24 additions & 1 deletion testapp-kotlin/src/main/kotlin/com/example/Bootstrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import com.github.mvysny.vaadinboot.VaadinBoot
import com.vaadin.flow.component.page.AppShellConfigurator
import com.vaadin.flow.component.page.Push
import com.vaadin.flow.server.PWA
import com.vaadin.flow.server.VaadinServlet
import jakarta.servlet.Servlet
import jakarta.servlet.ServletContextEvent
import jakarta.servlet.ServletContextListener
import jakarta.servlet.annotation.WebInitParam
import jakarta.servlet.annotation.WebListener
import jakarta.servlet.annotation.WebServlet
import org.slf4j.LoggerFactory
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
Expand All @@ -16,6 +20,13 @@ import java.util.concurrent.TimeUnit
class Bootstrap : ServletContextListener {
override fun contextInitialized(sce: ServletContextEvent?) {
executor = Executors.newSingleThreadScheduledExecutor()
fooInitParamValue = sce?.let {
val reg = it.servletContext.getServletRegistration("myservlet")
requireNotNull(reg) { "No servlet named 'myservlet': ${it.servletContext.servletRegistrations}" }
val param = reg.getInitParameter("foo")
requireNotNull(param) { "No 'foo' parameter: ${reg.initParameters} "}
param
} ?: ""
initialized = true
log.info("Testapp Initialized")
}
Expand All @@ -36,6 +47,10 @@ class Bootstrap : ServletContextListener {
@Volatile
var initialized = false

@JvmField
@Volatile
var fooInitParamValue = ""

@Volatile
lateinit var executor: ScheduledExecutorService
}
Expand All @@ -45,9 +60,17 @@ class Bootstrap : ServletContextListener {
@Push
class AppShell : AppShellConfigurator

/**
* Tests a custom servlet with an init parameter. Tests for https://github.com/mvysny/vaadin-boot/issues/22
*/
@WebServlet(name = "myservlet", initParams = [WebInitParam(name = "foo", value = "bar")])
class MyServlet : VaadinServlet()

object Main {
@JvmStatic
fun main(args: Array<String>) {
VaadinBoot().run()
VaadinBoot()
.withServlet(MyServlet::class.java as Class<Servlet>)
.run()
}
}
14 changes: 8 additions & 6 deletions testapp-kotlin/src/test/kotlin/com/example/JettyTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.example

import com.github.mvysny.vaadinboot.VaadinBoot
import jakarta.servlet.Servlet
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.IOException
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
Expand All @@ -18,21 +18,23 @@ class JettyTest {
private var vaadinBoot: VaadinBoot? = null
@BeforeEach
fun startJetty() {
Assertions.assertFalse(Bootstrap.initialized)
vaadinBoot = VaadinBoot().setPort(44312).localhostOnly()
assertFalse(Bootstrap.initialized)
vaadinBoot = VaadinBoot().setPort(44312).localhostOnly().withServlet(MyServlet::class.java as Class<Servlet>)
vaadinBoot!!.start()
}

@AfterEach
fun stopJetty() {
vaadinBoot!!.stop("tests")
Assertions.assertFalse(Bootstrap.initialized)
assertFalse(Bootstrap.initialized)
}

@Test
fun testAppIsUp() {
// make sure Bootstrap was called
Assertions.assertTrue(Bootstrap.initialized)
assertTrue(Bootstrap.initialized)
// make sure the init parameter is parsed by Jetty correctly
assertEquals("bar", Bootstrap.fooInitParamValue)

// make sure something is running on port 44312
val client = HttpClient.newBuilder().build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.github.mvysny.vaadinboot;

import com.vaadin.flow.server.VaadinServlet;
import jakarta.servlet.Servlet;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -8,8 +12,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;

public class VaadinBootTest {
/**
Expand Down Expand Up @@ -124,4 +127,12 @@ public void scanTestClasspathModifiesWebAppConfig() throws Exception {
final WebAppContext ctx = new VaadinBoot().scanTestClasspath().createWebAppContext();
assertEquals(".*\\.jar|.*/classes/.*|.*/test-classes/.*", ctx.getAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern"));
}

@WebServlet(name = "myservlet", initParams = @WebInitParam(name = "foo", value = "bar"))
public static class MyServlet extends VaadinServlet {}

@Test
public void smokeCustomServlet() throws Exception {
new VaadinBoot().withServlet((Class<? extends Servlet>) MyServlet.class);
}
}

0 comments on commit 0895844

Please sign in to comment.