Skip to content

Commit 9c4ab48

Browse files
committed
Using before/after pattern to ensure proper stopping of glassfish instances
- EmbeddedTest also should use before/after so properly do the cleanup when the test fails. I guess this will be on other places too. Signed-off-by: David Matějček <[email protected]>
1 parent 259cc73 commit 9c4ab48

File tree

23 files changed

+359
-323
lines changed

23 files changed

+359
-323
lines changed

appserver/tests/embedded/basic/lifecycle/src/test/java/org/glassfish/tests/embedded/basic/lifecycle/LifeCycleTest.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2023, 2025 Contributors to the Eclipse Foundation.
33
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -24,7 +24,11 @@
2424
import org.glassfish.embeddable.GlassFishException;
2525
import org.glassfish.embeddable.GlassFishProperties;
2626
import org.glassfish.embeddable.GlassFishRuntime;
27+
import org.junit.jupiter.api.AfterAll;
2728
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.io.TempDir;
30+
31+
import static org.glassfish.embeddable.GlassFish.Status.DISPOSED;
2832

2933
/**
3034
@@ -34,62 +38,77 @@ public class LifeCycleTest {
3438

3539
Logger logger = Logger.getAnonymousLogger();
3640

37-
GlassFishRuntime runtime;
41+
private GlassFishRuntime runtime;
42+
private static GlassFish instance1;
43+
private static GlassFish instance2;
44+
45+
@TempDir
46+
private static File tmpDir;
47+
48+
@AfterAll
49+
static void close() throws Exception {
50+
if (instance1 != null && instance1.getStatus() != DISPOSED) {
51+
instance1.dispose();
52+
}
53+
if (instance2 != null && instance2.getStatus() != DISPOSED) {
54+
instance2.dispose();
55+
}
56+
}
3857

3958
@Test
4059
public void test() throws GlassFishException {
4160
runtime = GlassFishRuntime.bootstrap();
4261

43-
GlassFish instance1 = runtime.newGlassFish();
62+
instance1 = runtime.newGlassFish();
4463
logger.info("Instance1 created" + instance1);
4564
instance1.start();
4665
logger.info("Instance1 started #1");
47-
sleep();
66+
sleep();
4867
instance1.stop();
4968
logger.info("Instance1 stopped #1");
5069
instance1.start();
5170
logger.info("Instance1 started #2");
52-
sleep();
71+
sleep();
5372
instance1.stop();
5473
logger.info("Instance1 stopped #2");
5574
instance1.dispose();
5675
logger.info("Instance1 disposed");
5776
checkDisposed();
5877

59-
6078
GlassFishProperties props = new GlassFishProperties();
61-
props.setProperty("glassfish.embedded.tmpdir", System.getProperty("user.dir"));
62-
GlassFish instance2 = runtime.newGlassFish(props);
79+
props.setProperty("glassfish.embedded.tmpdir", tmpDir.getAbsolutePath());
80+
instance2 = runtime.newGlassFish(props);
6381
logger.info("instance2 created" + instance2);
6482
instance2.start();
6583
logger.info("Instance2 started #1");
66-
sleep();
84+
sleep();
6785
instance2.stop();
6886
logger.info("Instance2 stopped #1");
6987
instance2.start();
7088
logger.info("Instance2 started #2");
71-
sleep();
89+
sleep();
7290
instance2.stop();
7391
logger.info("Instance2 stopped #2");
7492
instance2.dispose();
7593
logger.info("Instance2 disposed");
7694
checkDisposed();
7795
}
7896

97+
7998
private void sleep() {
80-
try {
81-
Thread.sleep(1000);
82-
} catch(Exception ex) {
83-
}
99+
try {
100+
Thread.sleep(1000);
101+
} catch (Exception ex) {
102+
}
84103
}
85104
// throws exception if the temp dir is not cleaned out.
86105

106+
87107
private void checkDisposed() {
88108
String instanceRoot = System.getProperty("com.sun.aas.instanceRoot");
89109
logger.info("Checking whether " + instanceRoot + " is disposed or not");
90110
if (new File(instanceRoot).exists()) {
91-
throw new RuntimeException("Directory " + instanceRoot +
92-
" is not cleaned up after glassfish.dispose()");
111+
throw new RuntimeException("Directory " + instanceRoot + " is not cleaned up after glassfish.dispose()");
93112
}
94113
}
95114
}

appserver/tests/embedded/cdi_basic/src/test/java/org/glassfish/tests/embedded/cdi_basic/BasicCDIUnitTest.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2023, 2025 Contributors to the Eclipse Foundation.
33
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -36,7 +36,9 @@
3636
import org.glassfish.embeddable.archive.ScatteredArchive;
3737
import org.glassfish.embeddable.web.HttpListener;
3838
import org.glassfish.embeddable.web.WebContainer;
39+
import org.junit.jupiter.api.AfterAll;
3940
import org.junit.jupiter.api.Assertions;
41+
import org.junit.jupiter.api.BeforeAll;
4042
import org.junit.jupiter.api.Test;
4143

4244
/**
@@ -47,13 +49,26 @@ public class BasicCDIUnitTest {
4749

4850
private static final String PROJECT_DIR = System.getProperty("project.directory");
4951

50-
@Test
51-
public void test() throws Exception {
52+
private static GlassFish glassfish;
5253

54+
@BeforeAll
55+
static void init() throws Exception {
5356
GlassFishProperties props = new GlassFishProperties();
5457
props.setPort("http-listener", 8080);
55-
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(props);
58+
glassfish = GlassFishRuntime.bootstrap().newGlassFish(props);
5659
glassfish.start();
60+
}
61+
62+
@AfterAll
63+
static void close() throws Exception {
64+
if (glassfish != null) {
65+
glassfish.dispose();
66+
}
67+
}
68+
69+
@Test
70+
public void test() throws Exception {
71+
5772

5873
// Test Scattered Web Archive
5974
ScatteredArchive sa = new ScatteredArchive("cdi_basic",
@@ -81,8 +96,6 @@ public void test() throws Exception {
8196

8297
deployer.undeploy(appname);
8398

84-
glassfish.dispose();
85-
8699
}
87100

88101
private void get(String urlStr, String result) throws Exception {

appserver/tests/embedded/cdi_ejb_jpa/src/test/java/org/glassfish/tests/embedded/cdi_ejb_jpa/BasicCDIUnitTest.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2023, 2025 Contributors to the Eclipse Foundation.
33
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -36,9 +36,13 @@
3636
import org.glassfish.embeddable.archive.ScatteredArchive;
3737
import org.glassfish.embeddable.web.HttpListener;
3838
import org.glassfish.embeddable.web.WebContainer;
39+
import org.junit.jupiter.api.AfterAll;
3940
import org.junit.jupiter.api.Assertions;
41+
import org.junit.jupiter.api.BeforeAll;
4042
import org.junit.jupiter.api.Test;
4143

44+
import static org.junit.jupiter.api.Assertions.assertTrue;
45+
4246
/**
4347
4448
*/
@@ -47,14 +51,25 @@ public class BasicCDIUnitTest {
4751

4852
private static final String PROJECT_DIR = System.getProperty("project.directory");
4953

50-
@Test
51-
public void test() throws Exception {
54+
private static GlassFish glassfish;
5255

56+
@BeforeAll
57+
static void init() throws Exception {
5358
GlassFishProperties props = new GlassFishProperties();
5459
props.setPort("http-listener", 8080);
55-
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(props);
60+
glassfish = GlassFishRuntime.bootstrap().newGlassFish(props);
5661
glassfish.start();
62+
}
63+
64+
@AfterAll
65+
static void close() throws Exception {
66+
if (glassfish != null) {
67+
glassfish.dispose();
68+
}
69+
}
5770

71+
@Test
72+
public void test() throws Exception {
5873
// Test Scattered Web Archive
5974
ScatteredArchive sa = new ScatteredArchive("cdi_ejb_jpa",
6075
ScatteredArchive.Type.WAR, new File(PROJECT_DIR, "src/main/webapp"));
@@ -80,37 +95,34 @@ public void test() throws Exception {
8095
"All CDI beans have been injected.");
8196

8297
deployer.undeploy(appname);
83-
84-
glassfish.dispose();
85-
8698
}
8799

88100
private void get(String urlStr, String result) throws Exception {
89101
URL url = new URL(urlStr);
90102
URLConnection yc = url.openConnection();
91103
System.out.println("\nURLConnection [" + yc + "] : ");
92-
BufferedReader in = new BufferedReader(new InputStreamReader(
93-
yc.getInputStream()));
94-
String line = null;
95104
boolean found = false;
96-
while ((line = in.readLine()) != null) {
97-
System.out.println(line);
98-
if (line.indexOf(result) != -1) {
99-
found = true;
105+
try (BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()))) {
106+
String line;
107+
while ((line = in.readLine()) != null) {
108+
System.out.println(line);
109+
if (line.indexOf(result) != -1) {
110+
found = true;
111+
}
100112
}
101113
}
102-
Assertions.assertTrue(found);
114+
assertTrue(found);
103115
System.out.println("\n***** SUCCESS **** Found [" + result + "] in the response.*****\n");
104116
}
105117

106118
void printContents(URI jarURI) throws IOException {
107-
JarFile jarfile = new JarFile(new File(jarURI));
108-
System.out.println("\n\n[" + jarURI + "] contents : \n");
109-
Enumeration<JarEntry> entries = jarfile.entries();
110-
while (entries.hasMoreElements()) {
111-
JarEntry entry = entries.nextElement();
112-
System.out.println(entry.getSize() + "\t" + new Date(entry.getTime()) +
113-
"\t" + entry.getName());
119+
try (JarFile jarfile = new JarFile(new File(jarURI))) {
120+
System.out.println("\n\n[" + jarURI + "] contents : \n");
121+
Enumeration<JarEntry> entries = jarfile.entries();
122+
while (entries.hasMoreElements()) {
123+
JarEntry entry = entries.nextElement();
124+
System.out.println(entry.getSize() + "\t" + new Date(entry.getTime()) + "\t" + entry.getName());
125+
}
114126
}
115127
System.out.println();
116128
}

appserver/tests/embedded/ejb/basic/src/test/java/org/glassfish/tests/embedded/ejb/basic/test/EmbeddedTest.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2023, 2025 Contributors to the Eclipse Foundation.
33
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -27,9 +27,13 @@
2727
import org.glassfish.embeddable.GlassFishRuntime;
2828
import org.glassfish.tests.embedded.ejb.basic.SampleEjb;
2929
import org.glassfish.tests.embedded.ejb.basic.TimerEjb;
30+
import org.junit.jupiter.api.AfterAll;
3031
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.BeforeAll;
3133
import org.junit.jupiter.api.Test;
3234

35+
import static org.junit.jupiter.api.Assertions.assertTrue;
36+
3337
/**
3438
* this test will use the ejb API testing.
3539
*
@@ -38,12 +42,23 @@
3842
*/
3943
public class EmbeddedTest {
4044

41-
@Test
42-
public void test() throws Exception {
43-
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish();
45+
private static GlassFish glassfish;
46+
47+
@BeforeAll
48+
static void init() throws Exception {
49+
glassfish = GlassFishRuntime.bootstrap().newGlassFish();
4450
glassfish.start();
51+
}
4552

53+
@AfterAll
54+
static void close() throws Exception {
55+
if (glassfish != null) {
56+
glassfish.dispose();
57+
}
58+
}
4659

60+
@Test
61+
public void test() throws Exception {
4762
Deployer deployer = glassfish.getDeployer();
4863
URI uri = new File(System.getProperty("project.directory"), "target/classes").toURI();
4964
System.out.println("Deploying [" + uri + "]");
@@ -64,13 +79,8 @@ public void test() throws Exception {
6479
System.out.println("Verifying TimerEjb [" + timerEjb + "]");
6580
Thread.sleep(4000);
6681
boolean result = timerEjb.verifyTimer();
67-
Assertions.assertTrue(result);
82+
assertTrue(result);
6883
System.out.println("TimerEJB tested successfully.");
69-
70-
glassfish.stop();
71-
glassfish.dispose();
72-
7384
System.out.println("EmbeddedTest completed.");
74-
7585
}
7686
}

0 commit comments

Comments
 (0)