Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestSSTRecordSizeCalculator.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 junit.framework.TestCase;
  17. import org.apache.poi.hssf.record.common.UnicodeString;
  18. import org.apache.poi.hssf.record.cont.ContinuableRecordOutput;
  19. import org.apache.poi.util.IntMapper;
  20. /**
  21. * Tests that records size calculates correctly.
  22. *
  23. * @author Glen Stampoultzis (glens at apache.org)
  24. */
  25. public final class TestSSTRecordSizeCalculator extends TestCase {
  26. private static final String SMALL_STRING = "Small string";
  27. private static final int COMPRESSED_PLAIN_STRING_OVERHEAD = 3;
  28. private static final int OPTION_FIELD_SIZE = 1;
  29. private final IntMapper<UnicodeString> strings = new IntMapper<UnicodeString>();
  30. private void confirmSize(int expectedSize) {
  31. ContinuableRecordOutput cro = ContinuableRecordOutput.createForCountingOnly();
  32. SSTSerializer ss = new SSTSerializer(strings, 0, 0);
  33. ss.serialize(cro);
  34. assertEquals(expectedSize, cro.getTotalSize());
  35. }
  36. public void testBasic() {
  37. strings.add(makeUnicodeString(SMALL_STRING));
  38. confirmSize(SSTRecord.SST_RECORD_OVERHEAD
  39. + COMPRESSED_PLAIN_STRING_OVERHEAD
  40. + SMALL_STRING.length());
  41. }
  42. public void testBigStringAcrossUnicode() {
  43. int bigString = SSTRecord.MAX_DATA_SPACE + 100;
  44. strings.add(makeUnicodeString(bigString));
  45. confirmSize(SSTRecord.SST_RECORD_OVERHEAD
  46. + COMPRESSED_PLAIN_STRING_OVERHEAD
  47. + SSTRecord.MAX_DATA_SPACE
  48. + SSTRecord.STD_RECORD_OVERHEAD
  49. + OPTION_FIELD_SIZE
  50. + 100);
  51. }
  52. public void testPerfectFit() {
  53. int perfectFit = SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD;
  54. strings.add(makeUnicodeString(perfectFit));
  55. confirmSize(SSTRecord.SST_RECORD_OVERHEAD
  56. + COMPRESSED_PLAIN_STRING_OVERHEAD
  57. + perfectFit);
  58. }
  59. public void testJustOversized() {
  60. int tooBig = SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD + 1;
  61. strings.add(makeUnicodeString(tooBig));
  62. confirmSize(SSTRecord.SST_RECORD_OVERHEAD
  63. + COMPRESSED_PLAIN_STRING_OVERHEAD
  64. + tooBig - 1
  65. // continue record
  66. + SSTRecord.STD_RECORD_OVERHEAD
  67. + OPTION_FIELD_SIZE + 1);
  68. }
  69. public void testSecondStringStartsOnNewContinuation() {
  70. int perfectFit = SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD;
  71. strings.add(makeUnicodeString(perfectFit));
  72. strings.add(makeUnicodeString(SMALL_STRING));
  73. confirmSize(SSTRecord.SST_RECORD_OVERHEAD
  74. + SSTRecord.MAX_DATA_SPACE
  75. // second string
  76. + SSTRecord.STD_RECORD_OVERHEAD
  77. + COMPRESSED_PLAIN_STRING_OVERHEAD
  78. + SMALL_STRING.length());
  79. }
  80. public void testHeaderCrossesNormalContinuePoint() {
  81. int almostPerfectFit = SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD - 2;
  82. strings.add(makeUnicodeString(almostPerfectFit));
  83. String oneCharString = new String(new char[1]);
  84. strings.add(makeUnicodeString(oneCharString));
  85. confirmSize(SSTRecord.SST_RECORD_OVERHEAD
  86. + COMPRESSED_PLAIN_STRING_OVERHEAD
  87. + almostPerfectFit
  88. // second string
  89. + SSTRecord.STD_RECORD_OVERHEAD
  90. + COMPRESSED_PLAIN_STRING_OVERHEAD
  91. + oneCharString.length());
  92. }
  93. private static UnicodeString makeUnicodeString(int size) {
  94. String s = new String(new char[size]);
  95. return makeUnicodeString(s);
  96. }
  97. private static UnicodeString makeUnicodeString(String s) {
  98. UnicodeString st = new UnicodeString(s);
  99. st.setOptionFlags((byte) 0);
  100. return st;
  101. }
  102. }