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.

HSSFRecordsStresser.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.stress;
  16. import static org.junit.Assert.assertArrayEquals;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.io.FileInputStream;
  20. import java.io.InputStream;
  21. import java.util.List;
  22. import org.apache.poi.hssf.HSSFTestDataSamples;
  23. import org.apache.poi.hssf.record.Record;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.junit.Test;
  26. /**
  27. * Needs to be implemented in this package to have access to
  28. * HSSFWorkbook.getWorkbook()
  29. */
  30. public class HSSFRecordsStresser {
  31. public static void handleWorkbook(HSSFWorkbook wb) {
  32. List<org.apache.poi.hssf.record.Record> records = wb.getWorkbook().getRecords();
  33. for(org.apache.poi.hssf.record.Record record : records) {
  34. // some Records do not implement clone ?!
  35. // equals instead of instanceof is on purpose here to only skip exactly this class and not any derived ones
  36. // if(record.getClass().equals(InterfaceHdrRecord.class) ||
  37. // record.getClass().equals(MMSRecord.class) ||
  38. // record.getClass().equals(InterfaceEndRecord.class) ||
  39. // record.getClass().equals(WriteAccessRecord.class) ||
  40. // record.getClass().equals(CodepageRecord.class) ||
  41. // record.getClass().equals(DSFRecord.class)) {
  42. // continue;
  43. // }
  44. try {
  45. Record newRecord = record.copy();
  46. assertEquals("Expecting the same class back from clone(), but had Record of type " + record.getClass() + " and got back a " + newRecord.getClass() + " from clone()",
  47. record.getClass(), newRecord.getClass());
  48. byte[] origBytes = record.serialize();
  49. byte[] newBytes = newRecord.serialize();
  50. assertArrayEquals("Record of type " + record.getClass() + " should return the same byte array via the clone() method, but did return a different array",
  51. origBytes, newBytes);
  52. } catch (RuntimeException e) {
  53. // some Records do not implement clone, ignore those for now
  54. assertTrue(e.getMessage().contains("needs to define a clone method"));
  55. }
  56. }
  57. }
  58. // a test-case to test this locally without executing the full TestAllFiles
  59. @Test
  60. public void test() throws Exception {
  61. try (InputStream stream = new FileInputStream(HSSFTestDataSamples.getSampleFile("15556.xls"))) {
  62. HSSFWorkbook wb = new HSSFWorkbook(stream);
  63. handleWorkbook(wb);
  64. wb.close();
  65. }
  66. }
  67. }