Refactor the HMEF contents checks to use a superclass, and stub out the Rtf Message body tests (disabled as there looks to be a padding issue still to solve)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1078304 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-03-05 15:37:24 +00:00
parent 0df8c11c4b
commit 20eea4a872
4 changed files with 93 additions and 32 deletions

View File

@ -0,0 +1,46 @@
/* ====================================================================
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.hmef;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import org.apache.poi.util.IOUtils;
public abstract class HMEFTest extends TestCase {
protected static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
protected void assertContents(String filename, Attachment attachment)
throws IOException {
assertEquals(filename, attachment.getLongFilename());
assertContents(filename, attachment.getContents());
}
protected void assertContents(String filename, byte[] actual)
throws IOException {
byte[] expected = IOUtils.toByteArray(
_samples.openResourceAsStream("quick-contents/" + filename)
);
assertEquals(expected.length, actual.length);
for(int i=0; i<expected.length; i++) {
assertEquals("Byte " + i + " wrong", expected[i], actual[i]);
}
}
}

View File

@ -17,19 +17,12 @@
package org.apache.poi.hmef; package org.apache.poi.hmef;
import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import junit.framework.TestCase; public final class TestAttachments extends HMEFTest {
import org.apache.poi.POIDataSamples;
import org.apache.poi.util.IOUtils;
public final class TestAttachments extends TestCase {
private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
private HMEFMessage quick; private HMEFMessage quick;
@Override @Override
@ -121,19 +114,4 @@ public final class TestAttachments extends TestCase {
assertContents("quick.txt", attachments.get(3)); assertContents("quick.txt", attachments.get(3));
assertContents("quick.xml", attachments.get(4)); assertContents("quick.xml", attachments.get(4));
} }
private void assertContents(String filename, Attachment attachment)
throws IOException {
assertEquals(filename, attachment.getLongFilename());
byte[] expected = IOUtils.toByteArray(
_samples.openResourceAsStream("quick-contents/" + filename)
);
byte[] actual = attachment.getContents();
assertEquals(expected.length, actual.length);
for(int i=0; i<expected.length; i++) {
assertEquals("Byte " + i + " wrong", expected[i], actual[i]);
}
}
} }

View File

@ -25,6 +25,7 @@ import org.apache.poi.POIDataSamples;
import org.apache.poi.hmef.attribute.MAPIAttribute; import org.apache.poi.hmef.attribute.MAPIAttribute;
import org.apache.poi.hmef.attribute.MAPIRtfAttribute; import org.apache.poi.hmef.attribute.MAPIRtfAttribute;
import org.apache.poi.hsmf.datatypes.MAPIProperty; import org.apache.poi.hsmf.datatypes.MAPIProperty;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
@ -145,13 +146,30 @@ public final class TestCompressedRTF extends TestCase {
/** /**
* Check that we can correctly decode the whole file * Check that we can correctly decode the whole file
* @throws Exception * TODO Fix what looks like a padding issue
*/ */
public void testFull() throws Exception { public void DISABLEDtestFull() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat") _samples.openResourceAsStream("quick-winmail.dat")
); );
// TODO MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertNotNull(attr);
MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute)attr;
byte[] expected = IOUtils.toByteArray(
_samples.openResourceAsStream("quick-contents/message.rtf")
);
byte[] decomp = rtfAttr.getData();
// By byte
assertEquals(expected.length, decomp.length);
assertEquals(expected, decomp);
// By String
String expString = new String(expected, "ASCII");
String decompStr = rtfAttr.getDataString();
assertEquals(expString.length(), decompStr.length());
assertEquals(expString, decompStr);
} }
} }

View File

@ -17,15 +17,12 @@
package org.apache.poi.hmef; package org.apache.poi.hmef;
import junit.framework.TestCase; import org.apache.poi.hmef.attribute.MAPIRtfAttribute;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hmef.attribute.TNEFProperty; import org.apache.poi.hmef.attribute.TNEFProperty;
import org.apache.poi.hsmf.datatypes.MAPIProperty;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
public final class TestHMEFMessage extends TestCase { public final class TestHMEFMessage extends HMEFTest {
private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
public void testOpen() throws Exception { public void testOpen() throws Exception {
HMEFMessage msg = new HMEFMessage( HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat") _samples.openResourceAsStream("quick-winmail.dat")
@ -102,4 +99,26 @@ public final class TestHMEFMessage extends TestCase {
assertEquals("This is a test message", msg.getSubject()); assertEquals("This is a test message", msg.getSubject());
assertEquals("{\\rtf1", msg.getBody().substring(0, 6)); assertEquals("{\\rtf1", msg.getBody().substring(0, 6));
} }
/**
* Checks that the compressed RTF message contents
* can be correctly extracted
* TODO Fix what looks like a padding issue
*/
public void DISABLEDtestMessageContents() throws Exception {
HMEFMessage msg = new HMEFMessage(
_samples.openResourceAsStream("quick-winmail.dat")
);
// Firstly by byte
MAPIRtfAttribute rtf = (MAPIRtfAttribute)
msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
assertContents("message.rtf", rtf.getData());
// Then by String
String contents = msg.getBody();
// It's all low bytes
byte[] contentsBytes = contents.getBytes("ASCII");
assertContents("message.rtf", contentsBytes);
}
} }