treilly 2004/05/31 00:08:14 Modified: id/src/test/org/apache/commons/id/uuid UUIDTest.java Log: Many changes + new methods Revision Changes Path 1.4 +406 -293 jakarta-commons-sandbox/id/src/test/org/apache/commons/id/uuid/UUIDTest.java Index: UUIDTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/id/src/test/org/apache/commons/id/uuid/UUIDTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- UUIDTest.java 4 Mar 2004 03:17:45 -0000 1.3 +++ UUIDTest.java 31 May 2004 07:08:14 -0000 1.4 @@ -1,293 +1,406 @@ -/*
- * Copyright 2004 The Apache Software Foundation. - * - * 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.commons.id.uuid; - -import java.math.BigInteger; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; -import junit.textui.TestRunner; - -/** - * Unit tests for [EMAIL PROTECTED] UUID}. - * - * @version $Revision$ $Date$ - * @author Commons-id team - */ -public class UUIDTest extends TestCase { - - /** - * Constructor for test - * - * @param name String name of the test - */ - public UUIDTest(String name) { - super(name); - } - - /** - * Main application method - * - * @param args String arguments array - */ - public static void main(String[] args) { - TestRunner.run(suite()); - } - - /** @see junit.framework.TestCase#suite() */ - - public static Test suite() { - TestSuite suite = new TestSuite(UUIDTest.class); - suite.setName("UUID Tests"); - return suite; - } - - //------------------------------------------------------------------------- - /** @see junit.framework.TestCase#setUp() */ - protected void setUp() throws Exception { - super.setUp(); - } - - /** - * Test the nil UUID() contructor returns a zero'd out UUID - * - * @throws Exception an exception while testing - */ - public void testNiltoString() throws Exception { - assertEquals( - (new UUID()).toString(), - "00000000-0000-0000-0000-000000000000"); - } - - /** - * Test copy constructor - * - * @throws Exception an exception while testing - */ - public void testCopyConstructor() throws Exception { - UUID uuidFrom = new UUID("B4F00409-CEF8-4822-802C-DEB20704C365"); - UUID uuidTo = new UUID(uuidFrom); - - //Assert all equals and output methods match - assertTrue(uuidFrom.equals(uuidTo)); - assertTrue(uuidTo.equals(uuidFrom)); - assertEquals(uuidTo.toString(), uuidFrom.toString()); - assertEquals(uuidTo.toBigInteger(), uuidFrom.toBigInteger()); - assertEquals(uuidTo.hashCode(), uuidFrom.hashCode()); - } - - /** - * Test BigInteger constructor - * - * @throws Exception an exception while testing - */ - public void testBigIntegerConstructor() throws Exception { - UUID uuidFrom = new UUID("B4F00409-CEF8-4822-802C-DEB20704C365"); - UUID uuidTo = new UUID(uuidFrom.toBigInteger()); - - //Assert all equals and output methods match - assertTrue(uuidFrom.equals(uuidTo)); - assertTrue(uuidTo.equals(uuidFrom)); - assertEquals(uuidTo.toString(), uuidFrom.toString()); - assertEquals(uuidTo.toBigInteger(), uuidFrom.toBigInteger()); - assertEquals(uuidTo.hashCode(), uuidFrom.hashCode()); - - //Test IllegalArgumentException in Construction - UUID badConstruction = null; - BigInteger maxUUIDInteger = - new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", UUID.BASE16); - try { - BigInteger tooBig = maxUUIDInteger.add(BigInteger.ONE); - badConstruction = new UUID(tooBig); - fail("Expecting IllegalArgumentException -- BigInteger too big."); - } - catch (IllegalArgumentException iea) { - // expected - } - - } - - /** - * Test String constructor - * - * @throws Exception an exception while testing - */ - public void testStringConstructor() throws Exception { - UUID uuidFrom = new UUID("B4F00409-CEF8-4822-802C-DEB20704C365"); - UUID uuidTo = new UUID(uuidFrom.toBigInteger()); - - //Assert all equals and output methods match - assertTrue(uuidFrom.equals(uuidTo)); - assertTrue(uuidTo.equals(uuidFrom)); - assertEquals(uuidTo.toString(), uuidFrom.toString()); - assertEquals(uuidTo.toBigInteger(), uuidFrom.toBigInteger()); - assertEquals(uuidTo.hashCode(), uuidFrom.hashCode()); - - //Test constructing prefixed strings - UUID fromPrefixed = - new UUID("urn:uuid:B4F00409-CEF8-4822-802C-DEB20704C365"); - assertTrue(uuidFrom.equals(fromPrefixed)); - - //Test constructing prefixed strings - UUID fromShortPrefixed = - new UUID("uuid:B4F00409-CEF8-4822-802C-DEB20704C365"); - assertTrue(uuidFrom.equals(fromPrefixed)); - - //Test IllegalArgumentException in Construction - UUID badConstruction = null; - - //try with not valid hexidecimal - try { - badConstruction = new UUID("G4F00409-CEF8-4822-802C-DEB20704C365"); - fail("Expecting UUIDFormatException -- invalid hex string"); - } - catch (UUIDFormatException iea) { - //Expected - } - - //try with too long a string - try { - badConstruction = new UUID("FF4F00409-CEF8-4822-802C-DEB20704C365"); - fail("Expecting UUIDFormatException -- string too long"); - } - catch (UUIDFormatException iea) { - //Expected - } - - //try with too short a string - try { - badConstruction = new UUID("4F00409-CEF8-4822-802C-DEB20704C365"); - fail("Expecting UUIDFormatException -- string too short"); - } - catch (UUIDFormatException iea) { - //Expected - } - - //try right string wrong order / format - try { - badConstruction = new UUID("F4F00409-CEF8-4822-802CD-EB20704C365"); - fail("Expecting UUIDFormatException -- wrong format"); - } - catch (UUIDFormatException iea) { - //Expected - } - } - - /** - * Test the static parseString method - * - * @throws Exception an exception while testing - */ - public void testParseString() throws Exception { - UUID baseline = new UUID("B4F00409-CEF8-4822-802C-DEB20704C365"); - assertEquals( - baseline, - UUID.parseString("urn:uuid:B4F00409-CEF8-4822-802C-DEB20704C365")); - assertEquals( - baseline, - UUID.parseString("uuid:B4F00409-CEF8-4822-802C-DEB20704C365")); - //High value test - String in = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"; - UUID high = UUID.parseString(in); - System.out.println(high.toString()); - assertTrue(high.toString().equals(in.toLowerCase())); - - //Negative testing - //try with not valid hexidecimal - UUID bad = null; - try { - bad = UUID.parseString("G4F00409-CEF8-4822-802C-DEB20704C365"); - fail("Expecting UUIDFormatException -- invalid hex"); - } - catch (UUIDFormatException iea) { - //Expected - } - - //try with too long a string - try { - bad = UUID.parseString("FF4F00409-CEF8-4822-802C-DEB20704C365"); - fail("Expecting UUIDFormatException -- string too long"); - } - catch (UUIDFormatException iea) { - //Expected - } - - //try with too short a string - try { - bad = UUID.parseString("4F00409-CEF8-4822-802C-DEB20704C365"); - fail("Expecting UUIDFormatException -- string too short"); - } - catch (UUIDFormatException iea) { - //Expected - } - - //try right string wrong order / format - try { - bad = UUID.parseString("F4F00409-CEF8-4822-802CD-EB20704C365"); - fail("Expecting UUIDFormatException -- wrong format"); - } - catch (UUIDFormatException iea) { - //Expected - } - } - - /** - * Test the toString of UUID - * - * @throws Exception an exception while testing - */ - public void testToString() throws Exception { - assertEquals( - (new UUID("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")).toString(), - "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"); - } - - /** - * Test the toUrn method - * - * @throws Exception an exception while testing - */ - public void testToUrn() throws Exception { - assertEquals( - (new UUID("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")).toUrn(), - "urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6"); - } - - /** - * Test the toBigInteger method - * - * @throws Exception an exception while testing - */ - public void testToBigInteger() throws Exception { - BigInteger bigInt = - new BigInteger("f81d4fae7dec11d0a76500a0c91e6bf6", UUID.BASE16); - assertEquals((new UUID(bigInt)).toBigInteger(), bigInt); - } - - /** - * Test the contract of Object.clone - * - * @throws Exception an exception while testing - */ - public void testClone() throws Exception { - UUID x = new UUID(); - assertTrue(x.clone() != x); - assertTrue(x.clone().getClass() == x.getClass()); - assertTrue(x.clone().equals(x)); - } -} +/* + * Copyright 2004 The Apache Software Foundation. + * + * 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.commons.id.uuid; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; + +import org.apache.commons.codec.binary.Hex; + +/** + * Unit tests for [EMAIL PROTECTED] UUID}. + * + * @version $Revision$ $Date$ + * @author Commons-id team + */ +public class UUIDTest extends TestCase { + + /** + * Constructor for test + * + * @param name String name of the test + */ + public UUIDTest(String name) { + super(name); + } + + /** + * Main application method + * + * @param args String arguments array + */ + public static void main(String[] args) { + TestRunner.run(suite()); + } + + /** @see junit.framework.TestCase#suite() */ + + public static Test suite() { + TestSuite suite = new TestSuite(UUIDTest.class); + suite.setName("UUID Tests"); + return suite; + } + + //------------------------------------------------------------------------- + /** @see junit.framework.TestCase#setUp() */ + protected void setUp() throws Exception { + super.setUp(); + } + + /** + * Test the nil UUID() contructor returns a zero'd out UUID + * + * @throws Exception an exception while testing + */ + public void testNiltoString() throws Exception { + assertEquals( + (new UUID()).toString(), + "00000000-0000-0000-0000-000000000000"); + } + + /** + * Test copy constructor + * + * @throws Exception an exception while testing + */ + public void testCopyConstructor() throws Exception { + UUID uuidFrom = new UUID("B4F00409-CEF8-4822-802C-DEB20704C365"); + UUID uuidTo = new UUID(uuidFrom); + + //Assert all equals and output methods match + assertTrue(uuidFrom.equals(uuidTo)); + assertTrue(uuidTo.equals(uuidFrom)); + assertEquals(uuidTo.toString(), uuidFrom.toString()); + assertTrue(Bytes.areEqual(uuidTo.getRawBytes(), uuidFrom.getRawBytes())); + assertEquals(uuidTo.hashCode(), uuidFrom.hashCode()); + } + + /** + * Test byte[] constructor + * + * @throws Exception an exception while testing + */ + public void testByteArrayConstructor() throws Exception { + String uuidString = "B4F00409CEF84822802CDEB20704C365".toLowerCase(); + String uuidFString = "B4F00409-CEF8-4822-802C-DEB20704C365".toLowerCase(); + byte[] bytes = Hex.decodeHex(uuidString.toCharArray()); + UUID uuid = new UUID(bytes); + + //Assert all equals and output methods match + assertTrue(uuid.equals(new UUID(uuidFString))); + assertEquals(uuidFString, uuid.toString()); + assertTrue(Bytes.areEqual(bytes, uuid.getRawBytes())); + } + + /** + * Test DataInput constructor + * + * @throws Exception an exception while testing + */ + public void testDataInputConstructor() throws Exception { + String uuidString = "B4F00409CEF84822802CDEB20704C365".toLowerCase(); + String uuidFString = "B4F00409-CEF8-4822-802C-DEB20704C365".toLowerCase(); + byte[] bytes = Hex.decodeHex(uuidString.toCharArray()); + //Set DataInput + ByteArrayOutputStream bytz = new ByteArrayOutputStream(Constants.UUID_BYTE_LENGTH); + DataOutputStream dos = new DataOutputStream(bytz); + dos.write(bytes); + ByteArrayInputStream bas = new ByteArrayInputStream(bytz.toByteArray()); + DataInput di = new DataInputStream(bas); + + UUID uuid = new UUID(di); + + //Assert all equals and output methods match + assertTrue(uuid.equals(new UUID(uuidFString))); + assertEquals(uuidFString, uuid.toString()); + assertTrue(Bytes.areEqual(bytes, uuid.getRawBytes())); + + //Close all + dos.close(); + bas.close(); + + //Test a too short DataInput + byte[] sBytes = Hex.decodeHex(uuidString.substring(2).toCharArray()); + //Set DataInput + bytz = new ByteArrayOutputStream(); + dos = new DataOutputStream(bytz); + dos.write(sBytes); + bas = new ByteArrayInputStream(bytz.toByteArray()); + di = new DataInputStream(bas); + + try { + UUID sUuid = new UUID(di); + fail(); + } catch (IOException ioe) { + //Expected + } + + //Close all + dos.close(); + bas.close(); + + } + + /** + * Test long long constructor + * + * @throws Exception an exception while testing + */ + public void testLongLongConstructor() throws Exception { + String uuidFString = "B4F00409-CEF8-4822-802C-DEB20704C365".toLowerCase(); + long mostSig = 0xB4F00409CEF84822L; + long leastSig = 0x802CDEB20704C365L; + + UUID uuid = new UUID(mostSig, leastSig); + + //Assert all equals and output methods match + assertTrue(uuid.equals(new UUID(uuidFString))); + assertEquals(uuidFString, uuid.toString()); + } + + /** + * Test String constructor + * + * @throws Exception an exception while testing + */ + public void testStringConstructor() throws Exception { + UUID uuidFrom = new UUID("B4F00409-CEF8-4822-802C-DEB20704C365"); + UUID uuidTo = new UUID(uuidFrom.getRawBytes()); + + //Assert all equals and output methods match + assertTrue(uuidFrom.equals(uuidTo)); + assertTrue(uuidTo.equals(uuidFrom)); + assertEquals(uuidTo.toString(), uuidFrom.toString()); + assertTrue(Bytes.areEqual(uuidTo.getRawBytes(), uuidFrom.getRawBytes())); + assertEquals(uuidTo.hashCode(), uuidFrom.hashCode()); + + //Test constructing prefixed strings + UUID fromPrefixed = + new UUID("urn:uuid:B4F00409-CEF8-4822-802C-DEB20704C365"); + assertTrue(uuidFrom.equals(fromPrefixed)); + + //Test constructing prefixed strings + UUID fromShortPrefixed = + new UUID("uuid:B4F00409-CEF8-4822-802C-DEB20704C365"); + assertTrue(uuidFrom.equals(fromPrefixed)); + + //Test IllegalArgumentException in Construction + UUID badConstruction = null; + + //try with not valid hexidecimal + try { + badConstruction = new UUID("G4F00409-CEF8-4822-802C-DEB20704C365"); + fail("Expecting UUIDFormatException -- invalid hex string"); + } catch (UUIDFormatException iea) { + //Expected + } + + //try with too long a string + try { + badConstruction = new UUID("FF4F00409-CEF8-4822-802C-DEB20704C365"); + fail("Expecting UUIDFormatException -- string too long"); + } catch (UUIDFormatException iea) { + //Expected + } + + //try with too short a string + try { + badConstruction = new UUID("4F00409-CEF8-4822-802C-DEB20704C365"); + fail("Expecting UUIDFormatException -- string too short"); + } catch (UUIDFormatException iea) { + //Expected + } + + //try right string wrong order / format + try { + badConstruction = new UUID("F4F00409-CEF8-4822-802CD-EB20704C365"); + fail("Expecting UUIDFormatException -- wrong format"); + } catch (UUIDFormatException iea) { + //Expected + } + } + + /** + * Test the static fromString method + * + * @throws Exception an exception while testing + */ + public void testFromString() throws Exception { + UUID baseline = new UUID("B4F00409-CEF8-4822-802C-DEB20704C365"); + assertEquals( + baseline, + UUID.fromString("urn:uuid:B4F00409-CEF8-4822-802C-DEB20704C365")); + assertEquals( + baseline, + UUID.fromString("uuid:B4F00409-CEF8-4822-802C-DEB20704C365")); + //High value test + String in = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"; + UUID high = UUID.fromString(in); + assertTrue(high.toString().equals(in.toLowerCase())); + + //Negative testing + //try with not valid hexidecimal + UUID bad = null; + try { + bad = UUID.fromString("G4F00409-CEF8-4822-802C-DEB20704C365"); + fail("Expecting UUIDFormatException -- invalid hex"); + } catch (UUIDFormatException iea) { + //Expected + } + + //try with too long a string + try { + bad = UUID.fromString("FF4F00409-CEF8-4822-802C-DEB20704C365"); + fail("Expecting UUIDFormatException -- string too long"); + } catch (UUIDFormatException iea) { + //Expected + } + + //try with too short a string + try { + bad = UUID.fromString("4F00409-CEF8-4822-802C-DEB20704C365"); + fail("Expecting UUIDFormatException -- string too short"); + } catch (UUIDFormatException iea) { + //Expected + } + + //try right string wrong order / format + try { + bad = UUID.fromString("F4F00409-CEF8-4822-802CD-EB20704C365"); + fail("Expecting UUIDFormatException -- wrong format"); + } catch (UUIDFormatException iea) { + //Expected + } + } + + /** + * Test the toString of UUID + * + * @throws Exception an exception while testing + */ + public void testToString() throws Exception { + assertEquals( + (new UUID("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")).toString(), + "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"); + assertEquals( + (new UUID("00000000-7dec-11d0-a765-00a0c91e6bf6")).toString(), + "00000000-7dec-11d0-a765-00a0c91e6bf6"); + } + + /** + * Test the toUrn method + * + * @throws Exception an exception while testing + */ + public void testToUrn() throws Exception { + assertEquals( + (new UUID("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")).toUrn(), + "urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6"); + } + + /** + * <p>Test compareTo of Comparable interface impl.</p> + * + * @throws Exception a testing Exception. + */ + public void testCompareTo() throws Exception { + UUID baseline = new UUID("ffffffff-ffff-ffef-ffff-ffffffffffff"); + UUID less = new UUID("ffffffff-ffff-ffdf-ffff-ffffffffffff"); + UUID same = new UUID("ffffffff-ffff-ffef-ffff-ffffffffffff"); + UUID more = new UUID("ffffffff-ffff-ffff-ffff-ffffffffffff"); + assertEquals(+1, baseline.compareTo(less)); + assertEquals(0, baseline.compareTo(same)); + assertEquals(-1, baseline.compareTo(more)); + } + + /** + * <p>Test the clockSequence() method.</p> + * @throws Exception a testing Exception. + */ + public void testClockSequence() throws Exception { + //Test against known value + UUID test = new UUID("c079ef59-f5b1-1801-a348-c38429e61be7"); + short val = 9032; + assertEquals(val, test.clockSequence()); + } + + /** + * <p>Test the version method.</p> + * + * @throws Exception a testing Exception. + */ + public void testVersion() throws Exception { + UUID v1 = new UUID("3051a8d7-aea7-1801-e0bf-bc539dd60cf3"); //Version one 0x18 = 0001 1000 + UUID v2 = new UUID("3051a8d7-aea7-2801-e0bf-bc539dd60cf3"); //Version two 0x28 = 0010 1000 + UUID v3 = new UUID("3051a8d7-aea7-3801-e0bf-bc539dd60cf3"); //Version three 0x38 = 0011 1000 + UUID v4 = new UUID("3051a8d7-aea7-4801-e0bf-bc539dd60cf3"); //Version four 0x48 = 0100 1000 + assertEquals(UUID.VERSION_ONE, v1.version()); + assertEquals(UUID.VERSION_TWO, v2.version()); + assertEquals(UUID.VERSION_THREE, v3.version()); + assertEquals(UUID.VERSION_FOUR, v4.version()); + } + + /** + * <p>Test the variant method.</p> + * + * @throws Exception a testing Exception. + */ + public void testVariant() throws Exception { + UUID testVariant0 = new UUID("d0e817e1-e4b1-1801-3fe6-b4b60ccecf9d"); + UUID testVariant2 = new UUID("d0e817e1-e4b1-1801-bfe6-b4b60ccecf9d"); + UUID testVariant6 = new UUID("d0e817e1-e4b1-1801-dfe6-b4b60ccecf9d"); + UUID testVariant7 = new UUID("d0e817e1-e4b1-1801-ffe6-b4b60ccecf9d"); + + assertEquals(UUID.VARIANT_NCS_COMPAT, testVariant0.variant()); + assertEquals(UUID.VARIANT_IETF_DRAFT, testVariant2.variant()); + assertEquals(UUID.VARIANT_MS, testVariant6.variant()); + assertEquals(UUID.VARIANT_FUTURE, testVariant7.variant()); + } + + /** + * <p>Test the timestamp method.</p> + * + * @throws Exception a testing Exception. + */ + public void testTimestamp() throws Exception { + //Test against known value + UUID test = new UUID("f8636b90-b207-11d8-b231-e33c9df047ca"); + long val = 133051936309210000L; + assertEquals(val, test.timestamp()); + } + + /** + * <p>Test the node method.</p> + * + * @throws Exception a testing Exception. + */ + public void testNode() throws Exception { + //Test against known value + UUID test = new UUID("547fa190-b209-11d8-bc4e-95ef8f69921e"); + long val = 164856135782942L; + assertEquals(val, test.node()); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
