From 0e39f4e524d6451288a789e39b1a7016fa1451c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 10 Jan 2024 12:49:41 +0100 Subject: [PATCH] Add testcase for version ranges in version-plugin --- .../consumer.narrow/META-INF/MANIFEST.MF | 8 ++ .../version_ranges/consumer.narrow/pom.xml | 14 ++++ .../consumer.open/META-INF/MANIFEST.MF | 8 ++ .../version_ranges/consumer.open/pom.xml | 14 ++++ .../consumer.wide/META-INF/MANIFEST.MF | 8 ++ .../version_ranges/consumer.wide/pom.xml | 14 ++++ .../set-version/version_ranges/pom.xml | 30 +++++++ .../provider.bundle/META-INF/MANIFEST.MF | 7 ++ .../version_ranges/provider.bundle/pom.xml | 14 ++++ .../TychoVersionsPluginTest.java | 80 ++++++++++++++++++- .../DefaultVersionRangeUpdateStrategy.java | 4 +- .../tycho/versions/engine/VersionsEngine.java | 2 +- 12 files changed, 198 insertions(+), 5 deletions(-) create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.narrow/META-INF/MANIFEST.MF create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.narrow/pom.xml create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.open/META-INF/MANIFEST.MF create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.open/pom.xml create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.wide/META-INF/MANIFEST.MF create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.wide/pom.xml create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/version_ranges/pom.xml create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/version_ranges/provider.bundle/META-INF/MANIFEST.MF create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/version_ranges/provider.bundle/pom.xml diff --git a/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.narrow/META-INF/MANIFEST.MF b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.narrow/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..e6f03b5ea7 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.narrow/META-INF/MANIFEST.MF @@ -0,0 +1,8 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Test Bundle +Bundle-SymbolicName: consumer.narrow +Bundle-Version: 1.0.0.qualifier +Import-Package: my.pkg;version="[1.0.0,1.1.0)" +Require-Bundle: provider.bundle;bundle-version="[1.0.0,1.1.0)" + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.narrow/pom.xml b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.narrow/pom.xml new file mode 100644 index 0000000000..04a3011c43 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.narrow/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + + + org.tycho.its + parent + 1.0.0-SNAPSHOT + + consumer.narrow + eclipse-plugin + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.open/META-INF/MANIFEST.MF b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.open/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..0eb3ef6540 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.open/META-INF/MANIFEST.MF @@ -0,0 +1,8 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Test Bundle +Bundle-SymbolicName: consumer.open +Bundle-Version: 1.0.0.qualifier +Import-Package: my.pkg;version="1.0.0" +Require-Bundle: provider.bundle;bundle-version="1.0.0" + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.open/pom.xml b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.open/pom.xml new file mode 100644 index 0000000000..7946f45536 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.open/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + + + org.tycho.its + parent + 1.0.0-SNAPSHOT + + consumer.open + eclipse-plugin + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.wide/META-INF/MANIFEST.MF b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.wide/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..aca62a2769 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.wide/META-INF/MANIFEST.MF @@ -0,0 +1,8 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Test Bundle +Bundle-SymbolicName: consumer.wide +Bundle-Version: 1.0.0.qualifier +Import-Package: my.pkg;version="[1.0.0,2.0.0)" +Require-Bundle: provider.bundle;bundle-version="[1.0.0,2.0.0)" + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.wide/pom.xml b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.wide/pom.xml new file mode 100644 index 0000000000..1b29ad9d6f --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/consumer.wide/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + + + org.tycho.its + parent + 1.0.0-SNAPSHOT + + consumer.wide + eclipse-plugin + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/pom.xml b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/pom.xml new file mode 100644 index 0000000000..51bc05f3bb --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + org.tycho.its + parent + 1.0.0-SNAPSHOT + pom + + provider.bundle + consumer.narrow + consumer.wide + consumer.open + + + 5.0.0-SNAPSHOT + + + + + org.eclipse.tycho + tycho-maven-plugin + ${tycho-version} + true + + + + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/provider.bundle/META-INF/MANIFEST.MF b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/provider.bundle/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..f19a740983 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/provider.bundle/META-INF/MANIFEST.MF @@ -0,0 +1,7 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Test Bundle +Bundle-SymbolicName: provider.bundle +Bundle-Version: 1.0.0.qualifier +Export-Package: my.pkg;version="1.0.0" + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/provider.bundle/pom.xml b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/provider.bundle/pom.xml new file mode 100644 index 0000000000..a1869a8c1b --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/version_ranges/provider.bundle/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + + + org.tycho.its + parent + 1.0.0-SNAPSHOT + + provider.bundle + eclipse-plugin + diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/versionsplugin/TychoVersionsPluginTest.java b/tycho-its/src/test/java/org/eclipse/tycho/test/versionsplugin/TychoVersionsPluginTest.java index 866427536c..d00d07e030 100644 --- a/tycho-its/src/test/java/org/eclipse/tycho/test/versionsplugin/TychoVersionsPluginTest.java +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/versionsplugin/TychoVersionsPluginTest.java @@ -19,13 +19,17 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileReader; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Properties; import java.util.jar.JarFile; import java.util.jar.Manifest; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.maven.it.Verifier; import org.apache.maven.model.Model; @@ -35,11 +39,15 @@ import org.eclipse.tycho.version.TychoVersion; import org.junit.Test; import org.osgi.framework.Constants; +import org.osgi.framework.Version; +import org.osgi.framework.VersionRange; public class TychoVersionsPluginTest extends AbstractTychoIntegrationTest { private static final String VERSION = TychoVersion.getTychoVersion(); + private static final Pattern VERSION_PATTERN = Pattern.compile("version=\"(.*)\""); + /** *

* This test verifies that current and future versions of the @@ -132,12 +140,58 @@ public void updateProjectVersionWithNestedPom() throws Exception { pomModel.getVersion()); } } - Manifest manifest = new Manifest( - new FileInputStream(new File(verifier.getBasedir(), "bundle/" + JarFile.MANIFEST_NAME))); + Manifest manifest = getManifest(verifier, "bundle"); assertEquals("version in manifest was not updated!", expectedNewVersion, manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION)); } + @Test + public void updateVersionRanges() throws Exception { + String expectedNewMavenVersion = "1.1.0-SNAPSHOT"; + String expectedNewOSGiVersion = "1.1.0.qualifier"; + String expectedPackageVersion = "1.1.0"; + String expectedNarrowVersionRange = "[" + expectedPackageVersion + ",1.2.0)"; + String expectedWideVersionRange = "[" + expectedPackageVersion + ",2)"; + + Verifier verifier = getVerifier("tycho-version-plugin/set-version/version_ranges", true); + verifier.addCliOption("-DnewVersion=" + expectedNewMavenVersion); + verifier.executeGoal("org.eclipse.tycho:tycho-versions-plugin:" + VERSION + ":set-version"); + {// check the package itself is updated + Manifest provider = getManifest(verifier, "provider.bundle"); + assertEquals("version in manifest was not updated for provider bundle!", expectedNewOSGiVersion, + provider.getMainAttributes().getValue(Constants.BUNDLE_VERSION)); + assertVersion(provider, expectedPackageVersion, Constants.EXPORT_PACKAGE); + } + {// check open range is updated + Manifest consumerOpen = getManifest(verifier, "consumer.open"); + assertEquals("version in manifest was not updated for open consumer bundle!", expectedNewOSGiVersion, + consumerOpen.getMainAttributes().getValue(Constants.BUNDLE_VERSION)); + assertVersion(consumerOpen, expectedPackageVersion, Constants.IMPORT_PACKAGE); + assertVersion(consumerOpen, expectedPackageVersion, Constants.REQUIRE_BUNDLE); + } + {// check wide version range is updated + Manifest consumerWide = getManifest(verifier, "consumer.wide"); + assertEquals("version in manifest was not updated for wide consumer bundle!", expectedNewOSGiVersion, + consumerWide.getMainAttributes().getValue(Constants.BUNDLE_VERSION)); + assertVersionRange(consumerWide, expectedWideVersionRange, Constants.IMPORT_PACKAGE); + assertVersionRange(consumerWide, expectedWideVersionRange, Constants.REQUIRE_BUNDLE); + } + {// check narrow version range is updated + Manifest consumerNarrow = getManifest(verifier, "consumer.narrow"); + assertEquals("version in manifest was not updated for narrow consumer bundle!", expectedNewOSGiVersion, + consumerNarrow.getMainAttributes().getValue(Constants.BUNDLE_VERSION)); + assertVersionRange(consumerNarrow, expectedNarrowVersionRange, Constants.IMPORT_PACKAGE); + assertVersionRange(consumerNarrow, expectedNarrowVersionRange, Constants.REQUIRE_BUNDLE); + } + } + + private Manifest getManifest(Verifier verifier, String bundle) throws IOException, FileNotFoundException { + try (FileInputStream stream = new FileInputStream( + new File(verifier.getBasedir(), bundle + "/" + JarFile.MANIFEST_NAME))) { + return new Manifest(stream); + } + } + @Test public void updateProjectMetadataVersionBndTest() throws Exception { String expectedNewVersion = "2.0.0.qualifier"; @@ -263,4 +317,26 @@ public static File file(Verifier verifier, String... path) { return Path.of(verifier.getBasedir(), path).toFile(); } + private static void assertVersionRange(Manifest manifest, String versionRange, String header) { + String value = manifest.getMainAttributes().getValue(header); + assertNotNull("Header " + header + " not found", value); + Matcher matcher = VERSION_PATTERN.matcher(value); + assertTrue("no version found on " + value, matcher.find()); + VersionRange expected = VersionRange.valueOf(versionRange); + VersionRange actual = VersionRange.valueOf(matcher.group(1)); + assertTrue(header + " " + value + ": expected version range = " + expected + " but actual version range = " + + actual, expected.equals(actual)); + } + + private static void assertVersion(Manifest manifest, String version, String header) { + String value = manifest.getMainAttributes().getValue(header); + assertNotNull("Header " + header + " not found", value); + Matcher matcher = VERSION_PATTERN.matcher(value); + assertTrue("no version found on " + value, matcher.find()); + Version expected = Version.valueOf(version); + Version actual = Version.valueOf(matcher.group(1)); + assertTrue(header + " " + value + ": expected version = " + expected + " but actual version = " + actual, + expected.equals(actual)); + } + } diff --git a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/DefaultVersionRangeUpdateStrategy.java b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/DefaultVersionRangeUpdateStrategy.java index 23b619f909..78df8d5f92 100644 --- a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/DefaultVersionRangeUpdateStrategy.java +++ b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/DefaultVersionRangeUpdateStrategy.java @@ -49,7 +49,7 @@ private VersionRange computeNewVersionRange(VersionRange versionRange, Version o Version newReferencedVersion, Version newArtifactVersion) { VersionRange newVersionRange; if (updateMatchingBounds) { - newVersionRange = handleMatchingBouds(versionRange, originalReferencedVersion, newReferencedVersion); + newVersionRange = handleMatchingBounds(versionRange, originalReferencedVersion, newReferencedVersion); } else { newVersionRange = versionRange; } @@ -57,7 +57,7 @@ private VersionRange computeNewVersionRange(VersionRange versionRange, Version o newArtifactVersion); } - private VersionRange handleMatchingBouds(VersionRange versionRange, Version originalReferencedVersion, + private VersionRange handleMatchingBounds(VersionRange versionRange, Version originalReferencedVersion, Version newReferencedVersion) { Version newLeft; if (versionRange.getLeft().equals(originalReferencedVersion)) { diff --git a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionsEngine.java b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionsEngine.java index ce8258f40e..5da2490aeb 100644 --- a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionsEngine.java +++ b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionsEngine.java @@ -112,7 +112,7 @@ public void reset() { public void apply() throws IOException { VersionChangesDescriptor versionChangeContext = new VersionChangesDescriptor(originalVersionChanges, - new DefaultVersionRangeUpdateStrategy(updateVersionRangeMatchingBounds), projects); + new DefaultVersionRangeUpdateStrategy(isUpdateVersionRangeMatchingBounds()), projects); // collecting secondary changes boolean newChanges = true;