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.

TestHyperlink.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.hslf.model;
  16. import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.getRawText;
  17. import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.toExternalString;
  18. import static org.junit.Assert.assertEquals;
  19. import static org.junit.Assert.assertNotNull;
  20. import java.util.List;
  21. import org.apache.poi.POIDataSamples;
  22. import org.apache.poi.hslf.usermodel.*;
  23. import org.junit.Test;
  24. /**
  25. * Test Hyperlink.
  26. *
  27. * @author Yegor Kozlov
  28. */
  29. public final class TestHyperlink {
  30. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  31. @Test
  32. public void testTextRunHyperlinks() throws Exception {
  33. HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("WithLinks.ppt"));
  34. HSLFSlide slide = ppt.getSlides().get(0);
  35. List<HSLFTextParagraph> para = slide.getTextParagraphs().get(1);
  36. String rawText = toExternalString(getRawText(para), para.get(0).getRunType());
  37. String expected =
  38. "This page has two links:\n"+
  39. "http://jakarta.apache.org/poi/\n"+
  40. "\n"+
  41. "http://slashdot.org/\n"+
  42. "\n"+
  43. "In addition, its notes has one link";
  44. assertEquals(expected, rawText);
  45. List<HSLFHyperlink> links = HSLFHyperlink.find(para);
  46. assertNotNull(links);
  47. assertEquals(2, links.size());
  48. assertEquals("http://jakarta.apache.org/poi/", links.get(0).getLabel());
  49. assertEquals("http://jakarta.apache.org/poi/", links.get(0).getAddress());
  50. assertEquals("http://jakarta.apache.org/poi/", rawText.substring(links.get(0).getStartIndex(), links.get(0).getEndIndex()-1));
  51. assertEquals("http://slashdot.org/", links.get(1).getLabel());
  52. assertEquals("http://slashdot.org/", links.get(1).getAddress());
  53. assertEquals("http://slashdot.org/", rawText.substring(links.get(1).getStartIndex(), links.get(1).getEndIndex()-1));
  54. slide = ppt.getSlides().get(1);
  55. para = slide.getTextParagraphs().get(1);
  56. rawText = toExternalString(getRawText(para), para.get(0).getRunType());
  57. expected =
  58. "I have the one link:\n" +
  59. "Jakarta HSSF";
  60. assertEquals(expected, rawText);
  61. links = HSLFHyperlink.find(para);
  62. assertNotNull(links);
  63. assertEquals(1, links.size());
  64. assertEquals("Open Jakarta POI HSSF module test ", links.get(0).getLabel());
  65. assertEquals("http://jakarta.apache.org/poi/hssf/", links.get(0).getAddress());
  66. assertEquals("Jakarta HSSF", rawText.substring(links.get(0).getStartIndex(), links.get(0).getEndIndex()-1));
  67. }
  68. }