diff options
author | Nick Burch <nick@apache.org> | 2010-01-12 12:02:18 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2010-01-12 12:02:18 +0000 |
commit | 2a4805f9382e4a7f085a74fb0ff53fa9fc216113 (patch) | |
tree | 444bd034f1ca95e1861944b7c59c2d3fa49e275f /src/scratchpad/testcases/org/apache | |
parent | ababd504b546f114169b8ffde7a18632fc1a816c (diff) | |
download | poi-2a4805f9382e4a7f085a74fb0ff53fa9fc216113.tar.gz poi-2a4805f9382e4a7f085a74fb0ff53fa9fc216113.zip |
Improved how HSMF handles multiple recipients
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@898295 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad/testcases/org/apache')
5 files changed, 300 insertions, 10 deletions
diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/AllHSMFTests.java b/src/scratchpad/testcases/org/apache/poi/hsmf/AllHSMFTests.java index 80660aa05a..710d991d96 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/AllHSMFTests.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/AllHSMFTests.java @@ -21,6 +21,7 @@ import junit.framework.Test; import junit.framework.TestSuite; import org.apache.poi.hsmf.datatypes.*; +import org.apache.poi.hsmf.extractor.TestOutlookTextExtractor; import org.apache.poi.hsmf.parsers.*; public final class AllHSMFTests { @@ -34,7 +35,10 @@ public final class AllHSMFTests { suite.addTestSuite(TestChunkData.class); suite.addTestSuite(TestTypes.class); + suite.addTestSuite(TestSorters.class); + suite.addTestSuite(TestOutlookTextExtractor.class); + suite.addTestSuite(TestPOIFSChunkParser.class); return suite; diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java b/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java index 008a4edba4..25c793339f 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java @@ -52,8 +52,17 @@ public final class TestBasics extends TestCase { public void testRecipientEmail() throws Exception { assertEquals("travis@overwrittenstack.com", simple.getRecipientEmailAddress()); assertEquals("kevin.roast@alfresco.org", quick.getRecipientEmailAddress()); - assertEquals("randall.scarberry@pnl.gov", outlook30.getRecipientEmailAddress()); assertEquals("nicolas1.23456@free.fr", attachments.getRecipientEmailAddress()); + + // This one has lots... + assertEquals(18, outlook30.getRecipientEmailAddressList().length); + assertEquals("shawn.bohn@pnl.gov; gus.calapristi@pnl.gov; Richard.Carter@pnl.gov; " + + "barb.cheney@pnl.gov; nick.cramer@pnl.gov; vern.crow@pnl.gov; Laura.Curtis@pnl.gov; " + + "julie.dunkle@pnl.gov; david.gillen@pnl.gov; michelle@pnl.gov; Jereme.Haack@pnl.gov; " + + "Michelle.Hart@pnl.gov; ranata.johnson@pnl.gov; grant.nakamura@pnl.gov; " + + "debbie.payne@pnl.gov; stuart.rose@pnl.gov; randall.scarberry@pnl.gov; Leigh.Williams@pnl.gov", + outlook30.getRecipientEmailAddress() + ); } /** diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/datatypes/TestSorters.java b/src/scratchpad/testcases/org/apache/poi/hsmf/datatypes/TestSorters.java new file mode 100644 index 0000000000..815fb2d661 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/datatypes/TestSorters.java @@ -0,0 +1,97 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hsmf.datatypes; + +import java.util.Arrays; + +import org.apache.poi.hsmf.datatypes.AttachmentChunks.AttachmentChunksSorter; +import org.apache.poi.hsmf.datatypes.RecipientChunks.RecipientChunksSorter; + +import junit.framework.TestCase; + +/** + * Checks that the sorters on the chunk groups order + * chunks properly. + */ +public final class TestSorters extends TestCase { + public void testAttachmentChunksSorter() { + AttachmentChunks[] chunks; + + // Simple + chunks = new AttachmentChunks[] { + new AttachmentChunks("__attach_version1.0_#00000001"), + new AttachmentChunks("__attach_version1.0_#00000000"), + }; + Arrays.sort(chunks, new AttachmentChunksSorter()); + assertEquals("__attach_version1.0_#00000000", chunks[0].getPOIFSName()); + assertEquals("__attach_version1.0_#00000001", chunks[1].getPOIFSName()); + + // Lots, with gaps + chunks = new AttachmentChunks[] { + new AttachmentChunks("__attach_version1.0_#00000101"), + new AttachmentChunks("__attach_version1.0_#00000001"), + new AttachmentChunks("__attach_version1.0_#00000002"), + new AttachmentChunks("__attach_version1.0_#00000005"), + new AttachmentChunks("__attach_version1.0_#00000026"), + new AttachmentChunks("__attach_version1.0_#00000000"), + new AttachmentChunks("__attach_version1.0_#000000AB"), + }; + Arrays.sort(chunks, new AttachmentChunksSorter()); + assertEquals("__attach_version1.0_#00000000", chunks[0].getPOIFSName()); + assertEquals("__attach_version1.0_#00000001", chunks[1].getPOIFSName()); + assertEquals("__attach_version1.0_#00000002", chunks[2].getPOIFSName()); + assertEquals("__attach_version1.0_#00000005", chunks[3].getPOIFSName()); + assertEquals("__attach_version1.0_#00000026", chunks[4].getPOIFSName()); + assertEquals("__attach_version1.0_#000000AB", chunks[5].getPOIFSName()); + assertEquals("__attach_version1.0_#00000101", chunks[6].getPOIFSName()); + } + + public void testRecipientChunksSorter() { + RecipientChunks[] chunks; + + // Simple + chunks = new RecipientChunks[] { + new RecipientChunks("__recip_version1.0_#00000001"), + new RecipientChunks("__recip_version1.0_#00000000"), + }; + Arrays.sort(chunks, new RecipientChunksSorter()); + assertEquals(0, chunks[0].recipientNumber); + assertEquals(1, chunks[1].recipientNumber); + + // Lots, with gaps + chunks = new RecipientChunks[] { + new RecipientChunks("__recip_version1.0_#00020001"), + new RecipientChunks("__recip_version1.0_#000000FF"), + new RecipientChunks("__recip_version1.0_#00000205"), + new RecipientChunks("__recip_version1.0_#00000001"), + new RecipientChunks("__recip_version1.0_#00000005"), + new RecipientChunks("__recip_version1.0_#00000009"), + new RecipientChunks("__recip_version1.0_#00000404"), + new RecipientChunks("__recip_version1.0_#00000000"), + }; + Arrays.sort(chunks, new RecipientChunksSorter()); + assertEquals(0, chunks[0].recipientNumber); + assertEquals(1, chunks[1].recipientNumber); + assertEquals(5, chunks[2].recipientNumber); + assertEquals(9, chunks[3].recipientNumber); + assertEquals(0xFF, chunks[4].recipientNumber); + assertEquals(0x205, chunks[5].recipientNumber); + assertEquals(0x404, chunks[6].recipientNumber); + assertEquals(0x20001, chunks[7].recipientNumber); + } +} diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java b/src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java index b15bbc7249..e8c9dfdc63 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java @@ -53,7 +53,7 @@ public final class TestOutlookTextExtractor extends TestCase { String text = ext.getText(); assertContains(text, "From: Kevin Roast\n"); - assertContains(text, "To: Kevin Roast\n"); + assertContains(text, "To: Kevin Roast <kevin.roast@alfresco.org>\n"); assertEquals(-1, text.indexOf("CC:")); assertEquals(-1, text.indexOf("BCC:")); assertContains(text, "Subject: Test the content transformer\n"); @@ -92,4 +92,77 @@ public final class TestOutlookTextExtractor extends TestCase { assertEquals(inp, poifs); assertEquals(inp, mapi); } + + /** + * Test that we correctly handle multiple To+CC+BCC + * recipients in an email we sent. + */ + public void testSentWithMulipleRecipients() throws Exception { + // To: 'Ashutosh Dandavate' <ashutosh.dandavate@alfresco.com>, + // 'Paul Holmes-Higgin' <paul.hh@alfresco.com>, + // 'Mike Farman' <mikef@alfresco.com> + // Cc: nickb@alfresco.com, nick.burch@alfresco.com, + // 'Roy Wetherall' <roy.wetherall@alfresco.com> + // Bcc: 'David Caruana' <dave.caruana@alfresco.com>, + // 'Vonka Jan' <roy.wetherall@alfresco.com> + + String[] files = new String[] { + "example_sent_regular.msg", "example_sent_unicode.msg" + }; + for(String file : files) { + MAPIMessage msg = new MAPIMessage(new POIFSFileSystem( + new FileInputStream(samples.getFile(file)) + )); + + OutlookTextExtactor ext = new OutlookTextExtactor(msg); + String text = ext.getText(); + + assertContains(text, "From: Mike Farman\n"); + assertContains(text, "To: 'Ashutosh Dandavate' <ashutosh.dandavate@alfresco.com>; " + + "'Paul Holmes-Higgin' <paul.hh@alfresco.com>; 'Mike Farman' <mikef@alfresco.com>\n"); + assertContains(text, "CC: 'nickb@alfresco.com' <nickb@alfresco.com>; " + + "'nick.burch@alfresco.com' <nick.burch@alfresco.com>; 'Roy Wetherall' <roy.wetherall@alfresco.com>\n"); + assertContains(text, "BCC: 'David Caruana' <dave.caruana@alfresco.com>; " + + "'Vonka Jan' <jan.vonka@alfresco.com>\n"); + assertContains(text, "Subject: This is a test message please ignore\n"); + assertEquals(-1, text.indexOf("Date:")); + assertContains(text, "The quick brown fox jumps over the lazy dog"); + } + } + + /** + * Test that we correctly handle multiple To+CC + * recipients in an email we received. + */ + public void testReceivedWithMultipleRecipients() throws Exception { + // To: 'Ashutosh Dandavate' <ashutosh.dandavate@alfresco.com>, + // 'Paul Holmes-Higgin' <paul.hh@alfresco.com>, + // 'Mike Farman' <mikef@alfresco.com> + // Cc: nickb@alfresco.com, nick.burch@alfresco.com, + // 'Roy Wetherall' <roy.wetherall@alfresco.com> + // (No BCC shown) + + + String[] files = new String[] { + "example_received_regular.msg", "example_received_unicode.msg" + }; + for(String file : files) { + MAPIMessage msg = new MAPIMessage(new POIFSFileSystem( + new FileInputStream(samples.getFile(file)) + )); + + OutlookTextExtactor ext = new OutlookTextExtactor(msg); + String text = ext.getText(); + + assertContains(text, "From: Mike Farman\n"); + assertContains(text, "To: 'Ashutosh Dandavate' <ashutosh.dandavate@alfresco.com>; " + + "'Paul Holmes-Higgin' <paul.hh@alfresco.com>; 'Mike Farman' <mikef@alfresco.com>\n"); + assertContains(text, "CC: nickb@alfresco.com; " + + "nick.burch@alfresco.com; 'Roy Wetherall' <roy.wetherall@alfresco.com>\n"); + assertEquals(-1, text.indexOf("BCC:")); + assertContains(text, "Subject: This is a test message please ignore\n"); + assertEquals(-1, text.indexOf("Date:")); + assertContains(text, "The quick brown fox jumps over the lazy dog"); + } + } } diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/parsers/TestPOIFSChunkParser.java b/src/scratchpad/testcases/org/apache/poi/hsmf/parsers/TestPOIFSChunkParser.java index 5df734f1f2..68094e2ee8 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/parsers/TestPOIFSChunkParser.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/parsers/TestPOIFSChunkParser.java @@ -21,6 +21,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Calendar; import org.apache.poi.hsmf.MAPIMessage; @@ -29,6 +30,7 @@ import org.apache.poi.hsmf.datatypes.ChunkGroup; import org.apache.poi.hsmf.datatypes.Chunks; import org.apache.poi.hsmf.datatypes.NameIdChunks; import org.apache.poi.hsmf.datatypes.RecipientChunks; +import org.apache.poi.hsmf.datatypes.RecipientChunks.RecipientChunksSorter; import org.apache.poi.hsmf.datatypes.StringChunk; import org.apache.poi.hsmf.datatypes.Types; import org.apache.poi.hsmf.exceptions.ChunkNotFoundException; @@ -81,7 +83,7 @@ public final class TestPOIFSChunkParser extends TestCase { } } - public void testFindsRecips() throws IOException { + public void testFindsRecips() throws IOException, ChunkNotFoundException { POIFSFileSystem simple = new POIFSFileSystem( new FileInputStream(samples.getFile("quick.msg")) ); @@ -95,7 +97,9 @@ public final class TestPOIFSChunkParser extends TestCase { assertTrue(groups[2] instanceof NameIdChunks); RecipientChunks recips = (RecipientChunks)groups[1]; - assertEquals("kevin.roast@alfresco.org", recips.recipientEmailChunk.getValue()); + assertEquals("kevin.roast@alfresco.org", recips.recipientSMTPChunk.getValue()); + assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben", + recips.recipientEmailChunk.getValue()); String search = new String(recips.recipientSearchChunk.getValue(), "ASCII"); assertEquals("CN=KEVIN.ROAST@BEN\0", search.substring(search.length()-19)); @@ -103,20 +107,123 @@ public final class TestPOIFSChunkParser extends TestCase { // Now via MAPIMessage MAPIMessage msg = new MAPIMessage(simple); assertNotNull(msg.getRecipientDetailsChunks()); + assertEquals(1, msg.getRecipientDetailsChunks().length); - assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks().recipientEmailChunk.getValue()); + assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].recipientSMTPChunk.getValue()); + assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].getRecipientEmailAddress()); + assertEquals("Kevin Roast", msg.getRecipientDetailsChunks()[0].getRecipientName()); + assertEquals("kevin.roast@alfresco.org", msg.getRecipientEmailAddress()); // Try both SMTP and EX files for recipient - assertEquals("EX", msg.getRecipientDetailsChunks().deliveryTypeChunk.getValue()); - assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks().recipientEmailChunk.getValue()); + assertEquals("EX", msg.getRecipientDetailsChunks()[0].deliveryTypeChunk.getValue()); + assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].recipientSMTPChunk.getValue()); + assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben", + msg.getRecipientDetailsChunks()[0].recipientEmailChunk.getValue()); + // Now look at another message msg = new MAPIMessage(new POIFSFileSystem( new FileInputStream(samples.getFile("simple_test_msg.msg")) )); - assertEquals("SMTP", msg.getRecipientDetailsChunks().deliveryTypeChunk.getValue()); - assertEquals(null, msg.getRecipientDetailsChunks().recipientEmailChunk); - assertEquals("travis@overwrittenstack.com", msg.getRecipientDetailsChunks().recipientNameChunk.getValue()); + assertNotNull(msg.getRecipientDetailsChunks()); + assertEquals(1, msg.getRecipientDetailsChunks().length); + + assertEquals("SMTP", msg.getRecipientDetailsChunks()[0].deliveryTypeChunk.getValue()); + assertEquals(null, msg.getRecipientDetailsChunks()[0].recipientSMTPChunk); + assertEquals(null, msg.getRecipientDetailsChunks()[0].recipientNameChunk); + assertEquals("travis@overwrittenstack.com", msg.getRecipientDetailsChunks()[0].recipientEmailChunk.getValue()); + assertEquals("travis@overwrittenstack.com", msg.getRecipientEmailAddress()); + } + + public void testFindsMultipleRecipients() throws IOException, ChunkNotFoundException { + POIFSFileSystem multiple = new POIFSFileSystem( + new FileInputStream(samples.getFile("example_received_unicode.msg")) + ); + + multiple.getRoot().getEntry("__recip_version1.0_#00000000"); + multiple.getRoot().getEntry("__recip_version1.0_#00000001"); + multiple.getRoot().getEntry("__recip_version1.0_#00000002"); + multiple.getRoot().getEntry("__recip_version1.0_#00000003"); + multiple.getRoot().getEntry("__recip_version1.0_#00000004"); + multiple.getRoot().getEntry("__recip_version1.0_#00000005"); + + ChunkGroup[] groups = POIFSChunkParser.parse(multiple.getRoot()); + assertEquals(9, groups.length); + assertTrue(groups[0] instanceof Chunks); + assertTrue(groups[1] instanceof RecipientChunks); + assertTrue(groups[2] instanceof AttachmentChunks); + assertTrue(groups[3] instanceof RecipientChunks); + assertTrue(groups[4] instanceof RecipientChunks); + assertTrue(groups[5] instanceof RecipientChunks); + assertTrue(groups[6] instanceof RecipientChunks); + assertTrue(groups[7] instanceof RecipientChunks); + assertTrue(groups[8] instanceof NameIdChunks); + + // In FS order initially + RecipientChunks[] chunks = new RecipientChunks[] { + (RecipientChunks)groups[1], + (RecipientChunks)groups[3], + (RecipientChunks)groups[4], + (RecipientChunks)groups[5], + (RecipientChunks)groups[6], + (RecipientChunks)groups[7], + }; + assertEquals(6, chunks.length); + assertEquals(0, chunks[0].recipientNumber); + assertEquals(4, chunks[1].recipientNumber); + assertEquals(3, chunks[2].recipientNumber); + assertEquals(2, chunks[3].recipientNumber); + assertEquals(1, chunks[4].recipientNumber); + assertEquals(5, chunks[5].recipientNumber); + + // Check + assertEquals("'Ashutosh Dandavate'", chunks[0].getRecipientName()); + assertEquals("ashutosh.dandavate@alfresco.com", chunks[0].getRecipientEmailAddress()); + assertEquals("nick.burch@alfresco.com", chunks[1].getRecipientName()); + assertEquals("nick.burch@alfresco.com", chunks[1].getRecipientEmailAddress()); + assertEquals("nickb@alfresco.com", chunks[2].getRecipientName()); + assertEquals("nickb@alfresco.com", chunks[2].getRecipientEmailAddress()); + assertEquals("'Mike Farman'", chunks[3].getRecipientName()); + assertEquals("mikef@alfresco.com", chunks[3].getRecipientEmailAddress()); + assertEquals("'Paul Holmes-Higgin'", chunks[4].getRecipientName()); + assertEquals("paul.hh@alfresco.com", chunks[4].getRecipientEmailAddress()); + assertEquals("'Roy Wetherall'", chunks[5].getRecipientName()); + assertEquals("roy.wetherall@alfresco.com", chunks[5].getRecipientEmailAddress()); + + // Now sort, and re-check + Arrays.sort(chunks, new RecipientChunksSorter()); + + assertEquals("'Ashutosh Dandavate'", chunks[0].getRecipientName()); + assertEquals("ashutosh.dandavate@alfresco.com", chunks[0].getRecipientEmailAddress()); + assertEquals("'Paul Holmes-Higgin'", chunks[1].getRecipientName()); + assertEquals("paul.hh@alfresco.com", chunks[1].getRecipientEmailAddress()); + assertEquals("'Mike Farman'", chunks[2].getRecipientName()); + assertEquals("mikef@alfresco.com", chunks[2].getRecipientEmailAddress()); + assertEquals("nickb@alfresco.com", chunks[3].getRecipientName()); + assertEquals("nickb@alfresco.com", chunks[3].getRecipientEmailAddress()); + assertEquals("nick.burch@alfresco.com", chunks[4].getRecipientName()); + assertEquals("nick.burch@alfresco.com", chunks[4].getRecipientEmailAddress()); + assertEquals("'Roy Wetherall'", chunks[5].getRecipientName()); + assertEquals("roy.wetherall@alfresco.com", chunks[5].getRecipientEmailAddress()); + + // Finally check on message + MAPIMessage msg = new MAPIMessage(multiple); + assertEquals(6, msg.getRecipientEmailAddressList().length); + assertEquals(6, msg.getRecipientNamesList().length); + + assertEquals("'Ashutosh Dandavate'", msg.getRecipientNamesList()[0]); + assertEquals("'Paul Holmes-Higgin'", msg.getRecipientNamesList()[1]); + assertEquals("'Mike Farman'", msg.getRecipientNamesList()[2]); + assertEquals("nickb@alfresco.com", msg.getRecipientNamesList()[3]); + assertEquals("nick.burch@alfresco.com", msg.getRecipientNamesList()[4]); + assertEquals("'Roy Wetherall'", msg.getRecipientNamesList()[5]); + + assertEquals("ashutosh.dandavate@alfresco.com", msg.getRecipientEmailAddressList()[0]); + assertEquals("paul.hh@alfresco.com", msg.getRecipientEmailAddressList()[1]); + assertEquals("mikef@alfresco.com", msg.getRecipientEmailAddressList()[2]); + assertEquals("nickb@alfresco.com", msg.getRecipientEmailAddressList()[3]); + assertEquals("nick.burch@alfresco.com", msg.getRecipientEmailAddressList()[4]); + assertEquals("roy.wetherall@alfresco.com", msg.getRecipientEmailAddressList()[5]); } public void testFindsNameId() throws IOException { |