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.

TestExcelConverterSuite.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.converter;
  16. import org.apache.poi.POIDataSamples;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  18. import org.apache.poi.util.XMLHelper;
  19. import org.junit.Test;
  20. import org.junit.runner.RunWith;
  21. import org.junit.runners.Parameterized;
  22. import javax.xml.transform.OutputKeys;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerFactory;
  25. import javax.xml.transform.dom.DOMSource;
  26. import javax.xml.transform.stream.StreamResult;
  27. import java.io.File;
  28. import java.io.FilenameFilter;
  29. import java.io.StringWriter;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import java.util.List;
  33. import static org.junit.Assert.assertNotNull;
  34. @RunWith(Parameterized.class)
  35. public class TestExcelConverterSuite
  36. {
  37. /**
  38. * YK: a quick hack to exclude failing documents from the suite.
  39. */
  40. @SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
  41. private static List<String> failingFiles = Arrays.asList(
  42. /* not failing, but requires more memory */
  43. "ex45698-22488.xls" );
  44. @Parameterized.Parameters(name="{index}: {0}")
  45. public static Iterable<Object[]> files() {
  46. List<Object[]> files = new ArrayList<Object[]>();
  47. File directory = POIDataSamples.getDocumentInstance().getFile(
  48. "../spreadsheet" );
  49. for ( final File child : directory.listFiles( new FilenameFilter()
  50. {
  51. public boolean accept( File dir, String name )
  52. {
  53. return name.endsWith( ".xls" ) && !failingFiles.contains( name );
  54. }
  55. } ) )
  56. {
  57. files.add(new Object[] { child });
  58. }
  59. return files;
  60. }
  61. @Parameterized.Parameter
  62. public File child;
  63. @Test
  64. public void testFo() throws Exception
  65. {
  66. HSSFWorkbook workbook;
  67. try {
  68. workbook = ExcelToHtmlUtils.loadXls( child );
  69. } catch ( Exception exc ) {
  70. // unable to parse file -- not ExcelToFoConverter fault
  71. return;
  72. }
  73. ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(
  74. XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
  75. excelToHtmlConverter.processWorkbook( workbook );
  76. StringWriter stringWriter = new StringWriter();
  77. Transformer transformer = TransformerFactory.newInstance()
  78. .newTransformer();
  79. transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
  80. transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
  81. transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
  82. transformer.transform(
  83. new DOMSource( excelToHtmlConverter.getDocument() ),
  84. new StreamResult( stringWriter ) );
  85. assertNotNull(stringWriter.toString());
  86. }
  87. @Test
  88. public void testHtml() throws Exception
  89. {
  90. HSSFWorkbook workbook;
  91. try {
  92. workbook = ExcelToHtmlUtils.loadXls( child );
  93. } catch ( Exception exc ) {
  94. // unable to parse file -- not ExcelToFoConverter fault
  95. return;
  96. }
  97. ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(
  98. XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
  99. excelToHtmlConverter.processWorkbook( workbook );
  100. StringWriter stringWriter = new StringWriter();
  101. Transformer transformer = TransformerFactory.newInstance()
  102. .newTransformer();
  103. transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
  104. transformer.setOutputProperty( OutputKeys.INDENT, "no" );
  105. transformer.setOutputProperty( OutputKeys.METHOD, "html" );
  106. transformer.transform(
  107. new DOMSource( excelToHtmlConverter.getDocument() ),
  108. new StreamResult( stringWriter ) );
  109. assertNotNull(stringWriter.toString());
  110. }
  111. }