|
| 1 | +# What is GlassFish? |
| 2 | + |
| 3 | +[Eclipse GlassFish](https://projects.eclipse.org/projects/ee4j.glassfish) is a Jakarta EE compatible implementation sponsored by the Eclipse Foundation. |
| 4 | + |
| 5 | +%%LOGO%% |
| 6 | + |
| 7 | +# How to use this image |
| 8 | + |
| 9 | +## Usual Start |
| 10 | + |
| 11 | +```bash |
| 12 | +docker run -d glassfish |
| 13 | +``` |
| 14 | + |
| 15 | +Let's try something more complicated: |
| 16 | + |
| 17 | +- `-d` starts the container as a daemon, so the shell doesn't print logs and finishes. |
| 18 | +- `AS_STOP_TIMEOUT=60000` configures the asadmin stop-domain timeout in millis. If it is exceeded, stop-domain can kill the JVM. |
| 19 | +- `AS_TRACE=true` enables tracing of the GlassFish startup. It is useful when the server doesn't start without any useful logs. |
| 20 | +- `--user 1000` configures explicit user id for the container. It can be useful for K8S containers. |
| 21 | +- `glassfish asadmin start-domain --verbose --debug=true` replaces default startup command with another. |
| 22 | +- Docker then returns the container id which you can use for further commands. |
| 23 | + |
| 24 | +```bash |
| 25 | +docker run -d -e AS_STOP_TIMEOUT=600000 -e AS_TRACE=true --user 1000 glassfish asadmin start-domain --verbose --debug=true |
| 26 | +5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72 |
| 27 | + |
| 28 | +docker logs 5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72 |
| 29 | +... |
| 30 | + |
| 31 | +docker stop 5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72 |
| 32 | +``` |
| 33 | + |
| 34 | +## TestContainers |
| 35 | + |
| 36 | +This is probably the simplest possible test with [GlassFish](https://glassfish.org/) and [TestContainers](https://www.testcontainers.org/). It automatically starts the GlassFish Docker Container and then stops it after the test. The test here is quite trivial - downloads the welcome page and verifies if it contains expected phrases. |
| 37 | + |
| 38 | +If you want to run more complicated tests, the good path is to |
| 39 | + |
| 40 | +1. Write a singleton managing the GlassFish Docker Container or the whole test environment. |
| 41 | +2. Write your own Junit5 extension which would start the container before your test and ensure that everything stops after the test including failures. |
| 42 | +3. You can also implement direct access to the virtual network, containers, so you can change the environment configuration in between tests and simulate network failures, etc. |
| 43 | + |
| 44 | +```java |
| 45 | +@Testcontainers |
| 46 | +public class WelcomePageITest { |
| 47 | + |
| 48 | + @Container |
| 49 | + private final GenericContainer server = new GenericContainer<>("glassfish:7.0.3").withExposedPorts(8080); |
| 50 | + |
| 51 | + @Test |
| 52 | + void getRoot() throws Exception { |
| 53 | + URL url = new URL("http://localhost:" + server.getMappedPort(8080) + "/"); |
| 54 | + StringBuilder content = new StringBuilder(); |
| 55 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 56 | + try { |
| 57 | + connection.setRequestMethod("GET"); |
| 58 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { |
| 59 | + String inputLine; |
| 60 | + while ((inputLine = in.readLine()) != null) { |
| 61 | + content.append(inputLine); |
| 62 | + } |
| 63 | + } |
| 64 | + } finally { |
| 65 | + connection.disconnect(); |
| 66 | + } |
| 67 | + assertThat(content.toString(), stringContainsInOrder("Eclipse GlassFish", "index.html", "production-quality")); |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | +``` |
0 commit comments