Skip to content

Commit 35c65cd

Browse files
committed
Test for Embedded GlassFish
1 parent 54d3c9c commit 35c65cd

12 files changed

Lines changed: 150 additions & 12 deletions

File tree

images/server/7.1.0/docs/content.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ Follow these steps:
4949

5050
1. Create an empty directory on your filesystem, e.g. `/deployment`
5151
2. Copy the application package to this directory - so that it's for example on the path `/deployment/application.war`
52-
3. Run the following command to start GlassFish in Docker with your application, where `/deployments` is path to the directory created in step 1:
52+
3. Run the following command to start GlassFish in Docker with your application, where `/deployments` is path to the directory created in step 1, and /deploy is the directory in the container where GlassFish expects applications:
5353

5454
```
55-
docker run -p 8080:8080 -p 4848:4848 -v /deployments:/opt/glassfish7/glassfish/domains/domain1/autodeploy @docker.glassfish.repository@
55+
docker run -p 8080:8080 -p 4848:4848 -v /deployments:/deploy @docker.glassfish.repository@
5656
```
5757

5858
Then you can open the application in the browser with:

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,25 @@
319319
</execution>
320320
</executions>
321321
</plugin>
322+
<plugin>
323+
<artifactId>maven-war-plugin</artifactId>
324+
<version>3.4.0</version>
325+
<executions>
326+
<execution>
327+
<id>build-test-app</id>
328+
<phase>test-compile</phase>
329+
<goals>
330+
<goal>war</goal>
331+
</goals>
332+
<configuration>
333+
<warSourceDirectory>src/test/webapp</warSourceDirectory>
334+
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
335+
<warName>application</warName>
336+
<classifier>test</classifier>
337+
</configuration>
338+
</execution>
339+
</executions>
340+
</plugin>
322341
</plugins>
323342
<pluginManagement>
324343
<plugins>
@@ -346,6 +365,7 @@
346365
<configuration>
347366
<systemPropertyVariables>
348367
<server.docker.glassfish.image>${server.docker.glassfish.image}</server.docker.glassfish.image>
368+
<embedded.docker.glassfish.image>${embedded.docker.glassfish.image}</embedded.docker.glassfish.image>
349369
</systemPropertyVariables>
350370
<includes>
351371
<include>**/*IT.java</include>

src/main/resources/images/embedded/docs/content.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ Follow these steps:
5656
docker run -p 8080:8080 -v /deployments:/deploy @docker.glassfish.embedded.repository@
5757
```
5858

59+
Alternatively, you can mount a specific WAR file directly:
60+
61+
```
62+
docker run -p 8080:8080 -v /deployments/application.war:/deploy/application.war @docker.glassfish.embedded.repository@
63+
```
64+
65+
**Note**: When deploying a single application, GlassFish Embedded automatically deploys it at the root context (`/`) rather than using the WAR filename as the context path.
66+
5967
Then you can open the application in the browser with:
6068

6169
* http://localhost:8080
@@ -98,12 +106,20 @@ docker run -p 8080:8080 -e 'JAVA_TOOL_OPTIONS=-Xmx512m -Dmy.property=value' @doc
98106

99107
### Custom application deployment
100108

101-
Applications placed in the `/deploy` directory will be automatically deployed at startup:
109+
Applications placed in the `/deploy` directory will be automatically deployed at startup. You can either:
102110

111+
- Mount a directory containing WAR files:
103112
```
104113
docker run -p 8080:8080 -v /path/to/apps:/deploy @docker.glassfish.embedded.repository@
105114
```
106115

116+
- Mount a specific WAR file directly:
117+
```
118+
docker run -p 8080:8080 -v /path/to/myapp.war:/deploy/myapp.war @docker.glassfish.embedded.repository@
119+
```
120+
121+
**Note**: When deploying a single application, GlassFish Embedded automatically deploys it at the root context (`/`) rather than using the WAR filename as the context path.
122+
107123
Paths to applications in different locations can be passed on command line, e.g.:
108124

109125
```

src/main/resources/images/server/docs/content.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ Follow these steps:
5555
docker run -p 8080:8080 -p 4848:4848 -v /deployments:/deploy @docker.glassfish.repository@
5656
```
5757

58+
Alternatively, you can mount a specific WAR file directly:
59+
60+
```
61+
docker run -p 8080:8080 -p 4848:4848 -v /deployment/application.war:/deploy/application.war @docker.glassfish.repository@
62+
```
63+
64+
**Note**: GlassFish Server deploys applications using the WAR filename as the context path (e.g., `application.war` becomes accessible at `/application/`).
65+
5866
Then you can open the application in the browser with:
5967

6068
* http://localhost:9080/application
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.main.distributions.docker.embedded;
18+
19+
import java.net.http.HttpResponse;
20+
import java.time.Duration;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.testcontainers.containers.BindMode;
24+
import org.testcontainers.containers.GenericContainer;
25+
import org.testcontainers.containers.wait.strategy.Wait;
26+
import org.testcontainers.junit.jupiter.Container;
27+
import org.testcontainers.junit.jupiter.Testcontainers;
28+
29+
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getEmbeddedApplication;
30+
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
33+
@Testcontainers
34+
public class EmbeddedDeploymentIT {
35+
36+
@SuppressWarnings({"rawtypes", "resource"})
37+
@Container
38+
private final GenericContainer server = new GenericContainer<>(System.getProperty("embedded.docker.glassfish.image"))
39+
.withExposedPorts(8080)
40+
.withFileSystemBind("target/test-classes/application-test.war", "/deploy/application.war", BindMode.READ_ONLY)
41+
.waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofMinutes(2)))
42+
.withLogConsumer(o -> System.err.print("GF-Embedded: " + o.getUtf8String()));
43+
44+
@Test
45+
void deployedApplicationIsAccessible() throws Exception {
46+
// Wait for the GLASSFISH STARTED message in logs
47+
long startTime = System.currentTimeMillis();
48+
boolean glassfishStarted = false;
49+
while (!glassfishStarted && (System.currentTimeMillis() - startTime) < 120000) { // 2 minutes
50+
String logs = server.getLogs();
51+
if (logs.contains("GLASSFISH STARTED")) {
52+
glassfishStarted = true;
53+
} else {
54+
Thread.sleep(1000); // Wait 1 second before checking again
55+
}
56+
}
57+
58+
if (!glassfishStarted) {
59+
System.err.println("Full logs: " + server.getLogs());
60+
throw new RuntimeException("GlassFish did not start within 2 minutes");
61+
}
62+
63+
System.err.println("GlassFish started successfully, checking application deployment...");
64+
65+
// Verify the application is deployed by checking if it's accessible
66+
final HttpResponse<String> appResponse = getEmbeddedApplication(server, "/index.html");
67+
System.err.println("Response status: " + appResponse.statusCode());
68+
System.err.println("Response body: " + appResponse.body());
69+
assertEquals(200, appResponse.statusCode(), "Application response status code");
70+
assertTrue(appResponse.body().contains("Hello from test app"), "Application should return Hello message");
71+
}
72+
}

src/test/java/org/glassfish/main/distributions/docker/server/AsadminIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import static org.hamcrest.MatcherAssert.assertThat;
2626
import static org.hamcrest.Matchers.stringContainsInOrder;
2727
import static org.junit.jupiter.api.Assertions.assertEquals;
28-
import static org.glassfish.main.distributions.docker.server.HttpUtilities.getServerDefaultRoot;
28+
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getServerDefaultRoot;
2929

3030
/**
3131
*

src/test/java/org/glassfish/main/distributions/docker/server/ChangePasswordsIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
import org.testcontainers.junit.jupiter.Container;
2323
import org.testcontainers.junit.jupiter.Testcontainers;
2424

25-
import static org.glassfish.main.distributions.docker.server.HttpUtilities.getServerDefaultRoot;
25+
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getServerDefaultRoot;
2626
import static org.hamcrest.MatcherAssert.assertThat;
2727
import static org.hamcrest.Matchers.stringContainsInOrder;
2828
import static org.junit.jupiter.api.Assertions.assertEquals;
29-
import static org.glassfish.main.distributions.docker.server.HttpUtilities.getAdminResource;
29+
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getAdminResource;
3030

3131
/**
3232
*

src/test/java/org/glassfish/main/distributions/docker/server/DefaultIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.testcontainers.junit.jupiter.Container;
2424
import org.testcontainers.junit.jupiter.Testcontainers;
2525

26-
import static org.glassfish.main.distributions.docker.server.HttpUtilities.getServerDefaultRoot;
26+
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getServerDefaultRoot;
2727
import static org.hamcrest.MatcherAssert.assertThat;
2828
import static org.hamcrest.Matchers.stringContainsInOrder;
2929
import static org.junit.jupiter.api.Assertions.assertEquals;

src/test/java/org/glassfish/main/distributions/docker/server/StartServIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.testcontainers.junit.jupiter.Container;
2424
import org.testcontainers.junit.jupiter.Testcontainers;
2525

26-
import static org.glassfish.main.distributions.docker.server.HttpUtilities.getServerDefaultRoot;
26+
import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getServerDefaultRoot;
2727
import static org.hamcrest.MatcherAssert.assertThat;
2828
import static org.hamcrest.Matchers.stringContainsInOrder;
2929
import static org.junit.jupiter.api.Assertions.assertEquals;

src/test/java/org/glassfish/main/distributions/docker/server/HttpUtilities.java renamed to src/test/java/org/glassfish/main/distributions/docker/testutils/HttpUtilities.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
*/
16-
package org.glassfish.main.distributions.docker.server;
16+
package org.glassfish.main.distributions.docker.testutils;
1717

1818
import java.net.URI;
1919
import java.net.http.HttpClient;
@@ -29,6 +29,7 @@
2929
import javax.net.ssl.TrustManager;
3030
import javax.net.ssl.X509TrustManager;
3131

32+
import org.glassfish.main.distributions.docker.server.UserPassword;
3233
import org.testcontainers.containers.GenericContainer;
3334

3435
import static java.net.http.HttpResponse.BodyHandlers.ofString;
@@ -42,23 +43,31 @@ public final class HttpUtilities {
4243
private HttpUtilities() {
4344
}
4445

45-
static HttpResponse<String> getServerDefaultRoot(GenericContainer server) throws Exception {
46+
public static HttpResponse<String> getServerDefaultRoot(GenericContainer server) throws Exception {
4647
URI uri = URI.create("http://localhost:" + server.getMappedPort(8080) + "/");
4748
try (HttpClient client = newInsecureHttpClient()) {
4849
final HttpRequest request = HttpRequest.newBuilder(uri).build();
4950
return client.send(request, ofString(StandardCharsets.UTF_8));
5051
}
5152
}
5253

53-
static HttpResponse<String> getEmbeddedDefaultRoot(GenericContainer server) throws Exception {
54+
public static HttpResponse<String> getEmbeddedDefaultRoot(GenericContainer server) throws Exception {
5455
URI uri = URI.create("http://localhost:" + server.getMappedPort(8080) + "/");
5556
try (HttpClient client = newInsecureHttpClient()) {
5657
final HttpRequest request = HttpRequest.newBuilder(uri).build();
5758
return client.send(request, ofString(StandardCharsets.UTF_8));
5859
}
5960
}
6061

61-
static HttpResponse<String> getAdminResource(GenericContainer server, String resourcePath, UserPassword userPass) throws Exception {
62+
public static HttpResponse<String> getEmbeddedApplication(GenericContainer server, String appPath) throws Exception {
63+
URI uri = URI.create("http://localhost:" + server.getMappedPort(8080) + appPath);
64+
try (HttpClient client = newInsecureHttpClient()) {
65+
final HttpRequest request = HttpRequest.newBuilder(uri).build();
66+
return client.send(request, ofString(StandardCharsets.UTF_8));
67+
}
68+
}
69+
70+
public static HttpResponse<String> getAdminResource(GenericContainer server, String resourcePath, UserPassword userPass) throws Exception {
6271
URI uri = URI.create("https://localhost:" + server.getMappedPort(4848) + resourcePath);
6372
try (HttpClient client = newInsecureHttpClient()) {
6473
final HttpRequest request = HttpRequest.newBuilder(uri)

0 commit comments

Comments
 (0)