diff --git a/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java b/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java index 6657fe4787da..93e679f6b44b 100644 --- a/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java +++ b/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java @@ -687,8 +687,9 @@ else if (properties.size() > 0) // TODO module path - for (Prop property : environment.getProperties()) - cmd.addArg(property.key, property.value); + Props props = environment.getProperties(); + for (Prop property : props) + cmd.addArg(property.key, props.expand(property.value)); for (Path xmlFile : environment.getXmlFiles()) cmd.addArg(xmlFile.toAbsolutePath().toString()); diff --git a/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java b/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java index 2b8d29468773..a4d76301ede7 100644 --- a/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java +++ b/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java @@ -14,7 +14,6 @@ package org.eclipse.jetty.start; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.PrintStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -67,7 +66,7 @@ public void testStopProcessing() throws Exception public void testListConfig() throws Exception { List cmdLineArgs = new ArrayList<>(); - File testJettyHome = MavenTestingUtils.getTestResourceDir("dist-home"); + Path testJettyHome = MavenPaths.findTestResourceDir("dist-home"); cmdLineArgs.add("user.dir=" + testJettyHome); cmdLineArgs.add("-Duser.dir=foo"); // used to test "source" display on "Java Environment" cmdLineArgs.add("jetty.home=" + testJettyHome); @@ -99,8 +98,8 @@ public void testListConfig() throws Exception public void testUnknownDistroCommand() throws Exception { List cmdLineArgs = new ArrayList<>(); - File testJettyHome = MavenTestingUtils.getTestResourceDir("dist-home"); - Path testJettyBase = MavenTestingUtils.getTargetTestingPath("base-example-unknown"); + Path testJettyHome = MavenPaths.targetTestDir("dist-home"); + Path testJettyBase = MavenPaths.targetTestDir("base-example-unknown"); FS.ensureDirectoryExists(testJettyBase); Path zedIni = testJettyBase.resolve("start.d/zed.ini"); FS.ensureDirectoryExists(zedIni.getParent()); diff --git a/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyDump.java b/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyDump.java index b7628290b70b..ab287a43f144 100644 --- a/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyDump.java +++ b/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyDump.java @@ -16,30 +16,35 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.util.Enumeration; +import java.util.Collections; +import java.util.Objects; import java.util.Properties; +import java.util.function.Predicate; public class PropertyDump { public static void main(String[] args) { System.out.printf("PropertyDump%n"); + + Predicate nameSelectionPredicate = + (name) -> + name.startsWith("test.") || + name.startsWith("jetty."); + // As System Properties Properties props = System.getProperties(); - Enumeration names = props.propertyNames(); - while (names.hasMoreElements()) - { - String name = (String)names.nextElement(); - // only interested in "test." prefixed properties - if (name.startsWith("test.")) - { - System.out.printf("System %s=%s%n", name, props.getProperty(name)); - } - } + props.stringPropertyNames() + .stream() + .filter(nameSelectionPredicate) + .sorted() + .forEach((name) -> + System.out.printf("System %s=%s%n", name, props.getProperty(name))); // As File Argument for (String arg : args) { + System.out.printf("Arg [%s]%n", arg); if (arg.endsWith(".properties")) { Properties aprops = new Properties(); @@ -47,15 +52,13 @@ public static void main(String[] args) try (FileReader reader = new FileReader(propFile)) { aprops.load(reader); - Enumeration anames = aprops.propertyNames(); - while (anames.hasMoreElements()) - { - String name = (String)anames.nextElement(); - if (name.startsWith("test.")) - { - System.out.printf("%s %s=%s%n", propFile.getName(), name, aprops.getProperty(name)); - } - } + Collections.list(aprops.propertyNames()) + .stream() + .map(Objects::toString) + .filter(nameSelectionPredicate) + .sorted() + .forEach((name) -> + System.out.printf("%s %s=%s%n", propFile.getName(), name, aprops.getProperty(name))); } catch (IOException e) { diff --git a/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyPassingTest.java b/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyPassingTest.java index 3e5bf9816d8f..5f609f36b1b6 100644 --- a/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyPassingTest.java +++ b/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyPassingTest.java @@ -20,19 +20,26 @@ import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.eclipse.jetty.toolchain.test.IO; -import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.toolchain.test.MavenPaths; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; +@ExtendWith(WorkDirExtension.class) public class PropertyPassingTest { private static class ConsoleCapture implements Runnable @@ -86,10 +93,12 @@ public ConsoleCapture start() } } + public WorkDir workDir; + @Test public void testAsJvmArg() throws IOException, InterruptedException { - File bogusXml = MavenTestingUtils.getTestResourceFile("bogus.xml"); + Path bogusXml = MavenPaths.findTestResourceFile("bogus.xml"); // Setup command line List commands = new ArrayList<>(); @@ -100,7 +109,7 @@ public void testAsJvmArg() throws IOException, InterruptedException // addDebug(commands); commands.add("-Dtest.foo=bar"); // TESTING THIS commands.add(getStartJarBin()); - commands.add(bogusXml.getAbsolutePath()); + commands.add(bogusXml.toAbsolutePath().toString()); // Run command, collect output String output = collectRunOutput(commands); @@ -112,7 +121,7 @@ public void testAsJvmArg() throws IOException, InterruptedException @Test public void testAsCommandLineArg() throws IOException, InterruptedException { - File bogusXml = MavenTestingUtils.getTestResourceFile("bogus.xml"); + Path bogusXml = MavenPaths.findTestResourceFile("bogus.xml"); // Setup command line List commands = new ArrayList<>(); @@ -123,7 +132,7 @@ public void testAsCommandLineArg() throws IOException, InterruptedException // addDebug(commands); commands.add(getStartJarBin()); commands.add("test.foo=bar"); // TESTING THIS - commands.add(bogusXml.getAbsolutePath()); + commands.add(bogusXml.toAbsolutePath().toString()); // Run command, collect output String output = collectRunOutput(commands); @@ -135,7 +144,7 @@ public void testAsCommandLineArg() throws IOException, InterruptedException @Test public void testAsDashDCommandLineArg() throws IOException, InterruptedException { - File bogusXml = MavenTestingUtils.getTestResourceFile("bogus.xml"); + Path bogusXml = MavenPaths.findTestResourceFile("bogus.xml"); // Setup command line List commands = new ArrayList<>(); @@ -146,7 +155,7 @@ public void testAsDashDCommandLineArg() throws IOException, InterruptedException // addDebug(commands); commands.add(getStartJarBin()); commands.add("-Dtest.foo=bar"); // TESTING THIS - commands.add(bogusXml.getAbsolutePath()); + commands.add(bogusXml.toAbsolutePath().toString()); // Run command, collect output String output = collectRunOutput(commands); @@ -155,16 +164,143 @@ public void testAsDashDCommandLineArg() throws IOException, InterruptedException assertThat(output, containsString("test.foo=bar")); } + @Test + public void testExpandPropertyArg() throws IOException, InterruptedException + { + Path bogusXml = MavenPaths.findTestResourceFile("bogus.xml"); + + // Setup command line + List commands = new ArrayList<>(); + commands.add(getJavaBin()); + commands.add("-Dmain.class=" + PropertyDump.class.getName()); + commands.add("-Dtest.dir=/opt/dists/jetty"); + commands.add("-cp"); + commands.add(getClassPath()); + // addDebug(commands); + commands.add(getStartJarBin()); + commands.add("test.config=${test.dir}/etc/config.ini"); // TESTING THIS + commands.add(bogusXml.toAbsolutePath().toString()); + + // Run command, collect output + String output = collectRunOutput(commands); + + // Test for values + assertThat(output, containsString("test.config=/opt/dists/jetty/etc/config.ini")); + } + + @Test + public void testExpandPropertyDArg() throws IOException, InterruptedException + { + Path bogusXml = MavenPaths.findTestResourceFile("bogus.xml"); + + // Setup command line + List commands = new ArrayList<>(); + commands.add(getJavaBin()); + commands.add("-Dmain.class=" + PropertyDump.class.getName()); + commands.add("-Dtest.dir=/opt/dists/jetty"); + commands.add("-cp"); + commands.add(getClassPath()); + // addDebug(commands); + commands.add(getStartJarBin()); + commands.add("-Dtest.config=${test.dir}/etc/config.ini"); // TESTING THIS + commands.add(bogusXml.toAbsolutePath().toString()); + + // Run command, collect output + String output = collectRunOutput(commands); + + // Test for values + assertThat(output, containsString("test.config=/opt/dists/jetty/etc/config.ini")); + } + + @Test + public void testExpandPropertyStartIni() throws IOException, InterruptedException + { + Path bogusXml = MavenPaths.findTestResourceFile("bogus.xml"); + Path base = workDir.getEmptyPathDir(); + Path ini = base.resolve("start.d/config.ini"); + FS.ensureDirectoryExists(ini.getParent()); + String iniBody = """ + # Enabling a single module (that does nothing) to let start.jar run + --module=empty + # TESTING THIS (it should expand the ${jetty.base} portion + test.config=${jetty.base}/etc/config.ini + """; + Files.writeString(ini, iniBody, StandardCharsets.UTF_8); + + // Setup command line + List commands = new ArrayList<>(); + commands.add(getJavaBin()); + commands.add("-Dmain.class=" + PropertyDump.class.getName()); + commands.add("-Djetty.base=" + base); + commands.add("-cp"); + commands.add(getClassPath()); + // addDebug(commands); + commands.add(getStartJarBin()); + commands.add(bogusXml.toAbsolutePath().toString()); + + // Run command, collect output + String output = collectRunOutput(commands); + + // Test for values + Path expectedPath = base.resolve("etc/config.ini"); + assertThat(output, containsString("test.config=" + expectedPath)); + } + + @Test + public void testExpandEnvProperty() throws IOException, InterruptedException + { + Path bogusXml = MavenPaths.findTestResourceFile("bogus.xml"); + Path base = workDir.getEmptyPathDir(); + Path module = base.resolve("modules/env-config.mod"); + FS.ensureDirectoryExists(module.getParent()); + String moduleBody = """ + [environment] + eex + + [ini-template] + # configuration option + # test.config=${jetty.home}/etc/eex-config.ini + """; + Files.writeString(module, moduleBody, StandardCharsets.UTF_8); + Path ini = base.resolve("start.d/config.ini"); + FS.ensureDirectoryExists(ini.getParent()); + String iniBody = """ + # Enabling a single module (that does nothing) to let start.jar run + --module=env-config + # TESTING THIS (it should expand the ${jetty.base} portion + test.config=${jetty.base}/etc/config.ini + """; + Files.writeString(ini, iniBody, StandardCharsets.UTF_8); + + // Setup command line + List commands = new ArrayList<>(); + commands.add(getJavaBin()); + commands.add("-Dmain.class=" + PropertyDump.class.getName()); + commands.add("-Djetty.base=" + base); + commands.add("-cp"); + commands.add(getClassPath()); + // addDebug(commands); + commands.add(getStartJarBin()); + commands.add(bogusXml.toAbsolutePath().toString()); + + // Run command, collect output + String output = collectRunOutput(commands); + + // Test for values + Path expectedPath = base.resolve("etc/config.ini"); + assertThat(output, containsString("test.config=" + expectedPath)); + } + private String getClassPath() { - StringBuilder cp = new StringBuilder(); - String pathSep = System.getProperty("path.separator"); - cp.append(MavenTestingUtils.getProjectDir("target/classes")); - cp.append(pathSep); - cp.append(MavenTestingUtils.getProjectDir("target/test-classes")); - cp.append(pathSep); - cp.append(MavenTestingUtils.getProjectDir("target/jetty-util")); - return cp.toString(); + return String.join( + File.pathSeparator, + List.of( + MavenPaths.projectBase().resolve("target/classes").toString(), + MavenPaths.projectBase().resolve("target/test-classes").toString(), + MavenPaths.projectBase().resolve("target/jetty-util").toString() + ) + ); } protected void addDebug(List commands) @@ -180,11 +316,10 @@ private String collectRunOutput(List commands) throws IOException, Inter { cline.append(command).append(" "); } - System.out.println("Command line: " + cline); ProcessBuilder builder = new ProcessBuilder(commands); // Set PWD - builder.directory(MavenTestingUtils.getTestResourceDir("empty.home")); + builder.directory(MavenPaths.findTestResourceDir("empty.home").toFile()); Process pid = builder.start(); ConsoleCapture stdOutPump = new ConsoleCapture("STDOUT", pid.getInputStream()).start(); @@ -193,6 +328,7 @@ private String collectRunOutput(List commands) throws IOException, Inter int exitCode = pid.waitFor(); if (exitCode != 0) { + System.out.println("Command line: " + cline); System.out.printf("STDERR: [" + stdErrPump.getConsoleOutput() + "]%n"); System.out.printf("STDOUT: [" + stdOutPump.getConsoleOutput() + "]%n"); assertThat("Exit code", exitCode, is(0));