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.

TestWordToConverterSuite.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.hwpf.converter;
  16. import java.io.File;
  17. import java.io.FilenameFilter;
  18. import java.io.StringWriter;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.List;
  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 org.apache.poi.POIDataSamples;
  28. import org.apache.poi.hwpf.HWPFDocumentCore;
  29. import org.apache.poi.util.XMLHelper;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.junit.runners.Parameterized;
  33. import static org.junit.Assert.assertNotNull;
  34. @RunWith(Parameterized.class)
  35. public class TestWordToConverterSuite
  36. {
  37. /**
  38. * YK: a quick hack to exclude failing documents from the suite.
  39. */
  40. private static List<String> failingFiles = Arrays
  41. .asList( "ProblemExtracting.doc" );
  42. @Parameterized.Parameters(name="{index}: {0}")
  43. public static Iterable<Object[]> files() {
  44. List<Object[]> files = new ArrayList<Object[]>();
  45. File directory = POIDataSamples.getDocumentInstance().getFile(
  46. "../document" );
  47. for ( final File child : directory.listFiles( new FilenameFilter()
  48. {
  49. public boolean accept( File dir, String name )
  50. {
  51. return name.endsWith( ".doc" ) && !failingFiles.contains( name );
  52. }
  53. } ) )
  54. {
  55. files.add(new Object[] { child });
  56. }
  57. return files;
  58. }
  59. @Parameterized.Parameter
  60. public File child;
  61. @Test
  62. public void testFo() throws Exception {
  63. HWPFDocumentCore hwpfDocument;
  64. try {
  65. hwpfDocument = AbstractWordUtils.loadDoc( child );
  66. } catch ( Exception exc ) {
  67. return;
  68. }
  69. WordToFoConverter wordToFoConverter = new WordToFoConverter(
  70. XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
  71. wordToFoConverter.processDocument( hwpfDocument );
  72. StringWriter stringWriter = new StringWriter();
  73. Transformer transformer = TransformerFactory.newInstance()
  74. .newTransformer();
  75. transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
  76. transformer.setOutputProperty( OutputKeys.INDENT, "false" );
  77. transformer.transform(
  78. new DOMSource( wordToFoConverter.getDocument() ),
  79. new StreamResult( stringWriter ) );
  80. // no exceptions
  81. assertNotNull(stringWriter.toString());
  82. }
  83. @Test
  84. public void testHtml() throws Exception
  85. {
  86. HWPFDocumentCore hwpfDocument;
  87. try {
  88. hwpfDocument = AbstractWordUtils.loadDoc( child );
  89. } catch ( Exception exc ) {
  90. return;
  91. }
  92. WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
  93. XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
  94. wordToHtmlConverter.processDocument( hwpfDocument );
  95. StringWriter stringWriter = new StringWriter();
  96. Transformer transformer = TransformerFactory.newInstance()
  97. .newTransformer();
  98. transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
  99. transformer.setOutputProperty( OutputKeys.INDENT, "false" );
  100. transformer.setOutputProperty( OutputKeys.METHOD, "html" );
  101. transformer.transform(
  102. new DOMSource( wordToHtmlConverter.getDocument() ),
  103. new StreamResult( stringWriter ) );
  104. // no exceptions
  105. assertNotNull(stringWriter.toString());
  106. }
  107. @Test
  108. public void testText() throws Exception
  109. {
  110. HWPFDocumentCore wordDocument;
  111. try {
  112. wordDocument = AbstractWordUtils.loadDoc( child );
  113. } catch ( Exception exc ) {
  114. return;
  115. }
  116. WordToTextConverter wordToTextConverter = new WordToTextConverter(
  117. XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
  118. wordToTextConverter.processDocument( wordDocument );
  119. StringWriter stringWriter = new StringWriter();
  120. Transformer transformer = TransformerFactory.newInstance()
  121. .newTransformer();
  122. transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
  123. transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
  124. transformer.setOutputProperty( OutputKeys.METHOD, "text" );
  125. transformer.transform(
  126. new DOMSource( wordToTextConverter.getDocument() ),
  127. new StreamResult( stringWriter ) );
  128. // no exceptions
  129. assertNotNull(stringWriter.toString());
  130. }
  131. }