diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2013-09-18 03:46:05 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2013-09-18 03:46:05 +0000 |
commit | 14afae193e91dad61180ab23ffbd2bc39dcfec2d (patch) | |
tree | 7ff4eef57b02c313f8896a803efaa6859a461763 /src/test | |
parent | 8b8fca0fce8aed540eccf979955493aa52db3664 (diff) | |
download | jackcess-14afae193e91dad61180ab23ffbd2bc39dcfec2d.tar.gz jackcess-14afae193e91dad61180ab23ffbd2bc39dcfec2d.zip |
add some oleblob unit tests
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@805 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java | 17 | ||||
-rw-r--r-- | src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java | 136 |
2 files changed, 148 insertions, 5 deletions
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"); @@ -1671,15 +1672,21 @@ 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(); + } + } +} |