[maven-repo-helper] 02/02: Rank substitution rules before star rules
Emmanuel Bourg
ebourg-guest at moszumanska.debian.org
Mon Sep 8 15:49:31 UTC 2014
This is an automated email from the git hooks/post-receive script.
ebourg-guest pushed a commit to branch master
in repository maven-repo-helper.
commit 44be5f49e8ce808e95731d66d5076ed5af9474dc
Author: Emmanuel Bourg <ebourg at apache.org>
Date: Mon Sep 8 17:49:11 2014 +0200
Rank substitution rules before star rules
---
.../java/org/debian/maven/repo/DependencyRule.java | 64 ++++++++++------------
src/main/java/org/debian/maven/repo/Rule.java | 6 +-
.../org/debian/maven/repo/DependencyRuleTest.java | 11 ++++
src/test/java/org/debian/maven/repo/RuleTest.java | 10 +++-
4 files changed, 50 insertions(+), 41 deletions(-)
diff --git a/src/main/java/org/debian/maven/repo/DependencyRule.java b/src/main/java/org/debian/maven/repo/DependencyRule.java
index d6904e9..ca73309 100644
--- a/src/main/java/org/debian/maven/repo/DependencyRule.java
+++ b/src/main/java/org/debian/maven/repo/DependencyRule.java
@@ -102,6 +102,10 @@ public class DependencyRule implements Comparable<DependencyRule> {
return classifierRule;
}
+ public Rule[] getRules() {
+ return new Rule[] { groupRule, artifactRule, typeRule, versionRule, classifierRule, scopeRule };
+ }
+
public boolean matches(Dependency dependency) {
return groupRule.match(dependency.getGroupId())
&& artifactRule.match(dependency.getArtifactId())
@@ -146,42 +150,30 @@ public class DependencyRule implements Comparable<DependencyRule> {
* sorting of the pattern strings.
*/
public int compareTo(DependencyRule other) {
- if (groupRule.isGeneric() && !other.groupRule.isGeneric()) {
- return 1;
- }
- if (!groupRule.isGeneric() && other.groupRule.isGeneric()) {
- return -1;
- }
- if (artifactRule.isGeneric() && !other.artifactRule.isGeneric()) {
- return 1;
- }
- if (!artifactRule.isGeneric() && other.artifactRule.isGeneric()) {
- return -1;
- }
- if (typeRule.isGeneric() && !other.typeRule.isGeneric()) {
- return 1;
- }
- if (!typeRule.isGeneric() && other.typeRule.isGeneric()) {
- return -1;
- }
- if (versionRule.isGeneric() && !other.versionRule.isGeneric()) {
- return 1;
- }
- if (!versionRule.isGeneric() && other.versionRule.isGeneric()) {
- return -1;
- }
- if (classifierRule.isGeneric() && !other.classifierRule.isGeneric()) {
- return 1;
- }
- if (!classifierRule.isGeneric() && other.classifierRule.isGeneric()) {
- return -1;
- }
- if (scopeRule.isGeneric() && !other.scopeRule.isGeneric()) {
- return 1;
- }
- if (!scopeRule.isGeneric() && other.scopeRule.isGeneric()) {
- return -1;
- }
+ Rule[] rules = getRules();
+ Rule[] otherRules = other.getRules();
+
+ for (int i = 0; i < rules.length; i++) {
+ if (rules[i].isGeneric() && !otherRules[i].isGeneric()) {
+ return 1;
+ }
+ if (!rules[i].isGeneric() &&otherRules[i].isGeneric()) {
+ return -1;
+ }
+ }
+
+ // if all rules are generic, rank substitution rules before star rules
+ if (groupRule.isGeneric()) {
+ for (int i = 0; i < rules.length; i++) {
+ if (rules[i].isSubstitution() && !otherRules[i].isSubstitution()) {
+ return -1;
+ }
+ if (!rules[i].isSubstitution() && otherRules[i].isSubstitution()) {
+ return 1;
+ }
+ }
+ }
+
return this.toPatternString().compareTo(other.toPatternString());
}
diff --git a/src/main/java/org/debian/maven/repo/Rule.java b/src/main/java/org/debian/maven/repo/Rule.java
index f78ab19..861d540 100644
--- a/src/main/java/org/debian/maven/repo/Rule.java
+++ b/src/main/java/org/debian/maven/repo/Rule.java
@@ -16,7 +16,6 @@
package org.debian.maven.repo;
-import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -27,7 +26,6 @@ public class Rule {
/** Regexp looking for characters found in regular expressions like '[', '?', '*', '+', '|' */
private static final Pattern GENERIC_PATTERN = Pattern.compile("([\\[\\?\\+\\*\\|])|([^\\\\]\\.[\\*\\+])"); // ([\[\?\+\*\|])|([^\\]\.[\*\+])
- private static Pattern generic = Pattern.compile("([\\[\\?\\+\\*\\|])|([^\\\\]\\.)");
/** Regexp matching a substitution expression like s/foo/bar/ */
private static final Pattern SUBSTITUTION_PATTERN = Pattern.compile("s/([^/]*)/([^/]*)/?");
@@ -86,6 +84,10 @@ public class Rule {
return matchesNull() || GENERIC_PATTERN.matcher(pattern.pattern()).find();
}
+ public boolean isSubstitution() {
+ return rule != null && rule.startsWith("s/");
+ }
+
public boolean matchesNull() {
String patternString = pattern.pattern();
return ".*".equals(patternString) || "(.*)".equals(patternString);
diff --git a/src/test/java/org/debian/maven/repo/DependencyRuleTest.java b/src/test/java/org/debian/maven/repo/DependencyRuleTest.java
index fb727e0..d98058c 100644
--- a/src/test/java/org/debian/maven/repo/DependencyRuleTest.java
+++ b/src/test/java/org/debian/maven/repo/DependencyRuleTest.java
@@ -75,6 +75,17 @@ public class DependencyRuleTest {
assertTrue(servlet23.compareTo(generic) < 0);
assertTrue(log4j12.compareTo(servlet23) < 0);
assertEquals(0, servlet23.compareTo(servlet23));
+
+ DependencyRule rule1 = new DependencyRule("* * * s/.*/debian/ * *");
+ DependencyRule rule2 = new DependencyRule("s/org.eclipse.jetty.orbit/javax.servlet/ s/javax.servlet/servlet-api/ * s/.*/3.0/ * *");
+ assertTrue(rule2.compareTo(rule1) < 0);
+ }
+
+ @Test
+ public void testCompareGenericRules() {
+ DependencyRule rule1 = new DependencyRule("* * * s/.*/debian/ * *");
+ DependencyRule rule2 = new DependencyRule("s/foo-(.*)/org.foo.$1/ * * s/.*/debian/ * *");
+ assertTrue(rule2.compareTo(rule1) < 0);
}
@Test
diff --git a/src/test/java/org/debian/maven/repo/RuleTest.java b/src/test/java/org/debian/maven/repo/RuleTest.java
index 9637a83..a0b64fe 100644
--- a/src/test/java/org/debian/maven/repo/RuleTest.java
+++ b/src/test/java/org/debian/maven/repo/RuleTest.java
@@ -58,7 +58,6 @@ public class RuleTest {
assertFalse(generic2.match("xxx"));
assertTrue(generic2.match("test"));
assertTrue(generic2.match("test2"));
-
}
/**
@@ -95,7 +94,6 @@ public class RuleTest {
Rule generic2 = new Rule("test*");
assertEquals("test", generic2.apply("test"));
assertEquals("test2", generic2.apply("test2"));
-
}
/**
@@ -126,7 +124,13 @@ public class RuleTest {
Rule generic2 = new Rule("test*");
assertTrue(generic2.isGeneric());
-
}
+ @Test
+ public void testIsSubstitution() {
+ assertFalse(new Rule("*").isSubstitution());
+ assertFalse(new Rule("jar").isSubstitution());
+ assertTrue(new Rule("s/foo/bar/").isSubstitution());
+ assertTrue(new Rule("s/.*/debian/").isSubstitution());
+ }
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/maven-repo-helper.git
More information about the pkg-java-commits
mailing list