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.

TestVisioExtractor.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.hdgf.extractor;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import org.apache.poi.POIDataSamples;
  21. import org.apache.poi.hdgf.HDGFDiagram;
  22. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  23. import org.junit.Test;
  24. public final class TestVisioExtractor {
  25. private static final POIDataSamples _dgTests = POIDataSamples.getDiagramInstance();
  26. private final String defFilename = "Test_Visio-Some_Random_Text.vsd";
  27. private final int defTextChunks = 5;
  28. /**
  29. * Test the 3 different ways of creating one
  30. */
  31. @Test
  32. public void testCreation() throws IOException {
  33. VisioTextExtractor extractor1 = openExtractor(defFilename);
  34. assertNotNull(extractor1);
  35. assertNotNull(extractor1.getAllText());
  36. assertEquals(defTextChunks, extractor1.getAllText().length);
  37. extractor1.close();
  38. InputStream is2 = _dgTests.openResourceAsStream(defFilename);
  39. POIFSFileSystem poifs2 = new POIFSFileSystem(is2);
  40. is2.close();
  41. VisioTextExtractor extractor2 = new VisioTextExtractor(poifs2);
  42. assertNotNull(extractor2);
  43. assertNotNull(extractor2.getAllText());
  44. assertEquals(defTextChunks, extractor2.getAllText().length);
  45. extractor2.close();
  46. poifs2.close();
  47. InputStream is3 = _dgTests.openResourceAsStream(defFilename);
  48. POIFSFileSystem poifs3 = new POIFSFileSystem(is3);
  49. is3.close();
  50. HDGFDiagram hdgf3 = new HDGFDiagram(poifs3);
  51. VisioTextExtractor extractor3 = new VisioTextExtractor(hdgf3);
  52. assertNotNull(extractor3);
  53. assertNotNull(extractor3.getAllText());
  54. assertEquals(defTextChunks, extractor3.getAllText().length);
  55. extractor3.close();
  56. hdgf3.close();
  57. poifs3.close();
  58. }
  59. @Test
  60. public void testExtraction() throws Exception {
  61. VisioTextExtractor extractor = openExtractor(defFilename);
  62. // Check the array fetch
  63. String[] text = extractor.getAllText();
  64. assertNotNull(text);
  65. assertEquals(defTextChunks, text.length);
  66. assertEquals("text\n", text[0]);
  67. assertEquals("View\n", text[1]);
  68. assertEquals("Test View\n", text[2]);
  69. assertEquals("I am a test view\n", text[3]);
  70. assertEquals("Some random text, on a page\n", text[4]);
  71. // And the all-in fetch
  72. String textS = extractor.getText();
  73. assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page\n", textS);
  74. extractor.close();
  75. }
  76. @Test
  77. public void testProblemFiles() throws Exception {
  78. String[] files = {
  79. "44594.vsd", "44594-2.vsd",
  80. "ShortChunk1.vsd", "ShortChunk2.vsd", "ShortChunk3.vsd",
  81. "NegativeChunkLength.vsd", "NegativeChunkLength2.vsd"
  82. };
  83. for(String file : files){
  84. VisioTextExtractor ex = openExtractor(file);
  85. ex.getText();
  86. ex.close();
  87. }
  88. }
  89. private VisioTextExtractor openExtractor(String fileName) throws IOException {
  90. try (InputStream is = _dgTests.openResourceAsStream(fileName)) {
  91. return new VisioTextExtractor(is);
  92. }
  93. }
  94. }