/
ScanningAppProviderRuntimeUpdatesTest.java
191 lines (162 loc) · 6 KB
/
ScanningAppProviderRuntimeUpdatesTest.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// ========================================================================
// 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 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.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.deploy.providers;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.deploy.AppProvider;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.deploy.test.XmlConfiguredJetty;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.Scanner;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.condition.OS.WINDOWS;
/**
* Similar in scope to {@link ScanningAppProviderStartupTest}, except is concerned with the modification of existing
* deployed webapps due to incoming changes identified by the {@link ScanningAppProvider}.
*/
@ExtendWith(WorkDirExtension.class)
public class ScanningAppProviderRuntimeUpdatesTest
{
private static final Logger LOG = LoggerFactory.getLogger(ScanningAppProviderRuntimeUpdatesTest.class);
public WorkDir testdir;
private static XmlConfiguredJetty jetty;
private final AtomicInteger _scans = new AtomicInteger();
private int _providers;
@BeforeEach
public void setupEnvironment() throws Exception
{
testdir.ensureEmpty();
Resource.setDefaultUseCaches(false);
jetty = new XmlConfiguredJetty(testdir.getEmptyPathDir());
jetty.addConfiguration("jetty.xml");
jetty.addConfiguration("jetty-http.xml");
jetty.addConfiguration("jetty-deploymgr-contexts.xml");
// Should not throw an Exception
jetty.load();
// Start it
jetty.start();
// monitor tick
DeploymentManager dm = jetty.getServer().getBean(DeploymentManager.class);
for (AppProvider provider : dm.getAppProviders())
{
if (provider instanceof ScanningAppProvider)
{
_providers++;
((ScanningAppProvider)provider).addScannerListener(new Scanner.ScanCycleListener()
{
@Override
public void scanEnded(int cycle)
{
_scans.incrementAndGet();
}
});
}
}
}
@AfterEach
public void teardownEnvironment() throws Exception
{
// Stop jetty.
jetty.stop();
}
public void waitForDirectoryScan()
{
int scan = _scans.get() + (2 * _providers);
do
{
try
{
Thread.sleep(200);
}
catch (InterruptedException e)
{
LOG.warn("Sleep failed", e);
}
}
while (_scans.get() < scan);
}
/**
* Simple webapp deployment after startup of server.
*
* @throws IOException on test failure
*/
@Test
public void testAfterStartupContext() throws IOException
{
jetty.copyWebapp("foo-webapp-1.war", "foo.war");
jetty.copyWebapp("foo.xml", "foo.xml");
waitForDirectoryScan();
waitForDirectoryScan();
jetty.assertWebAppContextsExists("/foo");
}
/**
* Simple webapp deployment after startup of server, and then removal of the webapp.
*
* @throws IOException on test failure
*/
@Test
public void testAfterStartupThenRemoveContext() throws IOException
{
jetty.copyWebapp("foo-webapp-1.war", "foo.war");
jetty.copyWebapp("foo.xml", "foo.xml");
waitForDirectoryScan();
waitForDirectoryScan();
jetty.assertWebAppContextsExists("/foo");
jetty.removeWebapp("foo.war");
jetty.removeWebapp("foo.xml");
waitForDirectoryScan();
waitForDirectoryScan();
jetty.assertNoWebAppContexts();
}
/**
* Simple webapp deployment after startup of server, and then removal of the webapp.
*
* @throws Exception on test failure
*/
@Test
@DisabledOnOs(WINDOWS)
// This test will not work on Windows as second war file would, not be written over the first one because of a file lock
public void testAfterStartupThenUpdateContext() throws Exception
{
jetty.copyWebapp("foo-webapp-1.war", "foo.war");
jetty.copyWebapp("foo.xml", "foo.xml");
waitForDirectoryScan();
waitForDirectoryScan();
jetty.assertWebAppContextsExists("/foo");
// Test that webapp response contains "-1"
jetty.assertResponseContains("/foo/info", "FooServlet-1");
waitForDirectoryScan();
//System.err.println("Updating war files");
jetty.copyWebapp("foo.xml", "foo.xml"); // essentially "touch" the context xml
jetty.copyWebapp("foo-webapp-2.war", "foo.war");
// This should result in the existing foo.war being replaced with the new foo.war
waitForDirectoryScan();
waitForDirectoryScan();
jetty.assertWebAppContextsExists("/foo");
// Test that webapp response contains "-2"
jetty.assertResponseContains("/foo/info", "FooServlet-2");
}
}