Regex version matching fix revisited

This commit is contained in:
aboyko
2022-08-09 10:32:53 -04:00
parent 3a0b66b50d
commit 2ae5240d8e
2 changed files with 65 additions and 26 deletions

View File

@@ -116,32 +116,9 @@ public class SpringProjectUtil {
public static Version getDependencyVersion(IJavaProject jp, String dependency) {
try {
for (File f : IClasspathUtil.getBinaryRoots(jp.getClasspath(), (cpe) -> !cpe.isSystem())) {
String fileName = f.getName();
if (fileName.startsWith(dependency)) {
StringBuilder sb = new StringBuilder();
sb.append('^');
sb.append(dependency);
sb.append('-');
sb.append(VERSION_PATTERN_STR);
sb.append(".jar$");
Pattern pattern = Pattern.compile(sb.toString());
Matcher matcher = pattern.matcher(fileName);
if (matcher.find() && matcher.groupCount() == 5) {
String major = matcher.group(1);
String minor = matcher.group(2);
String patch = matcher.group(3);
String qualifier = null;
// if (matcher.group(4) != null && matcher.group(4).length() > 1) {
qualifier = matcher.group(4);
// }
return new Version(
Integer.parseInt(major),
Integer.parseInt(minor),
Integer.parseInt(patch),
qualifier
);
}
Version version = getDependencyVersion(f.getName(), dependency);
if (version != null) {
return version;
}
}
} catch (Exception e) {
@@ -150,6 +127,33 @@ public class SpringProjectUtil {
return null;
}
public static Version getDependencyVersion(String fileName, String dependency) {
if (fileName.startsWith(dependency)) {
StringBuilder sb = new StringBuilder();
sb.append('^');
sb.append(dependency);
sb.append('-');
sb.append(VERSION_PATTERN_STR);
sb.append("\\.jar$");
Pattern pattern = Pattern.compile(sb.toString());
Matcher matcher = pattern.matcher(fileName);
if (matcher.find() && matcher.groupCount() >= 5) {
String major = matcher.group(1);
String minor = matcher.group(2);
String patch = matcher.group(3);
String qualifier = matcher.group(5);
return new Version(
Integer.parseInt(major),
Integer.parseInt(minor),
Integer.parseInt(patch),
qualifier
);
}
}
return null;
}
public static Predicate<IJavaProject> springBootVersionGreaterOrEqual(int major, int minor, int patch) {
return project -> {
Version version = getDependencyVersion(project, SPRING_BOOT);

View File

@@ -38,6 +38,7 @@ import org.springframework.ide.vscode.boot.validation.generations.json.Link;
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProject;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.SpringProjectUtil;
import org.springframework.ide.vscode.commons.java.Version;
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
import org.springframework.ide.vscode.project.harness.ProjectsHarness;
import org.springframework.test.context.junit4.SpringRunner;
@@ -92,6 +93,9 @@ public class ProjectGenerationsValidationTest {
version = SpringProjectUtil.getVersion("spring-batch-core-2.4.0-M4");
assertEquals("2.4.0-M4", version);
version = SpringProjectUtil.getVersion("spring-batch-core-2.4.0");
assertEquals("2.4.0", version);
version = SpringProjectUtil.getVersion("spring-boot-4.4.0-RC2");
assertEquals("4.4.0-RC2", version);
@@ -106,6 +110,7 @@ public class ProjectGenerationsValidationTest {
version = SpringProjectUtil.getVersion("springcore.f.b");
assertNull(version);
}
@Test
@@ -205,6 +210,36 @@ public class ProjectGenerationsValidationTest {
// Check that the message mentions the boot version of the project and the OSS support end date
assertEquals("Using spring-boot version: 1.3.2.RELEASE - OSS has ended on: 2020-01-01 - Commercial support has ended on: 2021-01-01", versionValidation.getMessage());
}
@Test
public void testDependencyVersionCalculation() throws Exception {
Version version = SpringProjectUtil.getDependencyVersion("spring-boot-1.2.3.jar", "spring-boot");
assertEquals(1, version.getMajor(), 1);
assertEquals(2, version.getMinor(), 2);
assertEquals(3, version.getPatch());
assertNull(version.getQualifier());
version = SpringProjectUtil.getDependencyVersion("spring-boot-1.2.3-RELEASE.jar", "spring-boot");
assertEquals(version.getMajor(), 1);
assertEquals(version.getMinor(), 2);
assertEquals(version.getPatch(), 3);
assertEquals(version.getQualifier(), "RELEASE");
version = SpringProjectUtil.getDependencyVersion("spring-boot-1.2.3.RELEASE.jar", "spring-boot");
assertEquals(1, version.getMajor(), 1);
assertEquals(2, version.getMinor(), 2);
assertEquals(3, version.getPatch());
assertEquals("RELEASE", version.getQualifier());
version = SpringProjectUtil.getDependencyVersion("spring-boot-1.2.3.BUILD-SNAPSHOT.jar", "spring-boot");
assertEquals(1, version.getMajor(), 1);
assertEquals(2, version.getMinor(), 2);
assertEquals(3, version.getPatch());
assertEquals("BUILD-SNAPSHOT", version.getQualifier());
version = SpringProjectUtil.getDependencyVersion("spring-boot-actuator-1.2.3.BUILD-SNAPSHOT.jar", "spring-boot");
assertNull(version);
}
/*
*