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
This commit is contained in:
Yegor Kozlov 2012-04-21 12:50:57 +00:00
parent 6fc404c4a6
commit 87099af3cb
3 changed files with 42 additions and 7 deletions

View 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>

View 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());

View 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,19 +52,51 @@ 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",
@ -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());