You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestLbsDataSubRecord.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 java.util.Arrays;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import org.apache.poi.util.HexRead;
  21. import org.apache.poi.util.HexDump;
  22. import org.apache.poi.util.LittleEndianInputStream;
  23. import org.apache.poi.util.LittleEndianOutputStream;
  24. import org.apache.poi.hssf.record.formula.Ptg;
  25. import org.apache.poi.hssf.record.formula.AreaPtg;
  26. import org.apache.poi.ss.util.CellRangeAddress;
  27. /**
  28. * Tests the serialization and deserialization of the LbsDataSubRecord class works correctly.
  29. *
  30. * @author Yegor Kozlov
  31. */
  32. public final class TestLbsDataSubRecord extends TestCase {
  33. /**
  34. * test read-write round trip
  35. * test data was taken from 47701.xls
  36. */
  37. public void test_47701(){
  38. byte[] data = HexRead.readFromString(
  39. "15, 00, 12, 00, 12, 00, 02, 00, 11, 20, " +
  40. "00, 00, 00, 00, 80, 3D, 03, 05, 00, 00, " +
  41. "00, 00, 0C, 00, 14, 00, 00, 00, 00, 00, " +
  42. "00, 00, 00, 00, 00, 00, 01, 00, 0A, 00, " +
  43. "00, 00, 10, 00, 01, 00, 13, 00, EE, 1F, " +
  44. "10, 00, 09, 00, 40, 9F, 74, 01, 25, 09, " +
  45. "00, 0C, 00, 07, 00, 07, 00, 07, 04, 00, " +
  46. "00, 00, 08, 00, 00, 00");
  47. RecordInputStream in = TestcaseRecordInputStream.create(ObjRecord.sid, data);
  48. // check read OK
  49. ObjRecord record = new ObjRecord(in);
  50. assertEquals(3, record.getSubRecords().size());
  51. SubRecord sr = record.getSubRecords().get(2);
  52. assertTrue(sr instanceof LbsDataSubRecord);
  53. LbsDataSubRecord lbs = (LbsDataSubRecord)sr;
  54. assertEquals(4, lbs.getNumberOfItems());
  55. assertTrue(lbs.getFormula() instanceof AreaPtg);
  56. AreaPtg ptg = (AreaPtg)lbs.getFormula();
  57. CellRangeAddress range = new CellRangeAddress(
  58. ptg.getFirstRow(), ptg.getLastRow(), ptg.getFirstColumn(), ptg.getLastColumn());
  59. assertEquals("H10:H13", range.formatAsString());
  60. // check that it re-serializes to the same data
  61. byte[] ser = record.serialize();
  62. TestcaseRecordInputStream.confirmRecordEncoding(ObjRecord.sid, data, ser);
  63. }
  64. /**
  65. * test data was taken from the file attached to Bugzilla 45778
  66. */
  67. public void test_45778(){
  68. byte[] data = HexRead.readFromString(
  69. "15, 00, 12, 00, 14, 00, 01, 00, 01, 00, " +
  70. "01, 21, 00, 00, 3C, 13, F4, 03, 00, 00, " +
  71. "00, 00, 0C, 00, 14, 00, 00, 00, 00, 00, " +
  72. "00, 00, 00, 00, 00, 00, 01, 00, 08, 00, 00, " +
  73. "00, 10, 00, 00, 00, " +
  74. "13, 00, EE, 1F, " +
  75. "00, 00, " +
  76. "08, 00, " + //number of items
  77. "08, 00, " + //selected item
  78. "01, 03, " + //flags
  79. "00, 00, " + //objId
  80. //LbsDropData
  81. "0A, 00, " + //flags
  82. "14, 00, " + //the number of lines to be displayed in the dropdown
  83. "6C, 00, " + //the smallest width in pixels allowed for the dropdown window
  84. "00, 00, " + //num chars
  85. "00, 00");
  86. RecordInputStream in = TestcaseRecordInputStream.create(ObjRecord.sid, data);
  87. // check read OK
  88. ObjRecord record = new ObjRecord(in);
  89. SubRecord sr = record.getSubRecords().get(2);
  90. assertTrue(sr instanceof LbsDataSubRecord);
  91. LbsDataSubRecord lbs = (LbsDataSubRecord)sr;
  92. assertEquals(8, lbs.getNumberOfItems());
  93. assertNull(lbs.getFormula());
  94. // check that it re-serializes to the same data
  95. byte[] ser = record.serialize();
  96. TestcaseRecordInputStream.confirmRecordEncoding(ObjRecord.sid, data, ser);
  97. }
  98. /**
  99. * Test data produced by OpenOffice 3.1 by opening and saving 47701.xls
  100. * There are 5 padding bytes that are removed by POI
  101. */
  102. public void test_remove_padding(){
  103. byte[] data = HexRead.readFromString(
  104. "5D, 00, 4C, 00, " +
  105. "15, 00, 12, 00, 12, 00, 01, 00, 11, 00, " +
  106. "00, 00, 00, 00, 00, 00, 00, 00, 00, 00, " +
  107. "00, 00, 0C, 00, 14, 00, 00, 00, 00, 00, " +
  108. "00, 00, 00, 00, 00, 00, 01, 00, 09, 00, " +
  109. "00, 00, 0F, 00, 01, 00, " +
  110. "13, 00, 1B, 00, " +
  111. "10, 00, " + //next 16 bytes is a ptg aray
  112. "09, 00, 00, 00, 00, 00, 25, 09, 00, 0C, 00, 07, 00, 07, 00, 00, " +
  113. "01, 00, " + //num lines
  114. "00, 00, " + //selected
  115. "08, 00, " +
  116. "00, 00, " +
  117. "00, 00, 00, 00, 00"); //padding bytes
  118. RecordInputStream in = TestcaseRecordInputStream.create(data);
  119. // check read OK
  120. ObjRecord record = new ObjRecord(in);
  121. // check that it re-serializes to the same data
  122. byte[] ser = record.serialize();
  123. assertEquals(data.length-5, ser.length);
  124. for(int i=0; i < ser.length; i++) assertEquals(data[i],ser[i]);
  125. //check we can read the trimmed record
  126. RecordInputStream in2 = TestcaseRecordInputStream.create(ser);
  127. ObjRecord record2 = new ObjRecord(in2);
  128. byte[] ser2 = record2.serialize();
  129. assertTrue(Arrays.equals(ser, ser2));
  130. }
  131. public void test_LbsDropData(){
  132. byte[] data = HexRead.readFromString(
  133. //LbsDropData
  134. "0A, 00, " + //flags
  135. "14, 00, " + //the number of lines to be displayed in the dropdown
  136. "6C, 00, " + //the smallest width in pixels allowed for the dropdown window
  137. "00, 00, " + //num chars
  138. "00, " + //compression flag
  139. "00"); //padding byte
  140. LittleEndianInputStream in = new LittleEndianInputStream(new ByteArrayInputStream(data));
  141. LbsDataSubRecord.LbsDropData lbs = new LbsDataSubRecord.LbsDropData(in);
  142. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  143. lbs.serialize(new LittleEndianOutputStream(baos));
  144. assertTrue(Arrays.equals(data, baos.toByteArray()));
  145. }
  146. }