Index: src/java/org/apache/poi/hssf/dev/ReSave.java =================================================================== --- src/java/org/apache/poi/hssf/dev/ReSave.java (revision 1696356) +++ src/java/org/apache/poi/hssf/dev/ReSave.java (working copy) @@ -33,11 +33,11 @@ public class ReSave { public static void main(String[] args) throws Exception { boolean initDrawing = false; - for(String arg : args) { - if(arg.equals("-dg")) initDrawing = true; + for(String filename : args) { + if(filename.equals("-dg")) initDrawing = true; else { - System.out.print("reading " + arg + "..."); - FileInputStream is = new FileInputStream(arg); + System.out.print("reading " + filename + "..."); + FileInputStream is = new FileInputStream(filename); HSSFWorkbook wb = new HSSFWorkbook(is); try { System.out.println("done"); @@ -49,7 +49,7 @@ } } - String outputFile = arg.replace(".xls", "-saved.xls"); + String outputFile = filename.replace(".xls", "-saved.xls"); System.out.print("saving to " + outputFile + "..."); FileOutputStream out = new FileOutputStream(outputFile); try { Index: src/java/org/apache/poi/hssf/dev/BiffViewer.java =================================================================== --- src/java/org/apache/poi/hssf/dev/BiffViewer.java (revision 1696356) +++ src/java/org/apache/poi/hssf/dev/BiffViewer.java (working copy) @@ -114,8 +114,8 @@ try { hasNext = recStream.hasNextRecord(); } catch (LeftoverDataException e) { - e.printStackTrace(); - System.err.println("Discarding " + recStream.remaining() + " bytes and continuing"); + e.printStackTrace(System.out); + System.out.println("Discarding " + recStream.remaining() + " bytes and continuing"); recStream.readRemainder(); hasNext = recStream.hasNextRecord(); } Index: src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java =================================================================== --- src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java (revision 1696356) +++ src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java (working copy) @@ -20,6 +20,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.PrintStream; import java.util.Date; import junit.framework.TestCase; @@ -34,6 +35,8 @@ import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Cell; +import org.junit.AfterClass; +import org.junit.BeforeClass; /** * @author Andrew C. Oliver (acoliver at apache dot org) Index: src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java =================================================================== --- src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java (revision 1696356) +++ src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java (working copy) @@ -21,6 +21,9 @@ import java.io.PrintStream; import java.util.List; +import org.junit.After; +import org.junit.Before; + public class TestEFBiffViewer extends BaseXLSIteratingTest { static { // Look at the output of the test for the detailed stacktrace of the failures... @@ -41,16 +44,23 @@ SILENT_EXCLUDED.add("35897-type4.xls"); // unsupported encryption } + private PrintStream sysout, ps; + + @Before + public void setUp() throws Exception { + // redirect standard out during the test to avoid spamming the console with output + sysout = System.out; + ps = new PrintStream(NULL_OUTPUT_STREAM); + System.setOut(ps); + } + + @After + public void tearDown() throws Exception { + System.setOut(sysout); + } + @Override void runOneFile(String dir, String file, List failed) 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)); - - EFBiffViewer.main(new String[] { new File(dir, file).getAbsolutePath() }); - } finally { - System.setOut(save); - } + EFBiffViewer.main(new String[] { new File(dir, file).getAbsolutePath() }); } } Index: src/testcases/org/apache/poi/hssf/dev/TestReSave.java =================================================================== --- src/testcases/org/apache/poi/hssf/dev/TestReSave.java (revision 1696356) +++ src/testcases/org/apache/poi/hssf/dev/TestReSave.java (working copy) @@ -24,6 +24,8 @@ import java.util.List; import org.apache.poi.POIDataSamples; +import org.junit.After; +import org.junit.Before; import org.junit.Test; public class TestReSave extends BaseXLSIteratingTest { @@ -37,50 +39,57 @@ SILENT_EXCLUDED.add("43493.xls"); // HSSFWorkbook cannot open it as well SILENT_EXCLUDED.add("46904.xls"); SILENT_EXCLUDED.add("51832.xls"); // password - SILENT_EXCLUDED.add("44958_1.xls"); // known bad file + SILENT_EXCLUDED.add("44958_1.xls"); // known bad file } + //private PrintStream sysout, ps; + + @Before @Override + public void setUp() throws Exception { + sysout = System.out; + ps = new PrintStream(BaseXLSIteratingTest.NULL_OUTPUT_STREAM); + System.setOut(ps); + } + + @After + @Override + public void tearDown() throws Exception { + System.setOut(sysout); + } + + @Override void runOneFile(String dir, String file, List failed) throws Exception { // avoid running on files leftover from previous failed runs if(file.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)); + ReSave.main(new String[] { new File(dir, file).getAbsolutePath() }); + + // also try BiffViewer on the saved file + new TestBiffViewer().runOneFile(dir, file.replace(".xls", "-saved.xls"), failed); try { - ReSave.main(new String[] { new File(dir, file).getAbsolutePath() }); - - // also try BiffViewer on the saved file - new TestBiffViewer().runOneFile(dir, file.replace(".xls", "-saved.xls"), failed); - - try { - // had one case where the re-saved could not be re-saved! - ReSave.main(new String[] { new File(dir, file.replace(".xls", "-saved.xls")).getAbsolutePath() }); - } finally { - // clean up the re-re-saved file - new File(dir, file.replace(".xls", "-saved.xls").replace(".xls", "-saved.xls")).delete(); - } + // had one case where the re-saved could not be re-saved! + ReSave.main(new String[] { new File(dir, file.replace(".xls", "-saved.xls")).getAbsolutePath() }); } finally { - // clean up the re-saved file - new File(dir, file.replace(".xls", "-saved.xls")).delete(); + // clean up the re-re-saved file + new File(dir, file.replace(".xls", "-saved.xls").replace(".xls", "-saved.xls")).delete(); } - } finally { - System.setOut(save); + // clean up the re-saved file + new File(dir, file.replace(".xls", "-saved.xls")).delete(); } } @Test public void testOneFile() throws Exception { - String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY); - if(dataDirName == null) { - dataDirName = "test-data"; - } + String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY); + if(dataDirName == null) { + dataDirName = "test-data"; + } List failed = new ArrayList(); runOneFile(dataDirName + "/spreadsheet", "49219.xls", failed); Index: src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java =================================================================== --- src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java (revision 1696356) +++ src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java (working copy) @@ -24,12 +24,15 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.OutputStream; +import java.io.PrintStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.POIDataSamples; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.junit.Test; +import org.junit.After; +import org.junit.Before; /** * Base class for integration-style tests which iterate over all test-files @@ -40,6 +43,20 @@ protected static final List EXCLUDED = new ArrayList(); protected static final List SILENT_EXCLUDED = new ArrayList(); + + protected PrintStream sysout, ps; + + @Before + public void setUp() throws Exception { + sysout = System.out; + ps = new PrintStream(BaseXLSIteratingTest.NULL_OUTPUT_STREAM); + System.setOut(ps); + } + + @After + public void tearDown() throws Exception { + System.setOut(sysout); + } @Test public void testMain() throws Exception { Index: src/testcases/org/apache/poi/POITestCase.java =================================================================== --- src/testcases/org/apache/poi/POITestCase.java (revision 1696356) +++ src/testcases/org/apache/poi/POITestCase.java (working copy) @@ -18,6 +18,8 @@ package org.apache.poi; +import java.io.IOException; +import java.io.OutputStream; import java.util.Collection; import junit.framework.TestCase; @@ -27,6 +29,25 @@ * features */ public class POITestCase extends TestCase { + protected static final OutputStream NULL_OUTPUT_STREAM = new NullOutputStream(); + /** + * Implementation of an OutputStream which does nothing, used + * to redirect stdout to avoid spamming the console with output + */ + public static class NullOutputStream extends OutputStream { + @Override + public void write(byte[] b, int off, int len) { + } + + @Override + public void write(int b) { + } + + @Override + public void write(byte[] b) throws IOException { + } + } + public static void assertContains(String haystack, String needle) { assertTrue( "Unable to find expected text '" + needle + "' in text:\n" + haystack, Index: src/testcases/org/apache/poi/ss/format/TestCellFormat.java =================================================================== --- src/testcases/org/apache/poi/ss/format/TestCellFormat.java (revision 1696356) +++ src/testcases/org/apache/poi/ss/format/TestCellFormat.java (working copy) @@ -835,8 +835,8 @@ Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue(123456.6); - System.out.println(cf1.apply(cell).text); - assertEquals("123456 3/5", cf1.apply(cell).text); + final CellFormatResult displayedValue = cf1.apply(cell); + assertEquals("123456 3/5", displayedValue.text); } finally { wb.close(); } Index: src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java =================================================================== --- src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java (revision 1696356) +++ src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java (working copy) @@ -42,78 +42,85 @@ private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception { Workbook workbook = new HSSFWorkbook(); - String sheetName; - if( dates ) { - if( times ) { - sheetName = "DateTimes"; + try { + String sheetName; + if( dates ) { + if( times ) { + sheetName = "DateTimes"; + } else { + sheetName = "Dates"; + } } else { - sheetName = "Dates"; + sheetName = "Times"; } - } else { - sheetName = "Times"; - } - Sheet sheet = workbook.createSheet(sheetName); - Row header = sheet.createRow(0); - header.createCell(0).setCellValue("locale"); - header.createCell(1).setCellValue("DisplayName"); - header.createCell(2).setCellValue("Excel " + styleName); - header.createCell(3).setCellValue("java.text.DateFormat"); - header.createCell(4).setCellValue("Equals"); - header.createCell(5).setCellValue("Java pattern"); - header.createCell(6).setCellValue("Excel pattern"); - - int rowNum = 1; - for( Locale locale : DateFormat.getAvailableLocales() ) { - try { - Row row = sheet.createRow(rowNum++); + Sheet sheet = workbook.createSheet(sheetName); + Row header = sheet.createRow(0); + header.createCell(0).setCellValue("locale"); + header.createCell(1).setCellValue("DisplayName"); + header.createCell(2).setCellValue("Excel " + styleName); + header.createCell(3).setCellValue("java.text.DateFormat"); + header.createCell(4).setCellValue("Equals"); + header.createCell(5).setCellValue("Java pattern"); + header.createCell(6).setCellValue("Excel pattern"); - row.createCell(0).setCellValue(locale.toString()); - row.createCell(1).setCellValue(locale.getDisplayName()); - - DateFormat dateFormat; - if( dates ) { - if( times ) { - dateFormat = DateFormat.getDateTimeInstance(style, style, locale); + int rowNum = 1; + for( Locale locale : DateFormat.getAvailableLocales() ) { + try { + Row row = sheet.createRow(rowNum++); + + row.createCell(0).setCellValue(locale.toString()); + row.createCell(1).setCellValue(locale.getDisplayName()); + + DateFormat dateFormat; + if( dates ) { + if( times ) { + dateFormat = DateFormat.getDateTimeInstance(style, style, locale); + } else { + dateFormat = DateFormat.getDateInstance(style, locale); + } } else { - dateFormat = DateFormat.getDateInstance(style, locale); + dateFormat = DateFormat.getTimeInstance(style, locale); } - } else { - dateFormat = DateFormat.getTimeInstance(style, locale); + + Cell cell = row.createCell(2); + + cell.setCellValue(date); + CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle(); + + String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern(); + String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern); + + DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat(); + cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern)); + row.createCell(3).setCellValue(dateFormat.format(date)); + + cell.setCellStyle(cellStyle); + + // the formula returns TRUE is the formatted date in column C equals to the string in column D + row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum); + row.createCell(5).setCellValue(javaDateFormatPattern); + row.createCell(6).setCellValue(excelFormatPattern); + } catch (Exception e) { + throw new RuntimeException("Failed for locale: " + locale + ", having locales: " + + Arrays.toString(DateFormat.getAvailableLocales()), e); } + } - Cell cell = row.createCell(2); - - cell.setCellValue(date); - CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle(); - - String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern(); - String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern); - - DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat(); - cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern)); - row.createCell(3).setCellValue(dateFormat.format(date)); - - cell.setCellStyle(cellStyle); - - // the formula returns TRUE is the formatted date in column C equals to the string in column D - row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum); - row.createCell(5).setCellValue(javaDateFormatPattern); - row.createCell(6).setCellValue(excelFormatPattern); - } catch (Exception e) { - throw new RuntimeException("Failed for locale: " + locale + ", having locales: " + - Arrays.toString(DateFormat.getAvailableLocales()), e); + File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx"); + FileOutputStream outputStream = new FileOutputStream(outputFile); + try { + workbook.write(outputStream); + } finally { + outputStream.close(); } + + // FIXME: can testing the output file be automated by POI? + // if not, should this message be sent to POILogger so as not to clutter ant test output? + System.out.println("Open " + outputFile.getAbsolutePath()+" in Excel"); } - - File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx"); - FileOutputStream outputStream = new FileOutputStream(outputFile); - try { - workbook.write(outputStream); - } finally { - outputStream.close(); + finally { + workbook.close(); } - - System.out.println("Open " + outputFile.getAbsolutePath()+" in Excel"); } public void testJavaDateFormatsInExcel() throws Exception { Index: src/testcases/org/apache/poi/ddf/TestEscherDump.java =================================================================== --- src/testcases/org/apache/poi/ddf/TestEscherDump.java (revision 1696356) +++ src/testcases/org/apache/poi/ddf/TestEscherDump.java (working copy) @@ -20,39 +20,57 @@ import static org.junit.Assert.*; import java.io.ByteArrayInputStream; +import java.io.PrintStream; import org.apache.poi.POIDataSamples; +import org.apache.poi.POITestCase; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.util.IOUtils; +import org.junit.After; +import org.junit.Before; import org.junit.Test; public class TestEscherDump { + private PrintStream sysout, ps; + + @Before + public void setUp() throws Exception { + sysout = System.out; + ps = new PrintStream(new POITestCase.NullOutputStream()); + System.setOut(ps); + } + + @After + public void tearDown() throws Exception { + System.setOut(sysout); + } + @Test public void testSimple() throws Exception { // simple test to at least cover some parts of the class EscherDump.main(new String[] {}); - new EscherDump().dump(0, new byte[] {}, System.out); - new EscherDump().dump(new byte[] {}, 0, 0, System.out); - new EscherDump().dumpOld(0, new ByteArrayInputStream(new byte[] {}), System.out); + new EscherDump().dump(0, new byte[] {}, ps); + new EscherDump().dump(new byte[] {}, 0, 0, ps); + new EscherDump().dumpOld(0, new ByteArrayInputStream(new byte[] {}), ps); } @Test public void testWithData() throws Exception { - new EscherDump().dumpOld(8, new ByteArrayInputStream(new byte[] { 00, 00, 00, 00, 00, 00, 00, 00 }), System.out); + new EscherDump().dumpOld(8, new ByteArrayInputStream(new byte[] { 00, 00, 00, 00, 00, 00, 00, 00 }), ps); } @Test public void testWithSamplefile() throws Exception { //InputStream stream = HSSFTestDataSamples.openSampleFileStream(") byte[] data = POIDataSamples.getDDFInstance().readFile("Container.dat"); - new EscherDump().dump(data.length, data, System.out); - //new EscherDump().dumpOld(data.length, new ByteArrayInputStream(data), System.out); + new EscherDump().dump(data.length, data, ps); + //new EscherDump().dumpOld(data.length, new ByteArrayInputStream(data), ps); data = new byte[2586114]; int bytes = IOUtils.readFully(HSSFTestDataSamples.openSampleFileStream("44593.xls"), data); assertTrue(bytes != -1); - //new EscherDump().dump(bytes, data, System.out); - //new EscherDump().dumpOld(bytes, new ByteArrayInputStream(data), System.out); + //new EscherDump().dump(bytes, data, ps); + //new EscherDump().dumpOld(bytes, new ByteArrayInputStream(data), ps); } } Index: src/testcases/org/apache/poi/util/TestHexDump.java =================================================================== --- src/testcases/org/apache/poi/util/TestHexDump.java (revision 1696356) +++ src/testcases/org/apache/poi/util/TestHexDump.java (working copy) @@ -26,6 +26,10 @@ import java.io.PrintStream; import java.lang.reflect.Constructor; +import org.apache.poi.POITestCase; +import org.junit.After; +import org.junit.Before; + import junit.framework.TestCase; /** @@ -34,6 +38,19 @@ */ public final class TestHexDump extends TestCase { + private PrintStream sysout, ps; + + @Before + public void setUp() throws Exception { + sysout = System.out; + ps = new PrintStream(new POITestCase.NullOutputStream()); + System.setOut(ps); + } + + @After + public void tearDown() throws Exception { + System.setOut(sysout); + } private static char toHex(int n) { return Character.toUpperCase(Character.forDigit(n & 0x0F, 16));