]> source.dussan.org Git - jackcess.git/commitdiff
add some oleblob unit tests
authorJames Ahlborn <jtahlborn@yahoo.com>
Wed, 18 Sep 2013 03:46:05 +0000 (03:46 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Wed, 18 Sep 2013 03:46:05 +0000 (03:46 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@805 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/util/OleBlob.java
src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java
src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java [new file with mode: 0644]

index 529ea0e27412c2592d9539ddd32648f399165e04..bcc807ec1139d8c64d86e6a1d5c7354e2664cc9a 100644 (file)
@@ -331,7 +331,7 @@ public interface OleBlob extends Blob, Closeable
 
     public Builder setSimplePackage(File f) throws FileNotFoundException {
       _fileName = f.getName();
-      _filePath = f.getPath();
+      _filePath = f.getAbsolutePath();
       return setSimplePackageStream(new FileInputStream(f), f.length());
     }
 
@@ -351,7 +351,7 @@ public interface OleBlob extends Blob, Closeable
 
     public Builder setLink(File f) {
       _fileName = f.getName();
-      _filePath = f.getPath();
+      _filePath = f.getAbsolutePath();
       setDefaultPackageType();
       _type = ContentType.LINK;
       return this;
@@ -380,6 +380,10 @@ public interface OleBlob extends Blob, Closeable
       return this;
     }
 
+    public Builder setOther(File f) {
+      return setOtherStream(new FileInputStream(f), f.length());
+    }
+
     public Builder setPackagePrettyName(String prettyName) {
       _prettyName = prettyName;
       return this;
index 3149f3274bd2e8b6268dfa79fae38d10dc2aa744..a21fb0b1ab951d0ad8cd710193d607f9c45826d7 100644 (file)
@@ -77,7 +77,8 @@ import junit.framework.TestCase;
 /**
  * @author Tim McCune
  */
-public class DatabaseTest extends TestCase {
+public class DatabaseTest extends TestCase 
+{
 
   public static final TimeZone TEST_TZ =
     TimeZone.getTimeZone("America/New_York");
@@ -1670,16 +1671,22 @@ public class DatabaseTest extends TestCase {
 
   public static byte[] toByteArray(File file)
     throws IOException
+  {
+    return toByteArray(new FileInputStream(file), file.length());
+  }
+
+  public static byte[] toByteArray(InputStream in, long length)
+    throws IOException
   {
     // FIXME should really be using commons io IOUtils here, but don't want
     // to add dep for one simple test method
-    FileInputStream istream = new FileInputStream(file);
     try {
-      byte[] bytes = new byte[(int)file.length()];
-      istream.read(bytes);
+      DataInputStream din = new DataInputStream(in);
+      byte[] bytes = new byte[(int)length];
+      din.readFully(bytes);
       return bytes;
     } finally {
-      istream.close();
+      in.close();
     }
   }
 
diff --git a/src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java b/src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java
new file mode 100644 (file)
index 0000000..c8ea778
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+Copyright (c) 2013 James Ahlborn
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+USA
+*/
+
+package com.healthmarketscience.jackcess.util;
+
+import static com.healthmarketscience.jackcess.DatabaseTest.*;
+
+/**
+ *
+ * @author James Ahlborn
+ */
+public class OleBlobTest extends TestCase 
+{
+
+  public OleBlobTest(String name) {
+    super(name);
+  }
+
+  public void testCreateBlob() throws Exception
+  {
+    File sampleFile = new File("src/test/data/sample-input.tab");
+    String sampleFilePath = sampleFileStr.getAbsolutePath();
+    String sampleFileName = sampleFile.getName();
+    byte[] sampleFileBytes = toByteArray(sampleFile);
+
+    for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
+      Database db = create(fileFormat);
+
+      Table t = new TableBuilder("TestOle")
+        .addColumn(new ColumnBuilder("id", DataType.LONG))
+        .addColumn(new ColumnBuilder("ole", DataType.OLE))
+        .toTable(db);
+
+      OleBlob blob = null;
+      try {
+        blob = new OleBlob.Builder()
+          .setSimplePackage(sampleFile)
+          .toBlob();
+        t.addRow(1, blob);
+      } finally {
+        ByteUtil.closeQuietly(blob);
+      }
+      
+      try {
+        blob = new OleBlob.Builder()
+          .setLink(sampleFile)
+          .toBlob();
+        t.addRow(2, blob);
+      } finally {
+        ByteUtil.closeQuietly(blob);
+      }
+      
+      try {
+        blob = new OleBlob.Builder()
+          .setPackagePrettyName("Text File")
+          .setPackageClassName("Text.File")
+          .setPackageTypeName("TextFile")
+          .setOtherBytes(sampleFileBytes)
+          .toBlob();
+        t.addRow(3, blob);
+      } finally {
+        ByteUtil.closeQuietly(blob);
+      }
+
+      for(Row row : t) {
+        OleBlob blob = null;
+        try {
+          blob = OleBlob.Builder.fromInternalData(
+              (byte[])row.get("ole"));
+          Content content = blob.getContent();
+          assertSame(blob, content.getBlob());
+          assertSame(content, blob.getContent());
+
+          switch((Integer)row.get("id")) {
+          case 1:
+            assertEquals(OleBlob.ContentType.SIMPLE_PACKAGE, content.getType());
+            assertEquals(sampleFilePath, content.getFilePath());
+            assertEquals(sampleFilePath, content.getLocalFilePath());
+            assertEquals(sampleFileName, content.getFileName());
+            assertEquals(OleBlob.Builder.PACKAGE_PRETTY_NAME, 
+                         content.getPrettyName());
+            assertEquals(OleBlob.Builder.PACKAGE_TYPE_NAME, 
+                         content.getTypeName());
+            assertEquals(OleBlob.Builder.PACKAGE_TYPE_NAME, 
+                         content.getClassName());
+            assertEquals(sampleFileBytes.length, content.length());
+            assertEquals(sampleFileBytes, toByteArray(content.getStream()));
+            break;
+          case 2:
+            assertEquals(OleBlob.ContentType.LINK, content.getType());
+            assertEquals(sampleFilePath, content.getLinkPath());
+            assertEquals(sampleFilePath, content.getFilePath());
+            assertEquals(sampleFileName, content.getFileName());
+            assertEquals(OleBlob.Builder.PACKAGE_PRETTY_NAME, 
+                         content.getPrettyName());
+            assertEquals(OleBlob.Builder.PACKAGE_TYPE_NAME, 
+                         content.getTypeName());
+            assertEquals(OleBlob.Builder.PACKAGE_TYPE_NAME, 
+                         content.getClassName());
+            break;
+          case 3:
+            assertEquals(OleBlob.ContentType.OTHER, content.getType());
+            assertEquals("Text File", content.getPrettyName());
+            assertEquals("Text.File", content.getClassName());
+            assertEquals("TextFile", content.getTypeName());
+            assertEquals(sampleFileBytes.length, content.length());
+            assertEquals(sampleFileBytes, toByteArray(content.getStream()));
+            break;
+          default:
+            throw new RuntimeException("unexpected id " + row);
+          }
+        } finally {
+          ByteUtil.closeQuietly(oleBlob);
+        }
+      }
+
+      db.close();      
+    }    
+  }
+}