aboutsummaryrefslogtreecommitdiffstats
path: root/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java
blob: c63361d0641a1ecd375cc3e8a761273c85dd55e1 (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
/* ====================================================================
   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.hwpf.usermodel;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFOldDocument;
import org.apache.poi.hwpf.HWPFTestDataSamples;
import org.apache.poi.hwpf.converter.AbstractWordUtils;
import org.apache.poi.hwpf.converter.WordToTextConverter;
import org.apache.poi.hwpf.extractor.Word6Extractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.model.FieldsDocumentPart;
import org.apache.poi.hwpf.model.FileInformationBlock;
import org.apache.poi.hwpf.model.PicturesTable;
import org.apache.poi.hwpf.model.PlexOfField;
import org.apache.poi.hwpf.model.SubdocumentType;
import org.apache.poi.hwpf.model.io.HWPFOutputStream;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;

import junit.framework.TestCase;

/**
 * Test different problems reported in the Apache Bugzilla
 *  against HWPF
 */
public class TestBugs extends TestCase
{
    private static final POILogger logger = POILogFactory
            .getLogger(TestBugs.class);

    public static void assertEqualsIgnoreNewline(String expected, String actual )
    {
        String newExpected = expected.replaceAll("\r\n", "\n" )
                .replaceAll("\r", "\n").trim();
        String newActual = actual.replaceAll("\r\n", "\n" )
                .replaceAll("\r", "\n").trim();
        TestCase.assertEquals(newExpected, newActual);
    }

    private static void assertTableStructures(Range expected, Range actual )
    {
        assertEquals(expected.numParagraphs(), actual.numParagraphs());
        for (int p = 0; p < expected.numParagraphs(); p++ )
        {
            Paragraph expParagraph = expected.getParagraph(p);
            Paragraph actParagraph = actual.getParagraph(p);

            assertEqualsIgnoreNewline(expParagraph.text(), actParagraph.text());
            assertEquals("Diffent isInTable flags for paragraphs #" + p
                    + " -- " + expParagraph + " -- " + actParagraph + ".",
                    expParagraph.isInTable(), actParagraph.isInTable());
            assertEquals(expParagraph.isTableRowEnd(),
                    actParagraph.isTableRowEnd());

            if (expParagraph.isInTable() && actParagraph.isInTable() )
            {
                Table expTable, actTable;
                try
                {
                    expTable = expected.getTable(expParagraph);
                    actTable = actual.getTable(actParagraph);
                }
                catch (Exception exc )
                {
                    continue;
                }

                assertEquals(expTable.numRows(), actTable.numRows());
                assertEquals(expTable.numParagraphs(),
                        actTable.numParagraphs());
            }
        }
    }

    private static void fixed(String bugzillaId )
    {
        throw new Error(
                "Bug "
                        + bugzillaId
                        + " seems to be fixed. "
                        + "Please resolve the issue in Bugzilla and remove fail() from the test");
    }

    private String getText(String samplefile) throws IOException {
        HWPFDocument doc = HWPFTestDataSamples.openSampleFile(samplefile);
        WordExtractor extractor = new WordExtractor(doc);
        try {
            return extractor.getText();
        } finally {
            extractor.close();
        }
    }
    
    private String getTextOldFile(String samplefile) throws IOException {
        HWPFOldDocument doc = HWPFTestDataSamples.openOldSampleFile(samplefile);
        Word6Extractor extractor = new Word6Extractor(doc);
        try {
            return extractor.getText();
        } finally {
            extractor.close();
        }
    }

    /**
     * Bug 33519 - HWPF fails to read a file
     * @throws IOException 
     */
    public void test33519() throws IOException
    {
        assertNotNull(getText("Bug33519.doc"));
    }

    /**
     * Bug 34898 - WordExtractor doesn't read the whole string from the file
     * @throws IOException 
     */
    public void test34898() throws IOException
    {
        assertEqualsIgnoreNewline("\u30c7\u30a3\u30ec\u30af\u30c8\u30ea", getText("Bug34898.doc").trim());
    }

    /**
     * [RESOLVED INVALID] 41898 - Word 2003 pictures cannot be extracted
     */
    public void test41898()
    {
        HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug41898.doc");
        List<Picture> pics = doc.getPicturesTable().getAllPictures();

        assertNotNull(pics);
        assertEquals(1, pics.size());

        Picture pic = pics.get(0);
        assertNotNull(pic.suggestFileExtension());
        assertNotNull(pic.suggestFullFileName());

        assertNotNull(pic.getContent());
        assertNotNull(pic.getRawContent());

        /*
         * This is a file with empty EMF image, but present Office Drawing
         * --sergey
         */
        final Collection<OfficeDrawing> officeDrawings = doc
                .getOfficeDrawingsMain().getOfficeDrawings();
        assertNotNull(officeDrawings);
        assertEquals(1, officeDrawings.size());

        OfficeDrawing officeDrawing = officeDrawings.iterator().next();
        assertNotNull(officeDrawing);
        assertEquals(1044, officeDrawing.getShapeId());
    }

    /**
     * Bug 44331 - HWPFDocument.write destroys fields
     * @throws IOException 
     */
    @SuppressWarnings("deprecation")
    public void test44431() throws IOException
    {
        HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug44431.doc");

        WordExtractor extractor1 = new WordExtractor(doc1);
        try {
            HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
            
            WordExtractor extractor2 = new WordExtractor(doc2);
            try {
                assertEqualsIgnoreNewline(extractor1.getFooterText(), extractor2.getFooterText());
                assertEqualsIgnoreNewline(extractor1.getHeaderText(), extractor2.getHeaderText());
                assertEqualsIgnoreNewline(Arrays.toString(extractor1.getParagraphText() ),
                        Arrays.toString(extractor2.getParagraphText()));
        
                assertEqualsIgnoreNewline(extractor1.getText(), extractor2.getText());
            } finally {
                extractor2.close();
            }
        } finally {
            extractor1.close();
        }
    }

    /**
     * Bug 44331 - HWPFDocument.write destroys fields
     * @throws IOException 
     */
    public void test44431_2() throws IOException
    {
        assertEqualsIgnoreNewline("File name=FieldsTest.doc\n" + 
        		"\n" + 
        		"\n" + 
        		"STYLEREF test\n" + 
        		"\n" + 
        		"\n" + 
        		"\n" + 
        		"TEST TABLE OF CONTENTS\n" + 
        		"\n" + 
        		"Heading paragraph in next page\t2\n" + 
        		"Another heading paragraph in further page\t3\n" + 
        		"Another heading paragraph in further page\t3\n" + 
        		"\n" + 
        		"\n" + 
        		"Heading paragraph in next page\n" + 
        		"Another heading paragraph in further page\n" + 
        		"\n" + 
        		"\n" + 
        		"\n" + 
        		"Page 3 of 3", getText("Bug44431.doc"));
    }

    /**
     * Bug 45473 - HWPF cannot read file after save
     * @throws IOException 
     */
    public void test45473() throws IOException
    {
        // Fetch the current text
        HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug45473.doc");
        WordExtractor wordExtractor = new WordExtractor(doc1);
        final String text1;
        try {
            text1 = wordExtractor.getText().trim();
        } finally {
            wordExtractor.close();
        }

        // Re-load, then re-save and re-check
        doc1 = HWPFTestDataSamples.openSampleFile("Bug45473.doc");
        HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
        WordExtractor wordExtractor2 = new WordExtractor(doc2);
        final String text2;
        try {
            text2 = wordExtractor2.getText().trim();
        } finally {
            wordExtractor2.close();
        }

        // the text in the saved document has some differences in line
        // separators but we tolerate that
        assertEqualsIgnoreNewline(text1.replaceAll("\n", "" ), text2.replaceAll("\n", ""));
    }

    /**
     * Bug 46220 - images are not properly extracted
     */
    public void test46220()
    {
        HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug46220.doc");
        // reference checksums as in Bugzilla
        String[] md5 = { "851be142bce6d01848e730cb6903f39e",
                "7fc6d8fb58b09ababd036d10a0e8c039",
                "a7dc644c40bc2fbf17b2b62d07f99248",
                "72d07b8db5fad7099d90bc4c304b4666" };
        List<Picture> pics = doc.getPicturesTable().getAllPictures();
        assertEquals(4, pics.size());
        for (int i = 0; i < pics.size(); i++ )
        {
            Picture pic = pics.get(i);
            byte[] data = pic.getRawContent();
            // use Apache Commons Codec utils to compute md5
            assertEqualsIgnoreNewline(md5[i], DigestUtils.md5Hex(data));
        }
    }

    /**
     * [RESOLVED FIXED] Bug 46817 - Regression: Text from some table cells
     * missing
     * @throws IOException 
     */
    public void test46817() throws IOException
    {
        String text = getText("Bug46817.doc").trim();

        assertTrue(text.contains("Nazwa wykonawcy"));
        assertTrue(text.contains("kujawsko-pomorskie"));
        assertTrue(text.contains("ekomel@ekomel.com.pl"));
    }

    /**
     * [FAILING] Bug 47286 - Word documents saves in wrong format if source
     * contains form elements
     * 
     * @throws IOException
     */
    @SuppressWarnings("deprecation")
    public void test47286() throws IOException
    {
        // Fetch the current text
        HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug47286.doc");
        WordExtractor wordExtractor = new WordExtractor(doc1);
        final String text1;
        try {
            text1 = wordExtractor.getText().trim();
        } finally {
            wordExtractor.close();
        }

        // Re-load, then re-save and re-check
        doc1 = HWPFTestDataSamples.openSampleFile("Bug47286.doc");
        HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
        WordExtractor wordExtractor2 = new WordExtractor(doc2);
        final String text2;
        try {
            text2 = wordExtractor2.getText().trim();
        } finally {
            wordExtractor2.close();
        }

        // the text in the saved document has some differences in line
        // separators but we tolerate that
        assertEqualsIgnoreNewline(text1.replaceAll("\n", "" ), text2.replaceAll("\n", ""));

        assertEquals(doc1.getCharacterTable().getTextRuns().size(), doc2
                .getCharacterTable().getTextRuns().size());

        List<PlexOfField> expectedFields = doc1.getFieldsTables()
                .getFieldsPLCF(FieldsDocumentPart.MAIN);
        List<PlexOfField> actualFields = doc2.getFieldsTables().getFieldsPLCF(
                FieldsDocumentPart.MAIN);
        assertEquals(expectedFields.size(), actualFields.size());

        assertTableStructures(doc1.getRange(), doc2.getRange());
    }

    /**
     * [RESOLVED FIXED] Bug 47287 - StringIndexOutOfBoundsException in
     * CharacterRun.replaceText()
     */
    public void test47287()
    {
        HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug47287.doc");
        String[] values = { "1-1", "1-2", "1-3", "1-4", "1-5", "1-6", "1-7",
                "1-8", "1-9", "1-10", "1-11", "1-12", "1-13", "1-14", "1-15", };
        int usedVal = 0;
        String PLACEHOLDER = "\u2002\u2002\u2002\u2002\u2002";
        Range r = doc.getRange();
        for (int x = 0; x < r.numSections(); x++ )
        {
            Section s = r.getSection(x);
            for (int y = 0; y < s.numParagraphs(); y++ )
            {
                Paragraph p = s.getParagraph(y);

                for (int z = 0; z < p.numCharacterRuns(); z++ )
                {
                    boolean isFound = false;

                    // character run
                    CharacterRun run = p.getCharacterRun(z);
                    // character run text
                    String text = run.text();
                    String oldText = text;
                    int c = text.indexOf("FORMTEXT ");
                    if (c < 0 )
                    {
                        int k = text.indexOf(PLACEHOLDER);
                        if (k >= 0 )
                        {
                            text = text.substring(0, k ) + values[usedVal]
                                    + text.substring(k + PLACEHOLDER.length());
                            usedVal++;
                            isFound = true;
                        }
                    }
                    else
                    {
                        for (; c >= 0; c = text.indexOf("FORMTEXT ", c
                                + "FORMTEXT ".length() ) )
                        {
                            int k = text.indexOf(PLACEHOLDER, c);
                            if (k >= 0 )
                            {
                                text = text.substring(0, k )
                                        + values[usedVal]
                                        + text.substring(k
                                                + PLACEHOLDER.length());
                                usedVal++;
                                isFound = true;
                            }
                        }
                    }
                    if (isFound )
                    {
                        run.replaceText(oldText, text, 0);
                    }

                }
            }
        }

        String docText = r.text();

        assertTrue(docText.contains("1-1"));
        assertTrue(docText.contains("1-12"));

        assertFalse(docText.contains("1-13"));
        assertFalse(docText.contains("1-15"));
    }

    /**
     * [RESOLVED FIXED] Bug 47731 - Word Extractor considers text copied from
     * some website as an embedded object
     */
    public void test47731() throws Exception
    {
        String foundText = getText("Bug47731.doc");

        assertTrue(foundText
                .contains("Soak the rice in water for three to four hours"));
    }

    /**
     * Bug 4774 - text extracted by WordExtractor is broken
     */
    public void test47742() throws Exception
    {
        // (1) extract text from MS Word document via POI
        String foundText = getText("Bug47742.doc");

        // (2) read text from text document (retrieved by saving the word
        // document as text file using encoding UTF-8)
        InputStream is = POIDataSamples.getDocumentInstance()
                .openResourceAsStream("Bug47742-text.txt");
        try {
            byte[] expectedBytes = IOUtils.toByteArray(is);
            String expectedText = new String(expectedBytes, "utf-8" )
                    .substring(1); // strip-off the unicode marker
    
            assertEqualsIgnoreNewline(expectedText, foundText);
        } finally {
            is.close();
        }
    }

    /**
     * Bug 47958 - Exception during Escher walk of pictures
     */
    public void test47958()
    {
        HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug47958.doc");
        doc.getPicturesTable().getAllPictures();
    }

    /**
     * [RESOLVED FIXED] Bug 48065 - Problems with save output of HWPF (losing
     * formatting)
     */
    public void test48065()
    {
        HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug48065.doc");
        HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);

        Range expected = doc1.getRange();
        Range actual = doc2.getRange();

        assertEqualsIgnoreNewline(
                expected.text().replace("\r", "\n").replaceAll("\n\n", "\n" ),
                actual.text().replace("\r", "\n").replaceAll("\n\n", "\n"));

        assertTableStructures(expected, actual);
    }

    public void test49933() throws IOException
    {
        String text = getTextOldFile("Bug49933.doc");

        assertTrue( text.contains( "best.wine.jump.ru" ) );
    }

    /**
     * Bug 50936 - Exception parsing MS Word 8.0 file
     */
    public void test50936_1()
    {
        HWPFDocument hwpfDocument = HWPFTestDataSamples
                .openSampleFile("Bug50936_1.doc");
        hwpfDocument.getPicturesTable().getAllPictures();
    }

    /**
     * Bug 50936 - Exception parsing MS Word 8.0 file
     */
    public void test50936_2()
    {
        HWPFDocument hwpfDocument = HWPFTestDataSamples
                .openSampleFile("Bug50936_2.doc");
        hwpfDocument.getPicturesTable().getAllPictures();
    }

    /**
     * Bug 50936 - Exception parsing MS Word 8.0 file
     */
    public void test50936_3()
    {
        HWPFDocument hwpfDocument = HWPFTestDataSamples
                .openSampleFile("Bug50936_3.doc");
        hwpfDocument.getPicturesTable().getAllPictures();
    }

    /**
     * [FAILING] Bug 50955 - error while retrieving the text file
     * @throws IOException 
     */
    public void test50955() throws IOException
    {
        try {
            getTextOldFile("Bug50955.doc");

            fixed("50955");
        } catch (IllegalStateException e) {
            // expected here
        }
    }

    /**
     * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta
     * release from download site )
     */
    public void test51604()
    {
        HWPFDocument document = HWPFTestDataSamples
                .openSampleFile("Bug51604.doc");

        Range range = document.getRange();
        int numParagraph = range.numParagraphs();
        int counter = 0;
        for (int i = 0; i < numParagraph; i++ )
        {
            Paragraph paragraph = range.getParagraph(i);
            int numCharRuns = paragraph.numCharacterRuns();
            for (int j = 0; j < numCharRuns; j++ )
            {
                CharacterRun charRun = paragraph.getCharacterRun(j);
                String text = charRun.text();
                charRun.replaceText(text, "+" + (++counter));
            }
        }

        document = HWPFTestDataSamples.writeOutAndReadBack(document);
        String text = document.getDocumentText();
        assertEqualsIgnoreNewline("+1+2+3+4+5+6+7+8+9+10+11+12", text);
    }

    /**
     * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta
     * release from download site )
     * 
     * @throws IOException
     * @throws FileNotFoundException
     */
    public void test51604p2() throws Exception
    {
        HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug51604.doc");

        Range range = doc.getRange();
        int numParagraph = range.numParagraphs();
        for (int i = 0; i < numParagraph; i++ )
        {
            Paragraph paragraph = range.getParagraph(i);
            int numCharRuns = paragraph.numCharacterRuns();
            for (int j = 0; j < numCharRuns; j++ )
            {
                CharacterRun charRun = paragraph.getCharacterRun(j);
                String text = charRun.text();
                if (text.contains("Header" ) )
                    charRun.replaceText(text, "added");
            }
        }

        doc = HWPFTestDataSamples.writeOutAndReadBack(doc);
        final FileInformationBlock fileInformationBlock = doc
                .getFileInformationBlock();

        int totalLength = 0;
        for (SubdocumentType type : SubdocumentType.values() )
        {
            final int partLength = fileInformationBlock
                    .getSubdocumentTextStreamLength(type);
            assert (partLength >= 0);

            totalLength += partLength;
        }
        assertEquals(doc.getText().length(), totalLength);
    }

    /**
     * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta
     * release from download site )
     */
    public void test51604p3() throws Exception
    {
        HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug51604.doc");

        byte[] originalData = new byte[doc.getFileInformationBlock()
                .getLcbDop()];
        System.arraycopy(doc.getTableStream(), doc.getFileInformationBlock()
                .getFcDop(), originalData, 0, originalData.length);

        HWPFOutputStream outputStream = new HWPFOutputStream();
        doc.getDocProperties().writeTo(outputStream);
        final byte[] oldData = outputStream.toByteArray();

        assertEqualsIgnoreNewline(Arrays.toString(originalData ),
                Arrays.toString(oldData));

        Range range = doc.getRange();
        int numParagraph = range.numParagraphs();
        for (int i = 0; i < numParagraph; i++ )
        {
            Paragraph paragraph = range.getParagraph(i);
            int numCharRuns = paragraph.numCharacterRuns();
            for (int j = 0; j < numCharRuns; j++ )
            {
                CharacterRun charRun = paragraph.getCharacterRun(j);
                String text = charRun.text();
                if (text.contains("Header" ) )
                    charRun.replaceText(text, "added");
            }
        }

        doc = HWPFTestDataSamples.writeOutAndReadBack(doc);

        outputStream = new HWPFOutputStream();
        doc.getDocProperties().writeTo(outputStream);
        final byte[] newData = outputStream.toByteArray();

        assertEqualsIgnoreNewline(Arrays.toString(oldData ), Arrays.toString(newData));
    }

    /**
     * [RESOLVED FIXED] Bug 51671 - HWPFDocument.write based on NPOIFSFileSystem
     * throws a NullPointerException
     */
    public void test51671() throws Exception
    {
        InputStream is = POIDataSamples.getDocumentInstance()
                .openResourceAsStream("empty.doc");
        NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is);
        try {
            HWPFDocument hwpfDocument = new HWPFDocument(
                    npoifsFileSystem.getRoot());
            hwpfDocument.write(new ByteArrayOutputStream());
        } finally {
            npoifsFileSystem.close();
        }
    }

    /**
     * Bug 51678 - Extracting text from Bug51524.zip is slow Bug 51524 -
     * PapBinTable constructor is slow
     * @throws IOException 
     */
    public void test51678And51524() throws IOException
    {
        // YK: the test will run only if the poi.test.remote system property is
        // set.
        // TODO: refactor into something nicer!
        if (System.getProperty("poi.test.remote" ) != null )
        {
            String href = "http://domex.nps.edu/corp/files/govdocs1/007/007488.doc";
            HWPFDocument hwpfDocument = HWPFTestDataSamples
                    .openRemoteFile(href);

            WordExtractor wordExtractor = new WordExtractor(hwpfDocument);
            try {
                wordExtractor.getText();
            } finally {
                wordExtractor.close();
            }
        }
    }

    /**
     * [FIXED] Bug 51902 - Picture.fillRawImageContent -
     * ArrayIndexOutOfBoundsException
     */
    public void testBug51890()
    {
        HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug51890.doc");
        for (Picture picture : doc.getPicturesTable().getAllPictures() )
        {
            PictureType pictureType = picture.suggestPictureType();
            logger.log(POILogger.DEBUG,
                    "Picture at offset " + picture.getStartOffset()
                            + " has type " + pictureType);
        }
    }

    /**
     * [RESOLVED FIXED] Bug 51834 - Opening and Writing .doc file results in
     * corrupt document
     */
    public void testBug51834() throws Exception
    {
        /*
         * we don't have Java test for this file - it should be checked using
         * Microsoft BFF Validator. But check read-write-read anyway. -- sergey
         */
        HWPFTestDataSamples.openSampleFile("Bug51834.doc");
        HWPFTestDataSamples.writeOutAndReadBack(HWPFTestDataSamples
                .openSampleFile("Bug51834.doc"));
    }

    /**
     * Bug 51944 - PAPFormattedDiskPage.getPAPX - IndexOutOfBounds
     */
    public void testBug51944() throws Exception
    {
        HWPFOldDocument doc = HWPFTestDataSamples.openOldSampleFile("Bug51944.doc");
        assertNotNull(WordToTextConverter.getText(doc));
    }

    /**
     * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutofBoundsException
     * with no stack trace (broken after revision 1178063)
     */
    public void testBug52032_1() throws Exception
    {
        assertNotNull(getText("Bug52032_1.doc"));
    }

    /**
     * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutofBoundsException
     * with no stack trace (broken after revision 1178063)
     */
    public void testBug52032_2() throws Exception
    {
        assertNotNull(getText("Bug52032_2.doc"));
    }

    /**
     * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutofBoundsException
     * with no stack trace (broken after revision 1178063)
     */
    public void testBug52032_3() throws Exception
    {
        assertNotNull(getText("Bug52032_3.doc"));
    }

    /**
     * Bug 53380 - ArrayIndexOutOfBounds Exception parsing word 97 document
     */
    public void testBug53380_1() throws Exception
    {
        assertNotNull(getText("Bug53380_1.doc"));
    }

    /**
     * Bug 53380 - ArrayIndexOutOfBounds Exception parsing word 97 document
     */
    public void testBug53380_2() throws Exception
    {
        assertNotNull(getText("Bug53380_2.doc"));
    }

    /**
     * Bug 53380 - ArrayIndexOutOfBounds Exception parsing word 97 document
     */
    public void testBug53380_3() throws Exception
    {
        assertNotNull(getText("Bug53380_3.doc"));
    }

    /**
     * Bug 53380 - ArrayIndexOutOfBounds Exception parsing word 97 document
     */
    public void testBug53380_4() throws Exception
    {
        assertNotNull(getText("Bug53380_4.doc"));
    }
    
    /**
     * java.lang.UnsupportedOperationException: Non-extended character 
     *  Pascal strings are not supported right now
     * 
     * Disabled pending a fix for the bug
     */
    public void DISABLEDtest56880() throws Exception {
        HWPFDocument doc =
                HWPFTestDataSamples.openSampleFile("56880.doc");
        assertEqualsIgnoreNewline("Check Request", doc.getRange().text());
    }

    
    // These are the values the are explected to be read when the file
    // is checked.
    private int section1LeftMargin = 1440;
    private int section1RightMargin = 1440;
    private int section1TopMargin = 1440;
    private int section1BottomMargin = 1440;
    private int section1NumColumns = 1;
    private int section2LeftMargin = 1440;
    private int section2RightMargin = 1440;
    private int section2TopMargin = 1440;
    private int section2BottomMargin = 1440;
    private int section2NumColumns = 3;
    
    public void testHWPFSections() {
        HWPFDocument document = null;
        Paragraph para = null;
        Section section = null;
        Range overallRange = null;
        int numParas = 0;
        int numSections = 0;
        document = HWPFTestDataSamples.openSampleFile("Bug53453Section.doc");
        overallRange = document.getOverallRange();
        numParas = overallRange.numParagraphs();
        for(int i = 0; i < numParas; i++) {
            para = overallRange.getParagraph(i);
            numSections = para.numSections();
            for(int j = 0; j < numSections; j++) {
                section = para.getSection(j);
                if(para.text().trim().equals("Section1")) {
                    assertEquals(section1BottomMargin, section.getMarginBottom());
                    assertEquals(section1LeftMargin, section.getMarginLeft());
                    assertEquals(section1RightMargin, section.getMarginRight());
                    assertEquals(section1TopMargin, section.getMarginTop());
                    assertEquals(section1NumColumns, section.getNumColumns());
                }
                else if(para.text().trim().equals("Section2")) {
                    assertEquals(section2BottomMargin, section.getMarginBottom());
                    assertEquals(section2LeftMargin, section.getMarginLeft());
                    assertEquals(section2RightMargin, section.getMarginRight());
                    assertEquals(section2TopMargin, section.getMarginTop());
                    assertEquals(section2NumColumns, section.getNumColumns());
                    
                    // Change the margin widths
                    this.section2BottomMargin = (int)(1.5 * AbstractWordUtils.TWIPS_PER_INCH);
                    this.section2TopMargin = (int)(1.75 * AbstractWordUtils.TWIPS_PER_INCH);
                    this.section2LeftMargin = (int)(0.5 * AbstractWordUtils.TWIPS_PER_INCH);
                    this.section2RightMargin = (int)(0.75 * AbstractWordUtils.TWIPS_PER_INCH);
                    section.setMarginBottom(this.section2BottomMargin);
                    section.setMarginLeft(this.section2LeftMargin);
                    section.setMarginRight(this.section2RightMargin);
                    section.setMarginTop(this.section2TopMargin);
                }
            }
        }
        
        // Save away and re-read the document to prove the chages are permanent
        document = HWPFTestDataSamples.writeOutAndReadBack(document);
        overallRange = document.getOverallRange();
        numParas = overallRange.numParagraphs();
        for(int i = 0; i < numParas; i++) {
            para = overallRange.getParagraph(i);
            numSections = para.numSections();
            for(int j = 0; j < numSections; j++) {
                section = para.getSection(j);
                if(para.text().trim().equals("Section1")) {
                    // No changes to the margins in Section1
                    assertEquals(section1BottomMargin, section.getMarginBottom());
                    assertEquals(section1LeftMargin, section.getMarginLeft());
                    assertEquals(section1RightMargin, section.getMarginRight());
                    assertEquals(section1TopMargin, section.getMarginTop());
                    assertEquals(section1NumColumns, section.getNumColumns());
                }
                else if(para.text().trim().equals("Section2")) {
                    // The margins in Section2 have kept the new settings.
                    assertEquals(section2BottomMargin, section.getMarginBottom());
                    assertEquals(section2LeftMargin, section.getMarginLeft());
                    assertEquals(section2RightMargin, section.getMarginRight());
                    assertEquals(section2TopMargin, section.getMarginTop());
                    assertEquals(section2NumColumns, section.getNumColumns());
                }
            }
        }
    }

    public void testRegressionIn315beta2() {
        HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile("cap.stanford.edu_profiles_viewbiosketch_facultyid=4009&name=m_maciver.doc");
        assertNotNull(hwpfDocument);
    }
}