|
| 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 | +} |
0 commit comments