Skip to content

Commit d2a27ca

Browse files
authored
Merge pull request #25724 from OndroMih/ondromih-embedded-fixes
Embedded GlassFish - fixes related to system properties
2 parents 2a4e8b1 + ca42dce commit d2a27ca

File tree

13 files changed

+474
-40
lines changed

13 files changed

+474
-40
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.glassfish.tests.embedded.runnable.app;
17+
18+
import jakarta.enterprise.context.ApplicationScoped;
19+
import jakarta.enterprise.context.Initialized;
20+
import jakarta.enterprise.event.Observes;
21+
22+
import java.util.logging.Logger;
23+
24+
/**
25+
* @author Ondro Mihalyi
26+
*/
27+
@ApplicationScoped
28+
public class SystemPropertyApp {
29+
30+
private static final Logger LOG = Logger.getLogger(SystemPropertyApp.class.getName());
31+
32+
public void init(@Observes @Initialized(ApplicationScoped.class) Object event) {
33+
String myName = System.getProperty("my.name");
34+
LOG.info("System property my.name: " + myName);
35+
}
36+
}

appserver/tests/embedded/runnable/src/test/java/org/glassfish/tests/embedded/runnable/GfEmbeddedUtils.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,55 @@
2020
import java.io.InputStream;
2121
import java.io.InputStreamReader;
2222
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Paths;
2324
import java.util.ArrayList;
2425
import java.util.List;
26+
import java.util.Optional;
2527
import java.util.stream.Stream;
2628

2729
import static java.lang.System.err;
30+
import static java.util.stream.Collectors.joining;
2831

2932
/**
3033
*
3134
* @author Ondro Mihalyi
3235
*/
3336
public class GfEmbeddedUtils {
3437

35-
public static Process runGlassFishEmbedded(String glassfishEmbeddedJarName, String... additionalArguments) throws IOException {
38+
public static final String DEBUG_PROPERTY_NAME = "glassfish.test.debug";
39+
40+
public static Optional<String> getDebugArg() {
41+
return Optional.ofNullable(System.getProperty(DEBUG_PROPERTY_NAME))
42+
.filter(debugPort -> List.of("none", "disabled", "false").stream()
43+
.allMatch(value -> !value.equalsIgnoreCase(debugPort))
44+
);
45+
}
46+
47+
public static boolean isDebugEnabled() {
48+
return getDebugArg().isPresent();
49+
}
50+
51+
public static Process runGlassFishEmbedded(String glassfishEmbeddedJarName, String... additionalArguments) throws
52+
IOException {
53+
return runGlassFishEmbedded(glassfishEmbeddedJarName, List.of(), additionalArguments);
54+
}
55+
56+
public static Process runGlassFishEmbedded(String glassfishEmbeddedJarName, List<String> jvmOpts, String... additionalArguments) throws IOException {
3657
List<String> arguments = new ArrayList<>();
37-
arguments.addAll(List.of(ProcessHandle.current().info().command().get(),
38-
// "-Xrunjdwp:transport=dt_socket,server=y,suspend=y", // enable debugging on random port
58+
arguments.add(ProcessHandle.current().info().command().get());
59+
addDebugArgsIfDebugEnabled(arguments);
60+
arguments.addAll(jvmOpts);
61+
arguments.addAll(List.of(
3962
"-jar", glassfishEmbeddedJarName,
63+
"--noPort",
4064
"--stop"));
4165
for (String argument : additionalArguments) {
4266
arguments.add(argument);
4367
}
68+
System.out.println("\nCurrent directory: " + Paths.get(".").toFile().getAbsolutePath());
69+
System.out.println("Going to run Embedded GlassFish: " + arguments.stream()
70+
.map(arg -> "'" + arg + "'")
71+
.collect(joining(" ")) + "\n");
4472
return new ProcessBuilder()
4573
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
4674
.redirectError(ProcessBuilder.Redirect.PIPE)
@@ -57,4 +85,12 @@ public static Stream<String> outputToStreamOfLines(Process gfEmbeddedProcess) {
5785
.peek(err::println);
5886
}
5987

88+
private static void addDebugArgsIfDebugEnabled(List<String> arguments) {
89+
getDebugArg()
90+
.ifPresent(debugArgs -> {
91+
arguments.add(debugArgs);
92+
});
93+
94+
}
95+
6096
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
package org.glassfish.tests.embedded.runnable;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
23+
import java.nio.file.StandardCopyOption;
24+
import java.util.List;
25+
import java.util.Optional;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.logging.Logger;
28+
29+
import org.glassfish.tests.embedded.runnable.TestArgumentProviders.GfEmbeddedJarNameProvider;
30+
import org.glassfish.tests.embedded.runnable.app.SystemPropertyApp;
31+
import org.jboss.shrinkwrap.api.ShrinkWrap;
32+
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
33+
import org.jboss.shrinkwrap.api.spec.WebArchive;
34+
import org.junit.jupiter.api.TestInfo;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.provider.ArgumentsSource;
37+
38+
import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.outputToStreamOfLines;
39+
import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.runGlassFishEmbedded;
40+
import static org.glassfish.tests.embedded.runnable.ShrinkwrapUtils.logArchiveContent;
41+
import static org.junit.jupiter.api.Assertions.assertTrue;
42+
43+
/**
44+
* @author Ondro Mihalyi
45+
*/
46+
public class SystemPropertyTest {
47+
48+
private static final Logger LOG = Logger.getLogger(SystemPropertyTest.class.getName());
49+
50+
@ParameterizedTest
51+
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
52+
void testSystemPropertyFromJvmOption(String gfEmbeddedJarName) throws Exception {
53+
File warFile = null;
54+
try {
55+
warFile = createSystemPropertyApp();
56+
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
57+
List.of("-Dmy.name=Embedded GlassFish"),
58+
warFile.getAbsolutePath()
59+
);
60+
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
61+
.anyMatch(line -> line.contains("System property my.name: Embedded GlassFish")),
62+
"Application should print the value of the my.name system property.");
63+
gfEmbeddedProcess.waitFor(30, TimeUnit.SECONDS);
64+
} finally {
65+
Optional.ofNullable(warFile).ifPresent(File::delete);
66+
}
67+
}
68+
69+
@ParameterizedTest
70+
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
71+
void testSystemPropertyFromAdminCommand(String gfEmbeddedJarName) throws Exception {
72+
File warFile = null;
73+
try {
74+
warFile = createSystemPropertyApp();
75+
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
76+
"create-system-properties my.name=Embedded\\ GlassFish",
77+
warFile.getAbsolutePath()
78+
);
79+
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
80+
.anyMatch(line -> line.contains("System property my.name: Embedded GlassFish")),
81+
"Application should print the value of the my.name system property.");
82+
gfEmbeddedProcess.waitFor(30, TimeUnit.SECONDS);
83+
} finally {
84+
Optional.ofNullable(warFile).ifPresent(File::delete);
85+
}
86+
}
87+
88+
@ParameterizedTest
89+
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
90+
void testSystemPropertyFromPropertyFileDirectly(String gfEmbeddedJarName, TestInfo testInfo) throws Exception {
91+
File warFile = null;
92+
Path propertiesFile = preparePropertiesFile(testInfo);
93+
try {
94+
warFile = createSystemPropertyApp();
95+
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
96+
"--properties=" + propertiesFile.toFile().getPath(),
97+
warFile.getAbsolutePath()
98+
);
99+
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
100+
.anyMatch(line -> line.contains("System property my.name: Embedded GlassFish")),
101+
"Application should print the value of the my.name system property.");
102+
gfEmbeddedProcess.waitFor(30, TimeUnit.SECONDS);
103+
} finally {
104+
Optional.ofNullable(warFile).ifPresent(File::delete);
105+
}
106+
}
107+
108+
@ParameterizedTest
109+
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
110+
void testSystemPropertyFromDomainConfig(String gfEmbeddedJarName, TestInfo testInfo) throws Exception {
111+
File warFile = null;
112+
Path domainConfigFile = prepareDomainConfig(testInfo);
113+
try {
114+
warFile = createSystemPropertyApp();
115+
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
116+
"--domainConfigFile=" + domainConfigFile.toFile().getPath(),
117+
warFile.getAbsolutePath()
118+
);
119+
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
120+
.anyMatch(line -> line.contains("System property my.name: Embedded GlassFish")),
121+
"Application should print the value of the my.name system property.");
122+
gfEmbeddedProcess.waitFor(30, TimeUnit.SECONDS);
123+
} finally {
124+
Optional.ofNullable(warFile).ifPresent(File::delete);
125+
}
126+
}
127+
128+
private File createSystemPropertyApp() throws Exception {
129+
String warName = "systemPropertyApp.war";
130+
WebArchive warArchive = ShrinkWrap.create(WebArchive.class, warName)
131+
.addClass(SystemPropertyApp.class);
132+
File warFile = new File(warName);
133+
warArchive.as(ZipExporter.class).exportTo(warFile, true);
134+
logArchiveContent(warArchive, warName, LOG::info);
135+
return warFile;
136+
}
137+
138+
private Path prepareDomainConfig(TestInfo testInfo) throws IOException {
139+
String testClassName = testInfo.getTestClass().get().getSimpleName();
140+
String testMethodName = testInfo.getTestMethod().get().getName();
141+
Path domainConfigFile = Paths.get(testClassName + "-" + testMethodName + "-" + "domain.xml");
142+
Files.copy(getClass().getClassLoader().getResourceAsStream(testClassName + "/domain.xml"),
143+
domainConfigFile,
144+
StandardCopyOption.REPLACE_EXISTING);
145+
return domainConfigFile;
146+
}
147+
148+
private Path preparePropertiesFile(TestInfo testInfo) throws IOException {
149+
String testClassName = testInfo.getTestClass().get().getSimpleName();
150+
String testMethodName = testInfo.getTestMethod().get().getName();
151+
Path propertiesFile = Paths.get(testClassName + "-" + testMethodName + "-" + "glassfish.properties");
152+
Files.copy(getClass().getClassLoader().getResourceAsStream(testClassName + "/glassfish.properties"),
153+
propertiesFile,
154+
StandardCopyOption.REPLACE_EXISTING);
155+
return propertiesFile;
156+
}
157+
158+
}

appserver/tests/embedded/runnable/src/test/java/org/glassfish/tests/embedded/runnable/TestArgumentProviders.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.glassfish.tests.embedded.runnable;
1717

18+
import java.util.ArrayList;
19+
import java.util.List;
1820
import java.util.stream.Stream;
1921

2022
import org.junit.jupiter.api.extension.ExtensionContext;
@@ -31,20 +33,14 @@ public static class GfEmbeddedJarNameProvider implements ArgumentsProvider {
3133

3234
@Override
3335
public Stream<? extends Arguments> provideArguments(ExtensionContext ec) throws Exception {
34-
return Stream.of(Arguments.of("glassfish-embedded-all.jar"), Arguments.of("glassfish-embedded-web.jar"));
36+
List<Arguments> arguments = new ArrayList<>();
37+
arguments.add(Arguments.of("glassfish-embedded-all.jar"));
38+
if (!GfEmbeddedUtils.isDebugEnabled()) {
39+
arguments.add(Arguments.of("glassfish-embedded-web.jar"));
40+
}
41+
return arguments.stream();
3542
}
3643

3744
}
3845

39-
/**
40-
* For testing a single execution when debugging
41-
*/
42-
public static class SingleGfEmbeddedJarNameProvider implements ArgumentsProvider {
43-
44-
@Override
45-
public Stream<? extends Arguments> provideArguments(ExtensionContext ec) throws Exception {
46-
return Stream.of(Arguments.of("glassfish-embedded-all.jar"));
47-
}
48-
49-
}
5046
}

0 commit comments

Comments
 (0)