r2281 - in trunk/nuiton-utils/src: main/java/org/nuiton/util test/java/org/nuiton/util
Author: tchemit Date: 2012-01-16 19:18:43 +0100 (Mon, 16 Jan 2012) New Revision: 2281 Url: http://nuiton.org/repositories/revision/nuiton-utils/2281 Log: Anomalie #1629: VersionUtil fail with 0.5-beta1 Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/Version.java trunk/nuiton-utils/src/main/java/org/nuiton/util/VersionUtil.java trunk/nuiton-utils/src/test/java/org/nuiton/util/VersionUtilTest.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/Version.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/Version.java 2012-01-16 16:32:34 UTC (rev 2280) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/Version.java 2012-01-16 18:18:43 UTC (rev 2281) @@ -25,7 +25,10 @@ package org.nuiton.util; +import org.apache.commons.lang3.ObjectUtils; + import java.io.Serializable; +import java.util.Arrays; import java.util.regex.Matcher; import static org.nuiton.i18n.I18n._; @@ -88,7 +91,23 @@ * @since 2.4.3 */ protected final boolean snapshot; + /** + * A flag to attach or not the classifier with his number. + * <p/> + * If set to false (the default cas then a - will be used to separe them). + * <p/> + * Even if the {@link Version} class is immutable, this state can be change + * since it is only used to build the string representation of a version. + * <p/> + * Notes that this state is NOT used to test equality of two version, + * neither for the comparaison. + * + * @since 2.4.3 + */ + protected boolean classifierNumberAttached; + + /** * representation textuelle de la version (celle utilisee dans le * {@link #toString()}. */ @@ -155,6 +174,13 @@ Matcher matcher = VersionUtil.VERSION_PATTERN.matcher(version); if (!matcher.matches()) { + + // try with classifier number attached to classifier + matcher = VersionUtil.VERSION_PATTERN2.matcher(version); + classifierNumberAttached = true; + } + + if (!matcher.matches()) { // not a known pattern throw new IllegalArgumentException( _("nuitonutil.error.version.pattern", version)); @@ -216,6 +242,10 @@ return numbers.length; } + public boolean isClassifierNumberAttached() { + return classifierNumberAttached; + } + public int getNumber(int level) { if (level < 0 || level >= numbers.length) { throw new IllegalArgumentException( @@ -234,7 +264,9 @@ if (hasClassifier()) { sb.append('-'); sb.append(classifier); - sb.append('-'); + if (!classifierNumberAttached) { + sb.append('-'); + } sb.append(classifierNumber); } if (isSnapshot()) { @@ -261,6 +293,19 @@ return validName; } + /** + * Change the internal state {@link #classifierNumberAttached}. + * + * @param classifierNumberAttached the new value of the classifierNumberAttached state + */ + public void setClassifierNumberAttached(boolean classifierNumberAttached) { + if (version != null) { + // will force + version = null; + } + this.classifierNumberAttached = classifierNumberAttached; + } + @Override public String toString() { String t = getVersion(); @@ -294,7 +339,10 @@ @Override public boolean equals(Object obj) { return obj != null && obj instanceof Version && - getVersion().equals(((Version) obj).getVersion()); + Arrays.equals(numbers, ((Version) obj).numbers) && + ObjectUtils.equals(classifier, ((Version) obj).classifier) && + ObjectUtils.equals(classifierNumber, ((Version) obj).classifierNumber) && + ObjectUtils.equals(snapshot, ((Version) obj).snapshot); } @Override Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/VersionUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/VersionUtil.java 2012-01-16 16:32:34 UTC (rev 2280) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/VersionUtil.java 2012-01-16 18:18:43 UTC (rev 2281) @@ -54,9 +54,10 @@ public class VersionUtil { /** - * Pattern pour detecter une version. + * Pattern pour detecter une version (avec si classifier et numéro de + * classifier séparé par un -, par exemple : 1.0-beta-1). * <p/> - * Le pattern possède toujours 3 groupes de captures. + * Le pattern possède toujours 4 groupes de captures. * <p/> * - Le groupe 1 est le nombre de la version * - Le groupe 2 est le classifier (peut-etre null) @@ -72,6 +73,25 @@ Pattern.compile("^(\\d+(?:\\.(?:\\d+))*)(?:-(\\w+?)-(\\d+?)){0,1}(-SNAPSHOT){0,1}$"); /** + * Pattern pour detecter une version (avec si classifier et numéro de + * classifier collé, par exemple : 1.0-rc1). + * <p/> + * Le pattern possède toujours 4 groupes de captures. + * <p/> + * - Le groupe 1 est le nombre de la version + * - Le groupe 2 est le classifier (peut-etre null) + * - Le groupe 3 est le numéro de classifier (peut-etre null) + * - Le groupe 4 est le suffix -SNAPSHOT (peut-etre null) + * <p/> + * Dans le cas d'une version simple (sans classifier), le groupe 2, 3 et + * 4 sont null. + * <p/> + * Dans le cas d'une version non snapshot, le groupe 4 est null. + */ + public static final Pattern VERSION_PATTERN2 = + Pattern.compile("^(\\d+(?:\\.(?:\\d+))*)(?:-(\\w+?)(\\d+?)){0,1}(-SNAPSHOT){0,1}$"); + + /** * Shared instance of default version comparator. * * @see VersionComparator Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/VersionUtilTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/VersionUtilTest.java 2012-01-16 16:32:34 UTC (rev 2280) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/VersionUtilTest.java 2012-01-16 18:18:43 UTC (rev 2281) @@ -115,6 +115,31 @@ assertPattern("1.2.3.4-classifier-9-SNAPSHOT", true, "1.2.3.4", "classifier", "9", "-SNAPSHOT"); } + @Test + public void testPatterns2() { + + assertPattern2("1-2.3.4", false); + assertPattern2("1.a", false); + assertPattern2("1.alpha", false); + assertPattern2("1.alpha-", false); + assertPattern2("1.alpha4", false); + assertPattern2("1-alpha", false); + assertPattern2("1-alpha-", false); + assertPattern2("1--3", false); + assertPattern2("-alpha1", false); + assertPattern2("a-alpha1", false); + assertPattern2("-alpha1-", false); + + assertPattern2("1.2.3.4", true, "1.2.3.4", null, null, null); + assertPattern2("1.2.3.4-SNAPSHOT", true, "1.2.3.4", null, null, "-SNAPSHOT"); + + assertPattern2("1.2.3.4-alpha1", true, "1.2.3.4", "alpha", "1", null); + assertPattern2("1.2.3.4-alpha1-SNAPSHOT", true, "1.2.3.4", "alpha", "1", "-SNAPSHOT"); + + assertPattern2("1.2.3.4-classifier9", true, "1.2.3.4", "classifier", "9", null); + assertPattern2("1.2.3.4-classifier9-SNAPSHOT", true, "1.2.3.4", "classifier", "9", "-SNAPSHOT"); + } + /** Test of the {@link VersionUtil#DEFAULT_VERSION_COMPARATOR}. */ @Test public void testVersionComparotor() { @@ -331,7 +356,6 @@ // filter [2,Null[ = [2,3,4,5] actual = VersionUtil.filterVersions(versions, v2, null, true, false); assertVersionsEquals(actual, v2, v3, v4, v5); - } @Test @@ -351,7 +375,18 @@ Assert.assertTrue(v4.isSnapshot()); Assert.assertTrue(v4.before(v2)); + } + @Test + public void testAnomalie1629() { + Version v1 = VersionUtil.valueOf("1-beta1"); + Version v2 = VersionUtil.valueOf("1-beta-1"); + Assert.assertEquals(v1, v2); + Assert.assertFalse(v1.toString().equals(v2.toString())); + v1.setClassifierNumberAttached(false); + Assert.assertEquals(v1, v2); + Assert.assertEquals(v1.toString(), v2.toString()); + } protected static void compareVersions(int order, Iterable<Version> versions) { @@ -404,6 +439,28 @@ } } + protected void assertPattern2(String t, boolean match, String... groups) { + if (log.isDebugEnabled()) { + log.debug("convert " + t + ", mustMatch : " + match); + } + Matcher m = VersionUtil.VERSION_PATTERN2.matcher(t); + + assertEquals(match, m.matches()); + + if (match) { + assertEquals(groups.length, m.groupCount()); + for (int i = 1; i <= m.groupCount(); i++) { + String group = m.group(i); + assertEquals(groups[i - 1], group); + if (group != null) { + if (log.isDebugEnabled()) { + log.debug("group " + i + " : " + group); + } + } + } + } + } + private void assertVersionsEquals(List<Version> actual, Version... expected) { assertEquals(expected.length, actual.size()); int i = 0;
participants (1)
-
tchemit@users.nuiton.org