Skip to content

Commit

Permalink
Issue #11408 - Environment property values are not expanded (#12169)
Browse files Browse the repository at this point in the history
If a property belonged to an Environment, then that property value was not expanded.
  • Loading branch information
joakime authored Aug 15, 2024
1 parent e7f1512 commit 457a672
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +23,6 @@
import java.util.List;

import org.eclipse.jetty.toolchain.test.MavenPaths;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -67,7 +65,7 @@ public void testStopProcessing() throws Exception
public void testListConfig() throws Exception
{
List<String> 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);
Expand Down Expand Up @@ -99,8 +97,8 @@ public void testListConfig() throws Exception
public void testUnknownDistroCommand() throws Exception
{
List<String> cmdLineArgs = new ArrayList<>();
File testJettyHome = MavenTestingUtils.getTestResourceDir("dist-home");
Path testJettyBase = MavenTestingUtils.getTargetTestingPath("base-example-unknown");
Path testJettyHome = MavenPaths.findTestResourceDir("dist-home");
Path testJettyBase = MavenPaths.targetTestDir("base-example-unknown");
FS.ensureDirectoryExists(testJettyBase);
Path zedIni = testJettyBase.resolve("start.d/zed.ini");
FS.ensureDirectoryExists(zedIni.getParent());
Expand Down Expand Up @@ -159,7 +157,7 @@ public void testJvmArgExpansion() throws Exception
{
List<String> cmdLineArgs = new ArrayList<>();

Path homePath = MavenTestingUtils.getTestResourcePathDir("dist-home").toRealPath();
Path homePath = MavenPaths.findTestResourceDir("dist-home");
cmdLineArgs.add("jetty.home=" + homePath);
cmdLineArgs.add("user.dir=" + homePath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,49 @@
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<String> 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();
File propFile = new File(arg);
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String> commands = new ArrayList<>();
Expand All @@ -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);
Expand All @@ -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<String> commands = new ArrayList<>();
Expand All @@ -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);
Expand All @@ -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<String> commands = new ArrayList<>();
Expand All @@ -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);
Expand All @@ -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<String> 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<String> 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<String> 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<String> 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<String> commands)
Expand All @@ -180,11 +316,10 @@ private String collectRunOutput(List<String> 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();
Expand All @@ -193,6 +328,7 @@ private String collectRunOutput(List<String> 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));
Expand Down

0 comments on commit 457a672

Please sign in to comment.