From 5ec57813ec28b75f654c7a0af82e6c0e522de9d9 Mon Sep 17 00:00:00 2001 From: Rainer Klute Date: Wed, 3 Dec 2003 20:00:14 +0000 Subject: [PATCH] HPSF: new testcase to check the new codepage support - plus a fix to correct a bug detected by the testcase. Hey, that's what testcases are good for! git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353461 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/contrib/poibrowser/Codec.java | 4 - .../apache/poi/hpsf/SummaryInformation.java | 10 +-- .../org/apache/poi/hpsf/VariantSupport.java | 8 -- .../org/apache/poi/hpsf/basic/TestWrite.java | 85 ++++++++++++++++--- 4 files changed, 76 insertions(+), 31 deletions(-) diff --git a/src/contrib/src/org/apache/poi/contrib/poibrowser/Codec.java b/src/contrib/src/org/apache/poi/contrib/poibrowser/Codec.java index 459e23ad36..bbd6c5e64b 100644 --- a/src/contrib/src/org/apache/poi/contrib/poibrowser/Codec.java +++ b/src/contrib/src/org/apache/poi/contrib/poibrowser/Codec.java @@ -90,10 +90,6 @@ public class Codec /** *

Converts a string into its hexadecimal notation.

- * - *

FIXME: If this method is called frequently, - * it should directly implement the algorithm in the called method - * in order to avoid creating a string instance.

*/ public static String hexEncode(final String s) { diff --git a/src/java/org/apache/poi/hpsf/SummaryInformation.java b/src/java/org/apache/poi/hpsf/SummaryInformation.java index 2b32f3fc34..0ad646e81e 100644 --- a/src/java/org/apache/poi/hpsf/SummaryInformation.java +++ b/src/java/org/apache/poi/hpsf/SummaryInformation.java @@ -310,12 +310,12 @@ public class SummaryInformation extends SpecialPropertySet * WMF or Clipboard (BMP?) format. He also provided two links that * might be helpful: http://www.csn.ul.ie/~caolan/publink/file/OLE2SummaryAgainst_file-3.27.patch - * and http://www.csn.ul.ie/~caolan/publink/file/OLE2SummaryAgainst_file-3.27.patch + * and http://msdn.microsoft.com/library/en-us/dno97ta/html/msdn_docprop.asp - * . However, we won't do any conversion into any image type - * but instead just return a byte array.

+ * target="_blank">http://msdn.microsoft.com/library/en-us/dno97ta/html/msdn_docprop.asp. + * However, we won't do any conversion into any image type but instead just + * return a byte array.

* * @return The thumbnail or null */ diff --git a/src/java/org/apache/poi/hpsf/VariantSupport.java b/src/java/org/apache/poi/hpsf/VariantSupport.java index 29360420dc..d85a588f33 100644 --- a/src/java/org/apache/poi/hpsf/VariantSupport.java +++ b/src/java/org/apache/poi/hpsf/VariantSupport.java @@ -230,7 +230,6 @@ public class VariantSupport extends Variant final int first = o1 + LittleEndian.INT_SIZE; long last = first + LittleEndian.getUInt(src, o1) - 1; o1 += LittleEndian.INT_SIZE; - final int rawLength = (int) (last - first + 1); while (src[(int) last] == 0 && first <= last) last--; final int l = (int) (last - first + 1); @@ -454,13 +453,6 @@ public class VariantSupport extends Variant } } - /* Add 0x00 characters to write a multiple of four bytes: */ - // FIXME (1) Try this! -// while (length % 4 != 0) -// { -// out.write(0); -// length++; -// } return length; } diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java b/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java index 95b78ea66b..609f0358af 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java @@ -65,6 +65,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringWriter; +import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -402,7 +403,6 @@ public class TestWrite extends TestCase { Throwable t = null; final int codepage = -1; - /* FIXME (2): Add tests for various codepages! */ try { check(Variant.VT_EMPTY, null, codepage); @@ -414,8 +414,8 @@ public class TestWrite extends TestCase check(Variant.VT_CF, new byte[]{0, 1, 2, 3}, codepage); check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4}, codepage); check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5}, codepage); - check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, - codepage); + check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6}, codepage); + check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6, 7}, codepage); check(Variant.VT_I2, new Integer(27), codepage); check(Variant.VT_I4, new Long(28), codepage); check(Variant.VT_FILETIME, new Date(), codepage); @@ -445,21 +445,80 @@ public class TestWrite extends TestCase t = ex; } if (t != null) + fail(org.apache.poi.hpsf.Util.toString(t)); + } + + + + /** + *

Writes and reads back strings using several different codepages and + * checks whether the stuff that has been read back equals the stuff that + * was written.

+ */ + public void testCodepages() + { + Throwable t = null; + final int[] validCodepages = new int[] {-1, 1252, 1200, 65001}; + for (int i = 0; i < validCodepages.length; i++) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - t.printStackTrace(pw); - pw.close(); + int codepage = validCodepages[i]; + try + { + check(Variant.VT_LPSTR, "", codepage); + check(Variant.VT_LPSTR, "ä", codepage); + check(Variant.VT_LPSTR, "äö", codepage); + check(Variant.VT_LPSTR, "äöü", codepage); + check(Variant.VT_LPSTR, "äöüÄ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖÜ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖÜß", codepage); + check(Variant.VT_LPSTR, "\u79D1\u5B78", codepage); + } + catch (Exception ex) + { + t = ex; + } + catch (Error ex) + { + t = ex; + } + if (t != null) + fail(org.apache.poi.hpsf.Util.toString(t)); + } + + final int[] invalidCodepages = new int[] {0, 1, 2, 4711, 815}; + for (int i = 0; i < invalidCodepages.length; i++) + { + int codepage = invalidCodepages[i]; try { - sw.close(); + check(Variant.VT_LPSTR, "", codepage); + check(Variant.VT_LPSTR, "ä", codepage); + check(Variant.VT_LPSTR, "äö", codepage); + check(Variant.VT_LPSTR, "äöü", codepage); + check(Variant.VT_LPSTR, "äöüÄ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖÜ", codepage); + check(Variant.VT_LPSTR, "äöüÄÖÜß", codepage); + fail("UnsupportedEncodingException for codepage " + codepage + + " expected."); } - catch (IOException ex2) + catch (UnsupportedEncodingException ex) { - t.printStackTrace(); + /* This is the expected behaviour. */ } - fail(sw.toString()); + catch (Exception ex) + { + t = ex; + } + catch (Error ex) + { + t = ex; + } + if (t != null) + fail(org.apache.poi.hpsf.Util.toString(t)); } + } @@ -482,11 +541,9 @@ public class TestWrite extends TestCase final byte[] b = out.toByteArray(); final Object objRead = VariantSupport.read(b, 0, b.length + LittleEndian.INT_SIZE, - variantType, -1); + variantType, codepage); if (objRead instanceof byte[]) { -// final int diff = diff(org.apache.poi.hpsf.Util.pad4 -// ((byte[]) value), (byte[]) objRead); final int diff = diff((byte[]) value, (byte[]) objRead); if (diff >= 0) fail("Byte arrays are different. First different byte is at " + -- 2.39.5