harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r470358 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/UUID.java main/java/org/apache/harmony/luni/util/ExternalMessages.properties test/java/org/apache/harmony/luni/tests/java/util/UUIDTest.java
Date Thu, 02 Nov 2006 13:01:32 GMT
Author: pyang
Date: Thu Nov  2 05:01:31 2006
New Revision: 470358

URL: http://svn.apache.org/viewvc?view=rev&rev=470358
Log:
Apply patch for HARMONY-2042 ([classlib][luni]UUID.fromString unexpectedly throws IllegalArgumentException
while the input string is not exactly 36 characters long.)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/UUIDTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java?view=diff&rev=470358&r1=470357&r2=470358
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java
Thu Nov  2 05:01:31 2006
@@ -24,6 +24,8 @@
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 
+import org.apache.harmony.luni.util.Msg;
+
 /**
  * <p>
  * UUID is an immutable representation of a 128-bit universally unique
@@ -213,48 +215,44 @@
         if (uuid == null) {
             throw new NullPointerException();
         }
-        if (uuid.length() != 36) {
-            throw new IllegalArgumentException(
-                    "The UUID String must be 36 chars long.");
-        }
-
-        try {
-            long m1 = Long.parseLong(uuid.substring(0, 8), 16);
-            if (uuid.charAt(8) != '-') {
-                throw new IllegalArgumentException();
-            }
-            long m2 = Long.parseLong(uuid.substring(9, 13), 16);
-            if (uuid.charAt(13) != '-') {
-                throw new IllegalArgumentException();
-            }
-            long m3 = Long.parseLong(uuid.substring(14, 18), 16);
-            if (uuid.charAt(18) != '-') {
-                throw new IllegalArgumentException();
-            }
-
-            long lsb1 = Long.parseLong(uuid.substring(19, 23), 16);
-            if (uuid.charAt(23) != '-') {
-                throw new IllegalArgumentException();
-            }
-            long lsb2 = Long.parseLong(uuid.substring(24), 16);
-
-            long msb = (m1 << 32) | (m2 << 16) | m3;
-            long lsb = (lsb1 << 48) | lsb2;
-            return new UUID(msb, lsb);
-        } catch (NumberFormatException e) {
-            throw new IllegalArgumentException(
-                    "The UUID String contained invalid hexadecimal characters.",
-                    e);
-        }
-    }
-
-    /**
-     * <p>
-     * The 64 least significant bits of the UUID.
-     * </p>
-     * 
-     * @return A long value.
-     */
+        
+        int[] position = new int[5];
+        int lastPosition = 1;
+        int startPosition = 0;
+        
+        int i = 0;
+        for (; i < position.length  && lastPosition > 0; i++) {
+        	position[i] = uuid.indexOf("-", startPosition); //$NON-NLS-1$
+        	lastPosition = position[i];
+        	startPosition = position[i] + 1;
+        }
+
+        // should have and only can have four "-" in UUID
+        if(i != position.length || lastPosition != -1)
+        {
+        	throw new IllegalArgumentException(Msg.getString("KA014") + uuid); //$NON-NLS-1$
+        }
+
+		long m1 = Long.parseLong(uuid.substring(0, position[0]), 16);
+		long m2 = Long.parseLong(uuid.substring(position[0]+ 1, position[1]), 16);
+		long m3 = Long.parseLong(uuid.substring(position[1] + 1, position[2]), 16);
+
+		long lsb1 = Long.parseLong(uuid.substring(position[2] + 1, position[3]), 16);
+		long lsb2 = Long.parseLong(uuid.substring(position[3]+ 1), 16);
+
+		long msb = (m1 << 32) | (m2 << 16) | m3;
+		long lsb = (lsb1 << 48) | lsb2;
+		
+		return new UUID(msb, lsb);
+	}
+
+    /**
+	 * <p>
+	 * The 64 least significant bits of the UUID.
+	 * </p>
+	 * 
+	 * @return A long value.
+	 */
     public long getLeastSignificantBits() {
         return leastSigBits;
     }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties?view=diff&rev=470358&r1=470357&r2=470358
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
Thu Nov  2 05:01:31 2006
@@ -305,4 +305,5 @@
 KA011=Malformed reply from SOCKS server
 KA012=No such file or directory
 KA013=Number of bytes to skip cannot be negative
+KA014=Invalit UUID string
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/UUIDTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/UUIDTest.java?view=diff&rev=470358&r1=470357&r2=470358
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/UUIDTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/UUIDTest.java
Thu Nov  2 05:01:31 2006
@@ -355,4 +355,103 @@
             fail("No IAE");
         } catch (IllegalArgumentException e) {}
     }
+
+	/**
+	 * @tests java.util.UUID#fromString(String)
+	 */
+	public void test_fromString_LString_Exception() {
+
+		UUID uuid = UUID.fromString("0-0-0-0-0");
+
+		try {
+			uuid = UUID.fromString("0-0-0-0-");
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+
+		try {
+			uuid = UUID.fromString("-0-0-0-0-0");
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+
+		try {
+			uuid = UUID.fromString("-0-0-0-0");
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+
+		try {
+			uuid = UUID.fromString("-0-0-0-");
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+
+		try {
+			uuid = UUID.fromString("0--0-0-0");
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+
+		try {
+			uuid = UUID.fromString("0-0-0-0-");
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+
+		try {
+			uuid = UUID.fromString("-1-0-0-0-0");
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+
+		uuid = UUID.fromString("123456789-0-0-0-0");
+		assertEquals(0x2345678900000000L, uuid.getMostSignificantBits());
+		assertEquals(0x0L, uuid.getLeastSignificantBits());
+
+		uuid = UUID.fromString("111123456789-0-0-0-0");
+		assertEquals(0x2345678900000000L, uuid.getMostSignificantBits());
+		assertEquals(0x0L, uuid.getLeastSignificantBits());
+
+		uuid = UUID.fromString("7fffffffffffffff-0-0-0-0");
+		assertEquals(0xffffffff00000000L, uuid.getMostSignificantBits());
+		assertEquals(0x0L, uuid.getLeastSignificantBits());
+
+		try {
+			uuid = UUID.fromString("8000000000000000-0-0-0-0");
+			fail("should throw NumberFormatException");
+		} catch (NumberFormatException e) {
+			// expected
+		}
+
+		uuid = UUID
+				.fromString("7fffffffffffffff-7fffffffffffffff-7fffffffffffffff-0-0");
+		assertEquals(0xffffffffffffffffL, uuid.getMostSignificantBits());
+		assertEquals(0x0L, uuid.getLeastSignificantBits());
+
+		uuid = UUID.fromString("0-0-0-7fffffffffffffff-7fffffffffffffff");
+		assertEquals(0x0L, uuid.getMostSignificantBits());
+		assertEquals(0xffffffffffffffffL, uuid.getLeastSignificantBits());
+
+		try {
+			uuid = UUID.fromString("0-0-0-8000000000000000-0");
+			fail("should throw NumberFormatException");
+		} catch (NumberFormatException e) {
+			// expected
+		}
+
+		try {
+			uuid = UUID.fromString("0-0-0-0-8000000000000000");
+			fail("should throw NumberFormatException");
+		} catch (NumberFormatException e) {
+			// expected
+		}
+	}
 }



Mime
View raw message