Author: ozizka(a)redhat.com
Date: 2009-11-11 11:26:05 -0500 (Wed, 11 Nov 2009)
New Revision: 852
Modified:
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EjtsSelfTest.java
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/EjtsEnvironmentInfo.java
Log:
* Version comparison
Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EjtsSelfTest.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EjtsSelfTest.java 2009-11-10
17:39:49 UTC (rev 851)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EjtsSelfTest.java 2009-11-11
16:26:05 UTC (rev 852)
@@ -44,20 +44,25 @@
logManifest( new Manifest( is ) );
- doCompareVersions("2.3.0.GA", "2.4.0-SNAPSHOT", -1);
- doCompareVersions("2.4.0.GA", "2.4.0-SNAPSHOT", 1);
- doCompareVersions("2.4.1.GA", "2.4.0-SNAPSHOT", 1);
- doCompareVersions("2.4.1", "2.4.0-SNAPSHOT", 1);
- doCompareVersions("2.4.0", "2.4.0-SNAPSHOT", 1);
- doCompareVersions("2.4.0", "2.4.0.GA", 0);
- doCompareVersions("2.4.0.CR1", "2.4.0.GA", -1);
- doCompareVersions("2.4.CR1", "2.4.0.GA", -1);
- doCompareVersions("2.4.CR1", "2.4.0", -1);
+ boolean succ = true;
+ succ &= doCompareVersions("2.3.0.GA", "2.4.0-SNAPSHOT", -1);
+ succ &= doCompareVersions("2.4.0.GA", "2.4.0-SNAPSHOT", 1);
+ succ &= doCompareVersions("2.4.1.GA", "2.4.0-SNAPSHOT", 1);
+ succ &= doCompareVersions("2.4.1", "2.4.0-SNAPSHOT", 1);
+ succ &= doCompareVersions("2.4.0", "2.4.0-SNAPSHOT", 1);
+ succ &= doCompareVersions("2.4.0", "2.4.0.GA", 0);
+ succ &= doCompareVersions("2.4.0.CR1", "2.4.0.GA", -1);
+ succ &= doCompareVersions("2.4.CR1", "2.4.0.GA", -1);
+ succ &= doCompareVersions("2.4.CR1", "2.4.0", -1);
+ if( ! succ )
+ fail("Version comparisons do not fit.");
}
- private void doCompareVersions( String v1, String v2, int expectedSignum ){
- log.info( String.format("Comparing versions: %s and %s produces: %d",
v1, v2, EjtsEnvironmentInfo.compareVersions(v1, v2) ) );
+ private boolean doCompareVersions( String v1, String v2, int expectedSignum ){
+ int compared = EjtsEnvironmentInfo.compareVersions(v1, v2);
+ log.info( String.format("Comparing versions: %s and %s produces: %d,
expected: %d", v1, v2, compared, expectedSignum ) );
+ return Integer.signum(expectedSignum) == Integer.signum( compared );
}
Modified:
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/EjtsEnvironmentInfo.java
===================================================================
---
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/EjtsEnvironmentInfo.java 2009-11-10
17:39:49 UTC (rev 851)
+++
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/EjtsEnvironmentInfo.java 2009-11-11
16:26:05 UTC (rev 852)
@@ -26,6 +26,7 @@
import org.apache.commons.lang.math.NumberUtils;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.jboss.jopr.jsfunit.JMXUtils;
+import org.jboss.logging.Logger;
/**
*
@@ -33,6 +34,8 @@
*/
public class EjtsEnvironmentInfo
{
+ private static final Logger log = Logger.getLogger( EjtsEnvironmentInfo.class.getName()
);
+
private static EjtsEnvironmentInfo INSTANCE;
public static EjtsEnvironmentInfo getInstance() throws Exception {
@@ -123,19 +126,56 @@
return envInfo;
}
+ /**
+ * Strings after release: SP, CP
+ * Strings for release: GA, GOLD, 0
+ * Latest before release: SNAPSHOT
+ * Before release: (any other)
+ * @param s
+ * @return
+ */
+ private static final int getStringLevel( String s ){
+ if( "SP".equalsIgnoreCase(s) || "CP".equalsIgnoreCase(s) )
+ return 1;
+ if( "GA".equalsIgnoreCase(s) || "GOLD".equalsIgnoreCase(s) ||
"0".equalsIgnoreCase(s) )
+ return 0;
+ if( "SNAPSHOT".equalsIgnoreCase(s) )
+ return -1;
+ return -2;
+ }
/**
+ * Compares two string parts of the version string, e.g. "GA" vs.
"SNAPSHOT".
+ * @returns like compareTo() does.
+ */
+ public static int compareStrings( String a, String b ){
+ int aLev = getStringLevel( a );
+ int bLev = getStringLevel( b );
+
+ if( aLev == bLev )
+ return a.compareToIgnoreCase(b);
+ else
+ return aLev - bLev;
+ }
+
+
+ /**
* Compares two version strings, like compareTo() does.
* TODO: Maven comparison algorithm works bad; Code own one.
+ *
+ * Strings after release: SP, CP
+ * Strings for release: GA, GOLD
+ * Latest before release: SNAPSHOT
+ * Before release: (any other)
*/
public static int compareVersions(String aStr, String bStr){
//*
- String[] aParts = StringUtils.split(aStr.toUpperCase(), ".-_");
- String[] bParts = StringUtils.split(bStr.toUpperCase(), ".-_");
+ final String[] aParts = StringUtils.split(aStr.toUpperCase(), ".-_");
+ final String[] bParts = StringUtils.split(bStr.toUpperCase(), ".-_");
- int aOff= 0;
+ int aOff = 0;
int bOff = 0;
//int longer = 0;
@@ -144,6 +184,8 @@
do {
String a = aParts[aOff];
String b = bParts[bOff];
+
+ log.debug( String.format("A[%d] = '%s', B[%d] = '%s'", aOff,
a, bOff, b) );
if( "GA".equals(a) ) a = "0";
if( "GA".equals(b) ) b = "0";
@@ -152,38 +194,78 @@
boolean bIsNum = NumberUtils.isDigits( b );
Integer aNum = aIsNum ? NumberUtils.createInteger( a ) : null;
Integer bNum = bIsNum ? NumberUtils.createInteger( b ) : null;
+ boolean aIsLast = aOff >= aParts.length - 1;
+ boolean bIsLast = bOff >= bParts.length - 1;
- if( aIsNum && bIsNum ){
- int diff = aNum - bNum;
- // Different numbers - return the diff.
- if( diff != 0 ) return diff;
- // The same numbers - skip to next position.
- aOff++; bOff++; continue;
- }
- else if( aIsNum ^ bIsNum ){
+ log.debug( String.format("A[%d] = '%s', B[%d] = '%s'", aOff,
a, bOff, b) );
- if( aIsNum ){
- // Skip aditional "decimal" zeros - e.g. 2.0.0.FOO vs. 2.0.BAR .
- if ( aNum == 0 ){ aOff++; continue; }
- // A non-zero number is always bigger than any string (except GA).
- else { return 1; }
+ evaluation: {
+
+ if( aIsNum && bIsNum ){
+ log.debug("aIsNum && bIsNum");
+ int diff = aNum - bNum;
+ // Different numbers - return the diff.
+ if( diff != 0 ) return diff;
+ // The same numbers - skip to next position.
+ if( !aIsLast ) aOff++;
+ if( !bIsLast ) bOff++;
+ continue;
}
- else /* vice versa for ( bIsNum ) */ {
- // Skip aditional "decimal" zeros - e.g. 2.0.0.FOO vs. 2.0.BAR .
- if ( bNum == 0 ){ bOff++; continue; }
- // A non-zero number is always bigger than any string (except GA).
- else { return -1; }
+ else if( aIsNum ^ bIsNum ){
+ log.debug("aIsNum ^ bIsNum");
+ if( aIsNum ){
+ // Skip aditional "decimal" zeros - e.g. 2.0.0.FOO vs. 2.0.BAR .
+ if ( aNum == 0 ){
+ if( ! aIsLast ){ aOff++; continue; }
+ return compareStrings("0", b); // or: +getStringLevel(a);
+ }
+ // 2.0.0.1 vs. 2.0.0.SNAPSHOT
+ return 1;
+
+ }
+ else /* vice versa for ( bIsNum ) */ {
+ // Skip aditional "decimal" zeros - e.g. 2.0.0.FOO vs. 2.0.BAR .
+ if ( bNum == 0 ){
+ if( ! bIsLast ){ bOff++; continue; }
+ return compareStrings(a, "0"); // or: -getStringLevel(b);
+ }
+ // 2.0.0.1 vs. 2.0.0.SNAPSHOT
+ // 2.0.0.1 vs. 2.0.0.SP2
+ return -1;
+ }
}
- }
- // Neither is a number -> Compare two strings.
- return a.compareTo(b);
+ // Neither is a number -> Compare two strings.
+ {
+ // CP - cumulative patch - is bigger than other strings. SP should be far enough in
the alphabet.
+ // (There is a need for standardized project versioning strings.)
+
+ //if( "CP".equals(a) ) a = "SP";
+ //if( "CP".equals(b) ) b = "SP";
+
+ log.debug("Comparing strings produces" + compareStrings(a, b) );
+ int strDiff = compareStrings(a, b);
+ if( strDiff != 0 )
+ return strDiff;
+ }
+
+ }// label evaluation:
+
- } while( aOff < aParts.length && bOff < bParts.length );
+ // Advance to the next part, unless we're at the last one..
+ if( aOff < aParts.length -1 ) aOff++;
+ if( bOff < bParts.length -1 ) bOff++;
+ // End when we reached the last parts of both versions.
+ } while( aOff < aParts.length || bOff < bParts.length );
+
+ log.debug("Reached comparison loop end.");
+
+ return 0; // TODO: Fix
+
/*
// Maven's algorithm.
DefaultArtifactVersion v1 = new DefaultArtifactVersion(v1s);
@@ -191,18 +273,7 @@
return v1.compareTo(v2);
/**/
- return 0;
}
- class VersionInfo{
-
- public VersionInfo( String str ){
-
-
- }
-
- }
-
-
}// class