Line 0
Link Here
|
|
|
1 |
/* ==================================================================== |
2 |
Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
contributor license agreements. See the NOTICE file distributed with |
4 |
this work for additional information regarding copyright ownership. |
5 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
(the "License"); you may not use this file except in compliance with |
7 |
the License. You may obtain a copy of the License at |
8 |
|
9 |
http://www.apache.org/licenses/LICENSE-2.0 |
10 |
|
11 |
Unless required by applicable law or agreed to in writing, software |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
See the License for the specific language governing permissions and |
15 |
limitations under the License. |
16 |
==================================================================== */ |
17 |
|
18 |
package org.apache.poi.xssf.usermodel; |
19 |
|
20 |
import static org.junit.Assert.assertEquals; |
21 |
import static org.junit.Assert.assertNotNull; |
22 |
|
23 |
import java.io.IOException; |
24 |
|
25 |
import org.apache.poi.common.usermodel.HyperlinkType; |
26 |
import org.apache.poi.ss.usermodel.Row; |
27 |
import org.apache.poi.ss.util.CellAddress; |
28 |
import org.apache.poi.ss.util.CellRangeAddress; |
29 |
import org.apache.poi.xssf.XSSFTestDataSamples; |
30 |
import org.junit.After; |
31 |
import org.junit.Before; |
32 |
import org.junit.Test; |
33 |
|
34 |
/** |
35 |
* @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=56527">bug 56527</a> |
36 |
* @throws Exception |
37 |
*/ |
38 |
public class TestCreateMultiCellHyperlink { |
39 |
|
40 |
private static final String URL = "http://www.multicell.com/"; |
41 |
|
42 |
// multi-cell hyperlink 2x3 B3:D4 |
43 |
final CellRangeAddress B3_D4 = CellRangeAddress.valueOf("B3:D4"); |
44 |
final int firstRow = B3_D4.getFirstRow(); |
45 |
final int firstCol = B3_D4.getFirstColumn(); |
46 |
final int lastRow = B3_D4.getLastRow(); |
47 |
final int lastCol = B3_D4.getLastColumn(); |
48 |
|
49 |
private XSSFWorkbook wb; |
50 |
private XSSFSheet sheet; |
51 |
private XSSFHyperlink hyperlink; |
52 |
|
53 |
@Before |
54 |
public void setUp() { |
55 |
wb = new XSSFWorkbook(); |
56 |
sheet = wb.createSheet(); |
57 |
|
58 |
// Create a 5x3 cell range (A1:E5) for a multi-cell hyperlink |
59 |
for (int r = 0; r < 5; r++) { |
60 |
Row row = sheet.createRow(r); |
61 |
for (int c = 1; c < 5; c++) { |
62 |
row.createCell(c); |
63 |
} |
64 |
} |
65 |
|
66 |
XSSFCreationHelper creationHelper = wb.getCreationHelper(); |
67 |
hyperlink = creationHelper.createHyperlink(HyperlinkType.URL); |
68 |
hyperlink.setAddress(URL); |
69 |
sheet.addHyperlink(hyperlink); |
70 |
} |
71 |
|
72 |
@After |
73 |
public void tearDown() throws IOException { |
74 |
wb.close(); |
75 |
} |
76 |
|
77 |
public void writeOutAndReadBack() throws IOException { |
78 |
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb); |
79 |
wb.close(); |
80 |
wb = wb2; |
81 |
sheet = wb.getSheetAt(0); |
82 |
hyperlink = sheet.getHyperlinkList().get(0); |
83 |
} |
84 |
|
85 |
@Test |
86 |
public void testCreateMuliCellHyperlink1() throws IOException { |
87 |
hyperlink.setFirstRow(firstRow); |
88 |
hyperlink.setFirstColumn(firstCol); |
89 |
hyperlink.setLastRow(lastRow); |
90 |
hyperlink.setLastColumn(lastCol); |
91 |
hyperlink.setAddress(URL); |
92 |
|
93 |
doChecks(); |
94 |
writeOutAndReadBack(); |
95 |
doChecks(); |
96 |
} |
97 |
|
98 |
@Test |
99 |
public void testCreateMuliCellHyperlink2() throws IOException { |
100 |
hyperlink.setFirstColumn(firstCol); |
101 |
hyperlink.setFirstRow(firstRow); |
102 |
hyperlink.setLastColumn(lastCol); |
103 |
hyperlink.setLastRow(lastRow); |
104 |
|
105 |
doChecks(); |
106 |
writeOutAndReadBack(); |
107 |
doChecks(); |
108 |
} |
109 |
|
110 |
@Test |
111 |
public void testCreateMuliCellHyperlink3() throws IOException { |
112 |
hyperlink.setLastRow(lastRow); |
113 |
hyperlink.setLastColumn(lastCol); |
114 |
hyperlink.setFirstRow(firstRow); |
115 |
hyperlink.setFirstColumn(firstCol); |
116 |
|
117 |
doChecks(); |
118 |
writeOutAndReadBack(); |
119 |
doChecks(); |
120 |
} |
121 |
|
122 |
@Test |
123 |
public void testCreateMuliCellHyperlink4() throws IOException { |
124 |
hyperlink.setLastColumn(lastCol); |
125 |
hyperlink.setLastRow(lastRow); |
126 |
hyperlink.setFirstColumn(firstCol); |
127 |
hyperlink.setFirstRow(firstRow); |
128 |
|
129 |
doChecks(); |
130 |
writeOutAndReadBack(); |
131 |
doChecks(); |
132 |
} |
133 |
|
134 |
|
135 |
private void doChecks() { |
136 |
for (int r = firstRow; r <= lastRow; r++) { |
137 |
XSSFRow row = sheet.getRow(r); |
138 |
for(int c = firstCol; c <= lastCol; c++) { |
139 |
CellAddress addr = new CellAddress(r, c); |
140 |
XSSFCell cell = row.getCell(c); |
141 |
XSSFHyperlink hyp = cell.getHyperlink(); |
142 |
assertNotNull("Expected hyperlink in " + addr, hyp); |
143 |
|
144 |
assertEquals("B3:D4", hyp.getCellRef()); |
145 |
|
146 |
// Check the methods getFistRow(), getFirstColumn(), getLastRow(), getLastColumn() |
147 |
assertEquals(firstRow, hyperlink.getFirstRow()); |
148 |
assertEquals(firstCol, hyperlink.getFirstColumn()); |
149 |
assertEquals(lastRow, hyperlink.getLastRow()); |
150 |
assertEquals(lastCol, hyperlink.getLastColumn()); |
151 |
|
152 |
assertEquals(URL, hyp.getAddress()); |
153 |
|
154 |
// cell.getHyperlink() and sheet.getHyperlink(CellAddress) should return the same hyperlink |
155 |
assertEquals(hyperlink, sheet.getHyperlink(addr)); |
156 |
} |
157 |
} |
158 |
} |
159 |
} |
160 |
native |