-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
DeploymentTempDirTest.java
226 lines (194 loc) · 8.93 KB
/
DeploymentTempDirTest.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// ========================================================================
// Copyright (c) 1995-2021 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.deploy;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.deploy.providers.WebAppProvider;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.Scanner;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.WebAppContext;
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.is;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DeploymentTempDirTest
{
private static final Logger LOG = Log.getLogger(DeploymentTempDirTest.class);
private final WebAppProvider webAppProvider = new WebAppProvider();
private final ContextHandlerCollection contexts = new ContextHandlerCollection();
private final Path testDir = MavenTestingUtils.getTargetTestingPath(DeploymentTempDirTest.class.getSimpleName());
private final Path tmpDir = testDir.resolve("tmpDir");
private final Path webapps = testDir.resolve("webapps");
private final Server server = new Server();
private final TestListener listener = new TestListener();
@BeforeEach
public void setup() throws Exception
{
ServerConnector connector = new ServerConnector(server);
server.addConnector(connector);
FS.ensureEmpty(testDir);
FS.ensureEmpty(tmpDir);
FS.ensureEmpty(webapps);
webAppProvider.setMonitoredDirName(webapps.toString());
webAppProvider.setScanInterval(0);
DeploymentManager deploymentManager = new DeploymentManager();
deploymentManager.addAppProvider(webAppProvider);
server.addBean(deploymentManager);
HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.addHandler(contexts);
handlerCollection.addHandler(new DefaultHandler());
deploymentManager.setContexts(contexts);
server.setHandler(handlerCollection);
}
@AfterEach
public void stop() throws Exception
{
server.stop();
}
@Test
public void testTmpDirectory() throws Exception
{
Path warPath = MavenTestingUtils.getTestResourcePath("webapps/foo-webapp-1.war");
String deploymentXml = "<!DOCTYPE Configure PUBLIC \"-//Jetty//Configure//EN\" \"https://www.eclipse.org/jetty/configure_10_0.dtd\">\n" +
"<Configure class=\"org.eclipse.jetty.webapp.WebAppContext\">\n" +
"<Set name=\"war\">" + warPath + "</Set>\n" +
"<Set name=\"tempDirectory\">" + tmpDir + "</Set>\n" +
"<Set name=\"persistTempDirectory\">false</Set>\n" +
"</Configure>";
server.start();
webAppProvider.addScannerListener(listener);
// Add the webapp xml which will will be detected after scan.
createNewFile(webapps, "foo-webapp.xml", deploymentXml);
webAppProvider.scan();
webAppProvider.scan();
listener.awaitChanges();
WebAppContext webAppContext = getWebAppContext();
assertThat(webAppContext.getTempDirectory(), is(tmpDir.toFile()));
// Add a known file to the temp directory, this file will be deleted when stopping as persistTempDirectory is false.
String content = UUID.randomUUID().toString();
createNewFile(webAppContext.getTempDirectory().toPath(), "myFile.txt", content);
// Touch the webapp and rescan to reload the WebAppContext.
long newModifiedTime = System.currentTimeMillis() + 1000;
assertTrue(webapps.resolve("foo-webapp.xml").toFile().setLastModified(newModifiedTime));
webAppProvider.scan();
webAppProvider.scan();
listener.awaitChanges();
// The second WebAppContext should be using the same temp directory but the file will have been deleted.
WebAppContext webAppContext2 = getWebAppContext();
assertNotSame(webAppContext, webAppContext2);
File tmpDir2 = webAppContext2.getTempDirectory();
assertThat(tmpDir2, is(tmpDir.toFile()));
// The temp directory has been cleared.
assertTrue(tmpDir2.exists());
assertThat(length(tmpDir2.listFiles()), is(0));
}
@Test
public void testPersistentTmpDirectory() throws Exception
{
Path warPath = MavenTestingUtils.getTestResourcePath("webapps/foo-webapp-1.war");
String deploymentXml = "<!DOCTYPE Configure PUBLIC \"-//Jetty//Configure//EN\" \"https://www.eclipse.org/jetty/configure_10_0.dtd\">\n" +
"<Configure class=\"org.eclipse.jetty.webapp.WebAppContext\">\n" +
"<Set name=\"war\">" + warPath + "</Set>\n" +
"<Set name=\"tempDirectory\">" + tmpDir + "</Set>\n" +
"<Set name=\"persistTempDirectory\">true</Set>\n" +
"</Configure>";
server.start();
webAppProvider.addScannerListener(listener);
// Add the webapp xml which will will be detected after scan.
createNewFile(webapps, "foo-webapp.xml", deploymentXml);
webAppProvider.scan();
webAppProvider.scan();
listener.awaitChanges();
WebAppContext webAppContext1 = getWebAppContext();
assertThat(webAppContext1.getTempDirectory(), is(tmpDir.toFile()));
// Add a known file to the temp directory, this file will be preserved after stop as persistTempDirectory is true.
String content = UUID.randomUUID().toString();
createNewFile(webAppContext1.getTempDirectory().toPath(), "myFile.txt", content);
// Touch the webapp and rescan to reload the WebAppContext.
long newModifiedTime = System.currentTimeMillis() + 1000;
assertTrue(webapps.resolve("foo-webapp.xml").toFile().setLastModified(newModifiedTime));
webAppProvider.scan();
webAppProvider.scan();
listener.awaitChanges();
// The second WebAppContext should be using the same temp directory and file will not have been deleted.
WebAppContext webAppContext2 = getWebAppContext();
assertNotSame(webAppContext1, webAppContext2);
assertThat(webAppContext2.getTempDirectory(), is(tmpDir.toFile()));
// Test file is still in the temp directory.
String contentAfterReload = getContent(webAppContext2.getTempDirectory().toPath().resolve("myFile.txt"));
assertThat(contentAfterReload, is(content));
}
public int length(File[] files)
{
if (files == null)
throw new IllegalStateException();
return files.length;
}
public WebAppContext getWebAppContext()
{
Handler[] handlers = contexts.getHandlers();
assertThat(handlers.length, is(1));
return Arrays.stream(contexts.getHandlers())
.filter(h -> h instanceof WebAppContext)
.map(h -> (WebAppContext)h)
.findFirst()
.orElseThrow(IllegalStateException::new);
}
public void createNewFile(Path directory, String filename, String content) throws IOException
{
File file = directory.resolve(filename).toFile();
try (FileWriter writer = new FileWriter(file))
{
writer.write(content);
}
}
public String getContent(Path filePath) throws IOException
{
return IO.toString(new FileReader(filePath.toFile()));
}
public static class TestListener implements Scanner.BulkListener
{
private CountDownLatch _latch = new CountDownLatch(1);
public void awaitChanges() throws Exception
{
assertTrue(_latch.await(5, TimeUnit.SECONDS));
_latch = new CountDownLatch(1);
}
@Override
public void filesChanged(Set<String> filenames)
{
_latch.countDown();
}
}
}