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.

TestXLS2CSVmra.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.examples.hssf.eventusermodel;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.PrintStream;
  21. import java.nio.charset.StandardCharsets;
  22. import org.apache.poi.hssf.HSSFTestDataSamples;
  23. import org.apache.poi.hssf.record.NumberRecord;
  24. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  25. import org.junit.jupiter.api.Test;
  26. class TestXLS2CSVmra {
  27. @Test
  28. void test() throws Exception {
  29. XLS2CSVmra.main(new String[] { HSSFTestDataSamples.getSampleFile("SampleSS.xls").getAbsolutePath() });
  30. }
  31. @Test
  32. void testWithMinCols() throws Exception {
  33. XLS2CSVmra.main(new String[] { HSSFTestDataSamples.getSampleFile("SampleSS.xls").getAbsolutePath(), "100" });
  34. }
  35. @Test
  36. void testProcess() throws IOException {
  37. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  38. PrintStream out = new PrintStream(outStream, false, StandardCharsets.UTF_8.name());
  39. XLS2CSVmra cvs = new XLS2CSVmra(
  40. new POIFSFileSystem(new FileInputStream(HSSFTestDataSamples.getSampleFile("SampleSS.xls").getAbsolutePath())),
  41. out, -1);
  42. cvs.process();
  43. outStream.flush();
  44. assertEquals(sanitize("\n"
  45. + "First Sheet [1]:\n"
  46. + "\"Test spreadsheet\"\n"
  47. + "\"2nd row\",\"2nd row 2nd column\"\n"
  48. + "\n"
  49. + "\"This one is red\"\n"
  50. + "\n"
  51. + "Sheet Number 2 [2]:\n"
  52. + "\"Start of 2nd sheet\"\n"
  53. + "\"Sheet 2 row 2\"\n"
  54. + "\n"
  55. + "\"I'm in bold blue, on a yellow background\"\n"
  56. + "\n"
  57. + "\"cb=1\",\"cb=10\",\"cb=2\",\"cb=sum\"\n"
  58. + "1,10,2,13\n"
  59. + "\n"
  60. + "Sheet3 [3]:\n"), sanitize(new String(outStream.toByteArray(), StandardCharsets.UTF_8)));
  61. }
  62. @Test
  63. void testProcessNumberRecord() throws IOException {
  64. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  65. PrintStream out = new PrintStream(outStream, false, StandardCharsets.UTF_8.name());
  66. XLS2CSVmra cvs = new XLS2CSVmra(
  67. new POIFSFileSystem(new FileInputStream(HSSFTestDataSamples.getSampleFile("empty.xls").getAbsolutePath())),
  68. out, -1);
  69. // need to call process() first to initialize members
  70. cvs.process();
  71. outStream.flush();
  72. assertEquals(sanitize("\n"
  73. + "Лист1 [1]:\n"
  74. + "\n"
  75. + "Лист2 [2]:\n"
  76. + "\n"
  77. + "Лист3 [3]:\n"), sanitize(new String(outStream.toByteArray(), StandardCharsets.UTF_8)));
  78. NumberRecord record = new NumberRecord();
  79. record.setValue(1.243);
  80. cvs.processRecord(record);
  81. outStream.flush();
  82. assertEquals(sanitize("\n"
  83. + "Лист1 [1]:\n"
  84. + "\n"
  85. + "Лист2 [2]:\n"
  86. + "\n"
  87. + "Лист3 [3]:\n"
  88. + "1.243"), sanitize(new String(outStream.toByteArray(), StandardCharsets.UTF_8)));
  89. }
  90. private String sanitize(String str) {
  91. return str.replace("\r\n", "\n");
  92. }
  93. }