From c8385025cad9150d3fe04dac5a8e3cdeb562bc61 Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Thu, 26 Nov 2020 12:31:31 +0300 Subject: [PATCH] fix: fix unit tests --- .../com/vaadin/flow/server/VaadinServlet.java | 16 ++++++++++++++++ .../flow/server/MockVaadinServletService.java | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/VaadinServlet.java b/flow-server/src/main/java/com/vaadin/flow/server/VaadinServlet.java index 14c55a0420b..aa87b97b5c8 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/VaadinServlet.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/VaadinServlet.java @@ -68,6 +68,22 @@ public void init(ServletConfig servletConfig) throws ServletException { CurrentInstance.clearAll(); try { + /* + * There are plenty of reasons why the check should be done. The + * main reason is: init method is public which means that everyone + * may call this method at any time (including an app developer). + * But it's not supposed to be called any times any time. + * + * This code protects weak API from being called several times so + * that config is reset after the very first initialization. + * + * Normally "init" method is called only once by the servlet + * container. But in a specific OSGi case {@code + * ServletCointextListener} may be called after the servlet + * initialized. To be able to initialize the VaadinServlet properly + * its "init" method is called from the {@code + * ServletCointextListener} with the same ServletConfig instance. + */ if (getServletConfig() == null) { super.init(servletConfig); } diff --git a/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java b/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java index 496554a8f65..b6fc5fc7653 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java @@ -109,7 +109,9 @@ public void init() { try { MockVaadinServlet servlet = (MockVaadinServlet) getServlet(); servlet.service = this; - getServlet().init(new MockServletConfig()); + if (getServlet().getServletConfig() == null) { + getServlet().init(new MockServletConfig()); + } super.init(); } catch (ServiceException | ServletException e) { throw new RuntimeException(e);