]> source.dussan.org Git - poi.git/commitdiff
Bugzilla 53043 - don't duplicate hyperlink relationships when saving XSSF file
authorYegor Kozlov <yegor@apache.org>
Sat, 21 Apr 2012 12:50:57 +0000 (12:50 +0000)
committerYegor Kozlov <yegor@apache.org>
Sat, 21 Apr 2012 12:50:57 +0000 (12:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1328653 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java

index d2a6d64e3b62a57fc86c8e165007c47b685f78d5..d96617f5c0d43505f928d815cd2971dcfe0f2533 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.9-beta1" date="2012-??-??">
+          <action dev="poi-developers" type="fix">53043 - don't duplicate hyperlink relationships when saving XSSF file</action>
           <action dev="poi-developers" type="fix">53101 - fixed evaluation of SUM over cell range &gt; 255</action>
           <action dev="poi-developers" type="fix">49529 - avoid exception when cloning sheets with no drawing records and initialized drawing patriarch</action>
         </release>
index 84a9ea51d805b6c3115ec31a93b09e329e844d86..dfe3423254c6e785ec34c4672d3caacf7b45b22b 100644 (file)
@@ -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());
index 0358133a2354aea7a78085f6288635bf88803e0e..cf51b40d86aea4bb82d727f265bd4abcb600d8ad 100644 (file)
@@ -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());