Skip to content

Commit

Permalink
Using Messages in exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jul 21, 2024
1 parent d88dbd1 commit f0ac2ec
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
16 changes: 14 additions & 2 deletions version/src/main/java/io/smallrye/common/version/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ interface Messages {
@Message(id = 3010, value = "Build string may not be empty")
VersionSyntaxException emptyBuild();

@Message(id = 3011, value = "Invalid range pattern: %s")
IllegalArgumentException invalidRangePattern(String pattern);
@Message(id = 3011, value = "Unbounded range: %s")
IllegalArgumentException unboundedRange(String pattern);

@Message(id = 3012, value = "Ranges overlap: %s")
IllegalArgumentException rangesOverlap(String version);

@Message(id = 3013, value = "Only fully-qualified sets allowed in multiple set scenario: %s")
IllegalArgumentException onlyFullyQualifiedSetsAllowed(String version);

@Message(id = 3014, value = "Single version must be surrounded by []: %s")
IllegalArgumentException singleVersionMustBeSurroundedByBrackets(String version);

@Message(id = 3015, value = "Range defies version ordering: %s")
IllegalArgumentException rangeDefiesVersionOrdering(String version);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.smallrye.common.version;

import static io.smallrye.common.version.Messages.msg;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
Expand Down Expand Up @@ -65,7 +67,7 @@ public static VersionRange createFromVersionSpec(VersionScheme scheme, String sp
}

if (index < 0) {
throw new IllegalArgumentException("Unbounded range: " + spec);
throw msg.unboundedRange(spec);
}

VersionRestriction restriction = VersionRestriction.parse(scheme, process.substring(0, index + 1));
Expand All @@ -75,7 +77,7 @@ public static VersionRange createFromVersionSpec(VersionScheme scheme, String sp
if (upperBound != null) {
if (restriction.getLowerBound() == null
|| scheme.compare(restriction.getLowerBound(), upperBound) < 0) {
throw new IllegalArgumentException("Ranges overlap: " + spec);
throw msg.rangesOverlap(spec);
}
}
restrictions.add(restriction);
Expand All @@ -90,8 +92,7 @@ public static VersionRange createFromVersionSpec(VersionScheme scheme, String sp

if (!process.isEmpty()) {
if (!restrictions.isEmpty()) {
throw new IllegalArgumentException(
"Only fully-qualified sets allowed in multiple set scenario: " + spec);
throw msg.onlyFullyQualifiedSetsAllowed(spec);
} else {
restrictions.add(VersionRestriction.EVERYTHING);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.smallrye.common.version;

import static io.smallrye.common.version.Messages.msg;

import java.util.function.Predicate;

class VersionRestriction implements Predicate<String> {
Expand Down Expand Up @@ -58,7 +60,7 @@ static VersionRestriction parse(VersionScheme versionScheme, String spec) {

if (index < 0) {
if (!lowerBoundInclusive || !upperBoundInclusive) {
throw new IllegalArgumentException("Single version must be surrounded by []: " + spec);
throw msg.singleVersionMustBeSurroundedByBrackets(spec);
}
restriction = new VersionRestriction(versionScheme, process, true, process, true);
} else {
Expand All @@ -78,7 +80,7 @@ static VersionRestriction parse(VersionScheme versionScheme, String spec) {
if (upperVersion != null && lowerVersion != null) {
int result = versionScheme.compare(upperVersion, lowerVersion);
if (result < 0 || (result == 0 && (!lowerBoundInclusive || !upperBoundInclusive))) {
throw new IllegalArgumentException("Range defies version ordering: " + spec);
throw msg.rangeDefiesVersionOrdering(spec);
}
}
restriction = new VersionRestriction(versionScheme, lowerVersion, lowerBoundInclusive, upperVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class VersionRangeTest {
Expand Down Expand Up @@ -95,4 +96,11 @@ public void testQualifiers() {
assertFalse(versionRange.test("3.8.5.redhat-00003"));
}

@Test
@Disabled("This test is failing")
public void testRangeQualifier() {
VersionRange versionRange = VersionRange.createFromVersionSpec(VersionScheme.MAVEN, "[3.8.0.redhat-00001,)");
assertTrue(versionRange.test("3.8.0.SP1-redhat-00001"));
}

}

0 comments on commit f0ac2ec

Please sign in to comment.