1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPhoneticRun;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
/**
* Test {@link SharedStringsTable}, the cache of strings in a workbook
*/
public final class TestSharedStringsTable {
@SuppressWarnings("deprecation")
@Test
public void testCreateNew() {
SharedStringsTable sst = new SharedStringsTable();
CTRst st;
int idx;
// Check defaults
assertNotNull(sst.getItems());
assertEquals(0, sst.getItems().size());
assertEquals(0, sst.getCount());
assertEquals(0, sst.getUniqueCount());
st = CTRst.Factory.newInstance();
st.setT("Hello, World!");
idx = sst.addEntry(st);
assertEquals(0, idx);
assertEquals(1, sst.getCount());
assertEquals(1, sst.getUniqueCount());
//add the same entry again
idx = sst.addEntry(st);
assertEquals(0, idx);
assertEquals(2, sst.getCount());
assertEquals(1, sst.getUniqueCount());
//and again
idx = sst.addEntry(st);
assertEquals(0, idx);
assertEquals(3, sst.getCount());
assertEquals(1, sst.getUniqueCount());
st = CTRst.Factory.newInstance();
st.setT("Second string");
idx = sst.addEntry(st);
assertEquals(1, idx);
assertEquals(4, sst.getCount());
assertEquals(2, sst.getUniqueCount());
//add the same entry again
idx = sst.addEntry(st);
assertEquals(1, idx);
assertEquals(5, sst.getCount());
assertEquals(2, sst.getUniqueCount());
st = CTRst.Factory.newInstance();
CTRElt r = st.addNewR();
CTRPrElt pr = r.addNewRPr();
pr.addNewColor().setRgb(new byte[]{(byte)0xFF, 0, 0}); //red
pr.addNewI().setVal(true); //bold
pr.addNewB().setVal(true); //italic
r.setT("Second string");
idx = sst.addEntry(st);
assertEquals(2, idx);
assertEquals(6, sst.getCount());
assertEquals(3, sst.getUniqueCount());
idx = sst.addEntry(st);
assertEquals(2, idx);
assertEquals(7, sst.getCount());
assertEquals(3, sst.getUniqueCount());
//OK. the sst table is filled, check the contents
assertEquals(3, sst.getItems().size());
assertEquals("Hello, World!", new XSSFRichTextString(sst.getEntryAt(0)).toString());
assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(1)).toString());
assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(2)).toString());
}
@Test
public void testCreateUsingRichTextStrings() {
SharedStringsTable sst = new SharedStringsTable();
// Check defaults
assertNotNull(sst.getSharedStringItems());
assertEquals(0, sst.getSharedStringItems().size());
assertEquals(0, sst.getCount());
assertEquals(0, sst.getUniqueCount());
int idx;
XSSFRichTextString rts = new XSSFRichTextString("Hello, World!");
idx = sst.addSharedStringItem(rts);
assertEquals(0, idx);
assertEquals(1, sst.getCount());
assertEquals(1, sst.getUniqueCount());
//add the same entry again
idx = sst.addSharedStringItem(rts);
assertEquals(0, idx);
assertEquals(2, sst.getCount());
assertEquals(1, sst.getUniqueCount());
//and again
idx = sst.addSharedStringItem(rts);
assertEquals(0, idx);
assertEquals(3, sst.getCount());
assertEquals(1, sst.getUniqueCount());
rts = new XSSFRichTextString("Second string");
idx = sst.addSharedStringItem(rts);
assertEquals(1, idx);
assertEquals(4, sst.getCount());
assertEquals(2, sst.getUniqueCount());
//add the same entry again
idx = sst.addSharedStringItem(rts);
assertEquals(1, idx);
assertEquals(5, sst.getCount());
assertEquals(2, sst.getUniqueCount());
rts = new XSSFRichTextString("Second string");
XSSFFont font = new XSSFFont();
font.setFontName("Arial");
font.setBold(true);
rts.applyFont(font);
idx = sst.addSharedStringItem(rts);
assertEquals(2, idx);
assertEquals(6, sst.getCount());
assertEquals(3, sst.getUniqueCount());
idx = sst.addSharedStringItem(rts);
assertEquals(2, idx);
assertEquals(7, sst.getCount());
assertEquals(3, sst.getUniqueCount());
//OK. the sst table is filled, check the contents
assertEquals(3, sst.getSharedStringItems().size());
assertEquals("Hello, World!", sst.getItemAt(0).toString());
assertEquals("Second string", sst.getItemAt(1).toString());
assertEquals("Second string", sst.getItemAt(2).toString());
}
@Test
@SuppressWarnings("deprecation")
public void testReadWrite() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
SharedStringsTable sst1 = wb1.getSharedStringSource();
//serialize, read back and compare with the original
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
SharedStringsTable sst2 = wb2.getSharedStringSource();
assertEquals(sst1.getCount(), sst2.getCount());
assertEquals(sst1.getUniqueCount(), sst2.getUniqueCount());
List<CTRst> items1 = sst1.getItems();
List<CTRst> items2 = sst2.getItems();
assertEquals(items1.size(), items2.size());
for (int i = 0; i < items1.size(); i++) {
CTRst st1 = items1.get(i);
CTRst st2 = items2.get(i);
assertEquals(st1.toString(), st2.toString());
// ensure that CTPhoneticRun is loaded by the ooxml test suite so that it is included in poi-ooxml-schemas
List<CTPhoneticRun> phList = st1.getRPhList();
assertEquals(phList, st2.getRPhList());
// this code is required to make sure all the necessary classes are loaded
CTPhoneticRun run = CTPhoneticRun.Factory.newInstance();
run.setEb(12);
assertEquals(12, run.getEb());
}
XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
assertNotNull(wb3);
wb3.close();
wb2.close();
wb1.close();
}
/**
* Test for Bugzilla 48936
*
* A specific sequence of strings can result in broken CDATA section in sharedStrings.xml file.
*/
@Test
public void testBug48936() throws IOException {
Workbook w1 = new XSSFWorkbook();
Sheet s = w1.createSheet();
int i = 0;
Path path = XSSFTestDataSamples.getSampleFile("48936-strings.txt").toPath();
final List<String> lst;
try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
lst = lines
.map(String::trim)
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(Collectors.toList());
}
for (String str : lst) {
s.createRow(i++).createCell(0).setCellValue(str);
}
Workbook w2 = XSSFTestDataSamples.writeOutAndReadBack(w1);
w1.close();
s = w2.getSheetAt(0);
i = 0;
for (String str : lst) {
String val = s.getRow(i++).getCell(0).getStringCellValue();
assertEquals(str, val);
}
Workbook w3 = XSSFTestDataSamples.writeOutAndReadBack(w2);
w2.close();
assertNotNull(w3);
w3.close();
}
}
|