commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons-sandbox/id/src/test/org/apache/commons/id/uuid UUIDTest.java
Date Sun, 11 Jan 2004 09:16:20 GMT
psteitz     2004/01/11 01:16:20

  Added:       id/src/java/org/apache/commons/id/uuid UUID.java
                        UUIDFormatException.java
               id/src/test/org/apache/commons/id/uuid UUIDTest.java
  Log:
  Added UUID, UUIDFormatException and associated tests.
  Contributed by: Tim Reilly
  Pr: #26035
  Reviewed by: Phil Steitz
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/UUID.java
  
  Index: UUID.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/UUID.java,v
1.1 2004/01/11 09:16:20 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2004 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.id.uuid;
  
  import java.io.Serializable;
  import java.math.BigInteger;
  import java.util.StringTokenizer;
  
  /**
   * <p><code>UUID</code> represents a Universally Unique Identifier per
IETF
   * Draft specification. For more information regarding the IETF Draft UUID
   * specification</p>
   *
   * <p>See: http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-01.txt</p>
   *
   * @version $Revision: 1.1 $ $Date: 2004/01/11 09:16:20 $
   *
   */
  
  public class UUID implements Serializable, Cloneable {
  
      /** Bits in a UUID */
      public static final int BIT_LENGTH = 128;
  
      /** Chars in a UUID String */
      public static final int RAW_CHAR_LENGTH = 32;
  
      /** Chars in a UUID String */
      public static final int CHAR_LENGTH = 36;
  
      /** Token length of '-' separated tokens */
      public static final int TOKENS_IN_UUID = 5;
  
      /** The string prefix for a urn UUID identifier */
      public static final String URN_PREFIX = "urn:uuid:";
  
      /** Base for hexidecimal used in parse and format */
      public static final int BASE16 = 16;
  
      /** Array to check tokenized UUID's segment lengths */
      private static final int[] TOKEN_LENGTHS = {8, 4, 4, 4, 12};
  
      /** Insertion point 1 for dashes in the string format */
      private static final int FORMAT_POSITION1 = 8;
  
      /** Insertion point 2 for dashes in the string format */
      private static final int FORMAT_POSITION2 = 13;
  
      /** Insertion point 3 for dashes in the string format */
      private static final int FORMAT_POSITION3 = 18;
  
      /** Insertion point 4 for dashes in the string format */
      private static final int FORMAT_POSITION4 = 23;
  
      /** byte array to store 128-bits composing this UUID */
      private BigInteger numberValue = null;
  
      /** Holds the internal string value of the UUID */
      private String stringValue = null;
  
      /** Constructs a nil UUID */
      public UUID() {
          super();
          numberValue = new BigInteger("0");
      }
  
      /**
       * <p>Copy constructor.</p>
       *
       * @param copyFrom the UUID to copy to create this UUID.
       * @throws IllegalArgumentException argument must be 128 bit UUID.
       */
      public UUID(UUID copyFrom) throws IllegalArgumentException {
          super();
          this.numberValue =
              new BigInteger(copyFrom.toBigInteger().toByteArray());
          this.stringValue = copyFrom.toString();
      }
  
      /**
       * <p>Constructs a UUID from a 128 bit java.math.BigInteger.</p>
       *
       *  @param bigIntValue the 128 bit BigInteger to construct this UUID from.
       *  @throws IllegalArgumentException argument must be 128 bit
       */
      public UUID(BigInteger bigIntValue) throws IllegalArgumentException {
          super();
          if ((bigIntValue.bitLength() > UUID.BIT_LENGTH) || (bigIntValue.signum() <
0) ) {
              throw new IllegalArgumentException("UUID must be contructed using a 128 bit
BigInteger");
          }
          numberValue = bigIntValue;
      }
  
      /**
       * <p>Constructs a UUID from a UUID formatted String.</p>
       *
       *  @param uuidString the String representing a UUID to construct this UUID
       *  @throws UUIDFormatException String must be a properly formatted UUID string
       */
      public UUID(String uuidString) throws UUIDFormatException {
          //Calls the copy constructor
          this(UUID.parseString(uuidString));
      }
  
      /**
       *  <p>Parses a string for a UUID.</p>
       *
       *  @param uuidString the UUID formatted String to parse.
       *  @throws UUIDFormatException the String must be a properly formatted UUID String.
       *  @return Returns a UUID or null if the formatted string could not be parsed.
       */
      public static UUID parseString(String uuidString)
          throws UUIDFormatException {
          String leanString = uuidString.toLowerCase();
          UUID tmpUUID = null;
  
          //Handle prefixed UUIDs
          // e.g. urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6
          int pos = uuidString.lastIndexOf(":");
          if (pos > 1) {
              leanString = uuidString.substring(++pos, uuidString.length());
          }
  
          //Check for 36 char length
          if (leanString.length() != CHAR_LENGTH) {
              throw new UUIDFormatException();
          }
  
          //Check for 5 fields
          StringTokenizer tok = new StringTokenizer(leanString, "-");
          if ( tok.countTokens() != TOKENS_IN_UUID ) {
              throw new UUIDFormatException();
          }
  
          //Remove the "-" from the formatted string and test token sizes
          StringBuffer buf = new StringBuffer();
          String token = null;
          int count = 0;
          while (tok.hasMoreTokens()) {
              token = tok.nextToken();
              if (token.length() != TOKEN_LENGTHS[count++]) {
                  throw new UUIDFormatException();
              }
              buf.append(token);
          }
  
          //Test creating a BigInteger from the hex value
          BigInteger bigIntValue = null;
          try {
              bigIntValue = new BigInteger(buf.toString(), BASE16);
          }
          catch (NumberFormatException nfe) {
                throw new UUIDFormatException();
         }
  
          //Construct the UUID to return
          tmpUUID = new UUID(bigIntValue);
          // Set the uuidString value - the stringValue is "lazy" so set it now.
          tmpUUID.stringValue = leanString;
  
          return tmpUUID;
      }
  
      /**
       * <p>Returns a string representation of the UUID.</p>
       *
       * @return a string representation of the UUID formatted according to the specification.
       */
      public String toString() {
          //set string value if not set
          if (stringValue == null) {
              StringBuffer buf = new StringBuffer(numberValue.toString(BASE16));
              while (buf.length() != RAW_CHAR_LENGTH) {
                  buf.insert(0, "0");
              }
  
              buf.insert(FORMAT_POSITION1, '-');
              buf.insert(FORMAT_POSITION2, '-');
              buf.insert(FORMAT_POSITION3, '-');
              buf.insert(FORMAT_POSITION4, '-');
              stringValue = buf.toString();
          }
          return stringValue;
      }
  
      /**
       * <p>Returns a urn representation of the UUID. This is same as the
       * toString() value prefixed with <code>urn:uuid:</code></p>
       *
       * @return Returns the urn string representation of the UUID
       */
      public String toUrn() {
          if (numberValue == null) {
              return null;
          }
          return URN_PREFIX + this.toString();
      }
  
      /**
       * <p>Returns the BigInteger representation of this UUID.</p>
       *
       * @return returns a BigInteger representing the value of this UUID
       */
      public BigInteger toBigInteger() {
          return numberValue;
      }
  
      /**
       * <p>Compares two UUID for equality.</p>
       *
       * @see java.lang.Object#equals(Object)
       */
  
      public boolean equals(Object obj) {
          if (!(obj instanceof UUID)) {
              return false;
          }
          return ((UUID) obj).toBigInteger().equals(this.numberValue);
      }
  
      /**
       * <p>Returns a hash code value for the object.</p>
       *
       * @see java.lang.Object#hashCode()
       */
      public int hashCode() {
         return this.numberValue.hashCode();
      }
  
      /**
       * </p>Clone method returns a copy of this UUID.</p>
       *
       * @see java.lang.Object#clone()
       */
      public Object clone() {
          return new UUID(this);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/UUIDFormatException.java
  
  Index: UUIDFormatException.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/UUIDFormatException.java,v
1.1 2004/01/11 09:16:20 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2004 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.id.uuid;
  
  /**
   * <p>Thrown to indicate an attempted to convert a string to a UUID, but that the
   * string does not have the appropriate format.</p>
   *
   * @see     UUID#toString()
   * @see     UUID#parseString(String)
   * @see     UUID#UUID(String)
   *
   * @version $Revision: 1.1 $ $Date: 2004/01/11 09:16:20 $
   *
   */
  
  public class UUIDFormatException extends Exception {
  
      /**
       * <p>Constructs a <code>UUIDFormatException</code> with no message.</p>
       */
      public UUIDFormatException() {
          super();
      }
  
      /**
       * </p>Constructs a <code>UUIDFormatException</code> with a message.</p>
       *
       * @param msg the String message for this exception.
       */
      public UUIDFormatException(String msg) {
          super(msg);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/id/src/test/org/apache/commons/id/uuid/UUIDTest.java
  
  Index: UUIDTest.java
  ===================================================================
  /*
   *===================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2004 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  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 {@link UUID}.
   *
   * @version $Revision: 1.1 $ $Date: 2004/01/11 09:16:20 $
   * @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", 16);
          try {
              BigInteger tooBig = maxUUIDInteger.add(BigInteger.ONE);
              badConstruction = new UUID(tooBig);
              fail("Expecting IllegalArgumentException -- BigInteger too big.");
          }
          catch (IllegalArgumentException iea) {
              // expected
          }
          try {
              BigInteger tooSmall = new BigInteger("-1");
              badConstruction = new UUID(tooSmall);
              fail("Expecting IllegalArgumentException -- BigInteger too small.");
          }
          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) {}
          
          //try with too long a string
          try {
              badConstruction = new UUID("FF4F00409-CEF8-4822-802C-DEB20704C365");
              fail("Expecting UUIDFormatException -- string too long");
          }
          catch (UUIDFormatException iea) {}
      
          //try with too short a string
          try {
              badConstruction = new UUID("4F00409-CEF8-4822-802C-DEB20704C365");
              fail("Expecting UUIDFormatException -- string too short");
          }
          catch (UUIDFormatException iea) {}
          
          //try right string wrong order / format
          try {
              badConstruction = new UUID("F4F00409-CEF8-4822-802CD-EB20704C365");
              fail("Expecting UUIDFormatException -- wrong format");
          }
          catch (UUIDFormatException iea) {}
      }
  
      /**
       * 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"));
  
          //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) {}
          
          //try with too long a string
          try {
              bad = UUID.parseString("FF4F00409-CEF8-4822-802C-DEB20704C365");
              fail("Expecting UUIDFormatException -- string too long");
          }
          catch (UUIDFormatException iea) {}
          
          //try with too short a string
          try {
              bad = UUID.parseString("4F00409-CEF8-4822-802C-DEB20704C365");
              fail("Expecting UUIDFormatException -- string too short");
          }
          catch (UUIDFormatException iea) {}
          
          //try right string wrong order / format
          try {
              bad = UUID.parseString("F4F00409-CEF8-4822-802CD-EB20704C365");
              fail("Expecting UUIDFormatException -- wrong format");
          }
          catch (UUIDFormatException iea) {}
      }
  
      /**
       * 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", 16);
          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));
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message