This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
/
StaticContentDeploymentTest.java
113 lines (103 loc) · 4.08 KB
/
StaticContentDeploymentTest.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
/**
* Copyright 2015 Red Hat, Inc, and individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.swarm.integration.staticcontent.deployment;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Test;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.integration.base.AbstractWildFlySwarmTestCase;
import org.wildfly.swarm.integration.staticcontent.StaticContentCommonTests;
import org.wildfly.swarm.undertow.WARArchive;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
/**
* @author Bob McWhirter
*/
public class StaticContentDeploymentTest extends AbstractWildFlySwarmTestCase implements StaticContentCommonTests {
@Test
public void testStaticContent() throws Exception {
Container container = newContainer();
container.start();
try {
WARArchive deployment = ShrinkWrap.create(WARArchive.class);
deployment.staticContent();
container.deploy(deployment);
assertBasicStaticContentWorks("");
assertFileChangesReflected("");
} finally {
container.stop();
}
}
@Test
public void testStaticContentWithContext() throws Exception {
Container container = newContainer();
container.start();
try {
WARArchive deployment = ShrinkWrap.create(WARArchive.class);
deployment.setContextRoot("/static");
deployment.staticContent();
container.deploy(deployment);
assertBasicStaticContentWorks("static");
assertFileChangesReflected("static");
} finally {
container.stop();
}
}
@Test
public void testStaticContentWithBase() throws Exception {
Container container = newContainer();
container.start();
try {
WARArchive deployment = ShrinkWrap.create(WARArchive.class);
deployment.staticContent("foo");
container.deploy(deployment);
assertContains("", "This is foo/index.html.");
assertContains("index.html", "This is foo/index.html.");
} finally {
container.stop();
}
}
private void assertFileChangesReflected(String context) throws Exception {
if (context.length() > 0 && !context.endsWith("/")) {
context = context + "/";
}
Path tmpDir = Paths.get(System.getProperty("user.dir"), "src", "main", "webapp", "tmp");
Files.createDirectories(tmpDir);
Path newFile = tmpDir.resolve("new-file.txt");
try {
Files.write(newFile, "This is new-file.txt.".getBytes());
assertContains(context + "tmp/new-file.txt", "This is new-file.txt.");
Files.write(newFile, "This is updated new-file.txt.".getBytes());
assertContains(context + "tmp/new-file.txt", "This is updated new-file.txt.");
} finally {
Files.deleteIfExists(newFile);
}
}
public void assertContains(String path, String text) throws Exception {
assertThat(fetch("http://localhost:8080/" + path)).contains(text);
}
public void assertNotFound(String path) throws Exception {
try {
fetch("http://localhost:8080/" + path);
fail("FileNotFoundException expected but content found for path " + path);
} catch (Exception ex) {
assertThat(ex).isInstanceOf(FileNotFoundException.class);
}
}
}