From 306579455bfd260adfb33b0b02dc262696a744fc Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sat, 21 Apr 2012 12:50:57 +0000 Subject: [PATCH] Bugzilla 53043 - don't duplicate hyperlink relationships when saving XSSF file git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1328653 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/xssf/usermodel/XSSFHyperlink.java | 2 +- .../poi/xssf/usermodel/TestXSSFHyperlink.java | 46 ++++++++++++++++--- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index d2a6d64e3b..d96617f5c0 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 53043 - don't duplicate hyperlink relationships when saving XSSF file 53101 - fixed evaluation of SUM over cell range > 255 49529 - avoid exception when cloning sheets with no drawing records and initialized drawing patriarch diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java index 84a9ea51d8..dfe3423254 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java @@ -111,7 +111,7 @@ public class XSSFHyperlink implements Hyperlink { * Generates the relation if required */ protected void generateRelationIfNeeded(PackagePart sheetPart) { - if (needsRelationToo()) { + if (_externalRel == null && needsRelationToo()) { // Generate the relation PackageRelationship rel = sheetPart.addExternalRelationship(_location, XSSFRelation.SHEET_HYPERLINKS.getRelation()); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java index 0358133a23..cf51b40d86 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java @@ -17,6 +17,8 @@ package org.apache.poi.xssf.usermodel; +import org.apache.poi.openxml4j.opc.PackageRelationship; +import org.apache.poi.openxml4j.opc.PackageRelationshipCollection; import org.apache.poi.ss.usermodel.BaseTestHyperlink; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CreationHelper; @@ -50,20 +52,52 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink { doTestHyperlinkContents(sheet); } - public void testCreate() { + public void testCreate() throws Exception { XSSFWorkbook workbook = new XSSFWorkbook(); + XSSFSheet sheet = workbook.createSheet(); + XSSFRow row = sheet.createRow(0); XSSFCreationHelper createHelper = workbook.getCreationHelper(); - String[] validURLs = { + String[] urls = { "http://apache.org", "www.apache.org", "/temp", "c:/temp", "http://apache.org/default.php?s=isTramsformed&submit=Search&la=*&li=*"}; - for(String s : validURLs){ - createHelper.createHyperlink(Hyperlink.LINK_URL).setAddress(s); + for(int i = 0; i < urls.length; i++){ + String s = urls[i]; + XSSFHyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL); + link.setAddress(s); + + XSSFCell cell = row.createCell(i); + cell.setHyperlink(link); + } + workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); + sheet = workbook.getSheetAt(0); + PackageRelationshipCollection rels = sheet.getPackagePart().getRelationships(); + assertEquals(urls.length, rels.size()); + for(int i = 0; i < rels.size(); i++){ + PackageRelationship rel = rels.getRelationship(i); + // there should be a relationship for each URL + assertEquals(urls[i], rel.getTargetURI().toString()); } + // Bugzilla 53041: Hyperlink relations are duplicated when saving XSSF file + workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); + sheet = workbook.getSheetAt(0); + rels = sheet.getPackagePart().getRelationships(); + assertEquals(urls.length, rels.size()); + for(int i = 0; i < rels.size(); i++){ + PackageRelationship rel = rels.getRelationship(i); + // there should be a relationship for each URL + assertEquals(urls[i], rel.getTargetURI().toString()); + } + } + + public void testInvalidURLs() { + XSSFWorkbook workbook = new XSSFWorkbook(); + XSSFCreationHelper createHelper = workbook.getCreationHelper(); + String[] invalidURLs = { "http:\\apache.org", "www.apache .org", @@ -74,12 +108,12 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink { createHelper.createHyperlink(Hyperlink.LINK_URL).setAddress(s); fail("expected IllegalArgumentException: " + s); } catch (IllegalArgumentException e){ - + } } } - public void testLoadSave() { + public void testLoadSave() { XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx"); CreationHelper createHelper = workbook.getCreationHelper(); assertEquals(3, workbook.getNumberOfSheets()); -- 2.39.5