aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
blob: 30f6343197164e4cbc899d0b6b9dc88dc410f5f1 (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
/* ====================================================================
   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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import javax.imageio.ImageIO;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.OldExcelFormatException;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.model.InternalSheet;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.TabIdRecord;
import org.apache.poi.hssf.record.UnknownRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
import org.apache.poi.hssf.record.aggregates.RecordAggregate;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.ptg.Area3DPtg;
import org.apache.poi.ss.formula.ptg.DeletedArea3DPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.LocaleUtil;
import org.junit.After;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;

/**
 * Testcases for bugs entered in bugzilla
 * the Test name contains the bugzilla bug id
 *
 * <b>YK: If a bug can be tested in terms of common ss interfaces,
 *  define the test in the base class {@link BaseTestBugzillaIssues}</b>
 */
public final class TestBugs extends BaseTestBugzillaIssues {
    // to not affect other tests running in the same JVM
    @After
    public void resetPassword() {
        Biff8EncryptionKey.setCurrentUserPassword(null);
    }

    public TestBugs() {
        super(HSSFITestDataProvider.instance);
    }

    private static HSSFWorkbook openSample(String sampleFileName) {
        return HSSFITestDataProvider.instance.openSampleWorkbook(sampleFileName);
    }

    private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
        return HSSFITestDataProvider.instance.writeOutAndReadBack(original);
    }

    /** Test reading AND writing a complicated workbook
     *Test opening resulting sheet in excel*/
    @Test
    public void bug15228() throws Exception {
        HSSFWorkbook wb = openSample("15228.xls");
        HSSFSheet s = wb.getSheetAt(0);
        HSSFRow r = s.createRow(0);
        HSSFCell c = r.createCell(0);
        c.setCellValue(10);
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    @Test
    public void bug13796() throws Exception {
        HSSFWorkbook wb = openSample("13796.xls");
        HSSFSheet s = wb.getSheetAt(0);
        HSSFRow r = s.createRow(0);
        HSSFCell c = r.createCell(0);
        c.setCellValue(10);
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /** test reading of a formula with a name and a cell ref in one
     **/
    @Test
    public void bug14460() throws Exception {
        HSSFWorkbook wb = openSample("14460.xls");
        wb.getSheetAt(0);
        wb.close();
    }

    @Test
    public void bug14330() throws Exception {
        HSSFWorkbook wb1 = openSample("14330-1.xls");
        wb1.getSheetAt(0);
        wb1.close();

        HSSFWorkbook wb2 = openSample("14330-2.xls");
        wb2.getSheetAt(0);
        wb2.close();
    }

    private static void setCellText(HSSFCell cell, String text) {
        cell.setCellValue(new HSSFRichTextString(text));
    }

    /** test rewriting a file with large number of unique strings
     *open resulting file in Excel to check results!*/
    @Test
    public void bug15375() throws Exception {
        HSSFWorkbook wb = openSample("15375.xls");
        HSSFSheet sheet = wb.getSheetAt(0);

        HSSFRow row = sheet.getRow(5);
        HSSFCell cell = row.getCell(3);
        if (cell == null)
            cell = row.createCell(3);

        // Write test
        cell.setCellType(Cell.CELL_TYPE_STRING);
        setCellText(cell, "a test");

        // change existing numeric cell value

        HSSFRow oRow = sheet.getRow(14);
        HSSFCell oCell = oRow.getCell(4);
        oCell.setCellValue(75);
        oCell = oRow.getCell(5);
        setCellText(oCell, "0.3");

        writeOutAndReadBack(wb).close();
        wb.close();
    }
    /**
     * test writing a file with large number of unique strings,
     * open resulting file in Excel to check results!
     */
    @Test
    public void bug15375_2() throws Exception {
        bug15375(6000);
    }

    /**Double byte strings*/
    @Test
    public void bug15556() throws Exception {
        HSSFWorkbook wb = openSample("15556.xls");
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row = sheet.getRow(45);
        assertNotNull("Read row fine!" , row);
        wb.close();
    }

    /**Double byte strings */
    @Test
    public void bug22742() {
        openSample("22742.xls");
    }

    /**Double byte strings */
    @Test
    public void bug12561_1() {
        openSample("12561-1.xls");
    }

    /** Double byte strings */
    @Test
    public void bug12561_2() {
        openSample("12561-2.xls");
    }

    /** Double byte strings
     File supplied by jubeson*/
    @Test
    public void bug12843_1() {
        openSample("12843-1.xls");
    }

    /** Double byte strings
     File supplied by Paul Chung*/
    @Test
    public void bug12843_2() {
        openSample("12843-2.xls");
    }

    /** Reference to Name*/
    @Test
    public void bug13224() {
        openSample("13224.xls");
    }

    /** Illegal argument exception - cannot store duplicate value in Map*/
    @Test
    public void bug19599() {
        openSample("19599-1.xls");
        openSample("19599-2.xls");
    }

    @Test
    public void bug24215() throws Exception {
        HSSFWorkbook wb = openSample("24215.xls");

        for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets();sheetIndex++) {
            HSSFSheet sheet = wb.getSheetAt(sheetIndex);
            int rows = sheet.getLastRowNum();

            for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
                HSSFRow row = sheet.getRow(rowIndex);
                int cells = row.getLastCellNum();

                for (int cellIndex = 0; cellIndex < cells; cellIndex++) {
                    row.getCell(cellIndex);
                }
            }
        }
        wb.close();
    }

    /**Tests read and write of Unicode strings in formula results
     * bug and testcase submitted by Sompop Kumnoonsate
     * The file contains THAI unicode characters.
     */
    @Test
    public void bugUnicodeStringFormulaRead() throws Exception {

        HSSFWorkbook w = openSample("25695.xls");

        HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell(0);
        HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell(1);
        HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell(0);
        HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell(1);
        HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell(0);
        HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell(1);
        HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell(0);
        HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell(1);

        /*
            // THAI code page
            System.out.println("a1="+unicodeString(a1));
            System.out.println("a2="+unicodeString(a2));
            // US code page
            System.out.println("b1="+unicodeString(b1));
            System.out.println("b2="+unicodeString(b2));
            // THAI+US
            System.out.println("c1="+unicodeString(c1));
            System.out.println("c2="+unicodeString(c2));
            // US+THAI
            System.out.println("d1="+unicodeString(d1));
            System.out.println("d2="+unicodeString(d2));
        */

        confirmSameCellText(a1, a2);
        confirmSameCellText(b1, b2);
        confirmSameCellText(c1, c2);
        confirmSameCellText(d1, d2);

        HSSFWorkbook rw = writeOutAndReadBack(w);
        w.close();

        HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0);
        HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1);
        HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell(0);
        HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell(1);
        HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell(0);
        HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell(1);
        HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell(0);
        HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell(1);

        confirmSameCellText(a1, ra1);
        confirmSameCellText(b1, rb1);
        confirmSameCellText(c1, rc1);
        confirmSameCellText(d1, rd1);

        confirmSameCellText(a1, ra2);
        confirmSameCellText(b1, rb2);
        confirmSameCellText(c1, rc2);
        confirmSameCellText(d1, rd2);
        
        rw.close();
    }

    private static void confirmSameCellText(HSSFCell a, HSSFCell b) {
        assertEquals(a.getRichStringCellValue().getString(), b.getRichStringCellValue().getString());
    }

    /*private static String unicodeString(HSSFCell cell) {
        String ss = cell.getRichStringCellValue().getString();
        char s[] = ss.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int x=0;x<s.length;x++) {
            sb.append("\\u").append(Integer.toHexString(s[x]));
        }
        return sb.toString();
    }*/

    /** Error in opening wb*/
    @Test
    public void bug32822() {
        openSample("32822.xls");
    }

    /**fail to read wb with chart */
    @Test
    public void bug15573() {
        openSample("15573.xls");
    }

    /**names and macros */
    @Test
    public void bug27852() throws Exception {
        HSSFWorkbook wb = openSample("27852.xls");

        for(int i = 0 ; i < wb.getNumberOfNames(); i++){
          HSSFName name = wb.getNameAt(i);
          name.getNameName();
          if (name.isFunctionName()) {
              continue;
          }
          name.getRefersToFormula();
        }
        wb.close();
    }

    @Test
    public void bug33082() {
        openSample("33082.xls");
    }

    @Test
    public void bug34775() {
        try {
            openSample("34775.xls");
        } catch (NullPointerException e) {
            fail("identified bug 34775");
        }
    }

    /** Error when reading then writing ArrayValues in NameRecord's*/
    @Test
    public void bug37630() throws Exception {
        HSSFWorkbook wb = openSample("37630.xls");
        writeOutAndReadBack(wb);
        wb.close();
    }

    /**
     * Bug 25183: org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet
     */
    @Test
    public void bug25183() throws Exception {
        HSSFWorkbook wb = openSample("25183.xls");
        writeOutAndReadBack(wb);
        wb.close();
    }

    /**
     * Bug 26100: 128-character message in IF statement cell causes HSSFWorkbook open failure
     */
    @Test
    public void bug26100() throws Exception {
        HSSFWorkbook wb = openSample("26100.xls");
        writeOutAndReadBack(wb);
        wb.close();
    }

    /**
     * Bug 27933: Unable to use a template (xls) file containing a wmf graphic
     */
    @Test
    public void bug27933() throws Exception {
        HSSFWorkbook wb = openSample("27933.xls");
        writeOutAndReadBack(wb);
        wb.close();
    }

    /**
     * Bug 29206:      NPE on HSSFSheet.getRow for blank rows
     */
    @Test
    public void bug29206() throws Exception {
        //the first check with blank workbook
        HSSFWorkbook wb = openSample("Simple.xls");
        HSSFSheet sheet = wb.createSheet();
        for(int i = 1; i < 400; i++) {
            HSSFRow row = sheet.getRow(i);
            if(row != null) {
                row.getCell(0);
            }
        }
        wb.close();
    }

    /**
     * Bug 29675: POI 2.5 final corrupts output when starting workbook has a graphic
     */
    @Test
    public void bug29675() throws Exception {
        HSSFWorkbook wb = openSample("29675.xls");
        writeOutAndReadBack(wb);
        wb.close();
    }

    /**
     * Bug 29942: Importing Excel files that have been created by Open Office on Linux
     */
    @Test
    public void bug29942() throws Exception {
        HSSFWorkbook wb = openSample("29942.xls");

        HSSFSheet sheet = wb.getSheetAt(0);
        int count = 0;
        for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
            HSSFRow row =  sheet.getRow(i);
            if (row != null) {
                HSSFCell cell = row .getCell(0);
                assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
                count++;
            }
        }
        assertEquals(85, count); //should read 85 rows

        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 29982: Unable to read spreadsheet when dropdown list cell is selected -
     *  Unable to construct record instance
     */
    @Test
    public void bug29982() throws Exception {
        HSSFWorkbook wb = openSample("29982.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 30540: HSSFSheet.setRowBreak throws NullPointerException
     */
    @Test
    public void bug30540() throws Exception {
        HSSFWorkbook wb = openSample("30540.xls");

        HSSFSheet s = wb.getSheetAt(0);
        s.setRowBreak(1);
        writeOutAndReadBack(wb).close();
        
        wb.close();
    }

    /**
     * Bug 31749: {Need help urgently}[This is critical] workbook.write() corrupts the file......?
     */
    @Test
    public void bug31749() throws Exception {
        HSSFWorkbook wb = openSample("31749.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 31979: {urgent help needed .....}poi library does not support form objects properly.
     */
    @Test
    public void bug31979() throws Exception {
        HSSFWorkbook wb = openSample("31979.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 35564: HSSFCell.java: NullPtrExc in isGridsPrinted() and getProtect()
     *  when HSSFWorkbook is created from file
     */
    @Test
    public void bug35564() throws Exception {
        HSSFWorkbook wb = openSample("35564.xls");

        HSSFSheet sheet = wb.getSheetAt( 0 );
        assertEquals(false, sheet.isGridsPrinted());
        assertEquals(false, sheet.getProtect());

        writeOutAndReadBack(wb).close();
        
        wb.close();
    }

    /**
     * Bug 35565: HSSFCell.java: NullPtrExc in getColumnBreaks() when HSSFWorkbook is created from file
     */
    @Test
    public void bug35565() throws Exception {
        HSSFWorkbook wb = openSample("35565.xls");

        HSSFSheet sheet = wb.getSheetAt( 0 );
        assertNotNull(sheet);
        writeOutAndReadBack(wb).close();
        
        wb.close();
    }

    /**
     * Bug 37376: Cannot open the saved Excel file if checkbox controls exceed certain limit
     */
    @Test
    public void bug37376() throws Exception {
        HSSFWorkbook wb = openSample("37376.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 40285:      CellIterator Skips First Column
     */
    @Test
    public void bug40285() throws Exception {
        HSSFWorkbook wb = openSample("40285.xls");

        HSSFSheet sheet = wb.getSheetAt( 0 );
        int rownum = 0;
        for (Iterator<Row> it = sheet.rowIterator(); it.hasNext(); rownum++) {
            Row row = it.next();
            assertEquals(rownum, row.getRowNum());
            int cellNum = 0;
            for (Iterator<Cell> it2 = row.cellIterator(); it2.hasNext(); cellNum++) {
                Cell cell = it2.next();
                assertEquals(cellNum, cell.getColumnIndex());
            }
        }
        wb.close();
    }

    /**
     * Test bug 38266: NPE when adding a row break
     *
     * User's diagnosis:
     * 1. Manually (i.e., not using POI) create an Excel Workbook, making sure it
     * contains a sheet that doesn't have any row breaks
     * 2. Using POI, create a new HSSFWorkbook from the template in step #1
     * 3. Try adding a row break (via sheet.setRowBreak()) to the sheet mentioned in step #1
     * 4. Get a NullPointerException
     */
    @Test
    public void bug38266() throws Exception {
        String[] files = {"Simple.xls", "SimpleMultiCell.xls", "duprich1.xls"};
        for (String file : files) {
            HSSFWorkbook wb = openSample(file);

            HSSFSheet sheet = wb.getSheetAt( 0 );
            int[] breaks = sheet.getRowBreaks();
            assertEquals(0, breaks.length);

            //add 3 row breaks
            for (int j = 1; j <= 3; j++) {
                sheet.setRowBreak(j*20);
            }
            wb.close();
        }
    }

    @Test
    public void bug40738() throws Exception {
        HSSFWorkbook wb = openSample("SimpleWithAutofilter.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 44200: Sheet not cloneable when Note added to excel cell
     */
    @Test
    public void bug44200() throws Exception {
        HSSFWorkbook wb = openSample("44200.xls");
        wb.cloneSheet(0);
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 44201: Sheet not cloneable when validation added to excel cell
     */
    @Test
    public void bug44201() throws Exception {
        HSSFWorkbook wb = openSample("44201.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 37684  : Unhandled Continue Record Error
     */
    @Test
    public void bug37684() throws Exception {
        HSSFWorkbook wb1 = openSample("37684-1.xls");
        writeOutAndReadBack(wb1).close();
        wb1.close();
        HSSFWorkbook wb2 = openSample("37684-2.xls");
        writeOutAndReadBack(wb2).close();
        wb2.close();
    }

    /**
     * Bug 41139: Constructing HSSFWorkbook is failed,threw threw ArrayIndexOutOfBoundsException for creating UnknownRecord
     */
    @Test
    public void bug41139() throws Exception {
        HSSFWorkbook wb = openSample("41139.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 41546: Constructing HSSFWorkbook is failed,
     *  Unknown Ptg in Formula: 0x1a (26)
     */
    @Test
    public void bug41546() throws Exception {
        HSSFWorkbook wb1 = openSample("41546.xls");
        assertEquals(1, wb1.getNumberOfSheets());
        HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
        assertEquals(1, wb2.getNumberOfSheets());
        wb2.close();
        wb1.close();
    }

    /**
     * Bug 42564: Some files from Access were giving a RecordFormatException
     *  when reading the BOFRecord
     */
    @Test
    public void bug42564() throws Exception {
        HSSFWorkbook wb = openSample("ex42564-21435.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 42564: Some files from Access also have issues
     *  with the NameRecord, once you get past the BOFRecord
     *  issue.
     */
    @Test
    public void bug42564Alt() throws Exception {
        HSSFWorkbook wb = openSample("ex42564-21503.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Bug 42618: RecordFormatException reading a file containing
     *     =CHOOSE(2,A2,A3,A4)
     */
    @Test
    public void bug42618() throws Exception {
        HSSFWorkbook wb1 = openSample("SimpleWithChoose.xls");
        HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
        wb1.close();
        // Check we detect the string properly too
        HSSFSheet s = wb2.getSheetAt(0);

        // Textual value
        HSSFRow r1 = s.getRow(0);
        HSSFCell c1 = r1.getCell(1);
        assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString());

        // Formula Value
        HSSFRow r2 = s.getRow(1);
        HSSFCell c2 = r2.getCell(1);
        assertEquals(25, (int)c2.getNumericCellValue());

        try {
            assertEquals("CHOOSE(2,A2,A3,A4)", c2.getCellFormula());
        } catch (IllegalStateException e) {
            if (e.getMessage().startsWith("Too few arguments")
                && e.getMessage().indexOf("ConcatPtg") > 0) {
                fail("identified bug 44306");
            }
        }
        wb2.close();
    }

    /**
     * Something up with the FileSharingRecord
     */
    @Test
    public void bug43251() throws Exception {
        // Used to blow up with an IllegalArgumentException
        //  when creating a FileSharingRecord
        HSSFWorkbook wb = openSample("43251.xls");
        assertEquals(1, wb.getNumberOfSheets());
        wb.close();
    }

    /**
     * Crystal reports generates files with short
     *  StyleRecords, which is against the spec
     */
    @Test
    public void bug44471() throws Exception {
        // Used to blow up with an ArrayIndexOutOfBounds
        //  when creating a StyleRecord
        HSSFWorkbook wb = openSample("OddStyleRecord.xls");
        assertEquals(1, wb.getNumberOfSheets());
        wb.close();
    }

    /**
     * Files with "read only recommended" were giving
     *  grief on the FileSharingRecord
     */
    @Test
    public void bug44536() throws Exception {
        // Used to blow up with an IllegalArgumentException
        //  when creating a FileSharingRecord
        HSSFWorkbook wb1 = openSample("ReadOnlyRecommended.xls");

        // Check read only advised
        assertEquals(3, wb1.getNumberOfSheets());
        assertTrue(wb1.isWriteProtected());
        wb1.close();

        // But also check that another wb isn't
        HSSFWorkbook wb2 = openSample("SimpleWithChoose.xls");
        assertFalse(wb2.isWriteProtected());
        wb2.close();
    }

    /**
     * Some files were having problems with the DVRecord,
     *  probably due to dropdowns
     */
    @Test
    public void bug44593() throws Exception {
        // Used to blow up with an IllegalArgumentException
        //  when creating a DVRecord
        // Now won't, but no idea if this means we have
        //  rubbish in the DVRecord or not...
        HSSFWorkbook wb = openSample("44593.xls");
        assertEquals(2, wb.getNumberOfSheets());
        wb.close();
    }

    /**
     * Used to give problems due to trying to read a zero
     *  length string, but that's now properly handled
     */
    @Test
    public void bug44643() throws Exception {
        // Used to blow up with an IllegalArgumentException
        HSSFWorkbook wb = openSample("44643.xls");
        assertEquals(1, wb.getNumberOfSheets());
        wb.close();
    }

    /**
     * User reported the wrong number of rows from the
     *  iterator, but we can't replicate that
     */
    @Test
    public void bug44693() throws Exception {
        HSSFWorkbook wb = openSample("44693.xls");
        HSSFSheet s = wb.getSheetAt(0);

        // Rows are 1 to 713
        assertEquals(0, s.getFirstRowNum());
        assertEquals(712, s.getLastRowNum());
        assertEquals(713, s.getPhysicalNumberOfRows());

        // Now check the iterator
        int rowsSeen = 0;
        for(Iterator<Row> i = s.rowIterator(); i.hasNext(); ) {
            Row r = i.next();
            assertNotNull(r);
            rowsSeen++;
        }
        assertEquals(713, rowsSeen);
        wb.close();
    }

    /**
     * Bug 28774: Excel will crash when opening xls-files with images.
     */
    @Test
    public void bug28774() throws Exception {
        HSSFWorkbook wb = openSample("28774.xls");
        assertTrue("no errors reading sample xls", true);
        writeOutAndReadBack(wb).close();;
        assertTrue("no errors writing sample xls", true);
        wb.close();
    }

    /**
     * Had a problem apparently, not sure what as it
     *  works just fine...
     */
    @Test
    public void bug44891() throws Exception {
        HSSFWorkbook wb = openSample("44891.xls");
        assertTrue("no errors reading sample xls", true);
        writeOutAndReadBack(wb).close();
        assertTrue("no errors writing sample xls", true);
        wb.close();
    }

    /**
     * Bug 44235: Ms Excel can't open save as excel file
     *
     * Works fine with poi-3.1-beta1.
     */
    @Test
    public void bug44235() throws Exception {
        HSSFWorkbook wb = openSample("44235.xls");
        assertTrue("no errors reading sample xls", true);
        writeOutAndReadBack(wb).close();
        assertTrue("no errors writing sample xls", true);
        wb.close();
    }

    @Test
    public void bug36947() throws Exception {
        HSSFWorkbook wb = openSample("36947.xls");
        assertTrue("no errors reading sample xls", true);
        writeOutAndReadBack(wb).close();
        assertTrue("no errors writing sample xls", true);
        wb.close();
    }

    @Test
    public void bug39634() throws Exception {
        HSSFWorkbook wb = openSample("39634.xls");
        assertTrue("no errors reading sample xls", true);
        writeOutAndReadBack(wb).close();
        assertTrue("no errors writing sample xls", true);
        wb.close();
    }

    /**
     * Problems with extracting check boxes from
     *  HSSFObjectData
     * @throws Exception
     */
    @Test(expected=FileNotFoundException.class)
    public void bug44840() throws Exception {
        HSSFWorkbook wb = openSample("WithCheckBoxes.xls");

        // Take a look at the embedded objects
        List<HSSFObjectData> objects = wb.getAllEmbeddedObjects();
        assertEquals(1, objects.size());

        HSSFObjectData obj = objects.get(0);
        assertNotNull(obj);

        // Peek inside the underlying record
        EmbeddedObjectRefSubRecord rec = obj.findObjectRecord();
        assertNotNull(rec);

//        assertEquals(32, rec.field_1_stream_id_offset);
        assertEquals(0, rec.getStreamId().intValue()); // WRONG!
        assertEquals("Forms.CheckBox.1", rec.getOLEClassName());
        assertEquals(12, rec.getObjectData().length);

        // Doesn't have a directory
        assertFalse(obj.hasDirectoryEntry());
        assertNotNull(obj.getObjectData());
        assertEquals(12, obj.getObjectData().length);
        assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName());

        try {
            obj.getDirectory();
        } finally {
            wb.close();
        }
    }

    /**
     * Test that we can delete sheets without
     *  breaking the build in named ranges
     *  used for printing stuff.
     */
    @Test
    public void bug30978() throws Exception {
        HSSFWorkbook wb1 = openSample("30978-alt.xls");
        assertEquals(1, wb1.getNumberOfNames());
        assertEquals(3, wb1.getNumberOfSheets());

        // Check all names fit within range, and use
        //  DeletedArea3DPtg
        InternalWorkbook w = wb1.getWorkbook();
        for(int i=0; i<w.getNumNames(); i++) {
            NameRecord r = w.getNameRecord(i);
            assertTrue(r.getSheetNumber() <= wb1.getNumberOfSheets());

            Ptg[] nd = r.getNameDefinition();
            assertEquals(1, nd.length);
            assertTrue(nd[0] instanceof DeletedArea3DPtg);
        }


        // Delete the 2nd sheet
        wb1.removeSheetAt(1);


        // Re-check
        assertEquals(1, wb1.getNumberOfNames());
        assertEquals(2, wb1.getNumberOfSheets());

        for(int i=0; i<w.getNumNames(); i++) {
            NameRecord r = w.getNameRecord(i);
            assertTrue(r.getSheetNumber() <= wb1.getNumberOfSheets());

            Ptg[] nd = r.getNameDefinition();
            assertEquals(1, nd.length);
            assertTrue(nd[0] instanceof DeletedArea3DPtg);
        }


        // Save and re-load
        HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
        wb1.close();
        w = wb2.getWorkbook();

        assertEquals(1, wb2.getNumberOfNames());
        assertEquals(2, wb2.getNumberOfSheets());

        for(int i=0; i<w.getNumNames(); i++) {
            NameRecord r = w.getNameRecord(i);
            assertTrue(r.getSheetNumber() <= wb2.getNumberOfSheets());

            Ptg[] nd = r.getNameDefinition();
            assertEquals(1, nd.length);
            assertTrue(nd[0] instanceof DeletedArea3DPtg);
        }
        wb2.close();
    }

    /**
     * Test that fonts get added properly
     * @throws IOException 
     */
    @Test
    public void bug45338() throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        assertEquals(4, wb.getNumberOfFonts());

        HSSFSheet s = wb.createSheet();
        s.createRow(0);
        s.createRow(1);
        s.getRow(0).createCell(0);
        s.getRow(1).createCell(0);

        assertEquals(4, wb.getNumberOfFonts());

        HSSFFont f1 = wb.getFontAt((short)0);
        assertEquals(400, f1.getBoldweight());

        // Check that asking for the same font
        //  multiple times gives you the same thing.
        // Otherwise, our tests wouldn't work!
        assertEquals(
                wb.getFontAt((short)0),
                wb.getFontAt((short)0)
        );
        assertEquals(
                wb.getFontAt((short)2),
                wb.getFontAt((short)2)
        );
        assertTrue(
                wb.getFontAt((short)0)
                !=
                wb.getFontAt((short)2)
        );

        // Look for a new font we have
        //  yet to add
        assertNull(
            wb.findFont(
                (short)11, (short)123, (short)22,
                "Thingy", false, true, (short)2, (byte)2
            )
        );

        HSSFFont nf = wb.createFont();
        assertEquals(5, wb.getNumberOfFonts());

        assertEquals(5, nf.getIndex());
        assertEquals(nf, wb.getFontAt((short)5));

        nf.setBoldweight((short)11);
        nf.setColor((short)123);
        nf.setFontHeight((short)22);
        nf.setFontName("Thingy");
        nf.setItalic(false);
        nf.setStrikeout(true);
        nf.setTypeOffset((short)2);
        nf.setUnderline((byte)2);

        assertEquals(5, wb.getNumberOfFonts());
        assertEquals(nf, wb.getFontAt((short)5));

        // Find it now
        assertNotNull(
            wb.findFont(
                (short)11, (short)123, (short)22,
                "Thingy", false, true, (short)2, (byte)2
            )
        );
        assertEquals(
            5,
            wb.findFont(
                   (short)11, (short)123, (short)22,
                   "Thingy", false, true, (short)2, (byte)2
               ).getIndex()
        );
        assertEquals(nf,
               wb.findFont(
                   (short)11, (short)123, (short)22,
                   "Thingy", false, true, (short)2, (byte)2
               )
        );
        
        wb.close();
    }

    /**
     * From the mailing list - ensure we can handle a formula
     *  containing a zip code, eg ="70164"
     */
    @Test
    public void bugZipCodeFormulas() throws Exception {
        HSSFWorkbook wb1 = new HSSFWorkbook();
        HSSFSheet s = wb1.createSheet();
        s.createRow(0);
        HSSFCell c1 = s.getRow(0).createCell(0);
        HSSFCell c2 = s.getRow(0).createCell(1);
        HSSFCell c3 = s.getRow(0).createCell(2);

        // As number and string
        c1.setCellFormula("70164");
        c2.setCellFormula("\"70164\"");
        c3.setCellFormula("\"90210\"");

        // Check the formulas
        assertEquals("70164", c1.getCellFormula());
        assertEquals("\"70164\"", c2.getCellFormula());

        // And check the values - blank
        confirmCachedValue(0.0, c1);
        confirmCachedValue(0.0, c2);
        confirmCachedValue(0.0, c3);

        // Try changing the cached value on one of the string
        //  formula cells, so we can see it updates properly
        c3.setCellValue(new HSSFRichTextString("test"));
        confirmCachedValue("test", c3);
        try {
            c3.getNumericCellValue();
            fail("exception should have been thrown");
        } catch (IllegalStateException e) {
            assertEquals("Cannot get a numeric value from a text formula cell", e.getMessage());
        }


        // Now evaluate, they should all be changed
        HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb1);
        eval.evaluateFormulaCell(c1);
        eval.evaluateFormulaCell(c2);
        eval.evaluateFormulaCell(c3);

        // Check that the cells now contain
        //  the correct values
        confirmCachedValue(70164.0, c1);
        confirmCachedValue("70164", c2);
        confirmCachedValue("90210", c3);


        // Write and read
        HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
        wb1.close();
        HSSFSheet ns = wb2.getSheetAt(0);
        HSSFCell nc1 = ns.getRow(0).getCell(0);
        HSSFCell nc2 = ns.getRow(0).getCell(1);
        HSSFCell nc3 = ns.getRow(0).getCell(2);

        // Re-check
        confirmCachedValue(70164.0, nc1);
        confirmCachedValue("70164", nc2);
        confirmCachedValue("90210", nc3);

        @SuppressWarnings("deprecation")
        CellValueRecordInterface[] cvrs = ns.getSheet().getValueRecords();
        for (int i = 0; i < cvrs.length; i++) {
            CellValueRecordInterface cvr = cvrs[i];
            if(cvr instanceof FormulaRecordAggregate) {
                FormulaRecordAggregate fr = (FormulaRecordAggregate)cvr;

                if(i == 0) {
                    assertEquals(70164.0, fr.getFormulaRecord().getValue(), 0.0001);
                    assertNull(fr.getStringRecord());
                } else if (i == 1) {
                    assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
                    assertNotNull(fr.getStringRecord());
                    assertEquals("70164", fr.getStringRecord().getString());
                } else {
                    assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
                    assertNotNull(fr.getStringRecord());
                    assertEquals("90210", fr.getStringRecord().getString());
                }
            }
        }
        assertEquals(3, cvrs.length);
        wb2.close();
    }

    private static void confirmCachedValue(double expectedValue, HSSFCell cell) {
        assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
        assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCachedFormulaResultType());
        assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
    }
    private static void confirmCachedValue(String expectedValue, HSSFCell cell) {
        assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
        assertEquals(Cell.CELL_TYPE_STRING, cell.getCachedFormulaResultType());
        assertEquals(expectedValue, cell.getRichStringCellValue().getString());
    }

    /**
     * Problem with "Vector Rows", eg a whole
     *  column which is set to the result of
     *  {=sin(B1:B9)}(9,1), so that each cell is
     *  shown to have the contents
     *  {=sin(B1:B9){9,1)[rownum][0]
     * In this sample file, the vector column
     *  is C, and the data column is B.
     *
     * For now, blows up with an exception from ExtPtg
     *  Expected ExpPtg to be converted from Shared to Non-Shared...
     */
    @Ignore
    public void test43623() throws Exception {
        HSSFWorkbook wb1 = openSample("43623.xls");
        assertEquals(1, wb1.getNumberOfSheets());

        HSSFSheet s1 = wb1.getSheetAt(0);

        HSSFCell c1 = s1.getRow(0).getCell(2);
        HSSFCell c2 = s1.getRow(1).getCell(2);
        HSSFCell c3 = s1.getRow(2).getCell(2);

        // These formula contents are a guess...
        assertEquals("{=sin(B1:B9){9,1)[0][0]", c1.getCellFormula());
        assertEquals("{=sin(B1:B9){9,1)[1][0]", c2.getCellFormula());
        assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula());

        // Save and re-open, ensure it still works
        HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
        wb1.close();
        HSSFSheet ns1 = wb2.getSheetAt(0);
        HSSFCell nc1 = ns1.getRow(0).getCell(2);
        HSSFCell nc2 = ns1.getRow(1).getCell(2);
        HSSFCell nc3 = ns1.getRow(2).getCell(2);

        assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula());
        assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula());
        assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula());
        wb2.close();
    }

    /**
     * People are all getting confused about the last
     *  row and cell number
     * @throws IOException 
     */
    @Test
    public void bug30635() throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet s = wb.createSheet();

        // No rows, everything is 0
        assertEquals(0, s.getFirstRowNum());
        assertEquals(0, s.getLastRowNum());
        assertEquals(0, s.getPhysicalNumberOfRows());

        // One row, most things are 0, physical is 1
        s.createRow(0);
        assertEquals(0, s.getFirstRowNum());
        assertEquals(0, s.getLastRowNum());
        assertEquals(1, s.getPhysicalNumberOfRows());

        // And another, things change
        s.createRow(4);
        assertEquals(0, s.getFirstRowNum());
        assertEquals(4, s.getLastRowNum());
        assertEquals(2, s.getPhysicalNumberOfRows());


        // Now start on cells
        HSSFRow r = s.getRow(0);
        assertEquals(-1, r.getFirstCellNum());
        assertEquals(-1, r.getLastCellNum());
        assertEquals(0, r.getPhysicalNumberOfCells());

        // Add a cell, things move off -1
        r.createCell(0);
        assertEquals(0, r.getFirstCellNum());
        assertEquals(1, r.getLastCellNum()); // last cell # + 1
        assertEquals(1, r.getPhysicalNumberOfCells());

        r.createCell(1);
        assertEquals(0, r.getFirstCellNum());
        assertEquals(2, r.getLastCellNum()); // last cell # + 1
        assertEquals(2, r.getPhysicalNumberOfCells());

        r.createCell(4);
        assertEquals(0, r.getFirstCellNum());
        assertEquals(5, r.getLastCellNum()); // last cell # + 1
        assertEquals(3, r.getPhysicalNumberOfCells());
        
        wb.close();
    }

    /**
     * Data Tables - ptg 0x2
     */
    @Test
    public void bug44958() throws Exception {
        HSSFWorkbook wb = openSample("44958.xls");
        HSSFSheet s;
        HSSFRow r;
        HSSFCell c;

        // Check the contents of the formulas

        // E4 to G9 of sheet 4 make up the table
        s = wb.getSheet("OneVariable Table Completed");
        r = s.getRow(3);
        c = r.getCell(4);
        assertEquals(Cell.CELL_TYPE_FORMULA, c.getCellType());

        // TODO - check the formula once tables and
        //  arrays are properly supported


        // E4 to H9 of sheet 5 make up the table
        s = wb.getSheet("TwoVariable Table Example");
        r = s.getRow(3);
        c = r.getCell(4);
        assertEquals(Cell.CELL_TYPE_FORMULA, c.getCellType());

        // TODO - check the formula once tables and
        //  arrays are properly supported
        wb.close();
    }

    /**
     * 45322: HSSFSheet.autoSizeColumn fails when style.getDataFormat() returns -1
     */
    @Test
    public void bug45322() throws Exception {
        HSSFWorkbook wb = openSample("44958.xls");
        HSSFSheet sh = wb.getSheetAt(0);
        for(short i=0; i < 30; i++) sh.autoSizeColumn(i);
        wb.close();
     }

    /**
     * We used to add too many UncalcRecords to sheets
     *  with diagrams on. Don't any more
     */
    @Test
    public void bug45414() throws Exception {
        HSSFWorkbook wb1 = openSample("WithThreeCharts.xls");
        wb1.getSheetAt(0).setForceFormulaRecalculation(true);
        wb1.getSheetAt(1).setForceFormulaRecalculation(false);
        wb1.getSheetAt(2).setForceFormulaRecalculation(true);

        // Write out and back in again
        // This used to break
        HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
        wb1.close();

        // Check now set as it should be
        assertTrue(wb2.getSheetAt(0).getForceFormulaRecalculation());
        assertFalse(wb2.getSheetAt(1).getForceFormulaRecalculation());
        assertTrue(wb2.getSheetAt(2).getForceFormulaRecalculation());
        wb2.close();
    }

    /**
     * Very hidden sheets not displaying as such
     */
    @Test
    public void bug45761() throws Exception {
        HSSFWorkbook wb1 = openSample("45761.xls");
        assertEquals(3, wb1.getNumberOfSheets());

        assertFalse(wb1.isSheetHidden(0));
        assertFalse(wb1.isSheetVeryHidden(0));
        assertTrue(wb1.isSheetHidden(1));
        assertFalse(wb1.isSheetVeryHidden(1));
        assertFalse(wb1.isSheetHidden(2));
        assertTrue(wb1.isSheetVeryHidden(2));

        // Change 0 to be very hidden, and re-load
        wb1.setSheetHidden(0, Workbook.SHEET_STATE_VERY_HIDDEN);

        HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
        wb1.close();

        assertFalse(wb2.isSheetHidden(0));
        assertTrue(wb2.isSheetVeryHidden(0));
        assertTrue(wb2.isSheetHidden(1));
        assertFalse(wb2.isSheetVeryHidden(1));
        assertFalse(wb2.isSheetHidden(2));
        assertTrue(wb2.isSheetVeryHidden(2));
        wb2.close();
    }

    /**
     * The resolution for bug 45777 assumed that the maximum text length in a header / footer
     * record was 256 bytes.  This assumption appears to be wrong.  Since the fix for bug 47244,
     * POI now supports header / footer text lengths beyond 256 bytes.
     */
    @Test
    public void bug45777() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet s = wb.createSheet();

        char[] cc248 = new char[248];
        Arrays.fill(cc248, 'x');
        String s248 = new String(cc248);

        String s249 = s248 + "1";
        String s250 = s248 + "12";
        String s251 = s248 + "123";
        assertEquals(248, s248.length());
        assertEquals(249, s249.length());
        assertEquals(250, s250.length());
        assertEquals(251, s251.length());


        // Try on headers
        s.getHeader().setCenter(s248);
        assertEquals(254, s.getHeader().getRawText().length());
        writeOutAndReadBack(wb).close();

        s.getHeader().setCenter(s251);
        assertEquals(257, s.getHeader().getRawText().length());
        writeOutAndReadBack(wb).close();

        // header can be more than 256 bytes
        s.getHeader().setCenter(s250); // 256 bytes required
        s.getHeader().setCenter(s251); // 257 bytes required

        // Now try on footers
        s.getFooter().setCenter(s248);
        assertEquals(254, s.getFooter().getRawText().length());
        writeOutAndReadBack(wb).close();

        s.getFooter().setCenter(s251);
        assertEquals(257, s.getFooter().getRawText().length());
        writeOutAndReadBack(wb).close();

        // footer can be more than 256 bytes
        s.getFooter().setCenter(s250); // 256 bytes required
        s.getFooter().setCenter(s251); // 257 bytes required
        
        wb.close();
    }

    /**
     * Charts with long titles
     */
    @Test
    public void bug45784() throws Exception {
        // This used to break
        HSSFWorkbook wb = openSample("45784.xls");
        assertEquals(1, wb.getNumberOfSheets());
        writeOutAndReadBack(wb).close();
        wb.close();
    }

   /**
     * Cell background colours
     */
    @Test
    public void bug45492() throws Exception {
        HSSFWorkbook wb = openSample("45492.xls");
        HSSFSheet s = wb.getSheetAt(0);
        HSSFRow r = s.getRow(0);
        HSSFPalette p = wb.getCustomPalette();

        HSSFCell auto = r.getCell(0);
        HSSFCell grey = r.getCell(1);
        HSSFCell red = r.getCell(2);
        HSSFCell blue = r.getCell(3);
        HSSFCell green = r.getCell(4);

        assertEquals(64, auto.getCellStyle().getFillForegroundColor());
        assertEquals(64, auto.getCellStyle().getFillBackgroundColor());
        assertEquals("0:0:0", p.getColor(64).getHexString());

        assertEquals(22, grey.getCellStyle().getFillForegroundColor());
        assertEquals(64, grey.getCellStyle().getFillBackgroundColor());
        assertEquals("C0C0:C0C0:C0C0", p.getColor(22).getHexString());

        assertEquals(10, red.getCellStyle().getFillForegroundColor());
        assertEquals(64, red.getCellStyle().getFillBackgroundColor());
        assertEquals("FFFF:0:0", p.getColor(10).getHexString());

        assertEquals(12, blue.getCellStyle().getFillForegroundColor());
        assertEquals(64, blue.getCellStyle().getFillBackgroundColor());
        assertEquals("0:0:FFFF", p.getColor(12).getHexString());

        assertEquals(11, green.getCellStyle().getFillForegroundColor());
        assertEquals(64, green.getCellStyle().getFillBackgroundColor());
        assertEquals("0:FFFF:0", p.getColor(11).getHexString());
        wb.close();
    }

    /**
     * ContinueRecord after EOF
     */
    @Test
    public void bug46137() throws Exception {
        // This used to break
        HSSFWorkbook wb = openSample("46137.xls");
        assertEquals(7, wb.getNumberOfSheets());
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Odd POIFS blocks issue:
     * block[ 44 ] already removed from org.apache.poi.poifs.storage.BlockListImpl.remove
     */
    @Test
    public void bug45290() throws Exception {
        HSSFWorkbook wb = openSample("45290.xls");
        assertEquals(1, wb.getNumberOfSheets());
        wb.close();
    }

    /**
     * In POI-2.5 user reported exception when parsing a name with a custom VBA function:
     *  =MY_VBA_FUNCTION("lskdjflsk")
     */
    @Test
    public void bug30070() throws Exception {
        HSSFWorkbook wb = openSample("30070.xls"); //contains custom VBA function 'Commission'
        HSSFSheet sh = wb.getSheetAt(0);
        HSSFCell cell = sh.getRow(0).getCell(1);

        //B1 uses VBA in the formula
        assertEquals("Commission(A1)", cell.getCellFormula());

        //name sales_1 refers to Commission(Sheet0!$A$1)
        int idx = wb.getNameIndex("sales_1");
        assertTrue(idx != -1);

        HSSFName name = wb.getNameAt(idx);
        assertEquals("Commission(Sheet0!$A$1)", name.getRefersToFormula());
        wb.close();
    }

    /**
     * The link formulas which is referring to other books cannot be taken (the bug existed prior to POI-3.2)
     * Expected:
     *
     * [link_sub.xls]Sheet1!$A$1
     * [link_sub.xls]Sheet1!$A$2
     * [link_sub.xls]Sheet1!$A$3
     *
     * POI-3.1 output:
     *
     * Sheet1!$A$1
     * Sheet1!$A$2
     * Sheet1!$A$3
     *
     */
    @Test
    public void bug27364() throws Exception {
        HSSFWorkbook wb = openSample("27364.xls");
        HSSFSheet sheet = wb.getSheetAt(0);

        assertEquals("[link_sub.xls]Sheet1!$A$1", sheet.getRow(0).getCell(0).getCellFormula());
        assertEquals("[link_sub.xls]Sheet1!$A$2", sheet.getRow(1).getCell(0).getCellFormula());
        assertEquals("[link_sub.xls]Sheet1!$A$3", sheet.getRow(2).getCell(0).getCellFormula());
        wb.close();
    }

    /**
     * Similar to bug#27364:
     * HSSFCell.getCellFormula() fails with references to external workbooks
     */
    @Test
    public void bug31661() throws Exception {
        HSSFWorkbook wb = openSample("31661.xls");
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFCell cell = sheet.getRow(11).getCell(10); //K11
        assertEquals("+'[GM Budget.xls]8085.4450'!$B$2", cell.getCellFormula());
        wb.close();
    }

    /**
     * Incorrect handling of non-ISO 8859-1 characters in Windows ANSII Code Page 1252
     */
    @Test
    public void bug27394() throws Exception {
        HSSFWorkbook wb = openSample("27394.xls");
        assertEquals("\u0161\u017E", wb.getSheetName(0));
        assertEquals("\u0161\u017E\u010D\u0148\u0159", wb.getSheetName(1));
        HSSFSheet sheet = wb.getSheetAt(0);

        assertEquals("\u0161\u017E", sheet.getRow(0).getCell(0).getStringCellValue());
        assertEquals("\u0161\u017E\u010D\u0148\u0159", sheet.getRow(1).getCell(0).getStringCellValue());
        wb.close();
    }

    /**
     * Multiple calls of HSSFWorkbook.write result in corrupted xls
     */
    @Test
    public void bug32191() throws IOException {
        HSSFWorkbook wb = openSample("27394.xls");

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        wb.write(out);
        out.close();
        int size1 = out.size();

        out = new ByteArrayOutputStream();
        wb.write(out);
        out.close();
        int size2 = out.size();

        assertEquals(size1, size2);
        out = new ByteArrayOutputStream();
        wb.write(out);
        out.close();
        int size3 = out.size();
        assertEquals(size2, size3);
        wb.close();
    }

    /**
     * java.io.IOException: block[ 0 ] already removed
     * (is an excel 95 file though)
     */
    @Test
    public void bug46904() throws Exception {
        try {
            OPOIFSFileSystem fs = new OPOIFSFileSystem(
                    HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
            new HSSFWorkbook(fs.getRoot(), false).close();
            fail("Should catch exception here");
        } catch(OldExcelFormatException e) {
            assertTrue(e.getMessage().startsWith(
                    "The supplied spreadsheet seems to be Excel"
            ));
        }
        try {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(
                    HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
            try {
                new HSSFWorkbook(fs.getRoot(), false).close();
            fail("Should catch exception here");
            } finally {
                fs.close();
            }
        } catch(OldExcelFormatException e) {
            assertTrue(e.getMessage().startsWith(
                    "The supplied spreadsheet seems to be Excel"
            ));
        }
    }

    /**
     * java.lang.NegativeArraySizeException reading long
     *  non-unicode data for a name record
     */
    @Test
    public void bug47034() throws Exception {
        HSSFWorkbook wb = openSample("47034.xls");
        assertEquals(893, wb.getNumberOfNames());
        assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300));
        wb.close();
    }

    /**
     * HSSFRichTextString.length() returns negative for really long strings.
     * The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer than 32,767 characters
     */
    @Test
    public void bug46368() throws Exception {
        HSSFWorkbook wb = openSample("46368.xls");
    	HSSFSheet s = wb.getSheetAt(0);
        HSSFCell cell1 = s.getRow(0).getCell(0);
        assertEquals(32770, cell1.getStringCellValue().length());

        HSSFCell cell2 = s.getRow(2).getCell(0);
        assertEquals(32766, cell2.getStringCellValue().length());
        wb.close();
    }

    /**
     * Short records on certain sheets with charts in them
     */
    @Test
    public void bug48180() throws Exception {
        HSSFWorkbook wb = openSample("48180.xls");

    	HSSFSheet s = wb.getSheetAt(0);
        HSSFCell cell1 = s.getRow(0).getCell(0);
        assertEquals("test ", cell1.getStringCellValue());

        HSSFCell cell2 = s.getRow(0).getCell(1);
        assertEquals(1.0, cell2.getNumericCellValue(), 0.0);
        wb.close();
    }

    /**
     * POI 3.5 beta 7 can not read excel file contain list box (Form Control)
     */
    @Test
    public void bug47701() {
        openSample("47701.xls");
    }

    @Test
    public void bug48026() {
        openSample("48026.xls");
    }

    @Test
    public void bug47251() {
        // Firstly, try with one that triggers on InterfaceHdrRecord
        openSample("47251.xls");

        // Now with one that triggers on NoteRecord
        openSample("47251_1.xls");
    }

    /**
     * Round trip a file with an unusual UnicodeString/ExtRst record parts
     */
    @Test
    public void bug47847() throws Exception {
       HSSFWorkbook wb1 = openSample("47847.xls");
       assertEquals(3, wb1.getNumberOfSheets());

       // Find the SST record
       UnicodeString withExt = wb1.getWorkbook().getSSTString(0);
       UnicodeString withoutExt = wb1.getWorkbook().getSSTString(31);

       assertEquals("O:Alloc:Qty", withExt.getString());
       assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);

       assertEquals("RT", withoutExt.getString());
       assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000);

       // Something about continues...


       // Write out and re-read
       HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
       wb1.close();
       assertEquals(3, wb2.getNumberOfSheets());

       // Check it's the same now
       withExt = wb2.getWorkbook().getSSTString(0);
       withoutExt = wb2.getWorkbook().getSSTString(31);

       assertEquals("O:Alloc:Qty", withExt.getString());
       assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);

       assertEquals("RT", withoutExt.getString());
       assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000);
       wb2.close();
    }

    /**
     * Problem with cloning a sheet with a chart
     *  contained in it.
     */
    @Test
    public void bug49096() throws Exception {
       HSSFWorkbook wb1 = openSample("49096.xls");
       assertEquals(1, wb1.getNumberOfSheets());

       assertNotNull(wb1.getSheetAt(0));
       wb1.cloneSheet(0);
       assertEquals(2, wb1.getNumberOfSheets());

       HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
       wb1.close();
       assertEquals(2, wb2.getNumberOfSheets());
       wb2.close();
    }

    /**
     * Newly created sheets need to get a
     *  proper TabID, otherwise print setup
     *  gets confused on them.
     * Also ensure that print setup refs are
     *  by reference not value
     */
    @Test
    public void bug46664() throws Exception {
       HSSFWorkbook wb1 = new HSSFWorkbook();
       HSSFSheet sheet = wb1.createSheet("new_sheet");
       HSSFRow row = sheet.createRow((short)0);
       row.createCell(0).setCellValue(new HSSFRichTextString("Column A"));
       row.createCell(1).setCellValue(new HSSFRichTextString("Column B"));
       row.createCell(2).setCellValue(new HSSFRichTextString("Column C"));
       row.createCell(3).setCellValue(new HSSFRichTextString("Column D"));
       row.createCell(4).setCellValue(new HSSFRichTextString("Column E"));
       row.createCell(5).setCellValue(new HSSFRichTextString("Column F"));

       //set print area from column a to column c (on first row)
       wb1.setPrintArea(
               0, //sheet index
               0, //start column
               2, //end column
               0, //start row
               0  //end row
       );

       HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
       wb1.close();

       // Ensure the tab index
       TabIdRecord tr = null;
       for(Record r : wb2.getWorkbook().getRecords()) {
          if(r instanceof TabIdRecord) {
             tr = (TabIdRecord)r;
          }
       }
       assertNotNull(tr);
       assertEquals(1, tr._tabids.length);
       assertEquals(0, tr._tabids[0]);

       // Ensure the print setup
       assertEquals("new_sheet!$A$1:$C$1", wb2.getPrintArea(0));
       assertEquals("new_sheet!$A$1:$C$1", wb2.getName("Print_Area").getRefersToFormula());

       // Needs reference not value
       NameRecord nr = wb2.getWorkbook().getNameRecord(
               wb2.getNameIndex("Print_Area")
       );
       assertEquals("Print_Area", nr.getNameText());
       assertEquals(1, nr.getNameDefinition().length);
       assertEquals(
             "new_sheet!$A$1:$C$1",
             ((Area3DPtg)nr.getNameDefinition()[0]).toFormulaString(HSSFEvaluationWorkbook.create(wb2))
       );
       assertEquals('R', nr.getNameDefinition()[0].getRVAType());
       wb2.close();
    }

    /**
     * Problems with formula references to
     *  sheets via URLs
     */
    @Test
    public void bug45970() throws Exception {
       HSSFWorkbook wb1 = openSample("FormulaRefs.xls");
       assertEquals(3, wb1.getNumberOfSheets());

       HSSFSheet s = wb1.getSheetAt(0);
       HSSFRow row;

       row = s.getRow(0);
       assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
       assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);

       row = s.getRow(1);
       assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
       assertEquals("B1", row.getCell(1).getCellFormula());
       assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);

       row = s.getRow(2);
       assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
       assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
       assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);

       row = s.getRow(3);
       assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
       assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
       assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);

       row = s.getRow(4);
       assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
       assertEquals("'[$http://gagravarr.org/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
       assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);

       // Change 4
       row.getCell(1).setCellFormula("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2");
       row.getCell(1).setCellValue(123.0);

       // Add 5
       row = s.createRow(5);
       row.createCell(1, Cell.CELL_TYPE_FORMULA);
       row.getCell(1).setCellFormula("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1");
       row.getCell(1).setCellValue(234.0);


       // Re-test
       HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
       wb1.close();
       s = wb2.getSheetAt(0);

       row = s.getRow(0);
       assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
       assertEquals(112.0, row.getCell(1).getNumericCellValue(),0);

       row = s.getRow(1);
       assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
       assertEquals("B1", row.getCell(1).getCellFormula());
       assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);

       row = s.getRow(2);
       assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
       assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
       assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);

       row = s.getRow(3);
       assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
       assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
       assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);

       // TODO - Fix these so they work...
       /*row = s.getRow(4);
       assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
       assertEquals("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula());
       assertEquals(123.0, row.getCell(1).getNumericCellValue(), 0);

       row = s.getRow(5);
       assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
       assertEquals("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
       assertEquals(234.0, row.getCell(1).getNumericCellValue(), 0);*/
       
       wb2.close();
    }

    /**
     * Test for a file with NameRecord with NameCommentRecord comments
     */
    @Test
    public void bug49185() throws Exception {
      HSSFWorkbook wb1 = openSample("49185.xls");
      Name name = wb1.getName("foobarName");
      assertEquals("This is a comment", name.getComment());

      // Rename the name, comment comes with it
      name.setNameName("ChangedName");
      assertEquals("This is a comment", name.getComment());

      // Save and re-check
      HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
      wb1.close();
      name = wb2.getName("ChangedName");
      assertEquals("This is a comment", name.getComment());

      // Now try to change it
      name.setComment("Changed Comment");
      assertEquals("Changed Comment", name.getComment());

      // Save and re-check
      HSSFWorkbook wb3 = writeOutAndReadBack(wb2);
      wb2.close();
      name = wb3.getName("ChangedName");
      assertEquals("Changed Comment", name.getComment());
      wb3.close();
    }

    /**
     * Vertically aligned text
     */
    @Test
    public void bug49524() throws Exception {
       HSSFWorkbook wb1 = openSample("49524.xls");
       Sheet s = wb1.getSheetAt(0);
       Row r = s.getRow(0);
       Cell rotated = r.getCell(0);
       Cell normal = r.getCell(1);

       // Check the current ones
       assertEquals(0, normal.getCellStyle().getRotation());
       assertEquals(0xff, rotated.getCellStyle().getRotation());

       // Add a new style, also rotated
       CellStyle cs = wb1.createCellStyle();
       cs.setRotation((short)0xff);
       Cell nc = r.createCell(2);
       nc.setCellValue("New Rotated Text");
       nc.setCellStyle(cs);
       assertEquals(0xff, nc.getCellStyle().getRotation());

       // Write out and read back
       HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
       wb1.close();

       // Re-check
       s = wb2.getSheetAt(0);
       r = s.getRow(0);
       rotated = r.getCell(0);
       normal = r.getCell(1);
       nc = r.getCell(2);

       assertEquals(0, normal.getCellStyle().getRotation());
       assertEquals(0xff, rotated.getCellStyle().getRotation());
       assertEquals(0xff, nc.getCellStyle().getRotation());
       wb2.close();
    }

    /**
     * Setting the user style name on custom styles
     */
    @Test
    public void bug49689() throws Exception {
       HSSFWorkbook wb1 = new HSSFWorkbook();
       HSSFSheet s = wb1.createSheet("Test");
       HSSFRow r = s.createRow(0);
       HSSFCell c = r.createCell(0);

       HSSFCellStyle cs1 = wb1.createCellStyle();
       HSSFCellStyle cs2 = wb1.createCellStyle();
       HSSFCellStyle cs3 = wb1.createCellStyle();

       assertEquals(21, cs1.getIndex());
       cs1.setUserStyleName("Testing");

       assertEquals(22, cs2.getIndex());
       cs2.setUserStyleName("Testing 2");

       assertEquals(23, cs3.getIndex());
       cs3.setUserStyleName("Testing 3");

       // Set one
       c.setCellStyle(cs1);

       // Write out and read back
       HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
       wb1.close();

       // Re-check
       assertEquals("Testing", wb2.getCellStyleAt((short)21).getUserStyleName());
       assertEquals("Testing 2", wb2.getCellStyleAt((short)22).getUserStyleName());
       assertEquals("Testing 3", wb2.getCellStyleAt((short)23).getUserStyleName());
       
       wb2.close();
    }

    @Test
    public void bug49751() throws Exception {
        HSSFWorkbook wb = openSample("49751.xls");
        short numCellStyles = wb.getNumCellStyles();
        List<String> namedStyles = Arrays.asList(
                "20% - Accent1", "20% - Accent2", "20% - Accent3", "20% - Accent4", "20% - Accent5",
                "20% - Accent6", "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4",
                "40% - Accent5", "40% - Accent6", "60% - Accent1", "60% - Accent2", "60% - Accent3",
                "60% - Accent4", "60% - Accent5", "60% - Accent6", "Accent1", "Accent2", "Accent3",
                "Accent4", "Accent5", "Accent6", "Bad", "Calculation", "Check Cell", "Explanatory Text",
                "Good", "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Input", "Linked Cell",
                "Neutral", "Note", "Output", "Title", "Total", "Warning Text");

        List<String> collecteddStyles = new ArrayList<String>();
        for (short i = 0; i < numCellStyles; i++) {
            HSSFCellStyle cellStyle = wb.getCellStyleAt(i);
            String styleName = cellStyle.getUserStyleName();
            if (styleName != null) {
                collecteddStyles.add(styleName);
            }
        }
        assertTrue(namedStyles.containsAll(collecteddStyles));
        wb.close();
    }

    /**
     * Regression with the PageSettingsBlock
     */
    @Test
    public void bug49931() throws Exception {
       HSSFWorkbook wb = openSample("49931.xls");
       assertEquals(1, wb.getNumberOfSheets());
       assertEquals("Foo", wb.getSheetAt(0).getRow(0).getCell(0).getRichStringCellValue().toString());
       wb.close();
    }

    /**
     * Missing left/right/centre options on a footer
     */
    @Test
    public void bug48325() throws Exception {
       HSSFWorkbook wb = openSample("48325.xls");
       HSSFSheet sh = wb.getSheetAt(0);
       HSSFFooter f = sh.getFooter();

       // Will show as the center, as that is what excel does
       //  with an invalid footer lacking left/right/center details
       assertEquals("Left text should be empty", "", f.getLeft());
       assertEquals("Right text should be empty", "", f.getRight());
       assertEquals(
             "Center text should contain the illegal value",
             "BlahBlah blah blah  ", f.getCenter()
       );
       wb.close();
    }

    /**
     * IllegalStateException received when creating Data validation in sheet with macro
     */
    @Test
    public void bug50020() throws Exception {
       HSSFWorkbook wb = openSample("50020.xls");
       writeOutAndReadBack(wb).close();
       wb.close();
    }

    @Test
    public void bug50426() throws Exception {
       HSSFWorkbook wb = openSample("50426.xls");
       writeOutAndReadBack(wb).close();
       wb.close();
    }

    /**
     * Last row number when shifting rows
     * @throws IOException 
     */
    @Test
    public void bug50416LastRowNumber() throws IOException {
       // Create the workbook with 1 sheet which contains 3 rows
       HSSFWorkbook workbook = new HSSFWorkbook();
       Sheet sheet = workbook.createSheet("Bug50416");
       Row row1 = sheet.createRow(0);
       Cell cellA_1 = row1.createCell(0,Cell.CELL_TYPE_STRING);
       cellA_1.setCellValue("Cell A,1");
       Row row2 = sheet.createRow(1);
       Cell cellA_2 = row2.createCell(0,Cell.CELL_TYPE_STRING);
       cellA_2.setCellValue("Cell A,2");
       Row row3 = sheet.createRow(2);
       Cell cellA_3 = row3.createCell(0,Cell.CELL_TYPE_STRING);
       cellA_3.setCellValue("Cell A,3");

       // Test the last Row number it currently correct
       assertEquals(2, sheet.getLastRowNum());

       // Shift the first row to the end
       sheet.shiftRows(0, 0, 3);
       assertEquals(3, sheet.getLastRowNum());
       assertEquals(-1,         sheet.getRow(0).getLastCellNum());
       assertEquals("Cell A,2", sheet.getRow(1).getCell(0).getStringCellValue());
       assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
       assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());

       // Shift the 2nd row up to the first one
       sheet.shiftRows(1, 1, -1);
       assertEquals(3, sheet.getLastRowNum());
       assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
       assertEquals(-1,         sheet.getRow(1).getLastCellNum());
       assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
       assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());

       // Shift the 4th row up into the gap in the 3rd row
       sheet.shiftRows(3, 3, -2);
       assertEquals(2, sheet.getLastRowNum());
       assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
       assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
       assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
       assertEquals(-1,         sheet.getRow(3).getLastCellNum());

       // Now zap the empty 4th row - won't do anything
       sheet.removeRow(sheet.getRow(3));

       // Test again the last row number which should be 2
       assertEquals(2, sheet.getLastRowNum());
       assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
       assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
       assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
       
       workbook.close();
    }

    /**
     * If you send a file between Excel and OpenOffice enough, something
     *  will turn the "General" format into "GENERAL"
     */
    @Test
    public void bug50756() throws Exception {
       HSSFWorkbook wb = openSample("50756.xls");
       HSSFSheet s = wb.getSheetAt(0);
       HSSFRow r17 = s.getRow(16);
       HSSFRow r18 = s.getRow(17);
       HSSFDataFormatter df = new HSSFDataFormatter();

       assertEquals(10.0, r17.getCell(1).getNumericCellValue(), 0);
       assertEquals(20.0, r17.getCell(2).getNumericCellValue(), 0);
       assertEquals(20.0, r17.getCell(3).getNumericCellValue(), 0);
       assertEquals("GENERAL", r17.getCell(1).getCellStyle().getDataFormatString());
       assertEquals("GENERAL", r17.getCell(2).getCellStyle().getDataFormatString());
       assertEquals("GENERAL", r17.getCell(3).getCellStyle().getDataFormatString());
       assertEquals("10", df.formatCellValue(r17.getCell(1)));
       assertEquals("20", df.formatCellValue(r17.getCell(2)));
       assertEquals("20", df.formatCellValue(r17.getCell(3)));

       assertEquals(16.0, r18.getCell(1).getNumericCellValue(), 0);
       assertEquals(35.0, r18.getCell(2).getNumericCellValue(), 0);
       assertEquals(123.0, r18.getCell(3).getNumericCellValue(), 0);
       assertEquals("GENERAL", r18.getCell(1).getCellStyle().getDataFormatString());
       assertEquals("GENERAL", r18.getCell(2).getCellStyle().getDataFormatString());
       assertEquals("GENERAL", r18.getCell(3).getCellStyle().getDataFormatString());
       assertEquals("16", df.formatCellValue(r18.getCell(1)));
       assertEquals("35", df.formatCellValue(r18.getCell(2)));
       assertEquals("123", df.formatCellValue(r18.getCell(3)));
       wb.close();
    }

    /**
     * A protected sheet with comments, when written out by
     *  POI, ends up upsetting excel.
     * TODO Identify the cause and add extra asserts for
     *  the bit excel cares about
     */
    @Test
    public void bug50833() throws Exception {
       Biff8EncryptionKey.setCurrentUserPassword(null);

       HSSFWorkbook wb1 = openSample("50833.xls");
       HSSFSheet s = wb1.getSheetAt(0);
       assertEquals("Sheet1", s.getSheetName());
       assertEquals(false, s.getProtect());

       HSSFCell c = s.getRow(0).getCell(0);
       assertEquals("test cell value", c.getRichStringCellValue().getString());

       HSSFComment cmt = c.getCellComment();
       assertNotNull(cmt);
       assertEquals("Robert Lawrence", cmt.getAuthor());
       assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());

       // Reload
       HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
       wb1.close();
       s = wb2.getSheetAt(0);
       c = s.getRow(0).getCell(0);

       // Re-check the comment
       cmt = c.getCellComment();
       assertNotNull(cmt);
       assertEquals("Robert Lawrence", cmt.getAuthor());
       assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());

       // TODO Identify what excel doesn't like, and check for that
       wb2.close();
    }

    @Test
    public void bug50779() throws Exception {
        HSSFWorkbook wb1 = openSample("50779_1.xls");
        writeOutAndReadBack(wb1).close();
        wb1.close();

        HSSFWorkbook wb2 = openSample("50779_2.xls");
        writeOutAndReadBack(wb2).close();
        wb2.close();
    }

    /**
     * The spec says that ChartEndObjectRecord has 6 reserved
     *  bytes on the end, but we sometimes find files without...
     */
    @Test
    public void bug50939() throws Exception {
       HSSFWorkbook wb = openSample("50939.xls");
       assertEquals(2, wb.getNumberOfSheets());
       wb.close();
    }

    @Test
    public void bug49219() throws Exception {
       HSSFWorkbook wb = openSample("49219.xls");
       assertEquals(1, wb.getNumberOfSheets());
       assertEquals("DGATE", wb.getSheetAt(0).getRow(1).getCell(0).getStringCellValue());
       wb.close();
    }

    @Test
    public void bug48968() throws Exception {
        TimeZone userTimeZone = LocaleUtil.getUserTimeZone();
        LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
        try {
           HSSFWorkbook wb = openSample("48968.xls");
           assertEquals(1, wb.getNumberOfSheets());
    
           DataFormatter fmt = new DataFormatter();
    
           // Check the dates
           HSSFSheet s = wb.getSheetAt(0);
           Cell cell_d20110325 = s.getRow(0).getCell(0);
           Cell cell_d19000102 = s.getRow(11).getCell(0);
           Cell cell_d19000100 = s.getRow(21).getCell(0);
           assertEquals(s.getRow(0).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d20110325));
           assertEquals(s.getRow(11).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d19000102));
           // There is no such thing as 00/01/1900...
           assertEquals("00/01/1900 06:14:24", s.getRow(21).getCell(3).getStringCellValue());
           assertEquals("31/12/1899 06:14:24", fmt.formatCellValue(cell_d19000100));
    
           // Check the cached values
           assertEquals("HOUR(A1)",   s.getRow(5).getCell(0).getCellFormula());
           assertEquals(11.0,         s.getRow(5).getCell(0).getNumericCellValue(), 0);
           assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
           assertEquals(39.0,         s.getRow(6).getCell(0).getNumericCellValue(), 0);
           assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
           assertEquals(54.0,         s.getRow(7).getCell(0).getNumericCellValue(), 0);
    
           // Re-evaluate and check
           HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
           assertEquals("HOUR(A1)",   s.getRow(5).getCell(0).getCellFormula());
           assertEquals(11.0,         s.getRow(5).getCell(0).getNumericCellValue(), 0);
           assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
           assertEquals(39.0,         s.getRow(6).getCell(0).getNumericCellValue(), 0);
           assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
           assertEquals(54.0,         s.getRow(7).getCell(0).getNumericCellValue(), 0);
    
           // Push the time forward a bit and check
           double date = s.getRow(0).getCell(0).getNumericCellValue();
           s.getRow(0).getCell(0).setCellValue(date + 1.26);
    
           HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
           assertEquals("HOUR(A1)",   s.getRow(5).getCell(0).getCellFormula());
           assertEquals(11.0+6.0,     s.getRow(5).getCell(0).getNumericCellValue(), 0);
           assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
           assertEquals(39.0+14.0+1,  s.getRow(6).getCell(0).getNumericCellValue(), 0);
           assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
           assertEquals(54.0+24.0-60, s.getRow(7).getCell(0).getNumericCellValue(), 0);
           
           wb.close();
        } finally {
            LocaleUtil.setUserTimeZone(userTimeZone);
        }
    }
       

    /**
     * Mixture of Ascii and Unicode strings in a
     *  NameComment record
     */
    @Test
    public void bug51143() throws Exception {
       HSSFWorkbook wb1 = openSample("51143.xls");
       assertEquals(1, wb1.getNumberOfSheets());
       HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
       wb1.close();
       assertEquals(1, wb2.getNumberOfSheets());
       wb2.close();
    }

    /**
     * File with exactly 256 data blocks (+header block)
     *  shouldn't break on POIFS loading
     */
    @SuppressWarnings("resource")
    @Test
    public void bug51461() throws Exception {
       byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51461.xls");

       HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(
             new ByteArrayInputStream(data)).getRoot(), false);
       HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem(
             new ByteArrayInputStream(data)).getRoot(), false);

       assertEquals(2, wbPOIFS.getNumberOfSheets());
       assertEquals(2, wbNPOIFS.getNumberOfSheets());
    }

    /**
     * Large row numbers and NPOIFS vs POIFS
     */
    @SuppressWarnings("resource")
    @Test
    public void bug51535() throws Exception {
       byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51535.xls");

       HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(
             new ByteArrayInputStream(data)).getRoot(), false);
       HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem(
             new ByteArrayInputStream(data)).getRoot(), false);

       for(HSSFWorkbook wb : new HSSFWorkbook[] {wbPOIFS, wbNPOIFS}) {
          assertEquals(3, wb.getNumberOfSheets());

          // Check directly
          HSSFSheet s = wb.getSheetAt(0);
          assertEquals("Top Left Cell", s.getRow(0).getCell(0).getStringCellValue());
          assertEquals("Top Right Cell", s.getRow(0).getCell(255).getStringCellValue());
          assertEquals("Bottom Left Cell", s.getRow(65535).getCell(0).getStringCellValue());
          assertEquals("Bottom Right Cell", s.getRow(65535).getCell(255).getStringCellValue());

          // Extract and check
          ExcelExtractor ex = new ExcelExtractor(wb);
          String text = ex.getText();
          assertTrue(text.contains("Top Left Cell"));
          assertTrue(text.contains("Top Right Cell"));
          assertTrue(text.contains("Bottom Left Cell"));
          assertTrue(text.contains("Bottom Right Cell"));
          ex.close();
       }
    }

    @Test
    public void bug51670() throws Exception {
        HSSFWorkbook wb = openSample("51670.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /**
     * Sum across multiple workbooks
     *  eg =SUM($Sheet2.A1:$Sheet3.A1)
     * DISABLED - We currently get the formula wrong, and mis-evaluate
     */
    @Ignore
    public void test48703() throws Exception {
        HSSFWorkbook wb = openSample("48703.xls");
        assertEquals(3, wb.getNumberOfSheets());

        // Check reading the formula
        Sheet sheet = wb.getSheetAt(0);
        Row r = sheet.getRow(0);
        Cell c = r.getCell(0);

        assertEquals("SUM(Sheet2!A1:Sheet3!A1)", c.getCellFormula());
        assertEquals(4.0, c.getNumericCellValue(), 0);

        // Check the evaluated result
        HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
        eval.evaluateFormulaCell(c);
        assertEquals(4.0, c.getNumericCellValue(), 0);
        wb.close();
    }

    /**
     * Normally encrypted files have BOF then FILEPASS, but
     *  some may squeeze a WRITEPROTECT in the middle
     */
    @Test(expected=EncryptedDocumentException.class)
    public void bug51832() {
        openSample("51832.xls");
    }

    @Test
    public void bug49896() throws Exception {
        HSSFWorkbook wb = openSample("49896.xls");
        HSSFCell  cell = wb.getSheetAt(0).getRow(1).getCell(1);
        String PATH_SEPARATOR = System.getProperty("file.separator");
        assertEquals("VLOOKUP(A2,'[C:Documents and Settings" + PATH_SEPARATOR+"Yegor"+PATH_SEPARATOR
                +"My Documents"+PATH_SEPARATOR+"csco.xls]Sheet1'!$A$2:$B$3,2,FALSE)",
                cell.getCellFormula());
        wb.close();
     }

    @Test
    public void bug49529() throws Exception {
        // user code reported in Bugzilla #49529
        HSSFWorkbook wb = openSample("49529.xls");
        wb.getSheetAt(0).createDrawingPatriarch();
        // prior to the fix the line below failed with
        // java.lang.IllegalStateException: EOF - next record not available
        wb.cloneSheet(0);

        // make sure we are still readable
        writeOutAndReadBack(wb).close();
        
        wb.close();
    }

    /**
     * Note - part of this test is still failing, see
     * {@link TestUnfixedBugs#test49612()}
     */
    @Test
    public void bug49612_part() throws IOException {
        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49612.xls");
        HSSFSheet sh = wb.getSheetAt(0);
        HSSFRow row = sh.getRow(0);
        HSSFCell c1 = row.getCell(2);
        HSSFCell d1 = row.getCell(3);
        HSSFCell e1 = row.getCell(2);

        assertEquals("SUM(BOB+JIM)", c1.getCellFormula());

        // Problem 1: See TestUnfixedBugs#test49612()
        // Problem 2: TestUnfixedBugs#test49612()

        // Problem 3: These used to fail, now pass
        HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
        assertEquals("evaluating c1", 30.0, eval.evaluate(c1).getNumberValue(), 0.001);
        assertEquals("evaluating d1", 30.0, eval.evaluate(d1).getNumberValue(), 0.001);
        assertEquals("evaluating e1", 30.0, eval.evaluate(e1).getNumberValue(), 0.001);
        wb.close();
    }

    @Test
    public void bug51675() throws Exception {
        final List<Short> list = new ArrayList<Short>();
        HSSFWorkbook wb = openSample("51675.xls");
        HSSFSheet sh = wb.getSheetAt(0);
        InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
        PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
        psb.visitContainedRecords(new RecordAggregate.RecordVisitor() {
            @Override
            public void visitRecord(Record r) {
                list.add(r.getSid());
            }
        });
        assertTrue(list.get(list.size()-1).intValue() == UnknownRecord.BITMAP_00E9);
        assertTrue(list.get(list.size()-2).intValue() == UnknownRecord.HEADER_FOOTER_089C);
        wb.close();
    }

    @Test
    public void bug52272() throws IOException{
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sh = wb.createSheet();
        HSSFPatriarch p = sh.createDrawingPatriarch();

        HSSFSimpleShape s = p.createSimpleShape(new HSSFClientAnchor());
        s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);

        HSSFSheet sh2 = wb.cloneSheet(0);
        assertNotNull(sh2.getDrawingPatriarch());
        
        wb.close();
    }

    @Test
    public void bug53432() throws IOException{
        Workbook wb1 = new HSSFWorkbook(); //or new HSSFWorkbook();
        wb1.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG);
        assertEquals(wb1.getAllPictures().size(), 1);
        wb1.close();

        wb1.close();
        wb1 = new HSSFWorkbook();

        Workbook wb2 = writeOutAndReadBack((HSSFWorkbook) wb1);
        wb1.close();
        assertEquals(wb2.getAllPictures().size(), 0);
        wb2.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG);
        assertEquals(wb2.getAllPictures().size(), 1);

        Workbook wb3 = writeOutAndReadBack((HSSFWorkbook) wb2);
        wb2.close();
        assertEquals(wb3.getAllPictures().size(), 1);
        
        wb3.close();
    }

    @Test
    public void bug46250() throws Exception {
        HSSFWorkbook wb = openSample("46250.xls");
        Sheet sh = wb.getSheet("Template");
        Sheet cSh = wb.cloneSheet(wb.getSheetIndex(sh));

        HSSFPatriarch patriarch = (HSSFPatriarch) cSh.createDrawingPatriarch();
        HSSFTextbox tb = (HSSFTextbox) patriarch.getChildren().get(2);

        tb.setString(new HSSFRichTextString("POI test"));
        tb.setAnchor(new HSSFClientAnchor(0,0,0,0,(short)0,0,(short)10,10));

        writeOutAndReadBack(wb).close();
        
        wb.close();
    }

    @Test
    public void bug53404() throws Exception {
        HSSFWorkbook wb = openSample("53404.xls");
        Sheet sheet = wb.getSheet("test-sheet");
        int rowCount = sheet.getLastRowNum() + 1;
        int newRows = 5;
        Calendar cal = LocaleUtil.getLocaleCalendar(); 
        for (int r = rowCount; r < rowCount + newRows; r++) {
            Row row = sheet.createRow((short) r);
            row.createCell(0).setCellValue(1.03 * (r + 7));
            row.createCell(1).setCellValue(cal.getTime());
            row.createCell(2).setCellValue(cal);
            row.createCell(3).setCellValue(String.format(Locale.ROOT, "row:%d/col:%d", r, 3));
            row.createCell(4).setCellValue(true);
            row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);
            row.createCell(6).setCellValue("added cells.");
        }

        writeOutAndReadBack(wb).close();
        
        wb.close();
    }

    @Test
    public void bug54016() throws Exception {
        // This used to break
        HSSFWorkbook wb = openSample("54016.xls");
        writeOutAndReadBack(wb).close();
        wb.close();
    }

    /** Row style information is 12 not 16 bits */
    @Test
    public void bug49237() throws Exception {
        HSSFWorkbook wb = openSample("49237.xls");
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row = sheet.getRow(0);
        HSSFCellStyle rstyle = row.getRowStyle();
        assertEquals(rstyle.getBorderBottom(), CellStyle.BORDER_DOUBLE);
        wb.close();
    }

    /** POI doesn't currently support the RC4 CryptoAPI encryption header structure */
    @Test(expected=EncryptedDocumentException.class)
    public void bug35897() throws Exception {
        // password is abc
        try {
            Biff8EncryptionKey.setCurrentUserPassword("abc");
            openSample("xor-encryption-abc.xls").close();
        } finally {
            Biff8EncryptionKey.setCurrentUserPassword(null);
        }

        // One using the only-recently-documented encryption header type 4,
        //  and the RC4 CryptoAPI encryption header structure
        openSample("35897-type4.xls").close();
    }

    @Test
    public void bug56450() throws Exception {
        HSSFWorkbook wb = openSample("56450.xls");
        HSSFSheet sheet = wb.getSheetAt(0);
        int comments = 0;
        for (Row r : sheet) {
            for (Cell c : r) {
                if (c.getCellComment() != null) {
                    assertNotNull(c.getCellComment().getString().getString());
                    comments++;
                }
            }
        }
        assertEquals(0, comments);
        wb.close();
    }

    /**
     * Files initially created with Excel 2010 can have >3 CF rules
     */
    @Test
    public void bug56482() throws Exception {
        HSSFWorkbook wb = openSample("56482.xls");
        assertEquals(1, wb.getNumberOfSheets());

        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFSheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();

        assertEquals(5, cf.getNumConditionalFormattings());
        wb.close();
    }

    @Test
    public void bug56325() throws IOException {
        HSSFWorkbook wb1;
        POIFSFileSystem fs;

        File file = HSSFTestDataSamples.getSampleFile("56325.xls");
        InputStream stream = new FileInputStream(file);
        try {
            fs = new POIFSFileSystem(stream);
            wb1 = new HSSFWorkbook(fs);
        } finally {
            stream.close();
        }

        assertEquals(3, wb1.getNumberOfSheets());
        wb1.removeSheetAt(0);
        assertEquals(2, wb1.getNumberOfSheets());

        HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
        wb1.close();
        fs.close();
        assertEquals(2, wb2.getNumberOfSheets());
        wb2.removeSheetAt(0);
        assertEquals(1, wb2.getNumberOfSheets());
        wb2.removeSheetAt(0);
        assertEquals(0, wb2.getNumberOfSheets());

        HSSFWorkbook wb3 = writeOutAndReadBack(wb2);
        wb2.close();
        
        assertEquals(0, wb3.getNumberOfSheets());
        wb3.close();
    }

    @Test
    public void bug56325a() throws IOException {
        HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("56325a.xls");

        HSSFSheet sheet = wb1.cloneSheet(2);
        wb1.setSheetName(3, "Clone 1");
        sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3"));
        wb1.setPrintArea(3, "$A$4:$C$10");

        sheet = wb1.cloneSheet(2);
        wb1.setSheetName(4, "Clone 2");
        sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3"));
        wb1.setPrintArea(4, "$A$4:$C$10");

        wb1.removeSheetAt(2);

        Workbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
        assertEquals(4, wb2.getNumberOfSheets());
        wb2.close();
        wb1.close();
    }

    /**
     * Formulas which reference named ranges, either in other
     *  sheets, or workbook scoped but in other workbooks.
     * Used to fail with
     * java.lang.RuntimeException: Unexpected eval class (org.apache.poi.ss.formula.eval.NameXEval)
     */
    @Test
    public void bug56737() throws IOException {
        Workbook wb = openSample("56737.xls");

        // Check the named range definitions
        Name nSheetScope = wb.getName("NR_To_A1");
        Name nWBScope = wb.getName("NR_Global_B2");

        assertNotNull(nSheetScope);
        assertNotNull(nWBScope);

        assertEquals("Defines!$A$1", nSheetScope.getRefersToFormula());
        assertEquals("Defines!$B$2", nWBScope.getRefersToFormula());

        // Check the different kinds of formulas
        Sheet s = wb.getSheetAt(0);
        Cell cRefSName = s.getRow(1).getCell(3);
        Cell cRefWName = s.getRow(2).getCell(3);

        assertEquals("Defines!NR_To_A1", cRefSName.getCellFormula());

        // TODO Correct this, so that the filename is shown too, see bug #56742
        // This is what Excel itself shows
        //assertEquals("'56737.xls'!NR_Global_B2", cRefWName.getCellFormula());
        // TODO This isn't right, but it's what we currently generate....
        assertEquals("NR_Global_B2", cRefWName.getCellFormula());

        // Try to evaluate them
        FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
        assertEquals("Test A1", eval.evaluate(cRefSName).getStringValue());
        assertEquals(142, (int)eval.evaluate(cRefWName).getNumberValue());

        // Try to evaluate everything
        eval.evaluateAll();
        wb.close();
    }

    /**
     * ClassCastException in HSSFOptimiser - StyleRecord cannot be cast to
     * ExtendedFormatRecord when removing un-used styles
     */
    @Test
    public void bug54443() throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook( );
        HSSFCellStyle style = workbook.createCellStyle();
        HSSFCellStyle newStyle = workbook.createCellStyle();

        HSSFSheet mySheet = workbook.createSheet();
        HSSFRow row = mySheet.createRow(0);
        HSSFCell cell = row.createCell(0);

        // Use style
        cell.setCellStyle(style);
        // Switch to newStyle, style is now un-used
        cell.setCellStyle(newStyle);

        // Optimize
        HSSFOptimiser.optimiseCellStyles(workbook);
        workbook.close();
    }

    /**
     * Intersection formula ranges, eg =(C2:D3 D3:E4)
     */
    @Test
    public void bug52111() throws Exception {
        Workbook wb = openSample("Intersection-52111.xls");
        Sheet s = wb.getSheetAt(0);
        assertFormula(wb, s.getRow(2).getCell(0), "(C2:D3 D3:E4)", "4.0");
        assertFormula(wb, s.getRow(6).getCell(0), "Tabelle2!E:E Tabelle2!$A11:$IV11", "5.0");
        assertFormula(wb, s.getRow(8).getCell(0), "Tabelle2!E:F Tabelle2!$A11:$IV12", null);
        wb.close();
    }
    
    private void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) {
        assertEquals(Cell.CELL_TYPE_FORMULA, intF.getCellType());
        if (null == expectedResultOrNull) {
            assertEquals(Cell.CELL_TYPE_ERROR, intF.getCachedFormulaResultType());
            expectedResultOrNull = "#VALUE!";
        }
        else {
            assertEquals(Cell.CELL_TYPE_NUMERIC, intF.getCachedFormulaResultType());
        }
        
        assertEquals(expectedFormula, intF.getCellFormula());

        // Check we can evaluate it correctly
        FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
        assertEquals(expectedResultOrNull, eval.evaluate(intF).formatAsString());
    }

    @Test
    public void bug42016() throws Exception {
        Workbook wb = openSample("42016.xls");
        Sheet s = wb.getSheetAt(0);
        for(int row = 0;row < 7;row++) {
            assertEquals("A$1+B$1", s.getRow(row).getCell(2).getCellFormula());
        }
        wb.close();
    }

    /**
     * Unexpected record type (org.apache.poi.hssf.record.ColumnInfoRecord)
     */
    @Test
    public void bug53984() throws Exception {
        Workbook wb = openSample("53984.xls");
        Sheet s = wb.getSheetAt(0);
        assertEquals("International Communication Services SA", s.getRow(2).getCell(0).getStringCellValue());
        assertEquals("Saudi Arabia-Riyadh", s.getRow(210).getCell(0).getStringCellValue());
        wb.close();
    }

    /**
     * Read, write, read for formulas point to cells in other files.
     * See {@link #bug46670()} for the main test, this just
     *  covers reading an existing file and checking it.
     * TODO Fix this so that it works - formulas are ending up as
     *  #REF when being changed
     */
    @Test
    @Ignore
    public void bug46670_existing() throws Exception {
        Sheet s;
        Cell c;

        // Expected values
        String refLocal = "'[refs/airport.xls]Sheet1'!$A$2";
        String refHttp  = "'[9http://www.principlesofeconometrics.com/excel/airline.xls]Sheet1'!$A$2";

        // Check we can read them correctly
        HSSFWorkbook wb1 = openSample("46670_local.xls");
        s = wb1.getSheetAt(0);
        assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
        wb1.close();

        HSSFWorkbook wb2 = openSample("46670_http.xls");
        s = wb2.getSheetAt(0);
        assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
        wb2.close();

        // Now try to set them to the same values, and ensure that
        //  they end up as they did before, even with a save and re-load
        HSSFWorkbook wb3 = openSample("46670_local.xls");
        s = wb3.getSheetAt(0);
        c = s.getRow(0).getCell(0);
        c.setCellFormula(refLocal);
        assertEquals(refLocal, c.getCellFormula());

        HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
        wb3.close();
        s = wb4.getSheetAt(0);
        assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
        wb4.close();

        HSSFWorkbook wb5 = openSample("46670_http.xls");
        s = wb5.getSheetAt(0);
        c = s.getRow(0).getCell(0);
        c.setCellFormula(refHttp);
        assertEquals(refHttp, c.getCellFormula());

        HSSFWorkbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
        wb5.close();
        s = wb6.getSheetAt(0);
        assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
        wb6.close();
    }

    @Test
    public void test57456() throws IOException {
        Workbook wb = openSample("57456.xls");
        wb.close();
    }

    @Test
    public void test57163() throws IOException {
        Workbook wb = openSample("57163.xls");

        while (wb.getNumberOfSheets() > 1) {
            wb.removeSheetAt(1);
        }
        wb.close();
    }

    @Test
    public void test53109() throws IOException {
        HSSFWorkbook wb1 = openSample("53109.xls");
        
        HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
        assertNotNull(wb2);
        wb2.close();
        
        wb1.close();
    }
    
    @Test
    public void test53109a() throws IOException {
        HSSFWorkbook wb1 = openSample("com.aida-tour.www_SPO_files_maldives%20august%20october.xls");
        
        Workbook wb2 = writeOutAndReadBack(wb1);
        assertNotNull(wb2);
        wb2.close();
        
        wb1.close();
    }
    
    @Test
    public void test48043() throws IOException {
        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("56325a.xls");
        
        wb.removeSheetAt(2);
        wb.removeSheetAt(1);
        
        //Sheet s = wb.createSheet("sheetname");
        Sheet s = wb.getSheetAt(0);
        Row row = s.createRow(0);
        Cell cell = row.createCell(0);

        cell.setCellFormula(
                "IF(AND(ISBLANK(A10)," +
                "ISBLANK(B10)),\"\"," + 
                "CONCATENATE(A10,\"-\",B10))");
        
        FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
        
        eval.evaluateAll();
        
        /*OutputStream out = new FileOutputStream("C:\\temp\\48043.xls");
        try {
          wb.write(out);
        } finally {
          out.close();
        }*/
        
        Workbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
        assertNotNull(wbBack);
        wbBack.close();

        wb.close();
    }
    
    @Test
    public void test57925() throws IOException {
        Workbook wb = HSSFTestDataSamples.openSampleWorkbook("57925.xls");
        
        wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
        
        for(int i = 0;i < wb.getNumberOfSheets();i++) {
            Sheet sheet = wb.getSheetAt(i);
            for(Row row : sheet) {
                for(Cell cell : row) {
                    new DataFormatter().formatCellValue(cell);
                }
            }
        }
        
        wb.close();
    }
    
    @Test
    public void test46515() throws IOException {
        Workbook wb = HSSFTestDataSamples.openSampleWorkbook("46515.xls");

        // Get structure from webservice
        String urlString = "http://poi.apache.org/resources/images/project-logo.jpg";
        URL structURL = new URL(urlString);
        BufferedImage bimage;
        try {
            bimage = ImageIO.read(structURL);
        } catch (IOException e) {
            Assume.assumeNoException("Downloading a jpg from poi.apache.org should work", e);
            return;
        } finally {
            wb.close();
        }

        // Convert BufferedImage to byte[]
        ByteArrayOutputStream imageBAOS = new ByteArrayOutputStream();
        ImageIO.write(bimage, "jpeg", imageBAOS);
        imageBAOS.flush();
        byte[]imageBytes = imageBAOS.toByteArray();
        imageBAOS.close();

        // Pop structure into Structure HSSFSheet
        int pict = wb.addPicture(imageBytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
        Sheet sheet = wb.getSheet("Structure");
        assertNotNull("Did not find sheet", sheet);
        HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 10, 22);
        anchor.setAnchorType(2);
        patriarch.createPicture(anchor, pict);

        // Write out destination file
//        FileOutputStream fileOut = new FileOutputStream("/tmp/46515.xls");
//        wb.write(fileOut);
//        fileOut.close();

        wb.close();
    }
}