utafrali commented on code in PR #1584:
URL: https://github.com/apache/poi/pull/1584#discussion_r4652128198
##########
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java:
##########
@@ -678,6 +683,98 @@ void testRemoveLoadedHyperlinkSharingItsRelationship()
throws IOException, Inval
}
}
+ @Test
+ void testAddRemovedLoadedHyperlinkAgain() throws IOException,
InvalidFormatException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFSheet sheet = wb.createSheet();
+ XSSFHyperlink link =
wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
+ link.setAddress("https://example.com/page");
+ link.setLocation("section");
+ sheet.createRow(0).createCell(0).setHyperlink(link);
+
+ try (XSSFWorkbook wb2 =
XSSFTestDataSamples.writeOutAndReadBack(wb)) {
+ // move the hyperlink from A1 to A2 by removing and adding the
same object
+ XSSFSheet sheet2 = wb2.getSheetAt(0);
+ XSSFHyperlink loaded = sheet2.getHyperlink(0, 0);
+ sheet2.getRow(0).getCell(0).removeHyperlink();
+ sheet2.createRow(1).createCell(0).setHyperlink(loaded);
+
+ try (XSSFWorkbook wb3 =
XSSFTestDataSamples.writeOutAndReadBack(wb2)) {
+ XSSFSheet sheet3 = wb3.getSheetAt(0);
+ assertNull(sheet3.getHyperlink(0, 0));
+ assertEquals("https://example.com/page#section",
sheet3.getHyperlink(1, 0).getAddress());
+ assertEquals(1, hyperlinkTargets(sheet3).size());
+ }
+ }
+ }
+ }
+
+ @Test
+ void testSetAddressOfRemovedHyperlink() throws IOException,
InvalidFormatException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFSheet sheet = wb.createSheet();
+ XSSFHyperlink link =
wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
+ link.setAddress("https://example.com/old");
+ sheet.createRow(0).createCell(0).setHyperlink(link);
+
+ try (XSSFWorkbook wb2 =
XSSFTestDataSamples.writeOutAndReadBack(wb)) {
+ XSSFSheet sheet2 = wb2.getSheetAt(0);
+ XSSFHyperlink loaded = sheet2.getHyperlink(0, 0);
+ sheet2.removeHyperlink(loaded);
+ loaded.setAddress("https://example.org/new");
+
+ // the removed hyperlink doesn't add a relationship to the
sheet it was removed from
+ assertTrue(hyperlinkTargets(sheet2).isEmpty());
+
+ // and gets one for its new address once it is added again
+ sheet2.getRow(0).getCell(0).setHyperlink(loaded);
+ try (XSSFWorkbook wb3 =
XSSFTestDataSamples.writeOutAndReadBack(wb2)) {
+ XSSFSheet sheet3 = wb3.getSheetAt(0);
+ assertEquals("https://example.org/new",
sheet3.getHyperlink(0, 0).getAddress());
+ assertEquals(List.of("https://example.org/new"),
hyperlinkTargets(sheet3));
+ }
+ }
+ }
+ }
+
+ @Test
+ void testCloneSheetWithLoadedHyperlinkKeepsWorkbookRelationships() throws
IOException, InvalidFormatException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFSheet sheet = wb.createSheet("src");
Review Comment:
The assertion
`assertNotNull(wb2.getPackagePart().getRelationship(linkRelId))` relies on the
sheet's hyperlink relationship ID (e.g. `rId1`) coincidentally colliding with
one of the workbook's own relationship IDs. If the OPC layer ever assigns
non-overlapping ID sequences to sheet vs. workbook parts, this assert would
vacuously pass while the test's premise (ID collision) no longer holds.
Consider adding a comment explaining why the overlap is expected, or
force-creating conditions that guarantee it, so the test stays meaningful.
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java:
##########
@@ -161,15 +161,26 @@ public boolean needsRelationToo() {
* Generates the relation if required
*/
protected void generateRelationIfNeeded(PackagePart sheetPart) {
- if (_externalRel == null && needsRelationToo()) {
+ if (!needsRelationToo()) {
+ return;
+ }
+ if (_externalRel == null) {
// Generate the relation
_externalRel =
sheetPart.addExternalRelationship(_location,
XSSFRelation.SHEET_HYPERLINKS.getRelation());
- _relShared = false;
-
- // Update the r:id
- _ctHyperlink.setId(_externalRel.getId());
+ } else if (sheetPart.equals(_externalRel.getSource()) &&
sheetPart.getRelationship(_externalRel.getId()) == null) {
+ // the relationship was removed together with this hyperlink,
which was added to the sheet
+ // again since, e.g. to move it to another cell. The old target is
used, as the location
+ // is still written as a separate attribute.
+ _externalRel = sheetPart.addExternalRelationship(
+ _externalRel.getTargetURI().toString(),
XSSFRelation.SHEET_HYPERLINKS.getRelation());
Review Comment:
The `else if` branch handles the case where the relationship was removed
from the same sheet and needs to be recreated. However, when a hyperlink is
moved from sheet A to sheet B (a cross-sheet move),
`sheetPart.equals(_externalRel.getSource())` will be `false`, causing it to
fall through to `else { return; }` without creating a new relationship on the
destination sheet. The `r:id` in `_ctHyperlink` would then reference a
non-existent relationship on the new sheet, which is exactly the error this PR
fixes for same-sheet moves.
This isn't a regression introduced here (the old code would also silently
skip that case), but cross-sheet moves remain broken. Worth a follow-up issue.
##########
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java:
##########
@@ -678,6 +683,98 @@ void testRemoveLoadedHyperlinkSharingItsRelationship()
throws IOException, Inval
}
}
+ @Test
+ void testAddRemovedLoadedHyperlinkAgain() throws IOException,
InvalidFormatException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFSheet sheet = wb.createSheet();
+ XSSFHyperlink link =
wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
+ link.setAddress("https://example.com/page");
+ link.setLocation("section");
+ sheet.createRow(0).createCell(0).setHyperlink(link);
+
+ try (XSSFWorkbook wb2 =
XSSFTestDataSamples.writeOutAndReadBack(wb)) {
+ // move the hyperlink from A1 to A2 by removing and adding the
same object
+ XSSFSheet sheet2 = wb2.getSheetAt(0);
Review Comment:
`testAddRemovedLoadedHyperlinkAgain` removes the hyperlink via
`cell.removeHyperlink()` rather than `sheet.removeHyperlink(hyperlink)`. That
means `hyperlink.setSheet(null)` (the fix added in `XSSFSheet.removeHyperlink`)
is not exercised by this test. The test still validates the
`generateRelationIfNeeded` path, which is the primary fix, but the
`setSheet(null)` guard is only covered by `testSetAddressOfRemovedHyperlink`.
Worth noting in a comment, or adding a second variant that also goes through
`sheet.removeHyperlink(hyperlink)`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]