Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TestSSTDeserializer.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import org.apache.poi.hssf.HSSFTestDataSamples;
  20. import org.apache.poi.hssf.record.common.UnicodeString;
  21. import org.apache.poi.util.HexRead;
  22. import org.apache.poi.util.IntMapper;
  23. import org.junit.jupiter.api.Test;
  24. /**
  25. * Exercise the SSTDeserializer class.
  26. */
  27. final class TestSSTDeserializer {
  28. private static final int FAKE_SID = -5555;
  29. private static byte[] concat(byte[] a, byte[] b) {
  30. byte[] result = new byte[a.length + b.length];
  31. System.arraycopy(a, 0, result, 0, a.length);
  32. System.arraycopy(b, 0, result, a.length, b.length);
  33. return result;
  34. }
  35. private static byte[] readSampleHexData(String sampleFileName, String sectionName, int recSid)
  36. throws IOException {
  37. InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
  38. byte[] data = HexRead.readData(is, sectionName);
  39. byte[] result = TestcaseRecordInputStream.mergeDataAndSid(recSid, data.length, data);
  40. is.close();
  41. return result;
  42. }
  43. @Test
  44. void testSpanRichTextToPlainText() throws IOException {
  45. byte[] header = readSampleHexData("richtextdata.txt", "header", FAKE_SID);
  46. byte[] continueBytes = readSampleHexData("richtextdata.txt", "continue1", ContinueRecord.sid);
  47. RecordInputStream in = TestcaseRecordInputStream.create(concat(header, continueBytes));
  48. IntMapper<UnicodeString> strings = new IntMapper<>();
  49. SSTDeserializer deserializer = new SSTDeserializer( strings );
  50. deserializer.manufactureStrings(1, in );
  51. assertEquals( "At a dinner party orAt At At ", strings.get( 0 ) + "" );
  52. }
  53. @Test
  54. void testContinuationWithNoOverlap() throws IOException {
  55. byte[] header = readSampleHexData("evencontinuation.txt", "header", FAKE_SID);
  56. byte[] continueBytes = readSampleHexData("evencontinuation.txt", "continue1", ContinueRecord.sid);
  57. RecordInputStream in = TestcaseRecordInputStream.create(concat(header, continueBytes));
  58. IntMapper<UnicodeString> strings = new IntMapper<>();
  59. SSTDeserializer deserializer = new SSTDeserializer( strings );
  60. deserializer.manufactureStrings( 2, in);
  61. assertEquals( "At a dinner party or", strings.get( 0 ) + "" );
  62. assertEquals( "At a dinner party", strings.get( 1 ) + "" );
  63. }
  64. /**
  65. * Strings can actually span across more than one continuation.
  66. */
  67. @Test
  68. void testStringAcross2Continuations() throws IOException {
  69. byte[] header = readSampleHexData("stringacross2continuations.txt", "header", FAKE_SID);
  70. byte[] continue1 = readSampleHexData("stringacross2continuations.txt", "continue1", ContinueRecord.sid);
  71. byte[] continue2 = readSampleHexData("stringacross2continuations.txt", "continue2", ContinueRecord.sid);
  72. RecordInputStream in = TestcaseRecordInputStream.create(concat(header, concat(continue1, continue2)));
  73. IntMapper<UnicodeString> strings = new IntMapper<>();
  74. SSTDeserializer deserializer = new SSTDeserializer( strings );
  75. deserializer.manufactureStrings( 2, in);
  76. assertEquals( "At a dinner party or", strings.get( 0 ) + "" );
  77. assertEquals( "At a dinner partyAt a dinner party", strings.get( 1 ) + "" );
  78. }
  79. @Test
  80. void testExtendedStrings() throws IOException {
  81. byte[] header = readSampleHexData("extendedtextstrings.txt", "rich-header", FAKE_SID);
  82. byte[] continueBytes = readSampleHexData("extendedtextstrings.txt", "rich-continue1", ContinueRecord.sid);
  83. RecordInputStream in = TestcaseRecordInputStream.create(concat(header, continueBytes));
  84. IntMapper<UnicodeString> strings = new IntMapper<>();
  85. SSTDeserializer deserializer = new SSTDeserializer( strings );
  86. deserializer.manufactureStrings( 1, in);
  87. assertEquals( "At a dinner party orAt At At ", strings.get( 0 ) + "" );
  88. header = readSampleHexData("extendedtextstrings.txt", "norich-header", FAKE_SID);
  89. continueBytes = readSampleHexData("extendedtextstrings.txt", "norich-continue1", ContinueRecord.sid);
  90. in = TestcaseRecordInputStream.create(concat(header, continueBytes));
  91. strings = new IntMapper<>();
  92. deserializer = new SSTDeserializer( strings );
  93. deserializer.manufactureStrings( 1, in);
  94. assertEquals( "At a dinner party orAt At At ", strings.get( 0 ) + "" );
  95. }
  96. /**
  97. * Ensure that invalid SST records with an incorrect number of strings specified, does not consume non-continuation records.
  98. */
  99. @Test
  100. void test65543() throws IOException {
  101. final byte[] sstRecord = readSampleHexData("notenoughstrings.txt", "sst-record", SSTRecord.sid);
  102. byte[] nonContinuationRecord = readSampleHexData("notenoughstrings.txt", "non-continuation-record", ExtSSTRecord.sid);
  103. RecordInputStream in = TestcaseRecordInputStream.create(concat(sstRecord, nonContinuationRecord));
  104. IntMapper<UnicodeString> strings = new IntMapper<>();
  105. SSTDeserializer deserializer = new SSTDeserializer(strings);
  106. // The record data in notenoughstrings.txt only contains 1 string, deliberately pass in a larger number.
  107. deserializer.manufactureStrings(2, in);
  108. assertEquals("At a dinner party or", strings.get(0) + "");
  109. assertEquals("", strings.get(1) + "");
  110. }
  111. }