poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kiwiwi...@apache.org
Subject svn commit: r1874989 - in /poi/trunk/src: java/org/apache/poi/hpsf/ClassID.java testcases/org/apache/poi/hpsf/basic/TestClassID.java
Date Sun, 08 Mar 2020 23:20:45 GMT
Author: kiwiwings
Date: Sun Mar  8 23:20:44 2020
New Revision: 1874989

URL: http://svn.apache.org/viewvc?rev=1874989&view=rev
Log:
Get UUID from ClassID

Modified:
    poi/trunk/src/java/org/apache/poi/hpsf/ClassID.java
    poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestClassID.java

Modified: poi/trunk/src/java/org/apache/poi/hpsf/ClassID.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hpsf/ClassID.java?rev=1874989&r1=1874988&r2=1874989&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hpsf/ClassID.java (original)
+++ poi/trunk/src/java/org/apache/poi/hpsf/ClassID.java Sun Mar  8 23:20:44 2020
@@ -17,9 +17,11 @@
 
 package org.apache.poi.hpsf;
 
+import java.nio.ByteBuffer;
 import java.util.Arrays;
+import java.util.Locale;
+import java.util.UUID;
 
-import org.apache.commons.codec.binary.Hex;
 import org.apache.poi.common.Duplicatable;
 import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
@@ -184,6 +186,7 @@ public class ClassID implements Duplicat
      * @param offset The offset within the {@code src} byte array
      * @return A byte array containing the class ID.
      */
+    @SuppressWarnings("PointlessArithmeticExpression")
     public byte[] read(final byte[] src, final int offset) {
         /* Read double word. */
         bytes[0] = src[3 + offset];
@@ -215,6 +218,7 @@ public class ClassID implements Duplicat
      * @exception ArrayStoreException if there is not enough room for the class
      * ID 16 bytes in the byte array after the {@code offset} position.
      */
+    @SuppressWarnings("PointlessArithmeticExpression")
     public void write(final byte[] dst, final int offset)
     throws ArrayStoreException {
         /* Check array size: */
@@ -310,14 +314,32 @@ public class ClassID implements Duplicat
      */
     @Override
     public String toString() {
-        String hex = Hex.encodeHexString(bytes, false);
-        return  "{" + hex.substring(0,8) +
-                "-" + hex.substring(8,12) +
-                "-" + hex.substring(12,16) +
-                "-" + hex.substring(16,20) +
-                "-" + hex.substring(20) + "}";
+        return "{" + toUUIDString() + "}";
     }
 
+    /**
+     * Returns a human-readable representation of the Class ID in UUID
+     * format {@code "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}.
+     *
+     * @return UUID String representation of the Class ID represented by this object.
+     */
+    public String toUUIDString() {
+        return toUUID().toString().toUpperCase(Locale.ROOT);
+    }
+
+    /**
+     * Converts the ClassID to an UUID
+     * @return the ClassID as UUID
+     *
+     * @since POI 4.1.3
+     */
+    public UUID toUUID() {
+        final long mostSigBits = ByteBuffer.wrap(bytes, 0, 8).getLong();
+        final long leastSigBits = ByteBuffer.wrap(bytes, 8, 8).getLong();
+        return new UUID(mostSigBits, leastSigBits);
+    }
+
+
     @Override
     public ClassID copy() {
         return new ClassID(this);

Modified: poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestClassID.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestClassID.java?rev=1874989&r1=1874988&r2=1874989&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestClassID.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestClassID.java Sun Mar  8 23:20:44
2020
@@ -17,113 +17,67 @@
 
 package org.apache.poi.hpsf.basic;
 
-import java.util.Locale;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 
 import org.apache.poi.hpsf.ClassID;
-import org.apache.poi.hpsf.DocumentSummaryInformation;
-import org.apache.poi.hpsf.PropertySet;
-import org.apache.poi.hpsf.SummaryInformation;
+import org.apache.poi.hpsf.ClassIDPredefined;
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertTrue;
-
 /**
- * <p>Tests ClassID structure.</p>
- *
- * @author Michael Zalewski (zalewski@optonline.net)
+ * Tests ClassID structure.
  */
 public final class TestClassID {
 
+    private static final byte[] BUF16 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
+
     /**
      * Various tests of overridden .equals()
      */
     @Test
     public void testEquals() {
-        ClassID clsidTest1 = new ClassID(
-              new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
-                          0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}
-            , 0
-        );
-        ClassID clsidTest2 = new ClassID(
-              new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
-                          0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}
-            , 0
-        );
-        ClassID clsidTest3 = new ClassID(
-              new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
-                          0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 }
-            , 0
-        );
+        ClassID clsidTest1 = new ClassID(BUF16, 0);
+        ClassID clsidTest2 = new ClassID(BUF16, 0);
+        byte[] buf2 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,17};
+        ClassID clsidTest3 = new ClassID(buf2, 0);
         assertEquals(clsidTest1, clsidTest1);
         assertEquals(clsidTest1, clsidTest2);
         assertNotEquals(clsidTest1, clsidTest3);
         assertNotEquals(null, clsidTest1);
     }
-    
+
     /**
      * Try to write to a buffer that is too small. This should
      *   throw an Exception
      */
+    @Test(expected = ArrayStoreException.class)
+    public void testWriteArrayStoreException1() {
+        new ClassID(BUF16, 0).write(new byte[15], 0);
+    }
+
+    @Test(expected = ArrayIndexOutOfBoundsException.class)
+    public void testWriteArrayStoreException2() {
+        new ClassID(BUF16, 0).write(new byte[16], 1);
+    }
+
     @Test
-    public void testWriteArrayStoreException() {
-        ClassID clsidTest = new ClassID(
-              new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
-                          0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}
-            , 0
-        );
-        boolean bExceptionOccurred = false;
-        try
-        {
-            clsidTest.write(new byte[15], 0);
-        }
-        catch (Exception e)
-        {
-            bExceptionOccurred = true;
-        }
-        assertTrue(bExceptionOccurred);
-
-        bExceptionOccurred = false;
-        try
-        {
-            clsidTest.write(new byte[16], 1);
-        }
-        catch (Exception e)
-        {
-            bExceptionOccurred = true;
-        }
-        assertTrue(bExceptionOccurred);
-
-        // These should work without throwing an Exception
-        bExceptionOccurred = false;
-        try
-        {
-            clsidTest.write(new byte[16], 0);
-            clsidTest.write(new byte[17], 1);
-        }
-        catch (Exception e)
-        {
-            bExceptionOccurred = true;
-        }
-        assertFalse(bExceptionOccurred);
+    public void testWriteArrayStoreException3() {
+        ClassID clsidTest = new ClassID(BUF16, 0);
+        clsidTest.write(new byte[16], 0);
+        clsidTest.write(new byte[17], 1);
     }
 
-    /**
-     * <p>Tests the {@link PropertySet} methods. The test file has two
-     * property set: the first one is a {@link SummaryInformation},
-     * the second one is a {@link DocumentSummaryInformation}.</p>
-     */
     @Test
     public void testClassID() {
-        ClassID clsidTest = new ClassID(
-              new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
-                          0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}
-            , 0
-        );
-        assertEquals(clsidTest.toString().toUpperCase(Locale.ROOT),
-                            "{04030201-0605-0807-090A-0B0C0D0E0F10}"
-        );
+        ClassID clsidTest = new ClassID(BUF16, 0);
+        assertEquals("{04030201-0605-0807-090A-0B0C0D0E0F10}", clsidTest.toString());
+    }
+
+    @Test
+    public void checkUUIDConversion() {
+        String exp = "EABCECDB-CC1C-4A6F-B4E3-7F888A5ADFC8";
+        ClassID clsId = ClassIDPredefined.EXCEL_V14_ODS.getClassID();
+        assertEquals(exp, clsId.toUUIDString());
+        assertEquals(exp, clsId.toUUID().toString().toUpperCase());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message