diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2013-09-28 02:30:18 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2013-09-28 02:30:18 +0000 |
commit | 1281baf89cf29452741bafc1a85e897e61aa86ee (patch) | |
tree | fadd442b7d9b20cfcbdf30cfec8bd5f1a6d885a5 /src/test | |
parent | 2b3eb67f9319fe0c8c42281741d48b294b029459 (diff) | |
download | jackcess-1281baf89cf29452741bafc1a85e897e61aa86ee.tar.gz jackcess-1281baf89cf29452741bafc1a85e897e61aa86ee.zip |
rework compound content api; add more ole blob unit tests
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@810 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/test')
-rwxr-xr-x | src/test/data/V2007/testOleV2007.accdb | bin | 0 -> 8634368 bytes | |||
-rw-r--r-- | src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java | 3 | ||||
-rw-r--r-- | src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java | 154 |
3 files changed, 156 insertions, 1 deletions
diff --git a/src/test/data/V2007/testOleV2007.accdb b/src/test/data/V2007/testOleV2007.accdb Binary files differnew file mode 100755 index 0000000..398818e --- /dev/null +++ b/src/test/data/V2007/testOleV2007.accdb diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java index 8ff2232..c5028cc 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java @@ -46,7 +46,8 @@ public class JetFormatTest extends TestCase { PROMOTION("testPromotion"), COMPLEX("complexDataTest"), UNSUPPORTED("unsupportedFieldsTest"), - LINKED("linkerTest"); + LINKED("linkerTest"), + BLOB("testOle"); private final String _basename; diff --git a/src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java b/src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java index 1c3e104..b519664 100644 --- a/src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/util/OleBlobTest.java @@ -20,6 +20,8 @@ USA package com.healthmarketscience.jackcess.util; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.util.Arrays; import com.healthmarketscience.jackcess.ColumnBuilder; @@ -30,9 +32,15 @@ import static com.healthmarketscience.jackcess.DatabaseTest.*; import com.healthmarketscience.jackcess.Row; import com.healthmarketscience.jackcess.Table; import com.healthmarketscience.jackcess.TableBuilder; +import com.healthmarketscience.jackcess.complex.Attachment; +import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey; import com.healthmarketscience.jackcess.impl.ByteUtil; +import com.healthmarketscience.jackcess.impl.CompoundOleUtil; import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; import junit.framework.TestCase; +import org.apache.poi.poifs.filesystem.DocumentEntry; +import org.apache.poi.poifs.filesystem.DocumentInputStream; +import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; /** * @@ -149,4 +157,150 @@ public class OleBlobTest extends TestCase db.close(); } } + + public void testReadBlob() throws Exception + { + for(TestDB testDb : TestDB.getSupportedForBasename(Basename.BLOB, true)) { + Database db = open(testDb); + + Table t = db.getTable("Table1"); + + for(Row row : t) { + + OleBlob oleBlob = null; + try { + + String name = (String)row.get("name"); + oleBlob = OleBlob.Builder.fromInternalData((byte[])row.get("ole_data")); + OleBlob.Content content = oleBlob.getContent(); + Attachment attach = null; + if(content.getType() != OleBlob.ContentType.LINK) { + attach = ((ComplexValueForeignKey)row.get("attach_data")) + .getAttachments().get(0); + } + + switch(content.getType()) { + case LINK: + OleBlob.LinkContent lc = (OleBlob.LinkContent)content; + if("test_link".equals(name)) { + assertEquals("Z:\\jackcess_test\\ole\\test_data.txt", lc.getLinkPath()); + } else { + assertEquals("Z:\\jackcess_test\\ole\\test_datau2.txt", lc.getLinkPath()); + } + break; + + case SIMPLE_PACKAGE: + OleBlob.SimplePackageContent spc = (OleBlob.SimplePackageContent)content; + byte[] packageBytes = toByteArray(spc.getStream(), spc.length()); + assertTrue(Arrays.equals(attach.getFileData(), packageBytes)); + break; + + case COMPOUND_STORAGE: + OleBlob.CompoundContent cc = (OleBlob.CompoundContent)content; + if(cc.hasContentsEntry()) { + OleBlob.CompoundContent.Entry entry = cc.getContentsEntry(); + byte[] entryBytes = toByteArray(entry.getStream(), entry.length()); + assertTrue(Arrays.equals(attach.getFileData(), entryBytes)); + } else { + + if("test_word.doc".equals(name)) { + checkCompoundEntries(cc, + "/%02OlePres000", 466, + "/WordDocument", 4096, + "/%05SummaryInformation", 4096, + "/%05DocumentSummaryInformation", 4096, + "/%03AccessObjSiteData", 56, + "/%02OlePres001", 1620, + "/1Table", 6380, + "/%01CompObj", 114, + "/%01Ole", 20); + checkCompoundStorage(cc, attach); + } else if("test_excel.xls".equals(name)) { + checkCompoundEntries(cc, + "/%02OlePres000", 1326, + "/%03AccessObjSiteData", 56, + "/%05SummaryInformation", 200, + "/%05DocumentSummaryInformation", 264, + "/%02OlePres001", 4208, + "/%01CompObj", 107, + "/Workbook", 13040, + "/%01Ole", 20); + // the excel data seems to be modified when embedded as ole, + // so we can't reallly test it against the attachment data + } else { + throw new RuntimeException("unexpected compound entry " + name); + } + } + break; + + case OTHER: + OleBlob.OtherContent oc = (OleBlob.OtherContent)content; + byte[] otherBytes = toByteArray(oc.getStream(), oc.length()); + assertTrue(Arrays.equals(attach.getFileData(), otherBytes)); + break; + + default: + throw new RuntimeException("unexpected type " + content.getType()); + } + + } finally { + ByteUtil.closeQuietly(oleBlob); + } + } + + db.close(); + } + } + + private static void checkCompoundEntries(OleBlob.CompoundContent cc, + Object... entryInfo) + throws Exception + { + int idx = 0; + for(OleBlob.CompoundContent.Entry e : cc) { + String entryName = (String)entryInfo[idx]; + int entryLen = (Integer)entryInfo[idx + 1]; + + assertEquals(entryName, e.getName()); + assertEquals(entryLen, e.length()); + + idx += 2; + } + } + + private static void checkCompoundStorage(OleBlob.CompoundContent cc, + Attachment attach) + throws Exception + { + File tmpData = File.createTempFile("attach_", ".dat"); + + try { + FileOutputStream fout = new FileOutputStream(tmpData); + fout.write(attach.getFileData()); + fout.close(); + + NPOIFSFileSystem attachFs = new NPOIFSFileSystem(tmpData, true); + + for(OleBlob.CompoundContent.Entry e : cc) { + DocumentEntry attachE = null; + try { + attachE = CompoundOleUtil.getDocumentEntry(e.getName(), attachFs.getRoot()); + } catch(FileNotFoundException fnfe) { + // ignored, the ole data has extra entries + continue; + } + + byte[] attachEBytes = toByteArray(new DocumentInputStream(attachE), + attachE.getSize()); + byte[] entryBytes = toByteArray(e.getStream(), e.length()); + + assertTrue(Arrays.equals(attachEBytes, entryBytes)); + } + + ByteUtil.closeQuietly(attachFs); + + } finally { + tmpData.delete(); + } + } } |