From 14afae193e91dad61180ab23ffbd2bc39dcfec2d Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Wed, 18 Sep 2013 03:46:05 +0000 Subject: [PATCH] add some oleblob unit tests git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@805 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/util/OleBlob.java | 8 +- .../jackcess/DatabaseTest.java | 17 ++- .../jackcess/util/OleBlobTest.java | 136 ++++++++++++++++++ 3 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java diff --git a/src/main/java/com/healthmarketscience/jackcess/util/OleBlob.java b/src/main/java/com/healthmarketscience/jackcess/util/OleBlob.java index 529ea0e..bcc807e 100644 --- a/src/main/java/com/healthmarketscience/jackcess/util/OleBlob.java +++ b/src/main/java/com/healthmarketscience/jackcess/util/OleBlob.java @@ -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; diff --git a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java index 3149f32..a21fb0b 100644 --- a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -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 index 0000000..c8ea778 --- /dev/null +++ b/src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java @@ -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(); + } + } +} -- 2.39.5