Centralized creation of temp files and made the default operation to delete on exit

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353606 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Glen Stampoultzis 2004-10-12 05:49:01 +00:00
parent 3ce646b3c9
commit 60aefda5a8
15 changed files with 180 additions and 69 deletions

View File

@ -47,6 +47,7 @@ import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.TempFile;
/**
* <p>This class copies a POI file system to a new file and compares the copy
@ -112,7 +113,7 @@ public class CopyCompare
if (args.length == 1)
{
originalFileName = args[0];
File f = File.createTempFile("CopyOfPOIFileSystem-", ".ole2");
File f = TempFile.createTempFile("CopyOfPOIFileSystem-", ".ole2");
f.deleteOnExit();
copyFileName = f.getAbsolutePath();
}

View File

@ -0,0 +1,57 @@
/* ====================================================================
Copyright 2002-2004 Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.util;
import java.io.File;
import java.io.IOException;
import java.util.Random;
/**
* Interface for creating temporary files. Collects them all into one directory.
*
* @author Glen Stampoultzis
*/
public class TempFile
{
static File dir;
static Random rnd = new Random();
/**
* Creates a temporary file. Files are collected into one directory and by default are
* deleted on exit from the VM. Files can be kept by defining the system property
* <code>poi.keep.tmp.files</code>.
* <p>
* Dont forget to close all files or it might not be possible to delete them.
*/
public static File createTempFile(String prefix, String suffix) throws IOException
{
if (dir == null)
{
dir = new File(System.getProperty("java.io.tmpdir"), "poifiles");
dir.mkdir();
if (System.getProperty("poi.keep.tmp.files") == null)
dir.deleteOnExit();
}
File newFile = new File(dir, prefix + rnd.nextInt() + suffix);
if (System.getProperty("poi.keep.tmp.files") == null)
newFile.deleteOnExit();
return newFile;
}
}

View File

@ -62,6 +62,7 @@ import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.TempFile;
@ -710,7 +711,7 @@ public class TestWrite extends TestCase
/* Create a new POI filesystem containing the origin file's
* property set streams: */
final File copy = File.createTempFile(f.getName(), "");
final File copy = TempFile.createTempFile(f.getName(), "");
copy.deleteOnExit();
final OutputStream out = new FileOutputStream(copy);
final POIFSFileSystem poiFs = new POIFSFileSystem();
@ -764,7 +765,7 @@ public class TestWrite extends TestCase
{
try
{
final File copy = File.createTempFile("Test-HPSF", "ole2");
final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
copy.deleteOnExit();
/* Write: */
@ -816,7 +817,7 @@ public class TestWrite extends TestCase
{
try
{
final File copy = File.createTempFile("Test-HPSF", "ole2");
final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
copy.deleteOnExit();
/* Write: */

View File

@ -24,6 +24,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
import org.apache.poi.util.TempFile;
import java.io.*;
import java.util.Arrays;
@ -458,7 +459,7 @@ public class TestSSTRecord
assertEquals( "01/05 (Wed) ", sheet.getRow( 0 ).getCell( (short) 8 ).getStringCellValue() );
assertEquals( "01/05 (Wed)", sheet.getRow( 1 ).getCell( (short) 8 ).getStringCellValue() );
file = File.createTempFile( "testout", "xls" );
file = TempFile.createTempFile( "testout", "xls" );
FileOutputStream outStream = new FileOutputStream( file );
wb.write( outStream );
outStream.close();
@ -479,7 +480,7 @@ public class TestSSTRecord
assertEquals( "Testing", sheet.getRow( row++ ).getCell( (short) 0 ).getStringCellValue() );
// file = new File("/tryme.xls");
file = File.createTempFile( "testout", ".xls" );
file = TempFile.createTempFile( "testout", ".xls" );
outStream = new FileOutputStream( file );
wb.write( outStream );
outStream.close();

View File

@ -20,6 +20,7 @@ package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.util.TempFile;
import java.io.*;
@ -48,7 +49,7 @@ extends TestCase {
HSSFRow r = s.createRow(0);
HSSFCell c = r.createCell((short)0);
c.setCellValue(10);
File file = File.createTempFile("test15228",".xls");
File file = TempFile.createTempFile("test15228",".xls");
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
assertTrue("No exception thrown", true);
@ -65,7 +66,7 @@ extends TestCase {
HSSFRow r = s.createRow(0);
HSSFCell c = r.createCell((short)0);
c.setCellValue(10);
File file = File.createTempFile("test13796",".xls");
File file = TempFile.createTempFile("test13796",".xls");
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
assertTrue("No exception thrown", true);
@ -75,7 +76,7 @@ extends TestCase {
/**Test writing a hyperlink
* Open resulting sheet in Excel and check that A1 contains a hyperlink*/
public void test23094() throws Exception {
File file = File.createTempFile("test23094",".xls");
File file = TempFile.createTempFile("test23094",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -98,7 +99,7 @@ extends TestCase {
cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
// Write out the workbook
File f = File.createTempFile("test15353",".xls");
File f = TempFile.createTempFile("test15353",".xls");
FileOutputStream fileOut = new FileOutputStream(f);
wb.write(fileOut);
fileOut.close();
@ -158,7 +159,7 @@ extends TestCase {
oCell.setCellValue("0.3");
// Write the output to a file
File f = File.createTempFile("test15375",".xls");
File f = TempFile.createTempFile("test15375",".xls");
FileOutputStream fileOut = new FileOutputStream(f);
wb.write(fileOut);
fileOut.close();
@ -199,7 +200,7 @@ extends TestCase {
cell = row.createCell((short)2);
cell.setCellValue(tmp3);
}
File f = File.createTempFile("test15375-2",".xls");
File f = TempFile.createTempFile("test15375-2",".xls");
FileOutputStream fileOut = new FileOutputStream(f);
wb.write(fileOut);
fileOut.close();
@ -238,7 +239,7 @@ extends TestCase {
sheet.setDefaultColumnWidth((short) 18) ;
try {
File f = File.createTempFile("test22568",".xls");
File f = TempFile.createTempFile("test22568",".xls");
FileOutputStream out = new FileOutputStream(f) ;
wb.write(out) ;
@ -425,7 +426,7 @@ extends TestCase {
assertEquals("String Cell value", c1.getStringCellValue(), c2.getStringCellValue());
assertEquals("String Cell value", d1.getStringCellValue(), d2.getStringCellValue());
File xls = File.createTempFile("testFormulaUnicode", ".xls");
File xls = TempFile.createTempFile("testFormulaUnicode", ".xls");
FileOutputStream out = new FileOutputStream(xls);
w.write(out);
out.close();

View File

@ -28,6 +28,7 @@ import java.io.*;
import java.util.*;
import junit.framework.*;
import org.apache.poi.util.TempFile;
/**
* Class to test cell styling functionality
@ -59,7 +60,7 @@ public class TestCellStyle
public void testWriteSheetFont()
throws IOException
{
File file = File.createTempFile("testWriteSheetFont",
File file = TempFile.createTempFile("testWriteSheetFont",
".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -104,7 +105,7 @@ public class TestCellStyle
public void testDataStyle()
throws Exception
{
File file = File.createTempFile("testWriteSheetStyleDate",
File file = TempFile.createTempFile("testWriteSheetStyleDate",
".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -150,7 +151,7 @@ public class TestCellStyle
public void testWriteSheetStyle()
throws IOException
{
File file = File.createTempFile("testWriteSheetStyle",
File file = TempFile.createTempFile("testWriteSheetStyle",
".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();

View File

@ -27,6 +27,7 @@ import java.util.Date;
import junit.framework.TestCase;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.util.TempFile;
/**
* @author Andrew C. Oliver (acoliver at apache dot org)
@ -47,7 +48,7 @@ extends TestCase {
throws Exception {
short rownum = 0;
File file = File.createTempFile("testFormula",".xls");
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -169,7 +170,7 @@ extends TestCase {
private void floatTest(String operator)
throws Exception {
short rownum = 0;
File file = File.createTempFile("testFormulaFloat",".xls");
File file = TempFile.createTempFile("testFormulaFloat",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -260,7 +261,7 @@ extends TestCase {
private void operationRefTest(String operator)
throws Exception {
File file = File.createTempFile("testFormula",".xls");
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -412,7 +413,7 @@ extends TestCase {
*/
private void orderTest(String formula)
throws Exception {
File file = File.createTempFile("testFormula",".xls");
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -450,7 +451,7 @@ extends TestCase {
private void binomialOperator(String operator)
throws Exception {
short rownum = 0;
File file = File.createTempFile("testFormula",".xls");
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -548,7 +549,7 @@ extends TestCase {
throws Exception {
short rownum = 0;
File file = File.createTempFile("testFormulaAreaFunction"+function,".xls");
File file = TempFile.createTempFile("testFormulaAreaFunction"+function,".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -586,7 +587,7 @@ extends TestCase {
throws Exception {
short rownum = 0;
File file = File.createTempFile("testFormulaArrayFunction"+function,".xls");
File file = TempFile.createTempFile("testFormulaArrayFunction"+function,".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -625,7 +626,7 @@ extends TestCase {
throws Exception {
short rownum = 0;
File file = File.createTempFile("testFormulaAreaArrayFunction"+function,".xls");
File file = TempFile.createTempFile("testFormulaAreaArrayFunction"+function,".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -664,7 +665,7 @@ extends TestCase {
public void testAbsRefs() throws Exception {
File file = File.createTempFile("testFormulaAbsRef",".xls");
File file = TempFile.createTempFile("testFormulaAbsRef",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -711,7 +712,7 @@ extends TestCase {
{
String filename = System.getProperty("HSSF.testdata.path");
File file = File.createTempFile("testSheetFormula",".xls");
File file = TempFile.createTempFile("testSheetFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("A");
@ -743,7 +744,7 @@ extends TestCase {
}
public void testRVAoperands() throws Exception {
File file = File.createTempFile("testFormulaRVA",".xls");
File file = TempFile.createTempFile("testFormulaRVA",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
@ -785,7 +786,7 @@ extends TestCase {
{
String readFilename = System.getProperty("HSSF.testdata.path");
File file = File.createTempFile("testStringFormula",".xls");
File file = TempFile.createTempFile("testStringFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("A");
@ -818,7 +819,7 @@ extends TestCase {
throws IOException
{
File file = File.createTempFile("testLogicalFormula",".xls");
File file = TempFile.createTempFile("testLogicalFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("A");
@ -847,7 +848,7 @@ extends TestCase {
{
String readFilename = System.getProperty("HSSF.testdata.path");
File file = File.createTempFile("testDateFormula",".xls");
File file = TempFile.createTempFile("testDateFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("Sheet1");
@ -885,7 +886,7 @@ extends TestCase {
{
String readFilename = System.getProperty("HSSF.testdata.path");
File file = File.createTempFile("testIfFormula",".xls");
File file = TempFile.createTempFile("testIfFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("Sheet1");
@ -922,7 +923,7 @@ extends TestCase {
//assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
in.close();
File simpleIf = File.createTempFile("testSimpleIfFormulaWrite",".xls");
File simpleIf = TempFile.createTempFile("testSimpleIfFormulaWrite",".xls");
out = new FileOutputStream(simpleIf);
wb = new HSSFWorkbook();
s = wb.createSheet("Sheet1");
@ -937,7 +938,7 @@ extends TestCase {
assertTrue("length of simpleIf file is zero", (simpleIf.length()>0));
File nestedIf = File.createTempFile("testNestedIfFormula",".xls");
File nestedIf = TempFile.createTempFile("testNestedIfFormula",".xls");
out = new FileOutputStream(nestedIf);
wb = new HSSFWorkbook();
s = wb.createSheet("Sheet1");
@ -987,7 +988,7 @@ extends TestCase {
assertEquals(function, c.getCellFormula());
File file = File.createTempFile("testSumIfFormula",".xls");
File file = TempFile.createTempFile("testSumIfFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
wb = new HSSFWorkbook();
s = wb.createSheet();

View File

@ -27,6 +27,7 @@ import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.EOFRecord;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.util.TempFile;
import java.io.File;
import java.io.FileInputStream;
@ -57,7 +58,7 @@ extends TestCase {
throws java.io.IOException {
String readFilename = System.getProperty("HSSF.testdata.path");
File file = File.createTempFile("testBoolErr",".xls");
File file = TempFile.createTempFile("testBoolErr",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("Sheet1");
@ -171,7 +172,7 @@ extends TestCase {
3, s.getActiveCellRow());
//write book to temp file; read and verify that position is serialized
File temp = File.createTempFile("testActiveCell", ".xls");
File temp = TempFile.createTempFile("testActiveCell", ".xls");
FileOutputStream fos = new FileOutputStream(temp);
book.write(fos);
fos.close();
@ -195,7 +196,7 @@ extends TestCase {
throws java.io.IOException {
String readFilename = System.getProperty("HSSF.testdata.path");
File file = File.createTempFile("testFormulaStyle",".xls");
File file = TempFile.createTempFile("testFormulaStyle",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("Sheet1");

View File

@ -26,6 +26,7 @@ import java.util.Map;
import junit.framework.TestCase;
import org.apache.poi.hssf.record.PaletteRecord;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.util.TempFile;
/**
* @author Brian Sanders (bsanders at risklabs dot com)
@ -65,7 +66,7 @@ public class TestHSSFPalette extends TestCase
palette.setColorAtIndex((short) 0x3b, (byte) 0, (byte) 255, (byte) 52);
//writing to disk; reading in and verifying palette
File temp = File.createTempFile("testCustomPalette", ".xls");
File temp = TempFile.createTempFile("testCustomPalette", ".xls");
FileOutputStream fos = new FileOutputStream(temp);
book.write(fos);
fos.close();

View File

@ -24,6 +24,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.util.TempFile;
/**
* Test HSSFRow is okay.
*
@ -83,7 +85,7 @@ public class TestHSSFRow
assertEquals(0, data[6]);
assertEquals(0, data[8]);
File file = File.createTempFile("XXX", "XLS");
File file = TempFile.createTempFile("XXX", "XLS");
FileOutputStream stream = new FileOutputStream(file);
workbook.write(stream);
stream.close();

View File

@ -32,6 +32,7 @@ import org.apache.poi.hssf.record.VCenterRecord;
import org.apache.poi.hssf.record.WSBoolRecord;
import org.apache.poi.hssf.record.WindowTwoRecord;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.util.TempFile;
/**
* Tests HSSFSheet. This test case is very incomplete at the moment.
@ -165,7 +166,7 @@ public class TestHSSFSheet
cell.setCellValue(true);
cell = row.createCell((short) 11);
cell.setCellValue(true);
File tempFile = File.createTempFile("bool", "test.xls");
File tempFile = TempFile.createTempFile("bool", "test.xls");
FileOutputStream stream = new FileOutputStream(tempFile);
workbook.write(stream);
stream.close();
@ -312,7 +313,7 @@ public class TestHSSFSheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
File tempFile = File.createTempFile("display", "test.xls");
File tempFile = TempFile.createTempFile("display", "test.xls");
FileOutputStream stream = new FileOutputStream(tempFile);
wb.write(stream);
stream.close();
@ -330,7 +331,7 @@ public class TestHSSFSheet
sheet.setDisplayRowColHeadings(false);
sheet.setDisplayFormulas(true);
tempFile = File.createTempFile("display", "test.xls");
tempFile = TempFile.createTempFile("display", "test.xls");
stream = new FileOutputStream(tempFile);
wb.write(stream);
stream.close();
@ -377,7 +378,7 @@ public class TestHSSFSheet
assertEquals("row breaks number", 2, sheet.getRowBreaks().length);
assertEquals("column breaks number", 2, sheet.getColumnBreaks().length);
File tempFile = File.createTempFile("display", "testPagebreaks.xls");
File tempFile = TempFile.createTempFile("display", "testPagebreaks.xls");
FileOutputStream stream = new FileOutputStream(tempFile);
wb.write(stream);
stream.close();

View File

@ -26,6 +26,7 @@ import java.io.IOException;
import junit.framework.TestCase;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.TempFile;
/**
@ -105,7 +106,7 @@ public class TestNamedRange
SanityChecker c = new SanityChecker();
c.checkHSSFWorkbook(wb);
File file = File.createTempFile("testNamedRange",
File file = TempFile.createTempFile("testNamedRange",
".xls");
FileOutputStream fileOut = new FileOutputStream(file);
@ -229,7 +230,7 @@ public class TestNamedRange
HSSFName namedRange1 = wb.getNameAt(0);
String referece = namedRange1.getReference();
File file = File.createTempFile("testMultiNamedRange", ".xls");
File file = TempFile.createTempFile("testMultiNamedRange", ".xls");
FileOutputStream fileOut = new FileOutputStream(file);
wb.write(fileOut);
@ -293,7 +294,7 @@ public class TestNamedRange
namedRange2.setReference("sheet2" + "!$A$1:$O$21");
// Write the workbook to a file
File file = File.createTempFile("testMuiltipletNamedRanges", ".xls");
File file = TempFile.createTempFile("testMuiltipletNamedRanges", ".xls");
FileOutputStream fileOut = new FileOutputStream(file);
wb.write(fileOut);
fileOut.close();
@ -401,7 +402,7 @@ public class TestNamedRange
String reference = sheetName+"!$A$1:$B$1";
workbook.setPrintArea(0, reference);
File file = File.createTempFile("testPrintArea",".xls");
File file = TempFile.createTempFile("testPrintArea",".xls");
FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);
@ -444,7 +445,7 @@ public class TestNamedRange
String reference3 = sheetName+"!$D$2:$F$5";
workbook.setPrintArea(2, reference3);
File file = File.createTempFile("testMultiPrintArea",".xls");
File file = TempFile.createTempFile("testMultiPrintArea",".xls");
FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);

View File

@ -21,6 +21,7 @@ package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.TempFile;
import java.io.File;
import java.io.FileInputStream;
@ -64,7 +65,7 @@ public class TestSheetShiftRows extends TestCase {
// Shift the second row down 1 and write to temp file
s.shiftRows( 1, 1, 1 );
File tempFile = File.createTempFile( "shift", "test.xls" );
File tempFile = TempFile.createTempFile( "shift", "test.xls" );
FileOutputStream fout = new FileOutputStream( tempFile );
wb.write( fout );
fout.close();
@ -85,7 +86,7 @@ public class TestSheetShiftRows extends TestCase {
// Shift rows 1-3 down 3 in the current one. This tests when
// 1 row is blank. Write to a another temp file
s.shiftRows( 0, 2, 3 );
tempFile = File.createTempFile( "shift", "test.xls" );
tempFile = TempFile.createTempFile( "shift", "test.xls" );
fout = new FileOutputStream( tempFile );
wb.write( fout );
fout.close();
@ -110,7 +111,7 @@ public class TestSheetShiftRows extends TestCase {
// Shift rows 3 and 4 up and write to temp file
s.shiftRows( 2, 3, -2 );
tempFile = File.createTempFile( "shift", "test.xls" );
tempFile = TempFile.createTempFile( "shift", "test.xls" );
fout = new FileOutputStream( tempFile );
wb.write( fout );
fout.close();

View File

@ -18,14 +18,7 @@
package org.apache.poi.hssf.usermodel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import junit.framework.TestCase;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.BackupRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
@ -33,6 +26,13 @@ import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.TempFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
/**
* Class to test Workbook functionality
@ -81,7 +81,7 @@ public class TestWorkbook
public void testWriteSheetSimple()
throws IOException
{
File file = File.createTempFile("testWriteSheetSimple",
File file = TempFile.createTempFile("testWriteSheetSimple",
".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -127,7 +127,7 @@ public class TestWorkbook
public void testWriteModifySheetSimple()
throws IOException
{
File file = File.createTempFile("testWriteSheetSimple",
File file = TempFile.createTempFile("testWriteSheetSimple",
".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -235,7 +235,7 @@ public class TestWorkbook
public void testWriteDataFormat()
throws IOException
{
File file = File.createTempFile("testWriteDataFormat",
File file = TempFile.createTempFile("testWriteDataFormat",
".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -335,7 +335,7 @@ public class TestWorkbook
sheet.getRow(( short ) 0).getCell(( short ) 0);
cell.setCellValue(REPLACED);
File destination = File.createTempFile("SimpleResult",
File destination = TempFile.createTempFile("SimpleResult",
".xls");
FileOutputStream outstream = new FileOutputStream(destination);
@ -381,7 +381,7 @@ public class TestWorkbook
cell = sheet.getRow(( short ) 1).getCell(( short ) 0);
cell.setCellValue(REPLACED);
File destination =
File.createTempFile("SimpleWithSkipResult", ".xls");
TempFile.createTempFile("SimpleWithSkipResult", ".xls");
FileOutputStream outstream = new FileOutputStream(destination);
workbook.write(outstream);
@ -433,7 +433,7 @@ public class TestWorkbook
cell.setCellValue(REPLACED);
}
File destination =
File.createTempFile("SimpleWithStylingResult", ".xls");
TempFile.createTempFile("SimpleWithStylingResult", ".xls");
FileOutputStream outstream = new FileOutputStream(destination);
workbook.write(outstream);
@ -483,7 +483,7 @@ public class TestWorkbook
cell.setCellValue(FIRST_NAME_VALUE);
cell = sheet.getRow(( short ) 5).getCell(( short ) 2);
cell.setCellValue(SSN_VALUE);
File destination = File.createTempFile("EmployeeResult",
File destination = TempFile.createTempFile("EmployeeResult",
".xls");
FileOutputStream outstream = new FileOutputStream(destination);
@ -550,7 +550,7 @@ public class TestWorkbook
public void testWriteModifySheetMerged()
throws IOException
{
File file = File.createTempFile("testWriteSheetMerged",
File file = TempFile.createTempFile("testWriteSheetMerged",
".xls");
FileOutputStream out = new FileOutputStream(file);
FileInputStream in = null;
@ -656,7 +656,7 @@ public class TestWorkbook
throws Exception
{
String testName = "TestManyRows";
File file = File.createTempFile(testName, ".xls");
File file = TempFile.createTempFile(testName, ".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
@ -704,7 +704,7 @@ public class TestWorkbook
workbook.setRepeatingRowsAndColumns(0, 0, 1, 0, 0);
File file = File.createTempFile("testPrintTitles",".xls");
File file = TempFile.createTempFile("testPrintTitles",".xls");
FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);

View File

@ -0,0 +1,41 @@
/* ====================================================================
Copyright 2002-2004 Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.util;
import junit.framework.TestCase;
import java.io.File;
import java.io.FileWriter;
/**
* @author Glen Stampoultzis
*/
public class TestTempFile extends TestCase {
TempFile tempFile;
public void testCreateTempFile()
throws Exception
{
File tempFile = TempFile.createTempFile("test", ".txt");
FileWriter w = new FileWriter(tempFile);
w.write("testing");
w.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.
}
}