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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/* ====================================================================
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.hssf.usermodel;
import junit.framework.TestCase;
public final class TestHSSFOptimiser extends TestCase {
public void testDoesNoHarmIfNothingToDo() {
HSSFWorkbook wb = new HSSFWorkbook();
// New files start with 4 built in fonts, and 21 built in styles
assertEquals(4, wb.getNumberOfFonts());
assertEquals(21, wb.getNumCellStyles());
// Create a test font and style, and use them
HSSFFont f = wb.createFont();
f.setFontName("Testing");
HSSFCellStyle s = wb.createCellStyle();
s.setFont(f);
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellStyle(s);
// Should have one more than the default of each
assertEquals(5, wb.getNumberOfFonts());
assertEquals(22, wb.getNumCellStyles());
// Optimise fonts
HSSFOptimiser.optimiseFonts(wb);
assertEquals(5, wb.getNumberOfFonts());
assertEquals(22, wb.getNumCellStyles());
assertEquals(f, s.getFont(wb));
// Optimise styles
HSSFOptimiser.optimiseCellStyles(wb);
assertEquals(5, wb.getNumberOfFonts());
assertEquals(22, wb.getNumCellStyles());
assertEquals(f, s.getFont(wb));
}
public void testOptimiseFonts() {
HSSFWorkbook wb = new HSSFWorkbook();
// Add 6 fonts, some duplicates
HSSFFont f1 = wb.createFont();
f1.setFontHeight((short) 11);
f1.setFontName("Testing");
HSSFFont f2 = wb.createFont();
f2.setFontHeight((short) 22);
f2.setFontName("Also Testing");
HSSFFont f3 = wb.createFont();
f3.setFontHeight((short) 33);
f3.setFontName("Unique");
HSSFFont f4 = wb.createFont();
f4.setFontHeight((short) 11);
f4.setFontName("Testing");
HSSFFont f5 = wb.createFont();
f5.setFontHeight((short) 22);
f5.setFontName("Also Testing");
HSSFFont f6 = wb.createFont();
f6.setFontHeight((short) 66);
f6.setFontName("Also Unique");
// Use all three of the four in cell styles
assertEquals(21, wb.getNumCellStyles());
HSSFCellStyle cs1 = wb.createCellStyle();
cs1.setFont(f1);
assertEquals(5, cs1.getFontIndex());
HSSFCellStyle cs2 = wb.createCellStyle();
cs2.setFont(f4);
assertEquals(8, cs2.getFontIndex());
HSSFCellStyle cs3 = wb.createCellStyle();
cs3.setFont(f5);
assertEquals(9, cs3.getFontIndex());
HSSFCellStyle cs4 = wb.createCellStyle();
cs4.setFont(f6);
assertEquals(10, cs4.getFontIndex());
assertEquals(25, wb.getNumCellStyles());
// And three in rich text
HSSFSheet s = wb.createSheet();
HSSFRow r = s.createRow(0);
HSSFRichTextString rtr1 = new HSSFRichTextString("Test");
rtr1.applyFont(0, 2, f1);
rtr1.applyFont(3, 4, f2);
r.createCell(0).setCellValue(rtr1);
HSSFRichTextString rtr2 = new HSSFRichTextString("AlsoTest");
rtr2.applyFont(0, 2, f3);
rtr2.applyFont(3, 5, f5);
rtr2.applyFont(6, 8, f6);
r.createCell(1).setCellValue(rtr2);
// Check what we have now
assertEquals(10, wb.getNumberOfFonts());
assertEquals(25, wb.getNumCellStyles());
// Optimise
HSSFOptimiser.optimiseFonts(wb);
// Check font count
assertEquals(8, wb.getNumberOfFonts());
assertEquals(25, wb.getNumCellStyles());
// Check font use in cell styles
assertEquals(5, cs1.getFontIndex());
assertEquals(5, cs2.getFontIndex()); // duplicate of 1
assertEquals(6, cs3.getFontIndex()); // duplicate of 2
assertEquals(8, cs4.getFontIndex()); // two have gone
// And in rich text
// RTR 1 had f1 and f2, unchanged
assertEquals(5, r.getCell(0).getRichStringCellValue().getFontAtIndex(0));
assertEquals(5, r.getCell(0).getRichStringCellValue().getFontAtIndex(1));
assertEquals(6, r.getCell(0).getRichStringCellValue().getFontAtIndex(3));
assertEquals(6, r.getCell(0).getRichStringCellValue().getFontAtIndex(4));
// RTR 2 had f3 (unchanged), f5 (=f2) and f6 (moved down)
assertEquals(7, r.getCell(1).getRichStringCellValue().getFontAtIndex(0));
assertEquals(7, r.getCell(1).getRichStringCellValue().getFontAtIndex(1));
assertEquals(6, r.getCell(1).getRichStringCellValue().getFontAtIndex(3));
assertEquals(6, r.getCell(1).getRichStringCellValue().getFontAtIndex(4));
assertEquals(8, r.getCell(1).getRichStringCellValue().getFontAtIndex(6));
assertEquals(8, r.getCell(1).getRichStringCellValue().getFontAtIndex(7));
}
public void testOptimiseStyles() {
HSSFWorkbook wb = new HSSFWorkbook();
// Two fonts
assertEquals(4, wb.getNumberOfFonts());
HSSFFont f1 = wb.createFont();
f1.setFontHeight((short) 11);
f1.setFontName("Testing");
HSSFFont f2 = wb.createFont();
f2.setFontHeight((short) 22);
f2.setFontName("Also Testing");
assertEquals(6, wb.getNumberOfFonts());
// Several styles
assertEquals(21, wb.getNumCellStyles());
HSSFCellStyle cs1 = wb.createCellStyle();
cs1.setFont(f1);
HSSFCellStyle cs2 = wb.createCellStyle();
cs2.setFont(f2);
HSSFCellStyle cs3 = wb.createCellStyle();
cs3.setFont(f1);
HSSFCellStyle cs4 = wb.createCellStyle();
cs4.setFont(f1);
cs4.setAlignment((short) 22);
HSSFCellStyle cs5 = wb.createCellStyle();
cs5.setFont(f2);
cs5.setAlignment((short) 111);
HSSFCellStyle cs6 = wb.createCellStyle();
cs6.setFont(f2);
assertEquals(27, wb.getNumCellStyles());
// Use them
HSSFSheet s = wb.createSheet();
HSSFRow r = s.createRow(0);
r.createCell(0).setCellStyle(cs1);
r.createCell(1).setCellStyle(cs2);
r.createCell(2).setCellStyle(cs3);
r.createCell(3).setCellStyle(cs4);
r.createCell(4).setCellStyle(cs5);
r.createCell(5).setCellStyle(cs6);
r.createCell(6).setCellStyle(cs1);
r.createCell(7).setCellStyle(cs2);
assertEquals(21, r.getCell(0).getCellValueRecord().getXFIndex());
assertEquals(26, r.getCell(5).getCellValueRecord().getXFIndex());
assertEquals(21, r.getCell(6).getCellValueRecord().getXFIndex());
// Optimise
HSSFOptimiser.optimiseCellStyles(wb);
// Check
assertEquals(6, wb.getNumberOfFonts());
assertEquals(25, wb.getNumCellStyles());
// cs1 -> 21
assertEquals(21, r.getCell(0).getCellValueRecord().getXFIndex());
// cs2 -> 22
assertEquals(22, r.getCell(1).getCellValueRecord().getXFIndex());
assertEquals(22, r.getCell(1).getCellStyle().getFont(wb).getFontHeight());
// cs3 = cs1 -> 21
assertEquals(21, r.getCell(2).getCellValueRecord().getXFIndex());
// cs4 --> 24 -> 23
assertEquals(23, r.getCell(3).getCellValueRecord().getXFIndex());
// cs5 --> 25 -> 24
assertEquals(24, r.getCell(4).getCellValueRecord().getXFIndex());
// cs6 = cs2 -> 22
assertEquals(22, r.getCell(5).getCellValueRecord().getXFIndex());
// cs1 -> 21
assertEquals(21, r.getCell(6).getCellValueRecord().getXFIndex());
// cs2 -> 22
assertEquals(22, r.getCell(7).getCellValueRecord().getXFIndex());
// Add a new duplicate, and two that aren't used
HSSFCellStyle csD = wb.createCellStyle();
csD.setFont(f1);
r.createCell(8).setCellStyle(csD);
HSSFFont f3 = wb.createFont();
f3.setFontHeight((short) 23);
f3.setFontName("Testing 3");
HSSFFont f4 = wb.createFont();
f4.setFontHeight((short) 24);
f4.setFontName("Testing 4");
HSSFCellStyle csU1 = wb.createCellStyle();
csU1.setFont(f3);
HSSFCellStyle csU2 = wb.createCellStyle();
csU2.setFont(f4);
// Check before the optimise
assertEquals(8, wb.getNumberOfFonts());
assertEquals(28, wb.getNumCellStyles());
// Optimise, should remove the two un-used ones and the one duplicate
HSSFOptimiser.optimiseCellStyles(wb);
// Check
assertEquals(8, wb.getNumberOfFonts());
assertEquals(25, wb.getNumCellStyles());
// csD -> cs1 -> 21
assertEquals(21, r.getCell(8).getCellValueRecord().getXFIndex());
}
public void testOptimiseStylesCheckActualStyles() {
HSSFWorkbook wb = new HSSFWorkbook();
// Several styles
assertEquals(21, wb.getNumCellStyles());
HSSFCellStyle cs1 = wb.createCellStyle();
cs1.setBorderBottom(HSSFCellStyle.BORDER_THICK);
HSSFCellStyle cs2 = wb.createCellStyle();
cs2.setBorderBottom(HSSFCellStyle.BORDER_DASH_DOT);
HSSFCellStyle cs3 = wb.createCellStyle(); // = cs1
cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK);
assertEquals(24, wb.getNumCellStyles());
// Use them
HSSFSheet s = wb.createSheet();
HSSFRow r = s.createRow(0);
r.createCell(0).setCellStyle(cs1);
r.createCell(1).setCellStyle(cs2);
r.createCell(2).setCellStyle(cs3);
assertEquals(21, r.getCell(0).getCellValueRecord().getXFIndex());
assertEquals(22, r.getCell(1).getCellValueRecord().getXFIndex());
assertEquals(23, r.getCell(2).getCellValueRecord().getXFIndex());
// Optimise
HSSFOptimiser.optimiseCellStyles(wb);
// Check
assertEquals(23, wb.getNumCellStyles());
assertEquals(HSSFCellStyle.BORDER_THICK, r.getCell(0).getCellStyle().getBorderBottom());
assertEquals(HSSFCellStyle.BORDER_DASH_DOT, r.getCell(1).getCellStyle().getBorderBottom());
assertEquals(HSSFCellStyle.BORDER_THICK, r.getCell(2).getCellStyle().getBorderBottom());
}
}
|