Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.tests.embedded.runnable.app;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Initialized;
import jakarta.enterprise.event.Observes;

import java.util.logging.Logger;

/**
* @author Ondro Mihalyi
*/
@ApplicationScoped
public class SystemPropertyApp {

private static final Logger LOG = Logger.getLogger(SystemPropertyApp.class.getName());

public void init(@Observes @Initialized(ApplicationScoped.class) Object event) {
String myName = System.getProperty("my.name");
LOG.info("System property my.name: " + myName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,55 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import static java.lang.System.err;
import static java.util.stream.Collectors.joining;

/**
*
* @author Ondro Mihalyi
*/
public class GfEmbeddedUtils {

public static Process runGlassFishEmbedded(String glassfishEmbeddedJarName, String... additionalArguments) throws IOException {
public static final String DEBUG_PROPERTY_NAME = "glassfish.test.debug";

public static Optional<String> getDebugArg() {
return Optional.ofNullable(System.getProperty(DEBUG_PROPERTY_NAME))
.filter(debugPort -> List.of("none", "disabled", "false").stream()
.allMatch(value -> !value.equalsIgnoreCase(debugPort))
);
}

public static boolean isDebugEnabled() {
return getDebugArg().isPresent();
}

public static Process runGlassFishEmbedded(String glassfishEmbeddedJarName, String... additionalArguments) throws
IOException {
return runGlassFishEmbedded(glassfishEmbeddedJarName, List.of(), additionalArguments);
}

public static Process runGlassFishEmbedded(String glassfishEmbeddedJarName, List<String> jvmOpts, String... additionalArguments) throws IOException {
List<String> arguments = new ArrayList<>();
arguments.addAll(List.of(ProcessHandle.current().info().command().get(),
// "-Xrunjdwp:transport=dt_socket,server=y,suspend=y", // enable debugging on random port
arguments.add(ProcessHandle.current().info().command().get());
addDebugArgsIfDebugEnabled(arguments);
arguments.addAll(jvmOpts);
arguments.addAll(List.of(
"-jar", glassfishEmbeddedJarName,
"--noPort",
"--stop"));
for (String argument : additionalArguments) {
arguments.add(argument);
}
System.out.println("\nCurrent directory: " + Paths.get(".").toFile().getAbsolutePath());
System.out.println("Going to run Embedded GlassFish: " + arguments.stream()
.map(arg -> "'" + arg + "'")
.collect(joining(" ")) + "\n");
return new ProcessBuilder()
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.PIPE)
Expand All @@ -57,4 +85,12 @@ public static Stream<String> outputToStreamOfLines(Process gfEmbeddedProcess) {
.peek(err::println);
}

private static void addDebugArgsIfDebugEnabled(List<String> arguments) {
getDebugArg()
.ifPresent(debugArgs -> {
arguments.add(debugArgs);
});

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.tests.embedded.runnable;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import org.glassfish.tests.embedded.runnable.TestArgumentProviders.GfEmbeddedJarNameProvider;
import org.glassfish.tests.embedded.runnable.app.SystemPropertyApp;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.outputToStreamOfLines;
import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.runGlassFishEmbedded;
import static org.glassfish.tests.embedded.runnable.ShrinkwrapUtils.logArchiveContent;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Ondro Mihalyi
*/
public class SystemPropertyTest {

private static final Logger LOG = Logger.getLogger(SystemPropertyTest.class.getName());

@ParameterizedTest
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
void testSystemPropertyFromJvmOption(String gfEmbeddedJarName) throws Exception {
File warFile = null;
try {
warFile = createSystemPropertyApp();
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
List.of("-Dmy.name=Embedded GlassFish"),
warFile.getAbsolutePath()
);
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
.anyMatch(line -> line.contains("System property my.name: Embedded GlassFish")),
"Application should print the value of the my.name system property.");
gfEmbeddedProcess.waitFor(30, TimeUnit.SECONDS);
} finally {
Optional.ofNullable(warFile).ifPresent(File::delete);
}
}

@ParameterizedTest
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
void testSystemPropertyFromAdminCommand(String gfEmbeddedJarName) throws Exception {
File warFile = null;
try {
warFile = createSystemPropertyApp();
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
"create-system-properties my.name=Embedded\\ GlassFish",
warFile.getAbsolutePath()
);
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
.anyMatch(line -> line.contains("System property my.name: Embedded GlassFish")),
"Application should print the value of the my.name system property.");
gfEmbeddedProcess.waitFor(30, TimeUnit.SECONDS);
} finally {
Optional.ofNullable(warFile).ifPresent(File::delete);
}
}

@ParameterizedTest
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
void testSystemPropertyFromPropertyFileDirectly(String gfEmbeddedJarName, TestInfo testInfo) throws Exception {
File warFile = null;
Path propertiesFile = preparePropertiesFile(testInfo);
try {
warFile = createSystemPropertyApp();
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
"--properties=" + propertiesFile.toFile().getPath(),
warFile.getAbsolutePath()
);
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
.anyMatch(line -> line.contains("System property my.name: Embedded GlassFish")),
"Application should print the value of the my.name system property.");
gfEmbeddedProcess.waitFor(30, TimeUnit.SECONDS);
} finally {
Optional.ofNullable(warFile).ifPresent(File::delete);
}
}

@ParameterizedTest
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
void testSystemPropertyFromDomainConfig(String gfEmbeddedJarName, TestInfo testInfo) throws Exception {
File warFile = null;
Path domainConfigFile = prepareDomainConfig(testInfo);
try {
warFile = createSystemPropertyApp();
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
"--domainConfigFile=" + domainConfigFile.toFile().getPath(),
warFile.getAbsolutePath()
);
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
.anyMatch(line -> line.contains("System property my.name: Embedded GlassFish")),
"Application should print the value of the my.name system property.");
gfEmbeddedProcess.waitFor(30, TimeUnit.SECONDS);
} finally {
Optional.ofNullable(warFile).ifPresent(File::delete);
}
}

private File createSystemPropertyApp() throws Exception {
String warName = "systemPropertyApp.war";
WebArchive warArchive = ShrinkWrap.create(WebArchive.class, warName)
.addClass(SystemPropertyApp.class);
File warFile = new File(warName);
warArchive.as(ZipExporter.class).exportTo(warFile, true);
logArchiveContent(warArchive, warName, LOG::info);
return warFile;
}

private Path prepareDomainConfig(TestInfo testInfo) throws IOException {
String testClassName = testInfo.getTestClass().get().getSimpleName();
String testMethodName = testInfo.getTestMethod().get().getName();
Path domainConfigFile = Paths.get(testClassName + "-" + testMethodName + "-" + "domain.xml");
Files.copy(getClass().getClassLoader().getResourceAsStream(testClassName + "/domain.xml"),
domainConfigFile,
StandardCopyOption.REPLACE_EXISTING);
return domainConfigFile;
}

private Path preparePropertiesFile(TestInfo testInfo) throws IOException {
String testClassName = testInfo.getTestClass().get().getSimpleName();
String testMethodName = testInfo.getTestMethod().get().getName();
Path propertiesFile = Paths.get(testClassName + "-" + testMethodName + "-" + "glassfish.properties");
Files.copy(getClass().getClassLoader().getResourceAsStream(testClassName + "/glassfish.properties"),
propertiesFile,
StandardCopyOption.REPLACE_EXISTING);
return propertiesFile;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.glassfish.tests.embedded.runnable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

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

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext ec) throws Exception {
return Stream.of(Arguments.of("glassfish-embedded-all.jar"), Arguments.of("glassfish-embedded-web.jar"));
List<Arguments> arguments = new ArrayList<>();
arguments.add(Arguments.of("glassfish-embedded-all.jar"));
if (!GfEmbeddedUtils.isDebugEnabled()) {
arguments.add(Arguments.of("glassfish-embedded-web.jar"));
}
return arguments.stream();
}

}

/**
* For testing a single execution when debugging
*/
public static class SingleGfEmbeddedJarNameProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext ec) throws Exception {
return Stream.of(Arguments.of("glassfish-embedded-all.jar"));
}

}
}
Loading