Skip to content

Commit

Permalink
Add testcase for version ranges in version-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Jan 18, 2024
1 parent d099c82 commit 0e39f4e
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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)"

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.tycho.its</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>consumer.narrow</artifactId>
<packaging>eclipse-plugin</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -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"

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.tycho.its</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>consumer.open</artifactId>
<packaging>eclipse-plugin</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -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)"

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.tycho.its</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>consumer.wide</artifactId>
<packaging>eclipse-plugin</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.tycho.its</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>provider.bundle</module>
<module>consumer.narrow</module>
<module>consumer.wide</module>
<module>consumer.open</module>
</modules>
<properties>
<tycho-version>5.0.0-SNAPSHOT</tycho-version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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"

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.tycho.its</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>provider.bundle</artifactId>
<packaging>eclipse-plugin</packaging>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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=\"(.*)\"");

/**
* <p>
* This test verifies that current and future versions of the
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ 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;
}
return handleNewlyOutOfScopeVersions(newVersionRange, originalReferencedVersion, newReferencedVersion,
newArtifactVersion);
}

private VersionRange handleMatchingBouds(VersionRange versionRange, Version originalReferencedVersion,
private VersionRange handleMatchingBounds(VersionRange versionRange, Version originalReferencedVersion,
Version newReferencedVersion) {
Version newLeft;
if (versionRange.getLeft().equals(originalReferencedVersion)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0e39f4e

Please sign in to comment.