diff --git a/build.xml b/build.xml index 7d64dd3a7..d849be219 100644 --- a/build.xml +++ b/build.xml @@ -1025,6 +1025,7 @@ under the License. + @@ -1065,6 +1066,7 @@ under the License. + @@ -1747,8 +1750,8 @@ under the License. - diff --git a/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java b/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java index e3beebf4b..37d2092bf 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java +++ b/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java @@ -124,7 +124,7 @@ public final class PowerPointExtractor extends POIOLE2TextExtractor { public PowerPointExtractor(DirectoryNode dir, POIFSFileSystem fs) throws IOException { this(new HSLFSlideShowImpl(dir, fs)); } - + /** * Creates a PowerPointExtractor, from a HSLFSlideShow * diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java b/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java index 8ab581ede..b66549235 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java @@ -17,13 +17,18 @@ package org.apache.poi.hslf.extractor; +import static org.apache.poi.POITestCase.assertContains; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; import java.util.Locale; import org.apache.poi.POIDataSamples; -import org.apache.poi.POITestCase; import org.apache.poi.hslf.model.OLEShape; import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl; @@ -33,11 +38,14 @@ import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.IOUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; /** * Tests that the extractor correctly gets the text out of our sample file */ -public final class TestExtractor extends POITestCase { +public final class TestExtractor { /** Extractor primed on the 2 page basic test data */ private PowerPointExtractor ppe; private static final String expectText = "This is a test title\nThis is a test subtitle\nThis is on page 1\nThis is the title on page 2\nThis is page two\nIt has several blocks of text\nNone of them have formatting\n"; @@ -47,15 +55,21 @@ public final class TestExtractor extends POITestCase { private static final String expectText2 = "Hello, World!!!\nI am just a poor boy\nThis is Times New Roman\nPlain Text \n"; /** Where our embeded files live */ - //private String pdirname; private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); - //private String pdirname; - protected void setUp() throws Exception { - ppe = new PowerPointExtractor(slTests.openResourceAsStream("basic_test_ppt_file.ppt")); - ppe2 = new PowerPointExtractor(slTests.openResourceAsStream("with_textbox.ppt")); + @Before + public void setUp() throws Exception { + ppe = new PowerPointExtractor(slTests.getFile("basic_test_ppt_file.ppt").getCanonicalPath()); + ppe2 = new PowerPointExtractor(slTests.getFile("with_textbox.ppt").getCanonicalPath()); } + @After + public void closeResources() throws Exception { + ppe2.close(); + ppe.close(); + } + + @Test public void testReadSheetText() { // Basic 2 page example String sheetText = ppe.getText(); @@ -69,20 +83,22 @@ public final class TestExtractor extends POITestCase { ensureTwoStringsTheSame(expectText2, sheetText); } + @Test public void testReadNoteText() { // Basic 2 page example String notesText = ppe.getNotes(); - String expectText = "These are the notes for page 1\nThese are the notes on page two, again lacking formatting\n"; + String expText = "These are the notes for page 1\nThese are the notes on page two, again lacking formatting\n"; - ensureTwoStringsTheSame(expectText, notesText); + ensureTwoStringsTheSame(expText, notesText); // Other one doesn't have notes notesText = ppe2.getNotes(); - expectText = ""; + expText = ""; - ensureTwoStringsTheSame(expectText, notesText); + ensureTwoStringsTheSame(expText, notesText); } + @Test public void testReadBoth() { String[] slText = new String[] { "This is a test title\nThis is a test subtitle\nThis is on page 1\n", @@ -111,6 +127,7 @@ public final class TestExtractor extends POITestCase { * core record, we can still get the rest of the text out * @throws Exception */ + @Test public void testMissingCoreRecords() throws Exception { ppe = new PowerPointExtractor(slTests.openResourceAsStream("missing_core_records.ppt")); @@ -137,6 +154,7 @@ public final class TestExtractor extends POITestCase { assertEquals(exp,act); } + @Test public void testExtractFromEmbeded() throws Exception { POIFSFileSystem fs = new POIFSFileSystem( POIDataSamples.getSpreadSheetInstance().openResourceAsStream("excel_with_embeded.xls") @@ -164,12 +182,14 @@ public final class TestExtractor extends POITestCase { assertEquals("Sample PowerPoint file\nThis is the 2nd file\nNot much too it either\n", ppe.getText(true, false) ); + fs.close(); } /** * A powerpoint file with embeded powerpoint files */ @SuppressWarnings("unused") + @Test public void testExtractFromOwnEmbeded() throws Exception { String path = "ppt_with_embeded.ppt"; ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path)); @@ -182,6 +202,7 @@ public final class TestExtractor extends POITestCase { if ("Worksheet".equals(name)) { HSSFWorkbook wb = new HSSFWorkbook(data); num_xls++; + wb.close(); } else if ("Document".equals(name)) { HWPFDocument doc = new HWPFDocument(data); num_doc++; @@ -189,6 +210,7 @@ public final class TestExtractor extends POITestCase { num_ppt++; HSLFSlideShow ppt = new HSLFSlideShow(data); } + data.close(); } assertEquals("Expected 2 embedded Word Documents", 2, num_doc); assertEquals("Expected 2 embedded Excel Spreadsheets", 2, num_xls); @@ -198,6 +220,7 @@ public final class TestExtractor extends POITestCase { /** * A powerpoint file with embeded powerpoint files */ + @Test public void test52991() throws Exception { String path = "badzip.ppt"; ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path)); @@ -211,6 +234,7 @@ public final class TestExtractor extends POITestCase { /** * From bug #45543 */ + @Test public void testWithComments() throws Exception { ppe = new PowerPointExtractor(slTests.openResourceAsStream("WithComments.ppt")); @@ -238,6 +262,7 @@ public final class TestExtractor extends POITestCase { /** * From bug #45537 */ + @Test public void testHeaderFooter() throws Exception { String text; @@ -278,6 +303,7 @@ public final class TestExtractor extends POITestCase { } @SuppressWarnings("unused") + @Test public void testSlideMasterText() throws Exception { String masterTitleText = "This is the Master Title"; String masterRandomText = "This text comes from the Master Slide"; @@ -292,7 +318,8 @@ public final class TestExtractor extends POITestCase { assertContains(text, masterFooterText); } - public void testMasterText() throws Exception { + @Test + public void testMasterText() throws Exception { ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt")); // Initially not there @@ -322,6 +349,7 @@ public final class TestExtractor extends POITestCase { /** * Bug #54880 Chinese text not extracted properly */ + @Test public void testChineseText() throws Exception { HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("54880_chinese.ppt")); ppe = new PowerPointExtractor(hslf); @@ -346,6 +374,7 @@ public final class TestExtractor extends POITestCase { * and {@link NPOIFSFileSystem} */ @SuppressWarnings("resource") + @Test public void testDifferentPOIFS() throws Exception { // Open the two filesystems DirectoryNode[] files = new DirectoryNode[2]; @@ -369,6 +398,7 @@ public final class TestExtractor extends POITestCase { npoifsFileSystem.close(); } + @Test public void testTable() throws Exception{ // ppe = new PowerPointExtractor(slTests.openResourceAsStream("54111.ppt")); // String text = ppe.getText(); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/AllHWPFTests.java b/src/scratchpad/testcases/org/apache/poi/hwpf/AllHWPFTests.java index fc54e2a99..efa586496 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/AllHWPFTests.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/AllHWPFTests.java @@ -17,8 +17,6 @@ package org.apache.poi.hwpf; -import junit.framework.Test; -import junit.framework.TestSuite; import org.apache.poi.hwpf.converter.AbstractWordUtilsTest; import org.apache.poi.hwpf.converter.TestWordToFoConverter; import org.apache.poi.hwpf.converter.TestWordToHtmlConverter; @@ -58,69 +56,65 @@ import org.apache.poi.hwpf.usermodel.TestRangeReplacement; import org.apache.poi.hwpf.usermodel.TestRangeSymbols; import org.apache.poi.hwpf.usermodel.TestShapes; import org.apache.poi.hwpf.usermodel.TestTableRow; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; -public final class AllHWPFTests -{ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + // org.apache.poi.hwpf + TestFieldsTables.class, + TestHWPFPictures.class, + TestHWPFRangeParts.class, - public static Test suite() - { - TestSuite suite = new TestSuite( AllHWPFTests.class.getName() ); + // org.apache.poi.hwpf.converter + // TestWordToConverterSuite.class, + AbstractWordUtilsTest.class, + TestWordToFoConverter.class, + TestWordToHtmlConverter.class, - // org.apache.poi.hwpf - suite.addTestSuite( TestFieldsTables.class ); - suite.addTestSuite( TestHWPFPictures.class ); - suite.addTestSuite( TestHWPFRangeParts.class ); + // org.apache.poi.hwpf.extractor + TestDifferentRoutes.class, + TestWordExtractor.class, + TestWordExtractorBugs.class, - // org.apache.poi.hwpf.converter - // suite.addTestSuite( TestWordToConverterSuite.class ); - suite.addTestSuite( AbstractWordUtilsTest.class ); - suite.addTestSuite( TestWordToFoConverter.class ); - suite.addTestSuite( TestWordToHtmlConverter.class ); + // org.apache.poi.hwpf.model + TestBookmarksTables.class, + TestCHPBinTable.class, + TestDocumentProperties.class, + TestFileInformationBlock.class, + TestFontTable.class, + TestListTables.class, + TestNotesTables.class, + TestPAPBinTable.class, + TestPlexOfCps.class, + TestRevisionMarkAuthorTable.class, + TestSavedByTable.class, + TestSectionTable.class, + TestStyleSheet.class, + TestTextPieceTable.class, - // org.apache.poi.hwpf.extractor - suite.addTestSuite( TestDifferentRoutes.class ); - suite.addTestSuite( TestWordExtractor.class ); - suite.addTestSuite( TestWordExtractorBugs.class ); + // org.apache.poi.hwpf.sprm + TestSprms.class, - // org.apache.poi.hwpf.model - suite.addTestSuite( TestBookmarksTables.class ); - suite.addTestSuite( TestCHPBinTable.class ); - suite.addTestSuite( TestDocumentProperties.class ); - suite.addTestSuite( TestFileInformationBlock.class ); - suite.addTestSuite( TestFontTable.class ); - suite.addTestSuite( TestListTables.class ); - suite.addTestSuite( TestNotesTables.class ); - suite.addTestSuite( TestPAPBinTable.class ); - suite.addTestSuite( TestPlexOfCps.class ); - suite.addTestSuite( TestRevisionMarkAuthorTable.class ); - suite.addTestSuite( TestSavedByTable.class ); - suite.addTestSuite( TestSectionTable.class ); - suite.addTestSuite( TestStyleSheet.class ); - suite.addTestSuite( TestTextPieceTable.class ); - - // org.apache.poi.hwpf.sprm - suite.addTestSuite( TestSprms.class ); - - // org.apache.poi.hwpf.usermodel - suite.addTestSuite( TestBorderCode.class ); - suite.addTestSuite( TestBug46610.class ); - suite.addTestSuite( TestBug49820.class ); - suite.addTestSuite( TestBug50075.class ); - suite.addTestSuite( TestBugs.class ); - suite.addTestSuite( TestHeaderStories.class ); - suite.addTestSuite( TestHWPFOldDocument.class ); - suite.addTestSuite( TestLists.class ); - suite.addTestSuite( TestPictures.class ); - suite.addTestSuite( TestProblems.class ); - suite.addTestSuite( TestRange.class ); - suite.addTestSuite( TestRangeDelete.class ); - suite.addTestSuite( TestRangeInsertion.class ); - suite.addTestSuite( TestRangeProperties.class ); - suite.addTestSuite( TestRangeReplacement.class ); - suite.addTestSuite( TestRangeSymbols.class ); - suite.addTestSuite( TestShapes.class ); - suite.addTestSuite( TestTableRow.class ); - - return suite; - } + // org.apache.poi.hwpf.usermodel + TestBorderCode.class, + TestBug46610.class, + TestBug49820.class, + TestBug50075.class, + TestBugs.class, + TestHeaderStories.class, + TestHWPFOldDocument.class, + TestLists.class, + TestPictures.class, + TestProblems.class, + TestRange.class, + TestRangeDelete.class, + TestRangeInsertion.class, + TestRangeProperties.class, + TestRangeReplacement.class, + TestRangeSymbols.class, + TestShapes.class, + TestTableRow.class +}) +public final class AllHWPFTests { } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java index 4b04bdffb..7dd60b0b8 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java @@ -18,6 +18,8 @@ */ package org.apache.poi.hwpf.converter; +import static org.apache.poi.POITestCase.assertContains; + import java.io.StringWriter; import javax.xml.transform.OutputKeys; @@ -27,14 +29,14 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.poi.POIDataSamples; -import org.apache.poi.POITestCase; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.util.XMLHelper; +import org.junit.Test; /** * Test cases for {@link WordToFoConverter} */ -public class TestWordToFoConverter extends POITestCase +public class TestWordToFoConverter { private static String getFoText( final String sampleFileName ) throws Exception @@ -59,6 +61,7 @@ public class TestWordToFoConverter extends POITestCase return result; } + @Test public void testDocumentProperties() throws Exception { String result = getFoText( "documentProperties.doc" ); @@ -71,6 +74,7 @@ public class TestWordToFoConverter extends POITestCase "This is document keywords" ); } + @Test public void testEndnote() throws Exception { String result = getFoText( "endingnote.doc" ); @@ -86,6 +90,7 @@ public class TestWordToFoConverter extends POITestCase assertContains( result, "Ending note text" ); } + @Test public void testEquation() throws Exception { final String sampleFileName = "equation.doc"; @@ -94,6 +99,7 @@ public class TestWordToFoConverter extends POITestCase assertContains( result, "" ); } + @Test public void testHyperlink() throws Exception { final String sampleFileName = "hyperlink.doc"; @@ -104,6 +110,7 @@ public class TestWordToFoConverter extends POITestCase assertContains( result, "Hyperlink text" ); } + @Test public void testInnerTable() throws Exception { final String sampleFileName = "innertable.doc"; @@ -113,6 +120,7 @@ public class TestWordToFoConverter extends POITestCase "padding-end=\"0.0in\" padding-start=\"0.0in\" width=\"1.0770833in\"" ); } + @Test public void testPageBreak() throws Exception { final String sampleFileName = "page-break.doc"; @@ -121,6 +129,7 @@ public class TestWordToFoConverter extends POITestCase assertContains( result, ""); } + @Test public void testBug33519() throws Exception { String result = getHtmlText( "Bug33519.doc"); @@ -98,6 +103,7 @@ public class TestWordToHtmlConverter extends POITestCase "\u042F\u0432\u043E\u0440 \u0410\u0441\u0435\u043D\u043E\u0432"); } + @Test public void testBug46610_2() throws Exception { String result = getHtmlText( "Bug46610_2.doc"); @@ -106,6 +112,7 @@ public class TestWordToHtmlConverter extends POITestCase "012345678911234567892123456789312345678941234567890123456789112345678921234567893123456789412345678"); } + @Test public void testBug46817() throws Exception { String result = getHtmlText( "Bug46817.doc"); @@ -113,6 +120,7 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, substring); } + @Test public void testBug47286() throws Exception { String result = getHtmlText( "Bug47286.doc"); @@ -124,11 +132,13 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, "mfa.gov.cy"); } + @Test public void testBug48075() throws Exception { getHtmlText( "Bug48075.doc"); } + @Test public void testBug52583() throws Exception { String result = getHtmlText( "Bug52583.doc"); @@ -137,12 +147,14 @@ public class TestWordToHtmlConverter extends POITestCase ""); } + @Test public void testBug53182() throws Exception { String result = getHtmlText( "Bug53182.doc"); assertFalse(result.contains( "italic" )); } + @Test public void testDocumentProperties() throws Exception { String result = getHtmlText( "documentProperties.doc"); @@ -152,6 +164,7 @@ public class TestWordToHtmlConverter extends POITestCase ""); } + @Test public void testEmailhyperlink() throws Exception { String result = getHtmlText( "Bug47286.doc"); @@ -159,6 +172,7 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, substring); } + @Test public void testEndnote() throws Exception { String result = getHtmlText( "endingnote.doc"); @@ -172,6 +186,7 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, "Ending note text"); } + @Test public void testEquation() throws Exception { String result = getHtmlText( "equation.doc"); @@ -179,6 +194,7 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, ""); } + @Test public void testHyperlink() throws Exception { String result = getHtmlText( "hyperlink.doc"); @@ -189,11 +205,13 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, "; after text"); } + @Test public void testInnerTable() throws Exception { getHtmlText( "innertable.doc"); } + @Test public void testListsMargins() throws Exception { String result = getHtmlText( "lists-margins.doc"); @@ -210,11 +228,13 @@ public class TestWordToHtmlConverter extends POITestCase ".p4{text-indent:-0.59652776in;margin-left:-0.70069444in;"); } + @Test public void testO_kurs_doc() throws Exception { getHtmlText( "o_kurs.doc"); } + @Test public void testPageref() throws Exception { String result = getHtmlText( "pageref.doc"); @@ -224,6 +244,7 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, "1"); } + @Test public void testPicture() throws Exception { String result = getHtmlText( "picture.doc", true); @@ -238,6 +259,7 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, "width:3.4125in;height:2.325in;"); } + @Test public void testPicturesEscher() throws Exception { String result = getHtmlText( "pictures_escher.doc", true); @@ -245,6 +267,7 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, ""); } + @Test public void testTableMerges() throws Exception { String result = getHtmlText( "table-merges.doc"); @@ -253,6 +276,7 @@ public class TestWordToHtmlConverter extends POITestCase assertContains(result, ""); } + @Test public void testBug52420() throws Exception { String result = getHtmlText( "52420.doc"); diff --git a/src/testcases/org/apache/poi/POITestCase.java b/src/testcases/org/apache/poi/POITestCase.java index 3d0cea51d..6de936fdd 100644 --- a/src/testcases/org/apache/poi/POITestCase.java +++ b/src/testcases/org/apache/poi/POITestCase.java @@ -17,16 +17,17 @@ package org.apache.poi; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.util.Collection; -import junit.framework.TestCase; - /** * Parent class for POI JUnit TestCases, which provide additional * features */ -public class POITestCase extends TestCase { +public class POITestCase { public static void assertContains(String haystack, String needle) { assertTrue( "Unable to find expected text '" + needle + "' in text:\n" + haystack, @@ -40,21 +41,6 @@ public class POITestCase extends TestCase { ); } - public static void assertEquals(T[] expected, T[] actual) - { - assertEquals("Non-matching lengths", expected.length, actual.length); - for (int i=0; i void assertContains(T needle, T[] haystack) { // Check diff --git a/src/testcases/org/apache/poi/ddf/TestEscherDump.java b/src/testcases/org/apache/poi/ddf/TestEscherDump.java index 718585592..efc642714 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherDump.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherDump.java @@ -23,12 +23,15 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.io.UnsupportedEncodingException; import org.apache.poi.POIDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.util.IOUtils; +import org.apache.poi.util.LocaleUtil; import org.junit.Test; +@SuppressWarnings("resource") public class TestEscherDump { @Test public void testSimple() throws Exception { @@ -64,8 +67,8 @@ public class TestEscherDump { * to redirect stdout to avoid spamming the console with output */ private static class NullPrinterStream extends PrintStream { - private NullPrinterStream() { - super(new NullOutputStream()); + private NullPrinterStream() throws UnsupportedEncodingException { + super(new NullOutputStream(),true,LocaleUtil.CHARSET_1252.name()); } /** * Implementation of an OutputStream which does nothing, used diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestBasic.java b/src/testcases/org/apache/poi/hpsf/basic/TestBasic.java index b9b0edec8..ef902c083 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestBasic.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestBasic.java @@ -17,6 +17,11 @@ package org.apache.poi.hpsf.basic; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; @@ -25,8 +30,6 @@ import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.List; -import junit.framework.TestCase; - import org.apache.poi.POIDataSamples; import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.HPSFException; @@ -37,13 +40,15 @@ import org.apache.poi.hpsf.PropertySetFactory; import org.apache.poi.hpsf.Section; import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hpsf.wellknown.SectionIDMap; +import org.junit.Before; +import org.junit.Test; /** *

Tests the basic HPSF functionality.

* * @author Rainer Klute (klute@rainer-klute.de) */ -public final class TestBasic extends TestCase { +public final class TestBasic { private static final String POI_FS = "TestGermanWord90.doc"; private static final String[] POI_FILES = new String[] @@ -80,6 +85,7 @@ public final class TestBasic extends TestCase { * @exception FileNotFoundException if the file to be read does not exist. * @exception IOException if any other I/O exception occurs. */ + @Before public void setUp() throws IOException { POIDataSamples samples = POIDataSamples.getHPSFInstance(); @@ -91,6 +97,7 @@ public final class TestBasic extends TestCase { *

Checks the names of the files in the POI filesystem. They * are expected to be in a certain order.

*/ + @Test public void testReadFiles() { String[] expected = POI_FILES; @@ -112,6 +119,7 @@ public final class TestBasic extends TestCase { * @exception UnsupportedEncodingException if a character encoding is not * supported. */ + @Test public void testCreatePropertySets() throws UnsupportedEncodingException, IOException { @@ -152,24 +160,21 @@ public final class TestBasic extends TestCase { * @exception IOException if an I/O exception occurs * @exception HPSFException if any HPSF exception occurs */ + @Test public void testPropertySetMethods() throws IOException, HPSFException { /* Loop over the two property sets. */ for (int i = 0; i < 2; i++) { byte[] b = poiFiles[i].getBytes(); - PropertySet ps = - PropertySetFactory.create(new ByteArrayInputStream(b)); - assertEquals(ps.getByteOrder(), BYTE_ORDER); - assertEquals(ps.getFormat(), FORMAT); - assertEquals(ps.getOSVersion(), OS_VERSION); - assertEquals(new String(ps.getClassID().getBytes()), - new String(CLASS_ID)); - assertEquals(ps.getSectionCount(), SECTION_COUNT[i]); - assertEquals(ps.isSummaryInformation(), - IS_SUMMARY_INFORMATION[i]); - assertEquals(ps.isDocumentSummaryInformation(), - IS_DOCUMENT_SUMMARY_INFORMATION[i]); + PropertySet ps = PropertySetFactory.create(new ByteArrayInputStream(b)); + assertEquals(BYTE_ORDER, ps.getByteOrder()); + assertEquals(FORMAT, ps.getFormat()); + assertEquals(OS_VERSION, ps.getOSVersion()); + assertArrayEquals(CLASS_ID, ps.getClassID().getBytes()); + assertEquals(SECTION_COUNT[i], ps.getSectionCount()); + assertEquals(IS_SUMMARY_INFORMATION[i], ps.isSummaryInformation()); + assertEquals(IS_DOCUMENT_SUMMARY_INFORMATION[i], ps.isDocumentSummaryInformation()); } } @@ -181,6 +186,7 @@ public final class TestBasic extends TestCase { * @exception IOException if an I/O exception occurs * @exception HPSFException if any HPSF exception occurs */ + @Test public void testSectionMethods() throws IOException, HPSFException { final SummaryInformation si = (SummaryInformation) diff --git a/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java index c96aec4f7..0463162da 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java @@ -19,9 +19,11 @@ package org.apache.poi.hssf.dev; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStreamWriter; import java.io.PrintWriter; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; +import org.apache.poi.util.LocaleUtil; public class TestBiffViewer extends BaseXLSIteratingTest { static { @@ -44,12 +46,13 @@ public class TestBiffViewer extends BaseXLSIteratingTest { } @Override - void runOneFile(File file) throws IOException { - NPOIFSFileSystem fs = new NPOIFSFileSystem(file, true); + void runOneFile(File fileIn) throws IOException { + NPOIFSFileSystem fs = new NPOIFSFileSystem(fileIn, true); InputStream is = BiffViewer.getPOIFSInputStream(fs); try { - // use a NullOutputStream to not write the bytes anywhere for best runtime - BiffViewer.runBiffViewer(new PrintWriter(NULL_OUTPUT_STREAM), is, true, true, true, false); + // use a NullOutputStream to not write the bytes anywhere for best runtime + PrintWriter dummy = new PrintWriter(new OutputStreamWriter(NULL_OUTPUT_STREAM, LocaleUtil.CHARSET_1252)); + BiffViewer.runBiffViewer(dummy, is, true, true, true, false); } finally { is.close(); fs.close(); diff --git a/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java index 32eaa924f..0763a5587 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java @@ -20,6 +20,8 @@ import java.io.File; import java.io.IOException; import java.io.PrintStream; +import org.apache.poi.util.LocaleUtil; + public class TestEFBiffViewer extends BaseXLSIteratingTest { static { // these are likely ok to fail @@ -39,13 +41,13 @@ public class TestEFBiffViewer extends BaseXLSIteratingTest { } @Override - void runOneFile(File file) throws IOException { + void runOneFile(File fileIn) throws IOException { PrintStream save = System.out; try { // redirect standard out during the test to avoid spamming the console with output - System.setOut(new PrintStream(NULL_OUTPUT_STREAM)); + System.setOut(new PrintStream(NULL_OUTPUT_STREAM,true,LocaleUtil.CHARSET_1252.name())); - EFBiffViewer.main(new String[] { file.getAbsolutePath() }); + EFBiffViewer.main(new String[] { fileIn.getAbsolutePath() }); } finally { System.setOut(save); } diff --git a/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java index b6014376c..1b2ef67e2 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java @@ -19,6 +19,7 @@ package org.apache.poi.hssf.dev; import java.io.File; import java.io.PrintStream; +import org.apache.poi.util.LocaleUtil; import org.junit.Ignore; import org.junit.Test; @@ -39,15 +40,15 @@ public class TestFormulaViewer extends BaseXLSIteratingTest { public void testMain() throws Exception { } - @Override - void runOneFile(File file) throws Exception { + @Override + void runOneFile(File fileIn) throws Exception { PrintStream save = System.out; try { // redirect standard out during the test to avoid spamming the console with output - System.setOut(new PrintStream(NULL_OUTPUT_STREAM)); + System.setOut(new PrintStream(NULL_OUTPUT_STREAM,true,LocaleUtil.CHARSET_1252.name())); FormulaViewer viewer = new FormulaViewer(); - viewer.setFile(file.getAbsolutePath()); + viewer.setFile(fileIn.getAbsolutePath()); viewer.setList(true); viewer.run(); } finally { diff --git a/src/testcases/org/apache/poi/hssf/dev/TestReSave.java b/src/testcases/org/apache/poi/hssf/dev/TestReSave.java index e26bffea3..d858f881e 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestReSave.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestReSave.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.List; import org.apache.poi.POIDataSamples; +import org.apache.poi.util.LocaleUtil; public class TestReSave extends BaseXLSIteratingTest { static { @@ -36,20 +37,20 @@ public class TestReSave extends BaseXLSIteratingTest { } @Override - void runOneFile(File file) throws Exception { + void runOneFile(File fileIn) throws Exception { // avoid running on files leftover from previous failed runs - if(file.getName().endsWith("-saved.xls")) { + if(fileIn.getName().endsWith("-saved.xls")) { return; } PrintStream save = System.out; try { // redirect standard out during the test to avoid spamming the console with output - System.setOut(new PrintStream(NULL_OUTPUT_STREAM)); + System.setOut(new PrintStream(NULL_OUTPUT_STREAM,true,LocaleUtil.CHARSET_1252.name())); - File reSavedFile = new File(file.getParentFile(), file.getName().replace(".xls", "-saved.xls")); + File reSavedFile = new File(fileIn.getParentFile(), fileIn.getName().replace(".xls", "-saved.xls")); try { - ReSave.main(new String[] { file.getAbsolutePath() }); + ReSave.main(new String[] { fileIn.getAbsolutePath() }); // also try BiffViewer on the saved file new TestBiffViewer().runOneFile(reSavedFile); @@ -59,7 +60,7 @@ public class TestReSave extends BaseXLSIteratingTest { ReSave.main(new String[] { reSavedFile.getAbsolutePath() }); } finally { // clean up the re-re-saved file - new File(file.getParentFile(), reSavedFile.getName().replace(".xls", "-saved.xls")).delete(); + new File(fileIn.getParentFile(), reSavedFile.getName().replace(".xls", "-saved.xls")).delete(); } } finally { // clean up the re-saved file diff --git a/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java b/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java index 4c0ad1add..7f5379304 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java @@ -20,6 +20,8 @@ import java.io.File; import java.io.IOException; import java.io.PrintStream; +import org.apache.poi.util.LocaleUtil; + public class TestRecordLister extends BaseXLSIteratingTest { static { // these are likely ok to fail @@ -31,14 +33,14 @@ public class TestRecordLister extends BaseXLSIteratingTest { } @Override - void runOneFile(File file) throws IOException { + void runOneFile(File fileIn) throws IOException { PrintStream save = System.out; try { // redirect standard out during the test to avoid spamming the console with output - System.setOut(new PrintStream(NULL_OUTPUT_STREAM)); + System.setOut(new PrintStream(NULL_OUTPUT_STREAM,true,LocaleUtil.CHARSET_1252.name())); RecordLister viewer = new RecordLister(); - viewer.setFile(file.getAbsolutePath()); + viewer.setFile(fileIn.getAbsolutePath()); viewer.run(); } finally { System.setOut(save); diff --git a/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java b/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java index bb2cfea66..0a236a290 100644 --- a/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java +++ b/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java @@ -17,27 +17,32 @@ package org.apache.poi.hssf.extractor; -import java.io.File; -import java.io.InputStream; +import static org.apache.poi.POITestCase.assertContains; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; -import org.apache.poi.POITestCase; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.junit.Test; /** * Unit tests for the Excel 5/95 and Excel 4 (and older) text * extractor */ -public final class TestOldExcelExtractor extends POITestCase { +public final class TestOldExcelExtractor { private static OldExcelExtractor createExtractor(String sampleFileName) { - InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName); + File file = HSSFTestDataSamples.getSampleFile(sampleFileName); try { - return new OldExcelExtractor(is); + return new OldExcelExtractor(file); } catch (Exception e) { throw new RuntimeException(e); } } + @Test public void testSimpleExcel3() { OldExcelExtractor extractor = createExtractor("testEXCEL_3.xls"); @@ -59,6 +64,8 @@ public final class TestOldExcelExtractor extends POITestCase { assertEquals(3, extractor.getBiffVersion()); assertEquals(0x10, extractor.getFileType()); } + + @Test public void testSimpleExcel4() { OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls"); @@ -77,6 +84,8 @@ public final class TestOldExcelExtractor extends POITestCase { assertEquals(4, extractor.getBiffVersion()); assertEquals(0x10, extractor.getFileType()); } + + @Test public void testSimpleExcel5() { for (String ver : new String[] {"5", "95"}) { OldExcelExtractor extractor = createExtractor("testEXCEL_"+ver+".xls"); @@ -101,6 +110,7 @@ public final class TestOldExcelExtractor extends POITestCase { } } + @Test public void testStrings() { OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls"); String text = extractor.getText(); @@ -119,6 +129,7 @@ public final class TestOldExcelExtractor extends POITestCase { // TODO Find some then test } + @Test public void testFormattedNumbersExcel4() { OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls"); String text = extractor.getText(); @@ -136,6 +147,8 @@ public final class TestOldExcelExtractor extends POITestCase { // assertContains(text, "55,624"); // assertContains(text, "11,743,477"); } + + @Test public void testFormattedNumbersExcel5() { for (String ver : new String[] {"5", "95"}) { OldExcelExtractor extractor = createExtractor("testEXCEL_"+ver+".xls"); @@ -160,6 +173,7 @@ public final class TestOldExcelExtractor extends POITestCase { } } + @Test public void testFromFile() throws Exception { for (String ver : new String[] {"4", "5", "95"}) { String filename = "testEXCEL_"+ver+".xls"; diff --git a/src/testcases/org/apache/poi/hssf/model/AllModelTests.java b/src/testcases/org/apache/poi/hssf/model/AllModelTests.java index a24322686..0d140ab7d 100644 --- a/src/testcases/org/apache/poi/hssf/model/AllModelTests.java +++ b/src/testcases/org/apache/poi/hssf/model/AllModelTests.java @@ -17,30 +17,26 @@ package org.apache.poi.hssf.model; -import junit.framework.Test; -import junit.framework.TestSuite; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; /** * Collects all tests for org.apache.poi.hssf.model. - * - * @author Josh Micich */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + TestDrawingManager.class, + TestDrawingManager2.class, + TestFormulaParser.class, + TestFormulaParserEval.class, + TestFormulaParserIf.class, + TestLinkTable.class, + TestOperandClassTransformer.class, + TestRowBlocksReader.class, + TestRVA.class, + TestSheet.class, + TestSheetAdditional.class, + TestWorkbook.class +}) public final class AllModelTests { - - public static Test suite() { - TestSuite result = new TestSuite(AllModelTests.class.getName()); - result.addTestSuite(TestDrawingManager.class); - result.addTestSuite(TestDrawingManager2.class); - result.addTestSuite(TestFormulaParser.class); - result.addTestSuite(TestFormulaParserEval.class); - result.addTestSuite(TestFormulaParserIf.class); - result.addTestSuite(TestLinkTable.class); - result.addTestSuite(TestOperandClassTransformer.class); - result.addTestSuite(TestRowBlocksReader.class); - result.addTestSuite(TestRVA.class); - result.addTestSuite(TestSheet.class); - result.addTestSuite(TestSheetAdditional.class); - result.addTestSuite(TestWorkbook.class); - return result; - } } diff --git a/src/testcases/org/apache/poi/hssf/model/TestRowBlocksReader.java b/src/testcases/org/apache/poi/hssf/model/TestRowBlocksReader.java index 3dd24aeee..bf1fce799 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestRowBlocksReader.java +++ b/src/testcases/org/apache/poi/hssf/model/TestRowBlocksReader.java @@ -17,10 +17,12 @@ package org.apache.poi.hssf.model; -import java.util.Arrays; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import java.util.Arrays; import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.Record; @@ -28,27 +30,30 @@ import org.apache.poi.hssf.record.RowRecord; import org.apache.poi.hssf.record.UnknownRecord; import org.apache.poi.hssf.record.WindowTwoRecord; import org.apache.poi.hssf.record.pivottable.ViewDefinitionRecord; +import org.apache.poi.util.LocaleUtil; +import org.junit.Test; /** * Tests for {@link RowBlocksReader} * * @author Josh Micich */ -public final class TestRowBlocksReader extends TestCase { +public final class TestRowBlocksReader { + @Test public void testAbnormalPivotTableRecords_bug46280() { int SXVIEW_SID = ViewDefinitionRecord.sid; Record[] inRecs = { new RowRecord(0), new NumberRecord(), // normally MSODRAWING(0x00EC) would come here before SXVIEW - new UnknownRecord(SXVIEW_SID, "dummydata (SXVIEW: View Definition)".getBytes()), + new UnknownRecord(SXVIEW_SID, "dummydata (SXVIEW: View Definition)".getBytes(LocaleUtil.CHARSET_1252)), new WindowTwoRecord(), }; RecordStream rs = new RecordStream(Arrays.asList(inRecs), 0); RowBlocksReader rbr = new RowBlocksReader(rs); if (rs.peekNextClass() == WindowTwoRecord.class) { // Should have stopped at the SXVIEW record - throw new AssertionFailedError("Identified bug 46280b"); + fail("Identified bug 46280b"); } RecordStream rbStream = rbr.getPlainRecordStream(); assertEquals(inRecs[0], rbStream.getNext()); diff --git a/src/testcases/org/apache/poi/hssf/record/TestSSTRecord.java b/src/testcases/org/apache/poi/hssf/record/TestSSTRecord.java index 30b9aff17..cd98ee084 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestSSTRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestSSTRecord.java @@ -18,6 +18,9 @@ package org.apache.poi.hssf.record; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.BufferedReader; import java.io.ByteArrayInputStream; @@ -28,21 +31,22 @@ import java.io.InputStreamReader; import java.util.Arrays; import java.util.Iterator; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; - import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.record.common.UnicodeString; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.util.HexRead; import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.LocaleUtil; +import org.junit.Test; + +import junit.framework.AssertionFailedError; /** * @author Marc Johnson (mjohnson at apache dot org) * @author Glen Stampoultzis (glens at apache.org) */ -public final class TestSSTRecord extends TestCase { +public final class TestSSTRecord { /** * decodes hexdump files and concatenates the results @@ -55,7 +59,7 @@ public final class TestSSTRecord extends TestCase { for (int i = 0; i < nFiles; i++) { String sampleFileName = hexDumpFileNames[i]; InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName); - BufferedReader br = new BufferedReader(new InputStreamReader(is)); + BufferedReader br = new BufferedReader(new InputStreamReader(is, LocaleUtil.CHARSET_1252)); while (true) { String line = br.readLine(); @@ -86,6 +90,7 @@ public final class TestSSTRecord extends TestCase { * SST is often split over several {@link ContinueRecord}s * @throws IOException */ + @Test public void testContinuedRecord() throws IOException { byte[] origData; SSTRecord record; @@ -109,20 +114,20 @@ public final class TestSSTRecord extends TestCase { assertEquals( 5249, record.getNumUniqueStrings() ); assertEquals( 5249, record.countStrings() ); ser_output = record.serialize(); - if (false) { // set true to observe make sure areSameSSTs() is working - ser_output[11000] = 'X'; - } +// if (false) { // set true to observe make sure areSameSSTs() is working +// ser_output[11000] = 'X'; +// } SSTRecord rec2 = createSSTFromRawData(ser_output); if (!areSameSSTs(record, rec2)) { throw new AssertionFailedError("large SST re-serialized incorrectly"); } - if (false) { - // TODO - trivial differences in ContinueRecord break locations - // Sample data should be checked against what most recent Excel version produces. - // maybe tweaks are required in ContinuableRecordOutput - assertArrayEquals(origData, ser_output); - } +// if (false) { +// // TODO - trivial differences in ContinueRecord break locations +// // Sample data should be checked against what most recent Excel version produces. +// // maybe tweaks are required in ContinuableRecordOutput +// assertArrayEquals(origData, ser_output); +// } } private boolean areSameSSTs(SSTRecord a, SSTRecord b) { @@ -147,7 +152,7 @@ public final class TestSSTRecord extends TestCase { * * @exception IOException */ - + @Test public void testHugeStrings() { SSTRecord record = new SSTRecord(); byte[][] bstrings = @@ -161,7 +166,7 @@ public final class TestSSTRecord extends TestCase { for ( int k = 0; k < bstrings.length; k++ ) { Arrays.fill( bstrings[k], (byte) ( 'a' + k ) ); - strings[k] = new UnicodeString( new String(bstrings[k]) ); + strings[k] = new UnicodeString( new String(bstrings[k], LocaleUtil.CHARSET_1252) ); record.addString( strings[k] ); total_length += 3 + bstrings[k].length; } @@ -198,7 +203,7 @@ public final class TestSSTRecord extends TestCase { if ( ( bstrings[k].length % 2 ) == 1 ) { Arrays.fill( bstrings[k], (byte) ( 'a' + k ) ); - strings[k] = new UnicodeString( new String(bstrings[k]) ); + strings[k] = new UnicodeString( new String(bstrings[k], LocaleUtil.CHARSET_1252) ); } else { @@ -230,6 +235,7 @@ public final class TestSSTRecord extends TestCase { /** * test SSTRecord boundary conditions */ + @Test public void testSSTRecordBug() { // create an SSTRecord and write a certain pattern of strings // to it ... then serialize it and verify the content @@ -265,6 +271,7 @@ public final class TestSSTRecord extends TestCase { /** * test simple addString */ + @Test public void testSimpleAddString() { SSTRecord record = new SSTRecord(); UnicodeString s1 = new UnicodeString("Hello world"); @@ -310,6 +317,7 @@ public final class TestSSTRecord extends TestCase { /** * test simple constructor */ + @Test public void testSimpleConstructor() { SSTRecord record = new SSTRecord(); @@ -334,13 +342,16 @@ public final class TestSSTRecord extends TestCase { /** * Tests that workbooks with rich text that duplicates a non rich text cell can be read and written. */ - public void testReadWriteDuplicatedRichText1() { + @Test + public void testReadWriteDuplicatedRichText1() throws Exception { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("duprich1.xls"); HSSFSheet sheet = wb.getSheetAt( 1 ); assertEquals( "01/05 (Wed)", sheet.getRow( 0 ).getCell(8 ).getStringCellValue() ); assertEquals( "01/05 (Wed)", sheet.getRow( 1 ).getCell(8 ).getStringCellValue() ); - HSSFTestDataSamples.writeOutAndReadBack(wb); + HSSFTestDataSamples.writeOutAndReadBack(wb).close(); + + wb.close(); // test the second file. wb = HSSFTestDataSamples.openSampleWorkbook("duprich2.xls"); @@ -353,7 +364,9 @@ public final class TestSSTRecord extends TestCase { assertEquals( "Testing", sheet.getRow( row++ ).getCell(0 ).getStringCellValue() ); assertEquals( "Testing", sheet.getRow( row++ ).getCell(0 ).getStringCellValue() ); - HSSFTestDataSamples.writeOutAndReadBack(wb); + HSSFTestDataSamples.writeOutAndReadBack(wb).close(); + + wb.close(); } /** @@ -1447,7 +1460,7 @@ public final class TestSSTRecord extends TestCase { /** * deep comparison of two SST records */ - public static void assertEquals(SSTRecord expected, SSTRecord actual){ + private static void assertRecordEquals(SSTRecord expected, SSTRecord actual){ assertEquals("number of strings", expected.getNumStrings(), actual.getNumStrings()); assertEquals("number of unique strings", expected.getNumUniqueStrings(), actual.getNumUniqueStrings()); assertEquals("count of strings", expected.countStrings(), actual.countStrings()); @@ -1458,8 +1471,8 @@ public final class TestSSTRecord extends TestCase { assertTrue("String at idx=" + k, us1.equals(us2)); } } - - + + @Test public void test50779_1(){ byte[] bytes = HexRead.readFromString(data_50779_1); @@ -1475,9 +1488,10 @@ public final class TestSSTRecord extends TestCase { SSTRecord dst = new SSTRecord(in); assertEquals(81, dst.getNumStrings()); - assertEquals(src, dst); + assertRecordEquals(src, dst); } + @Test public void test50779_2() { byte[] bytes = HexRead.readFromString(data_50779_2); @@ -1493,9 +1507,10 @@ public final class TestSSTRecord extends TestCase { SSTRecord dst = new SSTRecord(in); assertEquals(81, dst.getNumStrings()); - assertEquals(src, dst); + assertRecordEquals(src, dst); } + @Test public void test57456() { byte[] bytes = HexRead.readFromString("FC, 00, 08, 00, 00, 00, 00, 00, E1, 06, 00, 00"); RecordInputStream in = TestcaseRecordInputStream.create(bytes); diff --git a/src/testcases/org/apache/poi/hssf/record/aggregates/AllRecordAggregateTests.java b/src/testcases/org/apache/poi/hssf/record/aggregates/AllRecordAggregateTests.java index 57fe336b6..4edf3eb9c 100644 --- a/src/testcases/org/apache/poi/hssf/record/aggregates/AllRecordAggregateTests.java +++ b/src/testcases/org/apache/poi/hssf/record/aggregates/AllRecordAggregateTests.java @@ -17,26 +17,21 @@ package org.apache.poi.hssf.record.aggregates; -import junit.framework.Test; -import junit.framework.TestSuite; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; /** * Collects all tests for package org.apache.poi.hssf.record.aggregates. - * - * @author Josh Micich */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + TestCFRecordsAggregate.class, + TestColumnInfoRecordsAggregate.class, + TestFormulaRecordAggregate.class, + TestRowRecordsAggregate.class, + TestSharedValueManager.class, + TestValueRecordsAggregate.class, + TestPageSettingsBlock.class +}) public final class AllRecordAggregateTests { - - public static Test suite() { - TestSuite result = new TestSuite(AllRecordAggregateTests.class.getName()); - - result.addTestSuite(TestCFRecordsAggregate.class); - result.addTestSuite(TestColumnInfoRecordsAggregate.class); - result.addTestSuite(TestFormulaRecordAggregate.class); - result.addTestSuite(TestRowRecordsAggregate.class); - result.addTestSuite(TestSharedValueManager.class); - result.addTestSuite(TestValueRecordsAggregate.class); - result.addTestSuite(TestPageSettingsBlock.class); - return result; - } } diff --git a/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java b/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java index 14a968612..1d8b508c7 100644 --- a/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java +++ b/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java @@ -17,14 +17,12 @@ package org.apache.poi.hssf.record.aggregates; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.util.Arrays; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import java.util.Arrays; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.model.RecordStream; @@ -42,12 +40,15 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.RecordInspector; import org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector; import org.apache.poi.hssf.util.CellRangeAddress8Bit; +import org.apache.poi.util.LocaleUtil; +import org.junit.Test; /** * Tests for {@link RowRecordsAggregate} */ -public final class TestRowRecordsAggregate extends TestCase { +public final class TestRowRecordsAggregate { + @Test public void testRowGet() { RowRecordsAggregate rra = new RowRecordsAggregate(); RowRecord rr = new RowRecord(4); @@ -67,7 +68,8 @@ public final class TestRowRecordsAggregate extends TestCase { * {@link SharedFormulaRecord}s, these records should appear immediately after the first * {@link FormulaRecord}s that they apply to (and only once).
*/ - public void testArraysAndTables() { + @Test + public void testArraysAndTables() throws Exception { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testArraysAndTables.xls"); Record[] sheetRecs = RecordInspector.getRecords(wb.getSheetAt(0), 0); @@ -82,20 +84,22 @@ public final class TestRowRecordsAggregate extends TestCase { assertEquals(0, countSharedFormulas); - if (false) { // set true to observe re-serialized file - File f = new File(System.getProperty("java.io.tmpdir") + "/testArraysAndTables-out.xls"); - try { - OutputStream os = new FileOutputStream(f); - wb.write(os); - os.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - System.out.println("Output file to " + f.getAbsolutePath()); - } +// if (false) { // set true to observe re-serialized file +// File f = new File(System.getProperty("java.io.tmpdir") + "/testArraysAndTables-out.xls"); +// try { +// OutputStream os = new FileOutputStream(f); +// wb.write(os); +// os.close(); +// } catch (IOException e) { +// throw new RuntimeException(e); +// } +// System.out.println("Output file to " + f.getAbsolutePath()); +// } + + wb.close(); } - private static int verifySharedValues(Record[] recs, Class shfClass) { + private static int verifySharedValues(Record[] recs, Class shfClass) { int result =0; for(int i=0; i objects = workbook.getAllEmbeddedObjects(); assertEquals("Wrong number of objects", 2, objects.size()); assertEquals("Wrong name for first object", "MBD06CAB431", - ((HSSFObjectData) - objects.get(0)).getDirectory().getName()); + objects.get(0).getDirectory().getName()); assertEquals("Wrong name for second object", "MBD06CAC85A", - ((HSSFObjectData) - objects.get(1)).getDirectory().getName()); + objects.get(1).getDirectory().getName()); + + workbook.close(); } + @Test public void testReallyEmbedSomething() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); + HSSFWorkbook wb1 = new HSSFWorkbook(); + HSSFSheet sheet = wb1.createSheet(); HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("logoKarmokar4.png"); byte[] picturePPT = POIDataSamples.getSlideShowInstance().readFile("clock.jpg"); - int imgIdx = wb.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG); + int imgIdx = wb1.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG); POIFSFileSystem pptPoifs = getSamplePPT(); - int pptIdx = wb.addOlePackage(pptPoifs, "Sample-PPT", "sample.ppt", "sample.ppt"); + int pptIdx = wb1.addOlePackage(pptPoifs, "Sample-PPT", "sample.ppt", "sample.ppt"); POIFSFileSystem xlsPoifs = getSampleXLS(); - int imgPPT = wb.addPicture(picturePPT, HSSFWorkbook.PICTURE_TYPE_JPEG); - int xlsIdx = wb.addOlePackage(xlsPoifs, "Sample-XLS", "sample.xls", "sample.xls"); - int txtIdx = wb.addOlePackage(getSampleTXT(), "Sample-TXT", "sample.txt", "sample.txt"); + int imgPPT = wb1.addPicture(picturePPT, HSSFWorkbook.PICTURE_TYPE_JPEG); + int xlsIdx = wb1.addOlePackage(xlsPoifs, "Sample-XLS", "sample.xls", "sample.xls"); + int txtIdx = wb1.addOlePackage(getSampleTXT(), "Sample-TXT", "sample.txt", "sample.txt"); int rowoffset = 5; int coloffset = 5; - CreationHelper ch = wb.getCreationHelper(); + CreationHelper ch = wb1.getCreationHelper(); HSSFClientAnchor anchor = (HSSFClientAnchor)ch.createClientAnchor(); anchor.setAnchor((short)(2+coloffset), 1+rowoffset, 0, 0, (short)(3+coloffset), 5+rowoffset, 0, 0); anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE); @@ -107,31 +112,35 @@ public final class TestOLE2Embeding extends TestCase { circle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL); circle.setNoFill(true); - if (false) { - FileOutputStream fos = new FileOutputStream("embed.xls"); - wb.write(fos); - fos.close(); - } +// if (false) { +// FileOutputStream fos = new FileOutputStream("embed.xls"); +// wb.write(fos); +// fos.close(); +// } - wb = HSSFTestDataSamples.writeOutAndReadBack(wb); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); - HSSFObjectData od = wb.getAllEmbeddedObjects().get(0); + HSSFObjectData od = wb2.getAllEmbeddedObjects().get(0); Ole10Native ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)od.getDirectory()); bos.reset(); pptPoifs.writeFilesystem(bos); assertArrayEquals(ole10.getDataBuffer(), bos.toByteArray()); - od = wb.getAllEmbeddedObjects().get(1); + od = wb2.getAllEmbeddedObjects().get(1); ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)od.getDirectory()); bos.reset(); xlsPoifs.writeFilesystem(bos); assertArrayEquals(ole10.getDataBuffer(), bos.toByteArray()); - od = wb.getAllEmbeddedObjects().get(2); + od = wb2.getAllEmbeddedObjects().get(2); ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)od.getDirectory()); assertArrayEquals(ole10.getDataBuffer(), getSampleTXT()); + xlsPoifs.close(); + pptPoifs.close(); + wb2.close(); } static POIFSFileSystem getSamplePPT() throws IOException { @@ -150,12 +159,13 @@ public final class TestOLE2Embeding extends TestCase { ByteArrayOutputStream bos = new ByteArrayOutputStream(); wb.write(bos); + wb.close(); POIFSFileSystem poifs = new POIFSFileSystem(new ByteArrayInputStream(bos.toByteArray())); return poifs; } static byte[] getSampleTXT() { - return "All your base are belong to us".getBytes(); + return "All your base are belong to us".getBytes(LocaleUtil.CHARSET_1252); } } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java index 4f102084d..d8eb43b42 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java @@ -17,25 +17,30 @@ package org.apache.poi.poifs.filesystem; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import java.io.ByteArrayInputStream; import java.nio.ByteBuffer; import java.util.Iterator; import org.apache.poi.POIDataSamples; -import org.apache.poi.POITestCase; import org.apache.poi.poifs.common.POIFSConstants; import org.apache.poi.util.IOUtils; +import org.junit.Test; /** * Tests for the Mini Store in the NIO POIFS */ -public final class TestNPOIFSMiniStore extends POITestCase { +public final class TestNPOIFSMiniStore { private static final POIDataSamples _inst = POIDataSamples.getPOIFSInstance(); /** * Check that for a given mini block, we can correctly figure * out what the next one is */ + @Test public void testNextBlock() throws Exception { // It's the same on 512 byte and 4096 byte block files! NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi")); @@ -103,14 +108,17 @@ public final class TestNPOIFSMiniStore extends POITestCase { for(int i=181; i - * - * @author Josh Micich */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + TestDirectoryProperty.class, + TestDocumentProperty.class, + TestPropertyFactory.class, + TestPropertyTable.class, + TestRootProperty.class +}) public final class AllPOIFSPropertyTests { - - public static Test suite() { - TestSuite result = new TestSuite(AllPOIFSPropertyTests.class.getName()); - result.addTestSuite(TestDirectoryProperty.class); - result.addTestSuite(TestDocumentProperty.class); - result.addTestSuite(TestPropertyFactory.class); - result.addTestSuite(TestPropertyTable.class); - result.addTestSuite(TestRootProperty.class); - return result; - } } diff --git a/src/testcases/org/apache/poi/poifs/property/TestDirectoryProperty.java b/src/testcases/org/apache/poi/poifs/property/TestDirectoryProperty.java index 9569de1d4..8db101c25 100644 --- a/src/testcases/org/apache/poi/poifs/property/TestDirectoryProperty.java +++ b/src/testcases/org/apache/poi/poifs/property/TestDirectoryProperty.java @@ -17,6 +17,10 @@ package org.apache.poi.poifs.property; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -25,21 +29,20 @@ import java.util.Iterator; import java.util.List; import org.apache.poi.poifs.storage.RawDataUtil; - -import junit.framework.TestCase; +import org.apache.poi.util.LocaleUtil; +import org.junit.Test; /** * Class to test DirectoryProperty functionality - * - * @author Marc Johnson */ -public final class TestDirectoryProperty extends TestCase { +public final class TestDirectoryProperty { private DirectoryProperty _property; private byte[] _testblock; /** * Test constructing DirectoryProperty */ + @Test public void testConstructor() throws IOException { createBasicDirectoryProperty(); verifyProperty(); @@ -48,6 +51,7 @@ public final class TestDirectoryProperty extends TestCase { /** * Test pre-write functionality */ + @Test public void testPreWrite() throws IOException { createBasicDirectoryProperty(); _property.preWrite(); @@ -97,8 +101,8 @@ public final class TestDirectoryProperty extends TestCase { } private void verifyChildren(int count) { - Iterator iter = _property.getChildren(); - List children = new ArrayList(); + Iterator iter = _property.getChildren(); + List children = new ArrayList(); while (iter.hasNext()) { @@ -116,7 +120,7 @@ public final class TestDirectoryProperty extends TestCase { iter = children.iterator(); while (iter.hasNext()) { - Property child = ( Property ) iter.next(); + Property child = iter.next(); Child next = child.getNextChild(); if (next != null) @@ -175,7 +179,7 @@ public final class TestDirectoryProperty extends TestCase { { _testblock[ index ] = ( byte ) 0; } - byte[] name_bytes = name.getBytes(); + byte[] name_bytes = name.getBytes(LocaleUtil.CHARSET_1252); for (index = 0; index < limit; index++) { @@ -197,6 +201,7 @@ public final class TestDirectoryProperty extends TestCase { } } + @Test public void testAddChild() throws IOException { createBasicDirectoryProperty(); _property.addChild(new LocalProperty(1)); @@ -224,6 +229,7 @@ public final class TestDirectoryProperty extends TestCase { _property.addChild(new LocalProperty(3)); } + @Test public void testDeleteChild() throws IOException { createBasicDirectoryProperty(); Property p1 = new LocalProperty(1); @@ -244,6 +250,7 @@ public final class TestDirectoryProperty extends TestCase { _property.addChild(new LocalProperty(1)); } + @Test public void testChangeName() throws IOException { createBasicDirectoryProperty(); Property p1 = new LocalProperty(1); @@ -262,6 +269,7 @@ public final class TestDirectoryProperty extends TestCase { assertTrue(_property.changeName(p1, originalName)); } + @Test public void testReadingConstructor() { String[] input = { "42 00 6F 00 6F 00 74 00 20 00 45 00 6E 00 74 00 72 00 79 00 00 00 00 00 00 00 00 00 00 00 00 00", diff --git a/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java b/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java index 41acc760b..bd0b30edd 100644 --- a/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java +++ b/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import org.apache.poi.poifs.storage.RawDataUtil; +import org.apache.poi.util.LocaleUtil; import junit.framework.TestCase; @@ -137,7 +138,7 @@ public final class TestDocumentProperty extends TestCase { { testblock[ index ] = ( byte ) 0x0; } - byte[] name_bytes = name.getBytes(); + byte[] name_bytes = name.getBytes(LocaleUtil.CHARSET_1252); for (index = 0; index < limit; index++) { diff --git a/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java b/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java index 46c073096..27708a4b6 100644 --- a/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java +++ b/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java @@ -17,23 +17,27 @@ package org.apache.poi.poifs.property; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayOutputStream; import java.io.IOException; -import junit.framework.TestCase; - import org.apache.poi.poifs.common.POIFSConstants; import org.apache.poi.poifs.storage.RawDataUtil; +import org.apache.poi.util.LocaleUtil; +import org.junit.Test; /** * Class to test RootProperty functionality * * @author Marc Johnson */ -public final class TestRootProperty extends TestCase { +public final class TestRootProperty { private RootProperty _property; private byte[] _testblock; + @Test public void testConstructor() throws IOException { createBasicRootProperty(); verifyProperty(); @@ -67,7 +71,7 @@ public final class TestRootProperty extends TestCase { for (; index < 0x80; index++) { _testblock[index] = (byte) 0; } - byte[] name_bytes = name.getBytes(); + byte[] name_bytes = name.getBytes(LocaleUtil.CHARSET_1252); for (index = 0; index < limit; index++) { _testblock[index * 2] = name_bytes[index]; @@ -86,6 +90,7 @@ public final class TestRootProperty extends TestCase { } } + @Test public void testSetSize() { for (int j = 0; j < 10; j++) { createBasicRootProperty(); @@ -94,6 +99,7 @@ public final class TestRootProperty extends TestCase { } } + @Test public void testReadingConstructor() { String[] input = { "52 00 6F 00 6F 00 74 00 20 00 45 00 6E 00 74 00 72 00 79 00 00 00 00 00 00 00 00 00 00 00 00 00", diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java b/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java index 1db2507ea..6fd632c0c 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java @@ -17,35 +17,34 @@ package org.apache.poi.ss.usermodel; +import static org.junit.Assert.assertEquals; + import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; -import junit.framework.TestCase; - import org.apache.poi.hssf.HSSFTestDataSamples; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.DataFormatter; -import org.apache.poi.ss.usermodel.FormulaEvaluator; -import org.apache.poi.ss.usermodel.FractionFormat; -import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.util.LocaleUtil; +import org.junit.Test; /** * Tests for the Fraction Formatting part of DataFormatter. * Largely taken from bug #54686 */ -public final class TestFractionFormat extends TestCase { - public void testSingle() throws Exception { +public final class TestFractionFormat { + @Test + public void testSingle() throws Exception { FractionFormat f = new FractionFormat("", "##"); double val = 321.321; String ret = f.format(val); assertEquals("26027/81", ret); } + @Test public void testTruthFile() throws Exception { File truthFile = HSSFTestDataSamples.getSampleFile("54686_fraction_formats.txt"); - BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(truthFile))); + BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(truthFile), LocaleUtil.CHARSET_1252)); Workbook wb = HSSFTestDataSamples.openSampleWorkbook("54686_fraction_formats.xls"); Sheet sheet = wb.getSheetAt(0); DataFormatter formatter = new DataFormatter(); @@ -73,6 +72,7 @@ public final class TestFractionFormat extends TestCase { } truthLine = reader.readLine(); } + wb.close(); reader.close(); } diff --git a/src/testcases/org/apache/poi/util/TestHexDump.java b/src/testcases/org/apache/poi/util/TestHexDump.java index f6e149e75..7509363c5 100644 --- a/src/testcases/org/apache/poi/util/TestHexDump.java +++ b/src/testcases/org/apache/poi/util/TestHexDump.java @@ -28,20 +28,15 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.PrintStream; import java.lang.reflect.Constructor; import org.junit.Test; -public final class TestHexDump { +public class TestHexDump { @Test public void testDump() throws IOException { - byte[] testArray = new byte[ 256 ]; - - for (int j = 0; j < 256; j++) { - testArray[ j ] = ( byte ) j; - } + byte[] testArray = testArray(); ByteArrayOutputStream streamAct = new ByteArrayOutputStream(); HexDump.dump(testArray, 0, streamAct, 0); byte bytesAct[] = streamAct.toByteArray(); @@ -55,7 +50,7 @@ public final class TestHexDump { HexDump.dump(testArray, 0x10000000L, streamAct, 0); bytesAct = streamAct.toByteArray(); bytesExp = toHexDump(0x10000000L,0); - + assertEquals("array size mismatch", bytesExp.length, bytesAct.length); assertArrayEquals("array mismatch", bytesExp, bytesAct); @@ -108,15 +103,15 @@ public final class TestHexDump { // verify proper behaviour with empty byte array streamAct.reset(); HexDump.dump( new byte[0], 0, streamAct, 0 ); - assertEquals( "No Data" + System.getProperty( "line.separator"), streamAct.toString() ); + assertEquals( "No Data" + System.getProperty( "line.separator"), streamAct.toString(LocaleUtil.CHARSET_1252.name()) ); } - + private byte[] toHexDump(long offset, int index) { StringBuilder strExp = new StringBuilder(), chrs = new StringBuilder(); Object obj[] = new Object[33]; StringBuilder format = new StringBuilder(); - + for (int j = 0; j < 16 && (index + j*16) < 256; j++) { obj[0] = offset+index+j*16; chrs.setLength(0); @@ -133,7 +128,7 @@ public final class TestHexDump { } obj[17] = chrs.toString(); format.append("%18$s"+HexDump.EOL); - + String str = String.format(LocaleUtil.getUserLocale(), format.toString(), obj); strExp.append(str); } @@ -144,11 +139,11 @@ public final class TestHexDump { @Test public void testToHex() { assertEquals("000A", HexDump.toHex((short)0xA)); - + assertEquals("[]", HexDump.toHex(new short[] { })); assertEquals("[000A]", HexDump.toHex(new short[] { 0xA })); assertEquals("[000A, 000B]", HexDump.toHex(new short[] { 0xA, 0xB })); - + assertEquals("0A", HexDump.toHex((byte)0xA)); assertEquals("0000000A", HexDump.toHex(0xA)); @@ -163,9 +158,9 @@ public final class TestHexDump { assertEquals("0: 0A, 0B\n2: 0C, 0D\n4: 0E, 0F", HexDump.toHex(new byte[] { 0xA, 0xB, 0xC, 0xD, 0xE, 0xF }, 2)); assertEquals("FFFF", HexDump.toHex((short)0xFFFF)); - + assertEquals("00000000000004D2", HexDump.toHex(1234l)); - + assertEquals("0xFE", HexDump.byteToHex(-2)); assertEquals("0x25", HexDump.byteToHex(37)); assertEquals("0xFFFE", HexDump.shortToHex(-2)); @@ -178,20 +173,15 @@ public final class TestHexDump { @Test public void testDumpToString() throws Exception { - byte[] testArray = new byte[ 256 ]; - - for (int j = 0; j < 256; j++) - { - testArray[ j ] = ( byte ) j; - } + byte[] testArray = testArray(); String dump = HexDump.dump(testArray, 0, 0); //System.out.println("Hex: \n" + dump); - assertTrue("Had: \n" + dump, + assertTrue("Had: \n" + dump, dump.contains("0123456789:;<=>?")); dump = HexDump.dump(testArray, 2, 1); //System.out.println("Hex: \n" + dump); - assertTrue("Had: \n" + dump, + assertTrue("Had: \n" + dump, dump.contains("123456789:;<=>?@")); } @@ -208,88 +198,48 @@ public final class TestHexDump { public void testDumpToStringOutOfIndex3() throws Exception { HexDump.dump(new byte[ 1 ], 0, 1); } - - + + @Test public void testDumpToPrintStream() throws IOException { - byte[] testArray = new byte[ 256 ]; + byte[] testArray = testArray(); + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(byteOut,true,LocaleUtil.CHARSET_1252.name()); + ByteArrayInputStream byteIn = new ByteArrayInputStream(testArray); + byteIn.mark(256); + String str; - for (int j = 0; j < 256; j++) - { - testArray[ j ] = ( byte ) j; - } + byteIn.reset(); + byteOut.reset(); + HexDump.dump(byteIn, out, 0, 256); + str = new String(byteOut.toByteArray(), LocaleUtil.CHARSET_1252); + assertTrue("Had: \n" + str, str.contains("0123456789:;<=>?")); - InputStream in = new ByteArrayInputStream(testArray); - try { - ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(byteOut); - try { - HexDump.dump(in, out, 0, 256); - } finally { - out.close(); - } - - String str = new String(byteOut.toByteArray()); - assertTrue("Had: \n" + str, - str.contains("0123456789:;<=>?")); - } finally { - in.close(); - } - - in = new ByteArrayInputStream(testArray); - try { - ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(byteOut); - try { - // test with more than we have - HexDump.dump(in, out, 0, 1000); - } finally { - out.close(); - } - - String str = new String(byteOut.toByteArray()); - assertTrue("Had: \n" + str, - str.contains("0123456789:;<=>?")); - } finally { - in.close(); - } + // test with more than we have + byteIn.reset(); + byteOut.reset(); + HexDump.dump(byteIn, out, 0, 1000); + str = new String(byteOut.toByteArray(), LocaleUtil.CHARSET_1252); + assertTrue("Had: \n" + str, str.contains("0123456789:;<=>?")); - in = new ByteArrayInputStream(testArray); - try { - ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(byteOut); - try { - // test with -1 - HexDump.dump(in, out, 0, -1); - } finally { - out.close(); - } - - String str = new String(byteOut.toByteArray()); - assertTrue("Had: \n" + str, - str.contains("0123456789:;<=>?")); - } finally { - in.close(); - } - - in = new ByteArrayInputStream(testArray); - try { - ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(byteOut); - try { - HexDump.dump(in, out, 1, 235); - } finally { - out.close(); - } - - String str = new String(byteOut.toByteArray()); - assertTrue("Line contents should be moved by one now, but Had: \n" + str, + // test with -1 + byteIn.reset(); + byteOut.reset(); + HexDump.dump(byteIn, out, 0, -1); + str = new String(byteOut.toByteArray(), LocaleUtil.CHARSET_1252); + assertTrue("Had: \n" + str, str.contains("0123456789:;<=>?")); + + byteIn.reset(); + byteOut.reset(); + HexDump.dump(byteIn, out, 1, 235); + str = new String(byteOut.toByteArray(), LocaleUtil.CHARSET_1252); + assertTrue("Line contents should be moved by one now, but Had: \n" + str, str.contains("123456789:;<=>?@")); - } finally { - in.close(); - } + + byteIn.close(); + byteOut.close(); } - + @Test public void testConstruct() throws Exception { // to cover private constructor @@ -300,25 +250,35 @@ public final class TestHexDump { c.setAccessible(true); // call it - assertNotNull(c.newInstance((Object[]) null)); + assertNotNull(c.newInstance((Object[]) null)); } - + @Test public void testMain() throws Exception { File file = TempFile.createTempFile("HexDump", ".dat"); try { FileOutputStream out = new FileOutputStream(file); try { - IOUtils.copy(new ByteArrayInputStream("teststring".getBytes()), out); + IOUtils.copy(new ByteArrayInputStream("teststring".getBytes(LocaleUtil.CHARSET_1252)), out); } finally { out.close(); } assertTrue(file.exists()); assertTrue(file.length() > 0); - + HexDump.main(new String[] { file.getAbsolutePath() }); } finally { assertTrue(file.exists() && file.delete()); } } + + private static byte[] testArray() { + byte[] testArray = new byte[ 256 ]; + + for (int j = 0; j < 256; j++) { + testArray[ j ] = ( byte ) j; + } + + return testArray; + } } diff --git a/src/testcases/org/apache/poi/util/TestTempFile.java b/src/testcases/org/apache/poi/util/TestTempFile.java index c41032fe2..f22be76f7 100644 --- a/src/testcases/org/apache/poi/util/TestTempFile.java +++ b/src/testcases/org/apache/poi/util/TestTempFile.java @@ -16,36 +16,42 @@ ==================================================================== */ package org.apache.poi.util; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.io.File; -import java.io.FileWriter; +import java.io.FileOutputStream; import java.io.IOException; +import org.junit.Test; + /** * @author Glen Stampoultzis */ -public class TestTempFile extends TestCase { - TempFile tempFile; - +public class TestTempFile { + @Test public void testCreateTempFile() throws Exception { File tempFile = TempFile.createTempFile("test", ".txt"); - FileWriter w = new FileWriter(tempFile); - w.write("testing"); - w.close(); + FileOutputStream fos = new FileOutputStream(tempFile); + fos.write(1); + fos.close(); assertTrue(tempFile.exists()); assertEquals("poifiles", tempFile.getParentFile().getName()); // Can't think of a good way to check whether a file is actually deleted since it would require the VM to stop. } + @Test public void testConstructor() { // can currently be constructed... new TempFile(); } + @Test(expected=IllegalArgumentException.class) public void testSetTempFileCreationStrategy() throws IOException { TempFile.setTempFileCreationStrategy(new TempFile.DefaultTempFileCreationStrategy()); @@ -57,11 +63,6 @@ public class TestTempFile extends TestCase { assertNotNull(file1); assertTrue(file1.delete()); - try { - TempFile.setTempFileCreationStrategy(null); - fail("Expecting an exception here"); - } catch (IllegalArgumentException e) { - // expecting an exception here... - } + TempFile.setTempFileCreationStrategy(null); } }