|
| 1 | +/* |
| 2 | + * Copyright 2026 C Thing Software |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package org.cthing.versionparser.examples; |
| 6 | + |
| 7 | +import org.cthing.versionparser.Version; |
| 8 | +import org.cthing.versionparser.VersionConstraint; |
| 9 | +import org.cthing.versionparser.VersionParsingException; |
| 10 | +import org.cthing.versionparser.debian.DebVersion; |
| 11 | +import org.cthing.versionparser.debian.DebVersionScheme; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * Parse Debian package versions and version constraints. |
| 18 | + */ |
| 19 | +public final class DebianExample { |
| 20 | + |
| 21 | + private DebianExample() { |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Performs operations on Debian package versions and version constraints. |
| 26 | + * |
| 27 | + * @param args Not used |
| 28 | + * @throws VersionParsingException if there was a problem parsing. |
| 29 | + */ |
| 30 | + public static void main(final String[] args) throws VersionParsingException { |
| 31 | + // Parse versions |
| 32 | + final DebVersion version1 = DebVersionScheme.parseVersion("22.07.5-2ubuntu1.5"); |
| 33 | + final Version version2 = DebVersionScheme.parseVersion("20.01.2-1ubuntu1.5"); |
| 34 | + |
| 35 | + // Obtain information from the parsed version |
| 36 | + assertThat(version1.getOriginalVersion()).isEqualTo("22.07.5-2ubuntu1.5"); |
| 37 | + assertThat(version1.isPreRelease()).isFalse(); |
| 38 | + assertThat(version1.getEpoch()).isEqualTo(0); |
| 39 | + assertThat(version1.getUpstream()).isEqualTo("22.07.5"); |
| 40 | + assertThat(version1.getRevision()).isEqualTo("2ubuntu1.5"); |
| 41 | + |
| 42 | + // Verify ordering |
| 43 | + assertThat(version1.compareTo(version2)).isEqualTo(1); |
| 44 | + |
| 45 | + // Parse version constraints |
| 46 | + final VersionConstraint constraint1 = DebVersionScheme.parseConstraint(">20"); |
| 47 | + final VersionConstraint constraint2 = DebVersionScheme.parseConstraint(">21 <=23"); |
| 48 | + |
| 49 | + // Perform constraint checking |
| 50 | + assertThat(constraint1.allows(version2)).isTrue(); |
| 51 | + assertThat(constraint2.allows(version1)).isTrue(); |
| 52 | + assertThat(constraint2.allows(version2)).isFalse(); |
| 53 | + } |
| 54 | +} |
0 commit comments