Skip to content

Commit 8670151

Browse files
committed
Fix the way integration tests find version numbers
Update the integration tests so that the version number is found using the main POM.xml files. Without this change `${revision}` would be used. See gh-9316
1 parent 8056224 commit 8670151

File tree

1 file changed

+15
-10
lines changed
  • spring-boot-tests/spring-boot-integration-tests/spring-boot-integration-tests-embedded-servlet-container/src/test/java/org/springframework/boot/context/embedded

1 file changed

+15
-10
lines changed

spring-boot-tests/spring-boot-integration-tests/spring-boot-integration-tests-embedded-servlet-container/src/test/java/org/springframework/boot/context/embedded/Versions.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,42 @@
1616

1717
package org.springframework.boot.context.embedded;
1818

19+
import java.io.File;
1920
import java.io.FileReader;
2021

2122
import javax.xml.xpath.XPath;
22-
import javax.xml.xpath.XPathExpression;
2323
import javax.xml.xpath.XPathFactory;
2424

2525
import org.xml.sax.InputSource;
2626

27+
import org.springframework.util.StringUtils;
28+
2729
/**
2830
* Provides access to dependency versions by querying the project's pom.
2931
*
3032
* @author Andy Wilkinson
3133
*/
3234
final class Versions {
3335

36+
private static final String PROPERTIES = "/*[local-name()='project']/*[local-name()='properties']";
37+
3438
private Versions() {
3539
}
3640

3741
public static String getBootVersion() {
38-
return evaluateExpression(
39-
"/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']"
40-
+ "/text()");
42+
String baseDir = StringUtils.cleanPath(new File(".").getAbsolutePath());
43+
String mainBaseDir = evaluateExpression("pom.xml",
44+
PROPERTIES + "/*[local-name()='main.basedir']/text()");
45+
mainBaseDir = mainBaseDir.replace("${basedir}", baseDir);
46+
return evaluateExpression(mainBaseDir + "/pom.xml",
47+
PROPERTIES + "/*[local-name()='revision']/text()");
4148
}
4249

43-
private static String evaluateExpression(String expression) {
50+
private static String evaluateExpression(String file, String expression) {
4451
try {
45-
XPathFactory xPathFactory = XPathFactory.newInstance();
46-
XPath xpath = xPathFactory.newXPath();
47-
XPathExpression expr = xpath.compile(expression);
48-
String version = expr.evaluate(new InputSource(new FileReader("pom.xml")));
49-
return version;
52+
InputSource source = new InputSource(new FileReader(file));
53+
XPath xpath = XPathFactory.newInstance().newXPath();
54+
return xpath.compile(expression).evaluate(source);
5055
}
5156
catch (Exception ex) {
5257
throw new IllegalStateException("Failed to evaluate expression", ex);

0 commit comments

Comments
 (0)