Author: centic
Date: Sat Jan 11 09:23:23 2025
New Revision: 1923050

URL: http://svn.apache.org/viewvc?rev=1923050&view=rev
Log:
Bug 69265: Add test which verifies that Hyperlink Type "Email" is not supported 
for HSSF

Also update JavaDoc slightly to describe
the special handling of HyperlinkType.EMAIL

Modified:
    
poi/trunk/poi/src/main/java/org/apache/poi/common/usermodel/HyperlinkType.java
    
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestHyperlink.java

Modified: 
poi/trunk/poi/src/main/java/org/apache/poi/common/usermodel/HyperlinkType.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/common/usermodel/HyperlinkType.java?rev=1923050&r1=1923049&r2=1923050&view=diff
==============================================================================
--- 
poi/trunk/poi/src/main/java/org/apache/poi/common/usermodel/HyperlinkType.java 
(original)
+++ 
poi/trunk/poi/src/main/java/org/apache/poi/common/usermodel/HyperlinkType.java 
Sat Jan 11 09:23:23 2025
@@ -25,7 +25,7 @@ public enum HyperlinkType {
     /** Not a hyperlink */
     @Internal
     NONE(-1),
-    
+
     /**
      * Link to an existing file or web page
      */
@@ -37,7 +37,10 @@ public enum HyperlinkType {
     DOCUMENT(2),
 
     /**
-     * Link to an E-mail address
+     * Link to an E-mail address.
+     *
+     * Please note that this currently only works if the address in the 
hyperlink
+     * uses the prefix "mailto:"; as the binary formats do not persis this type.
      */
     EMAIL(3),
 
@@ -45,13 +48,13 @@ public enum HyperlinkType {
      * Link to a file
      */
     FILE(4);
-    
-    
+
+
     /** @deprecated POI 3.15 beta 3 */
     @Internal(since="3.15 beta 3")
     @Deprecated
     private final int code;
-    
+
     /**
      * The codes don't have any real meaning.
      * They are bytes that are read in and written out from HSSF, HSLF, XSSF, 
and XSLF are different
@@ -66,7 +69,7 @@ public enum HyperlinkType {
     HyperlinkType(int code) {
         this.code = code;
     }
-    
+
     /**
      * @deprecated POI 3.15 beta 3
      *

Modified: 
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestHyperlink.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestHyperlink.java?rev=1923050&r1=1923049&r2=1923050&view=diff
==============================================================================
--- 
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestHyperlink.java 
(original)
+++ 
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestHyperlink.java 
Sat Jan 11 09:23:23 2025
@@ -21,11 +21,14 @@ import static org.junit.jupiter.api.Asse
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotSame;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.List;
 
 import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.SpreadsheetVersion;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -142,5 +145,119 @@ public abstract class BaseTestHyperlink
         wb.close();
     }
 
+    @Test
+    void testHyperlinkEmailType69265_https() throws IOException {
+        boolean isHSSF = _testDataProvider.getSpreadsheetVersion() == 
SpreadsheetVersion.EXCEL97;
+
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            try (Workbook workbook = _testDataProvider.createWorkbook()) {
+                Sheet sheet = workbook.createSheet("Hyperlink Example");
+
+                Row row = sheet.createRow(0);
+                Cell cell = row.createCell(0);
+
+                // Create a hyperlink
+                CreationHelper createHelper = workbook.getCreationHelper();
+                Hyperlink hyperlink = 
createHelper.createHyperlink(HyperlinkType.EMAIL);
+                hyperlink.setLabel("mylabel");
+                hyperlink.setAddress("https://www.example.com";);
+
+                // Set the label and the hyperlink
+                cell.setCellValue("Click here");
+                cell.setHyperlink(hyperlink);
+
+                // Get the cell value and hyperlink address
+                assertEquals("Click here", cell.getStringCellValue());
+                Hyperlink cellHyperlink = cell.getHyperlink();
+                assertEquals("https://www.example.com";, 
cellHyperlink.getAddress());
+
+                // HSSF does not support Email, thus falls back to URL, HSSF 
also uses a hardcoded "label"
+                assertEquals(
+                        isHSSF ? HyperlinkType.URL : HyperlinkType.EMAIL,
+                        cellHyperlink.getType());
+                assertEquals(
+                        isHSSF ? "url" : "mylabel",
+                        cellHyperlink.getLabel());
+
+                workbook.write(out);
+            }
+
+            out.flush();
+
+            try (Workbook wbBack = WorkbookFactory.create(new 
ByteArrayInputStream(out.toByteArray()))) {
+                Sheet sheet = wbBack.getSheet("Hyperlink Example");
+                Row row = sheet.getRow(0);
+                Cell cell = row.getCell(0);
+
+                Hyperlink hyperlink = cell.getHyperlink();
+
+                // when not using "mailto:";, it is reverted back to URL 
currently
+                assertEquals(
+                        HyperlinkType.URL,
+                        hyperlink.getType());
+                assertEquals(
+                        isHSSF ? "url" : "mylabel",
+                        hyperlink.getLabel());
+            }
+        }
+    }
+
+    @Test
+    void testHyperlinkEmailType69265_mailto() throws IOException {
+        boolean isHSSF = _testDataProvider.getSpreadsheetVersion() == 
SpreadsheetVersion.EXCEL97;
+
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            try (Workbook workbook = _testDataProvider.createWorkbook()) {
+                Sheet sheet = workbook.createSheet("Hyperlink Example");
+
+                Row row = sheet.createRow(0);
+                Cell cell = row.createCell(0);
+
+                // Create a hyperlink
+                CreationHelper createHelper = workbook.getCreationHelper();
+                Hyperlink hyperlink = 
createHelper.createHyperlink(HyperlinkType.EMAIL);
+                hyperlink.setLabel("mylabel");
+                hyperlink.setAddress("mailto://www.example.com";);
+
+                // Set the label and the hyperlink
+                cell.setCellValue("Click here");
+                cell.setHyperlink(hyperlink);
+
+                // Get the cell value and hyperlink address
+                assertEquals("Click here", cell.getStringCellValue());
+                Hyperlink cellHyperlink = cell.getHyperlink();
+                assertEquals("mailto://www.example.com";, 
cellHyperlink.getAddress());
+
+                // "mailto:"; is converted to type "EMAIL"
+                assertEquals(
+                        HyperlinkType.EMAIL,
+                        cellHyperlink.getType());
+                assertEquals(
+                        isHSSF ? "url" : "mylabel",
+                        cellHyperlink.getLabel());
+
+                workbook.write(out);
+            }
+
+            out.flush();
+
+            try (Workbook wbBack = WorkbookFactory.create(new 
ByteArrayInputStream(out.toByteArray()))) {
+                Sheet sheet = wbBack.getSheet("Hyperlink Example");
+                Row row = sheet.getRow(0);
+                Cell cell = row.getCell(0);
+
+                Hyperlink hyperlink = cell.getHyperlink();
+
+                // "mailto:"; is converted to type "EMAIL"
+                assertEquals(
+                        HyperlinkType.EMAIL,
+                        hyperlink.getType());
+                assertEquals(
+                        isHSSF ? "url" : "mylabel",
+                        hyperlink.getLabel());
+            }
+        }
+    }
+
     public abstract Hyperlink copyHyperlink(Hyperlink link);
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to