You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/05/03 10:59:58 UTC
svn commit: r399214 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/util/ test/java/org/apache/harmony/tests/java/util/
test/resources/serialization/java/util/
Author: tellison
Date: Wed May 3 01:59:55 2006
New Revision: 399214
URL: http://svn.apache.org/viewcvs?rev=399214&view=rev
Log:
Apply patch HARMONY-426 ([classlib][luni] java.util.UUID)
Added:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/UUIDTest.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/util/UUID.ser (with props)
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java?rev=399214&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java Wed May 3 01:59:55 2006
@@ -0,0 +1,483 @@
+/*
+ * Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package java.util;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.Serializable;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+
+/**
+ * <p>
+ * UUID is an immutable representation of a 128-bit universally unique
+ * identifier (UUID).
+ * </p>
+ * <p>
+ * There are multiple, variant layouts of UUIDs, but this class is based upon
+ * variant 2 of <a href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>, the
+ * Leach-Salz variant. This class can be used to model alternate variants, but
+ * most of the methods will be unsupported in those cases; see each method for
+ * details.
+ * </p>
+ *
+ * @since 1.5
+ * @author Nathan Beyer (Harmony)
+ */
+public final class UUID implements Serializable, Comparable<UUID> {
+
+ private static final long serialVersionUID = -4856846361193249489L;
+
+ private static SecureRandom rng;
+
+ private long mostSigBits;
+ private long leastSigBits;
+
+ private transient int variant;
+ private transient int version;
+ private transient long timestamp;
+ private transient int clockSequence;
+ private transient long node;
+ private transient int hash;
+
+ /**
+ * <p>
+ * Constructs an instance with the specified bits.
+ * </p>
+ *
+ * @param mostSigBits The 64 most significant bits of the UUID.
+ * @param leastSigBits The 64 least significant bits of the UUID.
+ */
+ public UUID(long mostSigBits, long leastSigBits) {
+ super();
+ this.mostSigBits = mostSigBits;
+ this.leastSigBits = leastSigBits;
+ init();
+ }
+
+ /**
+ * <p>
+ * Sets up the transient fields of this instance based on the current values
+ * of the <code>mostSigBits</code> and <code>leastSigBits</code> fields.
+ * </p>
+ */
+ private void init() {
+ // setup hash field
+ int msbHash = (int) (mostSigBits ^ (mostSigBits >>> 32));
+ int lsbHash = (int) (leastSigBits ^ (leastSigBits >>> 32));
+ hash = msbHash ^ lsbHash;
+
+ // setup variant field
+ if ((leastSigBits & 0x8000000000000000L) == 0) {
+ // MSB0 not set, NCS backwards compatibility variant
+ variant = 0;
+ } else if ((leastSigBits & 0x4000000000000000L) != 0) {
+ // MSB1 set, either MS reserved or future reserved
+ variant = (int) ((leastSigBits & 0xE000000000000000L) >>> 61);
+ } else {
+ // MSB1 not set, RFC 4122 variant
+ variant = 2;
+ }
+
+ // setup version field
+ version = (int) ((mostSigBits & 0x000000000000F000) >>> 12);
+
+ if (variant != 2 && version != 1)
+ return;
+
+ // setup timestamp field
+ long timeLow = (mostSigBits & 0xFFFFFFFF00000000L) >>> 32;
+ long timeMid = (mostSigBits & 0x00000000FFFF0000L) << 16;
+ long timeHigh = (mostSigBits & 0x0000000000000FFFL) << 48;
+ timestamp = timeLow | timeMid | timeHigh;
+
+ // setup clock sequence field
+ clockSequence = (int) ((leastSigBits & 0x3FFF000000000000L) >>> 48);
+
+ // setup node field
+ node = (leastSigBits & 0x0000FFFFFFFFFFFFL);
+ }
+
+ /**
+ * <p>
+ * Generates a variant 2, version 4 (randomly generated number) UUID as per
+ * <a href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.
+ * </p>
+ *
+ * @return A UUID instance.
+ */
+ public static UUID randomUUID() {
+ byte[] data;
+ // lock on the class to protect lazy init
+ synchronized (UUID.class) {
+ if (rng == null) {
+ rng = new SecureRandom();
+ }
+ rng.nextBytes(data = new byte[16]);
+ }
+
+ long msb = (data[0] & 0xFFL) << 56;
+ msb |= (data[1] & 0xFFL) << 48;
+ msb |= (data[2] & 0xFFL) << 40;
+ msb |= (data[3] & 0xFFL) << 32;
+ msb |= (data[4] & 0xFFL) << 24;
+ msb |= (data[5] & 0xFFL) << 16;
+ msb |= (data[6] & 0x0FL) << 8;
+ msb |= (0x4L << 12); // set the version to 4
+ msb |= (data[7] & 0xFFL);
+
+ long lsb = (data[8] & 0x3FL) << 56;
+ lsb |= (0x2L << 62); // set the variant to bits 01
+ lsb |= (data[9] & 0xFFL) << 48;
+ lsb |= (data[10] & 0xFFL) << 40;
+ lsb |= (data[11] & 0xFFL) << 32;
+ lsb |= (data[12] & 0xFFL) << 24;
+ lsb |= (data[13] & 0xFFL) << 16;
+ lsb |= (data[14] & 0xFFL) << 8;
+ lsb |= (data[15] & 0xFFL);
+ return new UUID(msb, lsb);
+ }
+
+ /**
+ * <p>
+ * Generates a variant 2, version 3 (name-based, MD5-hashed) UUID as per <a
+ * href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.
+ * </p>
+ *
+ * @return A UUID instance.
+ */
+ public static UUID nameUUIDFromBytes(byte[] name) {
+ if (name == null)
+ throw new NullPointerException();
+
+ byte[] hash;
+ try {
+ MessageDigest md = MessageDigest.getInstance("MD5");
+ hash = md.digest(name);
+ } catch (NoSuchAlgorithmException e) {
+ throw new AssertionError(e);
+ }
+
+ long msb = (hash[0] & 0xFFL) << 56;
+ msb |= (hash[1] & 0xFFL) << 48;
+ msb |= (hash[2] & 0xFFL) << 40;
+ msb |= (hash[3] & 0xFFL) << 32;
+ msb |= (hash[4] & 0xFFL) << 24;
+ msb |= (hash[5] & 0xFFL) << 16;
+ msb |= (hash[6] & 0x0FL) << 8;
+ msb |= (0x3L << 12); // set the version to 3
+ msb |= (hash[7] & 0xFFL);
+
+ long lsb = (hash[8] & 0x3FL) << 56;
+ lsb |= (0x2L << 62); // set the variant to bits 01
+ lsb |= (hash[9] & 0xFFL) << 48;
+ lsb |= (hash[10] & 0xFFL) << 40;
+ lsb |= (hash[11] & 0xFFL) << 32;
+ lsb |= (hash[12] & 0xFFL) << 24;
+ lsb |= (hash[13] & 0xFFL) << 16;
+ lsb |= (hash[14] & 0xFFL) << 8;
+ lsb |= (hash[15] & 0xFFL);
+ return new UUID(msb, lsb);
+ }
+
+ /**
+ * <p>
+ * Parses a UUID string with the format defined by {@link #toString()}.
+ * </p>
+ *
+ * @param uuid The UUID string to parse.
+ * @return A UUID instance.
+ * @throws NullPointerException if <code>uuid</code> is <code>null</code>.
+ * @throws IllegalArgumentException if <code>uuid</code> is not formatted
+ * correctly.
+ */
+ public static UUID fromString(String uuid) {
+ 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.
+ */
+ public long getLeastSignificantBits() {
+ return leastSigBits;
+ }
+
+ /**
+ * <p>
+ * The 64 most significant bits of the UUID.
+ * </p>
+ *
+ * @return A long value.
+ */
+ public long getMostSignificantBits() {
+ return mostSigBits;
+ }
+
+ /**
+ * <p>
+ * The version of the variant 2 UUID as per <a
+ * href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>. If the variant
+ * is not 2, then the version will be 0.
+ * </p>
+ * <ul>
+ * <li>1 - Time-based UUID</li>
+ * <li>2 - DCE Security UUID</li>
+ * <li>3 - Name-based with MD5 hashing UUID ({@link #nameUUIDFromBytes(byte[])})</li>
+ * <li>4 - Randomly generated UUID ({@link #randomUUID()})</li>
+ * <li>5 - Name-based with SHA-1 hashing UUID</li>
+ * </ul>
+ *
+ * @return An int value.
+ */
+ public int version() {
+ return version;
+ }
+
+ /**
+ * <p>
+ * The variant of the UUID as per <a
+ * href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.
+ * </p>
+ * <ul>
+ * <li>0 - Reserved for NCS compatibility</li>
+ * <li>2 - RFC 4122/Leach-Salz</li>
+ * <li>6 - Reserved for Microsoft Corporation compatibility</li>
+ * <li>7 - Reserved for future use</li>
+ * </ul>
+ *
+ * @return An int value.
+ */
+ public int variant() {
+ return variant;
+ }
+
+ /**
+ * <p>
+ * The timestamp value of the version 1, variant 2 UUID as per <a
+ * href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.
+ * </p>
+ *
+ * @return A long value.
+ * @throws UnsupportedOperationException if {@link #version()} is not 1.
+ */
+ public long timestamp() {
+ if (version != 1)
+ throw new UnsupportedOperationException();
+ return timestamp;
+ }
+
+ /**
+ * <p>
+ * The clock sequence value of the version 1, variant 2 UUID as per <a
+ * href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.
+ * </p>
+ *
+ * @return A long value.
+ * @throws UnsupportedOperationException if {@link #version()} is not 1.
+ */
+ public int clockSequence() {
+ if (version != 1)
+ throw new UnsupportedOperationException();
+ return clockSequence;
+ }
+
+ /**
+ * <p>
+ * The node value of the version 1, variant 2 UUID as per <a
+ * href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.
+ * </p>
+ *
+ * @return A long value.
+ * @throws UnsupportedOperationException if {@link #version()} is not 1.
+ */
+ public long node() {
+ if (version != 1)
+ throw new UnsupportedOperationException();
+ return node;
+ }
+
+ /**
+ * <p>
+ * Compares this UUID to the specificed UUID. The natural ordering of UUIDs
+ * is based upon the value of the bits from most significant ot least
+ * significant.
+ * </p>
+ *
+ * @param uuid The UUID to compare to.
+ * @return A value of -1, 0 or 1 if this UUID is less than, equal to or
+ * greater than <code>uuid</code>.
+ */
+ public int compareTo(UUID uuid) {
+ if (uuid == this)
+ return 0;
+
+ if (this.mostSigBits != uuid.mostSigBits) {
+ return this.mostSigBits < uuid.mostSigBits ? -1 : 1;
+ }
+
+ assert this.mostSigBits == uuid.mostSigBits;
+
+ if (this.leastSigBits != uuid.leastSigBits) {
+ return this.leastSigBits < uuid.leastSigBits ? -1 : 1;
+ }
+
+ assert this.leastSigBits == uuid.leastSigBits;
+
+ return 0;
+ }
+
+ /**
+ * <p>
+ * Compares this UUID to another object for equality. If <code>object</code>
+ * is not <code>null</code>, a UUID instance and all bits are equals,
+ * then <code>true</code> is returned.
+ * </p>
+ *
+ * @param object The Object to compare to.
+ * @return A <code>true</code> if this UUID is equal to
+ * <code>object</code> or <code>false</code> if not.
+ */
+ public boolean equals(Object object) {
+ if (object == null)
+ return false;
+
+ if (this == object)
+ return true;
+
+ if (!(object instanceof UUID))
+ return false;
+
+ UUID that = (UUID) object;
+
+ return (this.leastSigBits == that.leastSigBits)
+ && (this.mostSigBits == that.mostSigBits);
+ }
+
+ /**
+ * <p>
+ * Returns a hash value for this UUID that is consistent with the
+ * {@link #equals(Object)} method.
+ * </p>
+ *
+ * @return An int value.
+ */
+ public int hashCode() {
+ return hash;
+ }
+
+ /**
+ * <p>
+ * Returns a string representation of this UUID in the following format, as
+ * per <a href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.
+ * </p>
+ *
+ * <pre>
+ * UUID = time-low "-" time-mid "-"
+ * time-high-and-version "-"
+ * clock-seq-and-reserved
+ * clock-seq-low "-" node
+ * time-low = 4hexOctet
+ * time-mid = 2hexOctet
+ * time-high-and-version = 2hexOctet
+ * clock-seq-and-reserved = hexOctet
+ * clock-seq-low = hexOctet
+ * node = 6hexOctet
+ * hexOctet = hexDigit hexDigit
+ * hexDigit =
+ * "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
+ * "a" / "b" / "c" / "d" / "e" / "f" /
+ * "A" / "B" / "C" / "D" / "E" / "F"
+ * </pre>
+ *
+ * @return A String instance.
+ */
+ public String toString() {
+ StringBuilder builder = new StringBuilder(36);
+ String msbStr = Long.toHexString(mostSigBits);
+ if (msbStr.length() < 16) {
+ int diff = 16 - msbStr.length();
+ for (int i = 0; i < diff; i++) {
+ builder.append('0');
+ }
+ }
+ builder.append(msbStr);
+ builder.insert(8, '-');
+ builder.insert(13, '-');
+ builder.append('-');
+ String lsbStr = Long.toHexString(leastSigBits);
+ if (lsbStr.length() < 16) {
+ int diff = 16 - lsbStr.length();
+ for (int i = 0; i < diff; i++) {
+ builder.append('0');
+ }
+ }
+ builder.append(lsbStr);
+ builder.insert(23, '-');
+ return builder.toString();
+ }
+
+ /**
+ * <p>
+ * Resets the transient fields to match the behavior of the constructor.
+ * </p>
+ *
+ * @param in The InputStream to read from.
+ * @throws IOException if <code>in</code> throws it.
+ * @throws ClassNotFoundException if <code>in</code> throws it.
+ */
+ private void readObject(ObjectInputStream in) throws IOException,
+ ClassNotFoundException {
+ // read in non-transient fields
+ in.defaultReadObject();
+ // setup transient fields
+ init();
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/UUID.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java?rev=399214&r1=399213&r2=399214&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/AllTests.java Wed May 3 01:59:55 2006
@@ -36,6 +36,7 @@
suite.addTestSuite(DateTest.class);
suite.addTestSuite(VectorTest.class);
suite.addTestSuite(LocaleTest.class);
+ suite.addTestSuite(UUIDTest.class);
//$JUnit-END$
return suite;
}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/UUIDTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/UUIDTest.java?rev=399214&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/UUIDTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/UUIDTest.java Wed May 3 01:59:55 2006
@@ -0,0 +1,352 @@
+/*
+ * Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package org.apache.harmony.tests.java.util;
+
+import java.util.UUID;
+
+import tests.util.SerializationTester;
+
+import junit.framework.TestCase;
+
+public class UUIDTest extends TestCase {
+
+ /**
+ * @see UUID#UUID(long, long)
+ */
+ public void test_ConstructurJJ() {
+ UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
+ assertEquals(2, uuid.variant());
+ assertEquals(1, uuid.version());
+ assertEquals(0x1d07decf81d4faeL, uuid.timestamp());
+ assertEquals(130742845922168750L, uuid.timestamp());
+ assertEquals(0x2765, uuid.clockSequence());
+ assertEquals(0xA0C91E6BF6L, uuid.node());
+ }
+
+ /**
+ * @see UUID#getLeastSignificantBits()
+ */
+ public void test_getLeastSignificantBits() {
+ UUID uuid = new UUID(0, 0);
+ assertEquals(0, uuid.getLeastSignificantBits());
+ uuid = new UUID(0, Long.MIN_VALUE);
+ assertEquals(Long.MIN_VALUE, uuid.getLeastSignificantBits());
+ uuid = new UUID(0, Long.MAX_VALUE);
+ assertEquals(Long.MAX_VALUE, uuid.getLeastSignificantBits());
+ }
+
+ /**
+ * @see UUID#getMostSignificantBits()
+ */
+ public void test_getMostSignificantBits() {
+ UUID uuid = new UUID(0, 0);
+ assertEquals(0, uuid.getMostSignificantBits());
+ uuid = new UUID(Long.MIN_VALUE, 0);
+ assertEquals(Long.MIN_VALUE, uuid.getMostSignificantBits());
+ uuid = new UUID(Long.MAX_VALUE, 0);
+ assertEquals(Long.MAX_VALUE, uuid.getMostSignificantBits());
+ }
+
+ /**
+ * @see UUID#version()
+ */
+ public void test_version() {
+ UUID uuid = new UUID(0, 0);
+ assertEquals(0, uuid.version());
+ uuid = new UUID(0x0000000000001000L, 0);
+ assertEquals(1, uuid.version());
+ uuid = new UUID(0x0000000000002000L, 0);
+ assertEquals(2, uuid.version());
+ uuid = new UUID(0x0000000000003000L, 0);
+ assertEquals(3, uuid.version());
+ uuid = new UUID(0x0000000000004000L, 0);
+ assertEquals(4, uuid.version());
+ uuid = new UUID(0x0000000000005000L, 0);
+ assertEquals(5, uuid.version());
+ }
+
+ /**
+ * @see UUID#variant()
+ */
+ public void test_variant() {
+ UUID uuid = new UUID(0, 0x0000000000000000L);
+ assertEquals(0, uuid.variant());
+ uuid = new UUID(0, 0x7000000000000000L);
+ assertEquals(0, uuid.variant());
+ uuid = new UUID(0, 0x3000000000000000L);
+ assertEquals(0, uuid.variant());
+ uuid = new UUID(0, 0x1000000000000000L);
+ assertEquals(0, uuid.variant());
+
+ uuid = new UUID(0, 0x8000000000000000L);
+ assertEquals(2, uuid.variant());
+ uuid = new UUID(0, 0xB000000000000000L);
+ assertEquals(2, uuid.variant());
+ uuid = new UUID(0, 0xA000000000000000L);
+ assertEquals(2, uuid.variant());
+ uuid = new UUID(0, 0x9000000000000000L);
+ assertEquals(2, uuid.variant());
+
+ uuid = new UUID(0, 0xC000000000000000L);
+ assertEquals(6, uuid.variant());
+ uuid = new UUID(0, 0xD000000000000000L);
+ assertEquals(6, uuid.variant());
+
+ uuid = new UUID(0, 0xE000000000000000L);
+ assertEquals(7, uuid.variant());
+ uuid = new UUID(0, 0xF000000000000000L);
+ assertEquals(7, uuid.variant());
+ }
+
+ /**
+ * @see UUID#timestamp()
+ */
+ public void test_timestamp() {
+ UUID uuid = new UUID(0x0000000000001000L, 0x8000000000000000L);
+ assertEquals(0x0, uuid.timestamp());
+
+ uuid = new UUID(0x7777777755551333L, 0x8000000000000000L);
+ assertEquals(0x333555577777777L, uuid.timestamp());
+
+ uuid = new UUID(0x0000000000000000L, 0x8000000000000000L);
+ try {
+ uuid.timestamp();
+ fail("No UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {}
+
+ uuid = new UUID(0x0000000000002000L, 0x8000000000000000L);
+ try {
+ uuid.timestamp();
+ fail("No UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {}
+ }
+
+ /**
+ * @see UUID#clockSequence()
+ */
+ public void test_clockSequence() {
+ UUID uuid = new UUID(0x0000000000001000L, 0x8000000000000000L);
+ assertEquals(0x0, uuid.clockSequence());
+
+ uuid = new UUID(0x0000000000001000L, 0x8FFF000000000000L);
+ assertEquals(0x0FFF, uuid.clockSequence());
+
+ uuid = new UUID(0x0000000000001000L, 0xBFFF000000000000L);
+ assertEquals(0x3FFF, uuid.clockSequence());
+
+ uuid = new UUID(0x0000000000000000L, 0x8000000000000000L);
+ try {
+ uuid.clockSequence();
+ fail("No UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {}
+
+ uuid = new UUID(0x0000000000002000L, 0x8000000000000000L);
+ try {
+ uuid.clockSequence();
+ fail("No UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {}
+ }
+
+ /**
+ * @see UUID#node()
+ */
+ public void test_node() {
+ UUID uuid = new UUID(0x0000000000001000L, 0x8000000000000000L);
+ assertEquals(0x0, uuid.node());
+
+ uuid = new UUID(0x0000000000001000L, 0x8000FFFFFFFFFFFFL);
+ assertEquals(0xFFFFFFFFFFFFL, uuid.node());
+
+ uuid = new UUID(0x0000000000000000L, 0x8000000000000000L);
+ try {
+ uuid.node();
+ fail("No UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {}
+
+ uuid = new UUID(0x0000000000002000L, 0x8000000000000000L);
+ try {
+ uuid.node();
+ fail("No UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {}
+ }
+
+ /**
+ * @see UUID#compareTo(UUID)
+ */
+ public void test_compareTo() {
+ UUID uuid1 = new UUID(0, 0);
+ assertEquals(0, uuid1.compareTo(uuid1));
+ UUID uuid2 = new UUID(1, 0);
+ assertEquals(-1, uuid1.compareTo(uuid2));
+ assertEquals(1, uuid2.compareTo(uuid1));
+
+ uuid2 = new UUID(0, 1);
+ assertEquals(-1, uuid1.compareTo(uuid2));
+ assertEquals(1, uuid2.compareTo(uuid1));
+ }
+
+ /**
+ * @see UUID#hashCode()
+ */
+ public void test_hashCode() {
+ UUID uuid = new UUID(0, 0);
+ assertEquals(0, uuid.hashCode());
+ uuid = new UUID(123, 123);
+ UUID uuidClone = new UUID(123, 123);
+ assertEquals(uuid.hashCode(), uuidClone.hashCode());
+ }
+
+ /**
+ * @see UUID#equals(Object)
+ */
+ public void test_equalsObject() {
+ UUID uuid1 = new UUID(0, 0);
+ assertEquals(uuid1, uuid1);
+ assertFalse(uuid1.equals(null));
+ assertFalse(uuid1.equals("NOT A UUID"));
+ UUID uuid2 = new UUID(0, 0);
+ assertEquals(uuid1, uuid2);
+ assertEquals(uuid2, uuid1);
+
+ uuid1 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
+ uuid2 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
+ assertEquals(uuid1, uuid2);
+ assertEquals(uuid2, uuid1);
+
+ uuid2 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf7L);
+ assertFalse(uuid1.equals(uuid2));
+ assertFalse(uuid2.equals(uuid1));
+ }
+
+ /**
+ * @see UUID#toString()
+ */
+ public void test_toString() {
+ UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
+ String actual = uuid.toString();
+ assertEquals("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", actual);
+
+ uuid = new UUID(0x0000000000001000L, 0x8000000000000000L);
+ actual = uuid.toString();
+ assertEquals("00000000-0000-1000-8000-000000000000", actual);
+ }
+
+ public void test_serialization() throws Exception {
+ UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
+ SerializationTester.assertEquals(uuid);
+ }
+
+ public void test_serializationCompatability() throws Exception {
+ UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
+ SerializationTester.assertCompabilityEquals(uuid,
+ "serialization/java/util/UUID.ser");
+ }
+
+ /**
+ * @see UUID#randomUUID()
+ */
+ public void test_randomUUID() {
+ UUID uuid = UUID.randomUUID();
+ assertEquals(2, uuid.variant());
+ assertEquals(4, uuid.version());
+ }
+
+ /**
+ * @see UUID#nameUUIDFromBytes(byte[])
+ */
+ public void test_nameUUIDFromBytes() throws Exception {
+ byte[] name = { (byte) 0x6b, (byte) 0xa7, (byte) 0xb8, (byte) 0x11,
+ (byte) 0x9d, (byte) 0xad, (byte) 0x11, (byte) 0xd1,
+ (byte) 0x80, (byte) 0xb4, (byte) 0x00, (byte) 0xc0,
+ (byte) 0x4f, (byte) 0xd4, (byte) 0x30, (byte) 0xc8 };
+
+ UUID uuid = UUID.nameUUIDFromBytes(name);
+
+ assertEquals(2, uuid.variant());
+ assertEquals(3, uuid.version());
+
+ assertEquals(0xaff565bc2f771745L, uuid.getLeastSignificantBits());
+ assertEquals(0x14cdb9b4de013faaL, uuid.getMostSignificantBits());
+
+ uuid = UUID.nameUUIDFromBytes(new byte[0]);
+ assertEquals(2, uuid.variant());
+ assertEquals(3, uuid.version());
+
+ assertEquals(0xa9800998ecf8427eL, uuid.getLeastSignificantBits());
+ assertEquals(0xd41d8cd98f003204L, uuid.getMostSignificantBits());
+
+ try {
+ UUID.nameUUIDFromBytes(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {}
+ }
+
+ /**
+ * @see UUID#fromString(String)
+ */
+ public void test_fromString() {
+ UUID actual = UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
+ UUID expected = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
+ assertEquals(expected, actual);
+
+ assertEquals(2, actual.variant());
+ assertEquals(1, actual.version());
+ assertEquals(130742845922168750L, actual.timestamp());
+ assertEquals(10085, actual.clockSequence());
+ assertEquals(690568981494L, actual.node());
+
+ actual = UUID.fromString("00000000-0000-1000-8000-000000000000");
+ expected = new UUID(0x0000000000001000L, 0x8000000000000000L);
+ assertEquals(expected, actual);
+
+ assertEquals(2, actual.variant());
+ assertEquals(1, actual.version());
+ assertEquals(0L, actual.timestamp());
+ assertEquals(0, actual.clockSequence());
+ assertEquals(0L, actual.node());
+
+ try {
+ UUID.fromString(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {}
+
+ try {
+ UUID.fromString("");
+ fail("No IAE");
+ } catch (IllegalArgumentException e) {}
+
+ try {
+ UUID.fromString("f81d4fae_7dec-11d0-a765-00a0c91e6bf6");
+ fail("No IAE");
+ } catch (IllegalArgumentException e) {}
+
+ try {
+ UUID.fromString("f81d4fae-7dec_11d0-a765-00a0c91e6bf6");
+ fail("No IAE");
+ } catch (IllegalArgumentException e) {}
+
+ try {
+ UUID.fromString("f81d4fae-7dec-11d0_a765-00a0c91e6bf6");
+ fail("No IAE");
+ } catch (IllegalArgumentException e) {}
+
+ try {
+ UUID.fromString("f81d4fae-7dec-11d0-a765_00a0c91e6bf6");
+ fail("No IAE");
+ } catch (IllegalArgumentException e) {}
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/UUIDTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/util/UUID.ser
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/util/UUID.ser?rev=399214&view=auto
==============================================================================
Binary file - no diff available.
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/util/UUID.ser
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream