From 1c8826e1f633ac2d33aa4e8a7be151a45492ece8 Mon Sep 17 00:00:00 2001 From: Sebastian Ratz Date: Wed, 27 Nov 2024 10:53:59 +0000 Subject: [PATCH] tycho-versions-plugin: PomUtil.expandProperties(): Quote replacements The replacement strings passed to java.util.regex.Matcher.appendReplacement(StringBuilder, String) are to be treated literally, no regex capture groups are to be evaluated if the replacement string happens to contains '${...}'. So they must be quoted using java.util.regex.Matcher.quoteReplacement(String) (cherry picked from commit e9781e6b34e6983315aab8f52ca28503a946fa80) --- .../eclipse/tycho/versions/pom/PomUtil.java | 4 +- .../tycho/versions/pom/tests/PomUtilTest.java | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tycho-versions-plugin/src/test/java/org/eclipse/tycho/versions/pom/tests/PomUtilTest.java diff --git a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/pom/PomUtil.java b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/pom/PomUtil.java index fda4d927ad..7d99264a14 100644 --- a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/pom/PomUtil.java +++ b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/pom/PomUtil.java @@ -48,8 +48,8 @@ public static String expandProperties(String str, List properties) { String unexpandedProperty = m.group(); String propertyName = m.group(1); m.appendReplacement(resolvedVersionBuilder, - properties.stream().filter(p -> p.getName().equals(propertyName)).map(p -> p.getValue()) - .findFirst().orElse(unexpandedProperty)); + Matcher.quoteReplacement(properties.stream().filter(p -> p.getName().equals(propertyName)) + .map(p -> p.getValue()).findFirst().orElse(unexpandedProperty))); } m.appendTail(resolvedVersionBuilder); return resolvedVersionBuilder.toString(); diff --git a/tycho-versions-plugin/src/test/java/org/eclipse/tycho/versions/pom/tests/PomUtilTest.java b/tycho-versions-plugin/src/test/java/org/eclipse/tycho/versions/pom/tests/PomUtilTest.java new file mode 100644 index 0000000000..412182d47d --- /dev/null +++ b/tycho-versions-plugin/src/test/java/org/eclipse/tycho/versions/pom/tests/PomUtilTest.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2024 SAP SE and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * SAP SE - initial API and implementation + *******************************************************************************/ +package org.eclipse.tycho.versions.pom.tests; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; + +import org.eclipse.tycho.versions.pom.PomFile; +import org.eclipse.tycho.versions.pom.PomUtil; +import org.junit.Test; + +public class PomUtilTest { + + @Test + public void expandProperties() throws Exception { + String pom = """ + + + fooValue + barValue + + + """; + PomFile pomFile = PomFile.read(new ByteArrayInputStream(pom.getBytes(StandardCharsets.UTF_8)), true); + + String expanded = PomUtil.expandProperties("${foo}-${bar}-${notFound}", pomFile.getProperties()); + + assertEquals("fooValue-barValue-${notFound}", expanded); + } +}