/
JavaxClientShutdownWithServerTest.java
150 lines (129 loc) · 6.07 KB
/
JavaxClientShutdownWithServerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests;
import java.net.URI;
import java.util.Collection;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.websocket.javax.client.JavaxWebSocketShutdownContainer;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class JavaxClientShutdownWithServerTest
{
private Server server;
private ServletContextHandler contextHandler;
private URI serverUri;
private HttpClient httpClient;
private volatile WebSocketContainer container;
private ContainerLifeCycle shutdownContainer;
public class ContextHandlerShutdownServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
{
container = ContainerProvider.getWebSocketContainer();
}
}
public class ServletContainerInitializerShutdownServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
{
JavaxWebSocketClientContainer clientContainer = new JavaxWebSocketClientContainer();
clientContainer.allowShutdownWithContextHandler(false);
LifeCycle.start(clientContainer);
container = clientContainer;
}
}
@BeforeEach
public void before() throws Exception
{
server = new Server();
ServerConnector connector = new ServerConnector(server);
server.addConnector(connector);
contextHandler = new ServletContextHandler();
contextHandler.setContextPath("/");
contextHandler.addServlet(new ServletHolder(new ContextHandlerShutdownServlet()), "/contextHandler");
contextHandler.addServlet(new ServletHolder(new ServletContainerInitializerShutdownServlet()), "/shutdownContainer");
server.setHandler(contextHandler);
// Because we are using embedded we must manually add the Javax WS Client Shutdown SCI.
JavaxWebSocketShutdownContainer javaxWebSocketClientShutdown = new JavaxWebSocketShutdownContainer();
shutdownContainer = javaxWebSocketClientShutdown;
contextHandler.addServletContainerInitializer(javaxWebSocketClientShutdown);
server.start();
serverUri = WSURI.toWebsocket(server.getURI());
httpClient = new HttpClient();
httpClient.start();
}
@AfterEach
public void after() throws Exception
{
httpClient.stop();
server.stop();
}
@Test
public void testShutdownWithContextHandler() throws Exception
{
ContentResponse response = httpClient.GET(serverUri.resolve("/contextHandler"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertNotNull(container);
assertThat(container, instanceOf(JavaxWebSocketClientContainer.class));
JavaxWebSocketClientContainer clientContainer = (JavaxWebSocketClientContainer)container;
assertThat(clientContainer.isRunning(), is(true));
// The container should be a bean on the ContextHandler.
Collection<WebSocketContainer> containedBeans = contextHandler.getBeans(WebSocketContainer.class);
assertThat(containedBeans.size(), is(1));
assertThat(containedBeans.toArray()[0], is(container));
// The client should be attached to the servers LifeCycle and should stop with it.
server.stop();
assertThat(clientContainer.isRunning(), is(false));
assertThat(server.getContainedBeans(WebSocketContainer.class), empty());
}
@Test
public void testShutdownWithShutdownContainer() throws Exception
{
ContentResponse response = httpClient.GET(serverUri.resolve("/shutdownContainer"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertNotNull(container);
assertThat(container, instanceOf(JavaxWebSocketClientContainer.class));
JavaxWebSocketClientContainer clientContainer = (JavaxWebSocketClientContainer)container;
assertThat(clientContainer.isRunning(), is(true));
// The container should be a bean on the ContextHandler.
Collection<WebSocketContainer> containedBeans = shutdownContainer.getBeans(WebSocketContainer.class);
assertThat(containedBeans.size(), is(1));
assertThat(containedBeans.toArray()[0], is(container));
// The client should be attached to the servers LifeCycle and should stop with it.
server.stop();
assertThat(clientContainer.isRunning(), is(false));
assertThat(server.getContainedBeans(WebSocketContainer.class), empty());
}
}