refactored all hssf junits to get test sample data in the in one place

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@645348 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-04-07 03:02:03 +00:00
parent 1051e2853c
commit 27761348b9
49 changed files with 3051 additions and 4113 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.0.3-beta1" date="2008-04-??">
<action dev="POI-DEVELOPERS" type="fix">refactored all junits' usage of HSSF.testdata.path to one place</action>
<action dev="POI-DEVELOPERS" type="fix">44739 - Small fixes for conditional formatting (regions with max row/col index)</action>
<action dev="POI-DEVELOPERS" type="add">Implement Sheet.removeShape(Shape shape) in HSLF</action>
<action dev="RK" type="add">44694 - HPSF: Support for property sets without sections</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.0.3-beta1" date="2008-04-??">
<action dev="POI-DEVELOPERS" type="fix">refactored all junits' usage of HSSF.testdata.path to one place</action>
<action dev="POI-DEVELOPERS" type="fix">44739 - Small fixes for conditional formatting (regions with max row/col index)</action>
<action dev="RK" type="add">44694 - HPSF: Support for property sets without sections</action>
<action dev="POI-DEVELOPERS" type="add">Implement Sheet.removeShape(Shape shape) in HSLF</action>

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,7 +15,6 @@
limitations under the License.
==================================================================== */
package org.apache.poi.util;
import java.io.*;
@ -25,6 +23,7 @@ import java.util.ArrayList;
/**
* Utilities to read hex from files.
* TODO - move to test packages
*
* @author Marc Johnson
* @author Glen Stampoultzis (glens at apache.org)
@ -62,10 +61,8 @@ public class HexRead
*
* @see #readData(String)
*/
public static byte[] readData( String filename, String section ) throws IOException
{
File file = new File( filename );
FileInputStream stream = new FileInputStream( file );
public static byte[] readData(InputStream stream, String section ) throws IOException {
try
{
StringBuffer sectionText = new StringBuffer();
@ -100,6 +97,12 @@ public class HexRead
}
throw new IOException( "Section '" + section + "' not found" );
}
public static byte[] readData( String filename, String section ) throws IOException
{
File file = new File( filename );
FileInputStream stream = new FileInputStream( file );
return readData(stream, section);
}
static public byte[] readData( InputStream stream, int eofChar )
throws IOException

View File

@ -16,16 +16,16 @@
limitations under the License.
==================================================================== */
package org.apache.poi;
import junit.framework.TestCase;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.*;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* Tests that POIDocument correctly loads and saves the common
@ -36,30 +36,18 @@ import org.apache.poi.poifs.filesystem.*;
*
* @author Nick Burch (nick at torchbox dot com)
*/
public class TestPOIDocumentMain extends TestCase {
public final class TestPOIDocumentMain extends TestCase {
// The POI Documents to work on
private POIDocument doc;
private POIDocument doc2;
// POIFS primed on the test (two different hssf) data
private POIFSFileSystem pfs;
private POIFSFileSystem pfs2;
/**
* Set things up, using a PowerPoint document and
* a Word Document for our testing
* Set things up, two spreadsheets for our testing
*/
public void setUp() throws Exception {
String dirnameHSSF = System.getProperty("HSSF.testdata.path");
String filenameHSSF = dirnameHSSF + "/DateFormats.xls";
String filenameHSSF2 = dirnameHSSF + "/StringFormulas.xls";
public void setUp() {
FileInputStream fisHSSF = new FileInputStream(filenameHSSF);
pfs = new POIFSFileSystem(fisHSSF);
doc = new HSSFWorkbook(pfs);
FileInputStream fisHSSF2 = new FileInputStream(filenameHSSF2);
pfs2 = new POIFSFileSystem(fisHSSF2);
doc2 = new HSSFWorkbook(pfs2);
doc = HSSFTestDataSamples.openSampleWorkbook("DateFormats.xls");
doc2 = HSSFTestDataSamples.openSampleWorkbook("StringFormulas.xls");
}
public void testReadProperties() throws Exception {

View File

@ -0,0 +1,175 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Centralises logic for finding/opening sample files in the src/testcases/org/apache/poi/hssf/hssf/data folder.
*
* @author Josh Micich
*/
public final class HSSFTestDataSamples {
private static final String TEST_DATA_DIR_SYS_PROPERTY_NAME = "HSSF.testdata.path";
private static boolean _isInitialised;
private static File _resolvedDataDir;
/** <code>true</code> if standard system propery is not set,
* but the data is available on the test runtime classpath */
private static boolean _sampleDataIsAvaliableOnClassPath;
/**
* Opens a sample file from the standard HSSF test data directory
*
* @return an open <tt>InputStream</tt> for the specified sample file
*/
public static InputStream openSampleFileStream(String sampleFileName) {
if(!_isInitialised) {
try {
initialise();
} finally {
_isInitialised = true;
}
}
if (_sampleDataIsAvaliableOnClassPath) {
InputStream result = openClasspathResource(sampleFileName);
if(result == null) {
throw new RuntimeException("specified test sample file '" + sampleFileName
+ "' not found on the classpath");
}
// System.out.println("opening cp: " + sampleFileName);
// wrap to avoid temp warning method about auto-closing input stream
return new NonSeekableInputStream(result);
}
if (_resolvedDataDir == null) {
throw new RuntimeException("Must set system property '"
+ TEST_DATA_DIR_SYS_PROPERTY_NAME
+ "' properly before running tests");
}
File f = new File(_resolvedDataDir, sampleFileName);
if (!f.exists()) {
throw new RuntimeException("Sample file '" + sampleFileName
+ "' not found in data dir '" + _resolvedDataDir.getAbsolutePath() + "'");
}
// System.out.println("opening " + f.getAbsolutePath());
try {
return new FileInputStream(f);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
private static void initialise() {
String dataDirName = System.getProperty(TEST_DATA_DIR_SYS_PROPERTY_NAME);
if (dataDirName == null) {
// check to see if we can just get the resources from the classpath
InputStream is = openClasspathResource("SampleSS.xls");
if(is != null) {
try {
is.close(); // be nice
} catch (IOException e) {
throw new RuntimeException(e);
}
_sampleDataIsAvaliableOnClassPath = true;
return;
}
throw new RuntimeException("Must set system property '"
+ TEST_DATA_DIR_SYS_PROPERTY_NAME
+ "' before running tests");
}
File dataDir = new File(dataDirName);
if (!dataDir.exists()) {
throw new RuntimeException("Data dir '" + dataDirName
+ "' specified by system property '"
+ TEST_DATA_DIR_SYS_PROPERTY_NAME + "' does not exist");
}
// convert to canonical file, to make any subsequent error messages clearer.
try {
_resolvedDataDir = dataDir.getCanonicalFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Opens a test sample file from the 'data' sub-package of this class's package.
* @return <code>null</code> if the sample file is not deployed on the classpath.
*/
private static InputStream openClasspathResource(String sampleFileName) {
return HSSFTestDataSamples.class.getResourceAsStream("data/" + sampleFileName);
}
private static final class NonSeekableInputStream extends InputStream {
private final InputStream _is;
public NonSeekableInputStream(InputStream is) {
_is = is;
}
public int read() throws IOException {
return _is.read();
}
public int read(byte[] b, int off, int len) throws IOException {
return _is.read(b, off, len);
}
public boolean markSupported() {
return false;
}
public void close() throws IOException {
_is.close();
}
}
public static HSSFWorkbook openSampleWorkbook(String sampleFileName) {
try {
return new HSSFWorkbook(openSampleFileStream(sampleFileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Writes a spreadsheet to a <tt>ByteArrayOutputStream</tt> and reads it back
* from a <tt>ByteArrayInputStream</tt>.<p/>
* Useful for verifying that the serialisation round trip
*/
public static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
original.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
return new HSSFWorkbook(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -16,85 +16,79 @@
==================================================================== */
package org.apache.poi.hssf.eventusermodel;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.ContinueRecord;
import org.apache.poi.hssf.record.DVALRecord;
import org.apache.poi.hssf.record.DVRecord;
import org.apache.poi.hssf.record.EOFRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.ContinueRecord;
import org.apache.poi.hssf.record.SelectionRecord;
import org.apache.poi.hssf.record.WindowTwoRecord;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*
*/
public final class TestHSSFEventFactory extends TestCase {
import junit.framework.TestCase;
public class TestHSSFEventFactory extends TestCase {
private String dirname;
public TestHSSFEventFactory() {
dirname = System.getProperty("HSSF.testdata.path");
private static final InputStream openSample(String sampleFileName) {
return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
}
public void testWithMissingRecords() throws Exception {
File f = new File(dirname + "/SimpleWithSkip.xls");
HSSFRequest req = new HSSFRequest();
MockHSSFListener mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
POIFSFileSystem fs = new POIFSFileSystem(openSample("SimpleWithSkip.xls"));
HSSFEventFactory factory = new HSSFEventFactory();
factory.processWorkbookEvents(req, fs);
Record[] recs = mockListen.getRecords();
// Check we got the records
System.out.println("Processed, found " + mockListen.records.size() + " records");
assertTrue( mockListen.records.size() > 100 );
assertTrue( recs.length > 100 );
// Check that the last few records are as we expect
// (Makes sure we don't accidently skip the end ones)
int numRec = mockListen.records.size();
assertEquals(WindowTwoRecord.class, mockListen.records.get(numRec-3).getClass());
assertEquals(SelectionRecord.class, mockListen.records.get(numRec-2).getClass());
assertEquals(EOFRecord.class, mockListen.records.get(numRec-1).getClass());
int numRec = recs.length;
assertEquals(WindowTwoRecord.class, recs[numRec-3].getClass());
assertEquals(SelectionRecord.class, recs[numRec-2].getClass());
assertEquals(EOFRecord.class, recs[numRec-1].getClass());
}
public void testWithCrazyContinueRecords() throws Exception {
// Some files have crazy ordering of their continue records
// Check that we don't break on them (bug #42844)
File f = new File(dirname + "/ContinueRecordProblem.xls");
HSSFRequest req = new HSSFRequest();
MockHSSFListener mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
POIFSFileSystem fs = new POIFSFileSystem(openSample("ContinueRecordProblem.xls"));
HSSFEventFactory factory = new HSSFEventFactory();
factory.processWorkbookEvents(req, fs);
Record[] recs = mockListen.getRecords();
// Check we got the records
System.out.println("Processed, found " + mockListen.records.size() + " records");
assertTrue( mockListen.records.size() > 100 );
assertTrue( recs.length > 100 );
// And none of them are continue ones
Record[] r = (Record[])mockListen.records.toArray(
new Record[mockListen.records.size()] );
for(int i=0; i<r.length; i++) {
assertFalse( r[i] instanceof ContinueRecord );
for(int i=0; i<recs.length; i++) {
assertFalse( recs[i] instanceof ContinueRecord );
}
// Check that the last few records are as we expect
// (Makes sure we don't accidently skip the end ones)
int numRec = mockListen.records.size();
assertEquals(DVALRecord.class, mockListen.records.get(numRec-3).getClass());
assertEquals(DVRecord.class, mockListen.records.get(numRec-2).getClass());
assertEquals(EOFRecord.class, mockListen.records.get(numRec-1).getClass());
int numRec = recs.length;
assertEquals(DVALRecord.class, recs[numRec-3].getClass());
assertEquals(DVRecord.class, recs[numRec-2].getClass());
assertEquals(EOFRecord.class, recs[numRec-1].getClass());
}
/**
@ -103,13 +97,12 @@ public class TestHSSFEventFactory extends TestCase {
* (the test file was provided in a reopen of bug #42844)
*/
public void testUnknownContinueRecords() throws Exception {
File f = new File(dirname + "/42844.xls");
HSSFRequest req = new HSSFRequest();
MockHSSFListener mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
POIFSFileSystem fs = new POIFSFileSystem(openSample("42844.xls"));
HSSFEventFactory factory = new HSSFEventFactory();
factory.processWorkbookEvents(req, fs);
@ -117,8 +110,14 @@ public class TestHSSFEventFactory extends TestCase {
}
private static class MockHSSFListener implements HSSFListener {
private MockHSSFListener() {}
private ArrayList records = new ArrayList();
private final List records = new ArrayList();
public MockHSSFListener() {}
public Record[] getRecords() {
Record[] result = new Record[records.size()];
records.toArray(result);
return result;
}
public void processRecord(Record record) {
records.add(record);

View File

@ -19,11 +19,13 @@ package org.apache.poi.hssf.eventusermodel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;
import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord;
@ -39,8 +41,6 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase {
private Record[] r;
public void setUp() {
String dirname = System.getProperty("HSSF.testdata.path");
File f = new File(dirname + "/MissingBits.xls");
HSSFRequest req = new HSSFRequest();
MockHSSFListener mockListen = new MockHSSFListener();
@ -49,7 +49,8 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase {
HSSFEventFactory factory = new HSSFEventFactory();
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
InputStream is = HSSFTestDataSamples.openSampleFileStream("MissingBits.xls");
POIFSFileSystem fs = new POIFSFileSystem(is);
factory.processWorkbookEvents(req, fs);
} catch (IOException e) {
throw new RuntimeException(e);

View File

@ -14,21 +14,36 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.extractor;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.TestCase;
public class TestExcelExtractor extends TestCase {
public void testSimple() throws Exception {
String path = System.getProperty("HSSF.testdata.path");
FileInputStream fin = new FileInputStream(path + File.separator + "Simple.xls");
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*
*/
public final class TestExcelExtractor extends TestCase {
ExcelExtractor extractor = new ExcelExtractor(new POIFSFileSystem(fin));
private static final ExcelExtractor createExtractor(String sampleFileName) {
InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
try {
return new ExcelExtractor(new POIFSFileSystem(is));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void testSimple() {
ExcelExtractor extractor = createExtractor("Simple.xls");
assertEquals("Sheet1\nreplaceMe\nSheet2\nSheet3\n", extractor.getText());
@ -37,11 +52,9 @@ public class TestExcelExtractor extends TestCase {
assertEquals("replaceMe\n", extractor.getText());
}
public void testNumericFormula() throws Exception {
String path = System.getProperty("HSSF.testdata.path");
FileInputStream fin = new FileInputStream(path + File.separator + "sumifformula.xls");
public void testNumericFormula() {
ExcelExtractor extractor = new ExcelExtractor(new POIFSFileSystem(fin));
ExcelExtractor extractor = createExtractor("sumifformula.xls");
assertEquals(
"Sheet1\n" +
@ -68,11 +81,9 @@ public class TestExcelExtractor extends TestCase {
);
}
public void testwithContinueRecords() throws Exception {
String path = System.getProperty("HSSF.testdata.path");
FileInputStream fin = new FileInputStream(path + File.separator + "StringContinueRecords.xls");
public void testwithContinueRecords() {
ExcelExtractor extractor = new ExcelExtractor(new POIFSFileSystem(fin));
ExcelExtractor extractor = createExtractor("StringContinueRecords.xls");
extractor.getText();
@ -82,11 +93,9 @@ public class TestExcelExtractor extends TestCase {
assertTrue(extractor.getText().length() > 40960);
}
public void testStringConcat() throws Exception {
String path = System.getProperty("HSSF.testdata.path");
FileInputStream fin = new FileInputStream(path + File.separator + "SimpleWithFormula.xls");
public void testStringConcat() {
ExcelExtractor extractor = new ExcelExtractor(new POIFSFileSystem(fin));
ExcelExtractor extractor = createExtractor("SimpleWithFormula.xls");
// Comes out as NaN if treated as a number
// And as XYZ if treated as a string
@ -97,11 +106,9 @@ public class TestExcelExtractor extends TestCase {
assertEquals("Sheet1\nreplaceme\nreplaceme\nCONCATENATE(A1,A2)\nSheet2\nSheet3\n", extractor.getText());
}
public void testStringFormula() throws Exception {
String path = System.getProperty("HSSF.testdata.path");
FileInputStream fin = new FileInputStream(path + File.separator + "StringFormulas.xls");
public void testStringFormula() {
ExcelExtractor extractor = new ExcelExtractor(new POIFSFileSystem(fin));
ExcelExtractor extractor = createExtractor("StringFormulas.xls");
// Comes out as NaN if treated as a number
// And as XYZ if treated as a string

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,35 +15,27 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.record;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.InputStream;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
public class TestBOFRecord extends TestCase
{
private String _test_file_path;
private static final String _test_file_path_property = "HSSF.testdata.path";
public TestBOFRecord()
{
super();
_test_file_path = System.getProperty( _test_file_path_property ) +
File.separator + "bug_42794.xls";
}
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
*/
public final class TestBOFRecord extends TestCase {
public void testBOFRecord() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(
new FileInputStream(_test_file_path)
);
InputStream is = HSSFTestDataSamples.openSampleFileStream("bug_42794.xls");
// This used to throw an error before
HSSFWorkbook hssf = new HSSFWorkbook(fs);
try {
new HSSFWorkbook(is);
} catch (ArrayIndexOutOfBoundsException e) {
throw new AssertionFailedError("Identified bug 42794");
}
}
}

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,47 +15,35 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.record;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.util.List;
import junit.framework.TestCase;
public class TestChartTitleFormatRecord extends TestCase
{
private String _test_file_path;
private static final String _test_file_path_property = "HSSF.testdata.path";
public TestChartTitleFormatRecord()
{
super();
_test_file_path = System.getProperty( _test_file_path_property ) +
File.separator + "WithFormattedGraphTitle.xls";
}
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*
*/
public final class TestChartTitleFormatRecord extends TestCase {
public void testRecord() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(
new FileInputStream(_test_file_path)
);
HSSFTestDataSamples.openSampleFileStream("WithFormattedGraphTitle.xls"));
// Check we can open the file via usermodel
HSSFWorkbook hssf = new HSSFWorkbook(fs);
// Now process it through eventusermodel, and
// look out for the title records
ChartTitleFormatRecordGrabber grabber =
new ChartTitleFormatRecordGrabber();
ChartTitleFormatRecordGrabber grabber = new ChartTitleFormatRecordGrabber();
InputStream din = fs.createDocumentInputStream("Workbook");
HSSFRequest req = new HSSFRequest();
req.addListenerForAllRecords(grabber);
@ -72,8 +59,12 @@ public class TestChartTitleFormatRecord extends TestCase
assertEquals(3, r.getFormatCount());
}
public static class ChartTitleFormatRecordGrabber implements HSSFListener {
private ArrayList chartTitleFormatRecords = new ArrayList();
private static final class ChartTitleFormatRecordGrabber implements HSSFListener {
private final List chartTitleFormatRecords;
public ChartTitleFormatRecordGrabber() {
chartTitleFormatRecords = new ArrayList();
}
public void processRecord(Record record) {
if(record instanceof ChartTitleFormatRecord) {

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,8 +15,6 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.record;
@ -39,14 +36,7 @@ import junit.framework.TestCase;
*
* @author Andrew C. Oliver
*/
public class TestFormulaRecord
extends TestCase
{
public TestFormulaRecord(String name)
{
super(name);
}
public final class TestFormulaRecord extends TestCase {
public void testCreateFormulaRecord () {
FormulaRecord record = new FormulaRecord();
@ -92,7 +82,6 @@ public class TestFormulaRecord
for (int i = 5; i < 13;i++) {
assertEquals("FormulaByte NaN doesn't match", formulaByte[i], output[i+4]);
}
}
/**
@ -164,15 +153,7 @@ public class TestFormulaRecord
assertEquals("CHOOSE", choose.getName());
}
public static void main(String [] ignored_args)
{
String filename = System.getProperty("HSSF.testdata.path");
System.out
.println("Testing org.apache.poi.hssf.record.FormulaRecord");
public static void main(String [] ignored_args) {
junit.textui.TestRunner.run(TestFormulaRecord.class);
}
}

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,7 +15,6 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.record;
import java.io.ByteArrayInputStream;
@ -34,20 +32,8 @@ import org.apache.poi.util.HexRead;
* @author Andrew C. Oliver (acoliver at apache dot org)
* @author Csaba Nagy (ncsaba at yahoo dot com)
*/
public final class TestRecordFactory extends TestCase {
public class TestRecordFactory
extends TestCase
{
/**
* Creates new TestRecordFactory
* @param testCaseName
*/
public TestRecordFactory(String testCaseName)
{
super(testCaseName);
}
/**
* TEST NAME: Test Basic Record Construction <P>
@ -57,13 +43,9 @@ public class TestRecordFactory
* FAILURE: The wrong records are creates or contain the wrong values <P>
*
*/
public void testBasicRecordConstruction()
throws Exception
{
public void testBasicRecordConstruction() {
short recType = BOFRecord.sid;
byte[] data = new byte[]
{
byte[] data = {
0, 6, 5, 0, -2, 28, -51, 7, -55, 64, 0, 0, 6, 1, 0, 0
};
short size = 16;
@ -106,13 +88,9 @@ public class TestRecordFactory
* FAILURE: The wrong records are created or contain the wrong values <P>
*
*/
public void testSpecial()
throws Exception
{
public void testSpecial() {
short recType = RKRecord.sid;
byte[] data = new byte[]
{
byte[] data = {
0, 0, 0, 0, 21, 0, 0, 0, 0, 0
};
short size = 10;
@ -138,10 +116,8 @@ public class TestRecordFactory
* FAILURE: The wrong records are created or contain the wrong values <P>
*
*/
public void testContinuedUnknownRecord()
{
byte[] data = new byte[]
{
public void testContinuedUnknownRecord() {
byte[] data = {
0, -1, 0, 0, // an unknown record with 0 length
0x3C , 0, 3, 0, 1, 2, 3, // a continuation record with 3 bytes of data
0x3C , 0, 1, 0, 4 // one more continuation record with 1 byte of data
@ -178,7 +154,7 @@ public class TestRecordFactory
*/
public void testMixedContinue() throws Exception {
/**
* Taken from a real file $HSSF.testdata.path/39512.xls. See Bug 39512 for details.
* Taken from a real test sample file 39512.xls. See Bug 39512 for details.
*/
String dump =
//OBJ
@ -228,10 +204,7 @@ public class TestRecordFactory
assertTrue(Arrays.equals(data, ser));
}
public static void main(String [] ignored_args)
{
System.out
.println("Testing org.apache.poi.hssf.record.TestRecordFactory");
public static void main(String [] ignored_args) {
junit.textui.TestRunner.run(TestRecordFactory.class);
}
}

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,50 +15,46 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.record;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.IntMapper;
import org.apache.poi.hssf.record.TestcaseRecordInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.IntMapper;
/**
* Exercise the SSTDeserializer class.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class TestSSTDeserializer
extends TestCase
{
private String _test_file_path;
private static final String _test_file_path_property = "HSSF.testdata.path";
public final class TestSSTDeserializer extends TestCase {
public TestSSTDeserializer( String s )
{
super( s );
}
protected void setUp() throws Exception
{
_test_file_path = System.getProperty( _test_file_path_property );
}
private byte[] joinArray(byte[] array1, byte[] array2) {
byte[] bigArray = new byte[array1.length+array2.length];
byte[] bigArray = new byte[array1.length + array2.length];
System.arraycopy(array1, 0, bigArray, 0, array1.length);
System.arraycopy(array2, 0, bigArray, array1.length, array2.length);
return bigArray;
}
private static byte[] readSampleHexData(String sampleFileName, String sectionName) {
InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
try {
return HexRead.readData(is, sectionName);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void testSpanRichTextToPlainText()
throws Exception
{
byte[] header = HexRead.readData( _test_file_path + File.separator + "richtextdata.txt", "header" );
byte[] continueBytes = HexRead.readData( _test_file_path + File.separator + "richtextdata.txt", "continue1" );
byte[] header = readSampleHexData("richtextdata.txt", "header" );
byte[] continueBytes = readSampleHexData("richtextdata.txt", "continue1" );
continueBytes = TestcaseRecordInputStream.mergeDataAndSid(ContinueRecord.sid, (short)continueBytes.length, continueBytes);
TestcaseRecordInputStream in = new TestcaseRecordInputStream((short)0, (short)header.length, joinArray(header, continueBytes));
@ -74,8 +69,8 @@ public class TestSSTDeserializer
public void testContinuationWithNoOverlap()
throws Exception
{
byte[] header = HexRead.readData( _test_file_path + File.separator + "evencontinuation.txt", "header" );
byte[] continueBytes = HexRead.readData( _test_file_path + File.separator + "evencontinuation.txt", "continue1" );
byte[] header = readSampleHexData("evencontinuation.txt", "header" );
byte[] continueBytes = readSampleHexData("evencontinuation.txt", "continue1" );
continueBytes = TestcaseRecordInputStream.mergeDataAndSid(ContinueRecord.sid, (short)continueBytes.length, continueBytes);
TestcaseRecordInputStream in = new TestcaseRecordInputStream((short)0, (short)header.length, joinArray(header, continueBytes));
@ -93,10 +88,10 @@ public class TestSSTDeserializer
public void testStringAcross2Continuations()
throws Exception
{
byte[] header = HexRead.readData( _test_file_path + File.separator + "stringacross2continuations.txt", "header" );
byte[] continue1 = HexRead.readData( _test_file_path + File.separator + "stringacross2continuations.txt", "continue1" );
byte[] header = readSampleHexData("stringacross2continuations.txt", "header" );
byte[] continue1 = readSampleHexData("stringacross2continuations.txt", "continue1" );
continue1 = TestcaseRecordInputStream.mergeDataAndSid(ContinueRecord.sid, (short)continue1.length, continue1);
byte[] continue2 = HexRead.readData( _test_file_path + File.separator + "stringacross2continuations.txt", "continue2" );
byte[] continue2 = readSampleHexData("stringacross2continuations.txt", "continue2" );
continue2 = TestcaseRecordInputStream.mergeDataAndSid(ContinueRecord.sid, (short)continue2.length, continue2);
byte[] bytes = joinArray(header, continue1);
@ -111,11 +106,9 @@ public class TestSSTDeserializer
assertEquals( "At a dinner partyAt a dinner party", strings.get( 1 ) + "" );
}
public void testExtendedStrings()
throws Exception
{
byte[] header = HexRead.readData( _test_file_path + File.separator + "extendedtextstrings.txt", "rich-header" );
byte[] continueBytes = HexRead.readData( _test_file_path + File.separator + "extendedtextstrings.txt", "rich-continue1" );
public void testExtendedStrings() {
byte[] header = readSampleHexData("extendedtextstrings.txt", "rich-header" );
byte[] continueBytes = readSampleHexData("extendedtextstrings.txt", "rich-continue1" );
continueBytes = TestcaseRecordInputStream.mergeDataAndSid(ContinueRecord.sid, (short)continueBytes.length, continueBytes);
TestcaseRecordInputStream in = new TestcaseRecordInputStream((short)0, (short)header.length, joinArray(header, continueBytes));
@ -126,8 +119,8 @@ public class TestSSTDeserializer
assertEquals( "At a dinner party orAt At At ", strings.get( 0 ) + "" );
header = HexRead.readData( _test_file_path + File.separator + "extendedtextstrings.txt", "norich-header" );
continueBytes = HexRead.readData( _test_file_path + File.separator + "extendedtextstrings.txt", "norich-continue1" );
header = readSampleHexData("extendedtextstrings.txt", "norich-header" );
continueBytes = readSampleHexData("extendedtextstrings.txt", "norich-continue1" );
continueBytes = TestcaseRecordInputStream.mergeDataAndSid(ContinueRecord.sid, (short)continueBytes.length, continueBytes);
in = new TestcaseRecordInputStream((short)0, (short)header.length, joinArray(header, continueBytes));
@ -136,7 +129,5 @@ public class TestSSTDeserializer
deserializer.manufactureStrings( 1, in);
assertEquals( "At a dinner party orAt At At ", strings.get( 0 ) + "" );
}
}

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,53 +15,32 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.record;
import junit.framework.TestCase;
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.LittleEndianConsts;
import org.apache.poi.util.TempFile;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.LittleEndian;
/**
* @author Marc Johnson (mjohnson at apache dot org)
* @author Glen Stampoultzis (glens at apache.org)
*/
public class TestSSTRecord
extends TestCase
{
private String _test_file_path;
private static final String _test_file_path_property = "HSSF.testdata.path";
/**
* Creates new TestSSTRecord
*
* @param name
*/
public TestSSTRecord( String name )
{
super( name );
_test_file_path = System.getProperty( _test_file_path_property );
}
public final class TestSSTRecord extends TestCase {
/**
* test processContinueRecord
*
* @exception IOException
*/
public void testProcessContinueRecord()
throws IOException
{
public void testProcessContinueRecord() {
//jmh byte[] testdata = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord" );
//jmh byte[] input = new byte[testdata.length - 4];
//jmh
@ -182,9 +160,7 @@ public class TestSSTRecord
* @exception IOException
*/
public void testHugeStrings()
throws IOException
{
public void testHugeStrings() {
SSTRecord record = new SSTRecord();
byte[][] bstrings =
{
@ -265,12 +241,8 @@ public class TestSSTRecord
/**
* test SSTRecord boundary conditions
*
* @exception IOException
*/
public void testSSTRecordBug()
throws IOException
{
public void testSSTRecordBug() {
// create an SSTRecord and write a certain pattern of strings
// to it ... then serialize it and verify the content
SSTRecord record = new SSTRecord();
@ -349,38 +321,6 @@ public class TestSSTRecord
}
}
/**
* test reader constructor
*
* @exception IOException
*/
public void testReaderConstructor()
throws IOException
{
/* JMH this test case data is crap because it does not contain a full record. Ie the last string
is missing a record
byte[] testdata = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord" );
// byte[] input = new byte[testdata.length - 4];
System.arraycopy( testdata, 4, input, 0, input.length );
SSTRecord record = new SSTRecord( new TestcaseRecordInputStream(LittleEndian.getShort( testdata, 0 ),
LittleEndian.getShort( testdata, 2 ),
input) );
assertEquals( 1464, record.getNumStrings() );
assertEquals( 688, record.getNumUniqueStrings() );
assertEquals( 492, record.countStrings() );
assertEquals( 1, record.getDeserializer().getContinuationExpectedChars() );
assertEquals( "Consolidated B-24J Liberator The Dragon & His Tai",
record.getDeserializer().getUnfinishedString() );
// assertEquals( 52, record.getDeserializer().getTotalLength() );
// assertEquals( 3, record.getDeserializer().getStringDataOffset() );
assertTrue( !record.getDeserializer().isWideChar() );
*/
}
/**
* test simple constructor
*/
@ -413,9 +353,7 @@ public class TestSSTRecord
* @param ignored_args
*/
public static void main( String[] ignored_args )
{
System.out.println( "Testing hssf.record.SSTRecord functionality" );
public static void main( String[] ignored_args ) {
junit.textui.TestRunner.run( TestSSTRecord.class );
}
@ -425,25 +363,16 @@ public class TestSSTRecord
public void testReadWriteDuplicatedRichText1()
throws Exception
{
File file = new File( _test_file_path + File.separator + "duprich1.xls" );
InputStream stream = new FileInputStream( file );
HSSFWorkbook wb = new HSSFWorkbook( stream );
stream.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("duprich1.xls");
HSSFSheet sheet = wb.getSheetAt( 1 );
assertEquals( "01/05 (Wed)", sheet.getRow( 0 ).getCell( (short) 8 ).getStringCellValue() );
assertEquals( "01/05 (Wed)", sheet.getRow( 1 ).getCell( (short) 8 ).getStringCellValue() );
file = TempFile.createTempFile( "testout", "xls" );
FileOutputStream outStream = new FileOutputStream( file );
wb.write( outStream );
outStream.close();
file.delete();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write( baos );
// test the second file.
file = new File( _test_file_path + File.separator + "duprich2.xls" );
stream = new FileInputStream( file );
wb = new HSSFWorkbook( stream );
stream.close();
wb = HSSFTestDataSamples.openSampleWorkbook("duprich2.xls");
sheet = wb.getSheetAt( 0 );
int row = 0;
assertEquals( "Testing", sheet.getRow( row++ ).getCell( (short) 0 ).getStringCellValue() );
@ -453,12 +382,6 @@ public class TestSSTRecord
assertEquals( "Testing", sheet.getRow( row++ ).getCell( (short) 0 ).getStringCellValue() );
assertEquals( "Testing", sheet.getRow( row++ ).getCell( (short) 0 ).getStringCellValue() );
// file = new File("/tryme.xls");
file = TempFile.createTempFile( "testout", ".xls" );
outStream = new FileOutputStream( file );
wb.write( outStream );
outStream.close();
file.delete();
wb.write( baos );
}
}

View File

@ -15,38 +15,22 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.record;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*
*/
public final class TestUnicodeNameRecord extends TestCase {
public class TestUnicodeNameRecord extends TestCase {
private String _test_file_path;
private static final String _test_file_path_property = "HSSF.testdata.path";
public TestUnicodeNameRecord()
{
super();
_test_file_path = System.getProperty( _test_file_path_property ) +
File.separator + "unicodeNameRecord.xls";
}
public void testReadBook() throws IOException {
POIFSFileSystem fs = new POIFSFileSystem(
new FileInputStream(_test_file_path)
);
public void testReadBook() {
// This bit used to crash
HSSFWorkbook book = new HSSFWorkbook(fs);
HSSFSheet sheet = book.getSheetAt(0);
HSSFWorkbook book = HSSFTestDataSamples.openSampleWorkbook("unicodeNameRecord.xls");
book.getSheetAt(0);
}
}

View File

@ -29,6 +29,7 @@ import java.util.zip.CRC32;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.Record;
@ -250,22 +251,14 @@ public class TestValueRecordsAggregate extends TestCase
*
*/
public void testSpuriousSharedFormulaFlag() {
File dataDir = new File(System.getProperty("HSSF.testdata.path"));
File testFile = new File(dataDir, ABNORMAL_SHARED_FORMULA_FLAG_TEST_FILE);
long actualCRC = getFileCRC(testFile);
long actualCRC = getFileCRC(HSSFTestDataSamples.openSampleFileStream(ABNORMAL_SHARED_FORMULA_FLAG_TEST_FILE));
long expectedCRC = 2277445406L;
if(actualCRC != expectedCRC) {
System.err.println("Expected crc " + expectedCRC + " but got " + actualCRC);
throw failUnexpectedTestFileChange();
}
HSSFWorkbook wb;
try {
FileInputStream in = new FileInputStream(testFile);
wb = new HSSFWorkbook(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook(ABNORMAL_SHARED_FORMULA_FLAG_TEST_FILE);
HSSFSheet s = wb.getSheetAt(0); // Sheet1
@ -311,11 +304,10 @@ public class TestValueRecordsAggregate extends TestCase
/**
* gets a CRC checksum for the content of a file
*/
private static long getFileCRC(File f) {
private static long getFileCRC(InputStream is) {
CRC32 crc = new CRC32();
byte[] buf = new byte[2048];
try {
InputStream is = new FileInputStream(f);
while(true) {
int bytesRead = is.read(buf);
if(bytesRead < 1) {

View File

@ -25,6 +25,7 @@ import java.io.InputStream;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@ -36,28 +37,15 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
* @author Daniel Noll (daniel at nuix dot com dot au)
*/
public abstract class AbstractPtgTestCase extends TestCase {
/** Directory containing the test data. */
private static String dataDir = System.getProperty("HSSF.testdata.path");
/**
* Loads a workbook from the given filename in the test data dir.
*
* @param filename the filename.
* @param sampleFileName the filename.
* @return the loaded workbook.
* @throws IOException if an error occurs loading the workbook.
*/
protected static final HSSFWorkbook loadWorkbook(String filename)
throws IOException {
File file = new File(dataDir, filename);
InputStream stream = new BufferedInputStream(new FileInputStream(file));
// TODO - temp workaround to keep stdout quiet due to warning msg in POIFS
// When that warning msg is disabled, remove this wrapper and the close() call,
InputStream wrappedStream = POIFSFileSystem.createNonClosingInputStream(stream);
try {
return new HSSFWorkbook(wrappedStream);
} finally {
stream.close();
}
protected static final HSSFWorkbook loadWorkbook(String sampleFileName) {
return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
}
/**

View File

@ -17,11 +17,9 @@
package org.apache.poi.hssf.record.formula;
import java.io.FileInputStream;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
@ -37,15 +35,7 @@ public final class TestExternalFunctionFormulas extends TestCase {
* tests <tt>NameXPtg.toFormulaString(Workbook)</tt> and logic in Workbook below that
*/
public void testReadFormulaContainingExternalFunction() {
String filePath = System.getProperty("HSSF.testdata.path")+ "/"
+ "externalFunctionExample.xls";
HSSFWorkbook wb;
try {
FileInputStream fin = new FileInputStream(filePath);
wb = new HSSFWorkbook( fin );
} catch (IOException e) {
throw new RuntimeException(e);
}
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("externalFunctionExample.xls");
String expectedFormula = "YEARFRAC(B1,C1)";
HSSFSheet sht = wb.getSheetAt(0);

View File

@ -17,9 +17,6 @@
package org.apache.poi.hssf.record.formula.eval;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -27,6 +24,7 @@ import java.io.InputStream;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRow;
@ -43,49 +41,13 @@ import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.CellValue;
*/
public final class TestFormulaBugs extends TestCase {
private static final String TEST_DATA_DIR_SYS_PROPERTY_NAME = "HSSF.testdata.path";
/**
* Opens a sample file from the standard HSSF test data directory
*
* @return an open <tt>InputStream</tt> for the specified sample file
*/
private static InputStream openSampleFileStream(String sampleFileName) {
// TODO - move this method somewhere common
String dataDirName = System
.getProperty(TEST_DATA_DIR_SYS_PROPERTY_NAME);
if (dataDirName == null) {
throw new RuntimeException("Must set system property '"
+ TEST_DATA_DIR_SYS_PROPERTY_NAME
+ "' before running tests");
}
File dataDir = new File(dataDirName);
if (!dataDir.exists()) {
throw new RuntimeException("Data dir '" + dataDirName
+ "' specified by system property '"
+ TEST_DATA_DIR_SYS_PROPERTY_NAME + "' does not exist");
}
File f = new File(dataDir, sampleFileName);
if (!f.exists()) {
throw new RuntimeException("Sample file '" + sampleFileName
+ "' not found in data dir '" + dataDirName + "'");
}
InputStream is;
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
return is;
}
/**
* Bug 27349 - VLOOKUP with reference to another sheet.<p/> This test was
* added <em>long</em> after the relevant functionality was fixed.
*/
public void test27349() {
// 27349-vlookupAcrossSheets.xls is bugzilla/attachment.cgi?id=10622
InputStream is = openSampleFileStream("27349-vlookupAcrossSheets.xls");
InputStream is = HSSFTestDataSamples.openSampleFileStream("27349-vlookupAcrossSheets.xls");
HSSFWorkbook wb;
try {
// original bug may have thrown exception here, or output warning to

View File

@ -17,13 +17,13 @@
package org.apache.poi.hssf.record.formula.eval;
import java.io.FileInputStream;
import java.io.PrintStream;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.formula.functions.TestMathX;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
@ -154,9 +154,7 @@ public final class TestFormulasFromSpreadsheet extends TestCase {
protected void setUp() throws Exception {
if (workbook == null) {
String filePath = System.getProperty("HSSF.testdata.path")+ "/" + SS.FILENAME;
FileInputStream fin = new FileInputStream( filePath );
workbook = new HSSFWorkbook( fin );
workbook = HSSFTestDataSamples.openSampleWorkbook(SS.FILENAME);
sheet = workbook.getSheetAt( 0 );
}
_functionFailureCount = 0;

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.RecordFormatException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -45,14 +46,7 @@ public final class TestReadMissingBuiltInFuncs extends TestCase {
private static HSSFSheet getSheet() {
if (_sheet == null) {
String cwd = System.getProperty("HSSF.testdata.path");
HSSFWorkbook wb;
try {
InputStream is = new FileInputStream(new File(cwd, SAMPLE_SPREADSHEET_FILE_NAME));
wb = new HSSFWorkbook(is);
} catch (IOException e) {
throw new RuntimeException(e);
}
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook(SAMPLE_SPREADSHEET_FILE_NAME);
_sheet = wb.getSheetAt(0);
}
return _sheet;

View File

@ -26,6 +26,7 @@ import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
@ -185,14 +186,7 @@ public final class TestLookupFunctionsFromSpreadsheet extends TestCase {
}
public void testFunctionsFromTestSpreadsheet() {
String filePath = System.getProperty("HSSF.testdata.path")+ "/" + SS.FILENAME;
HSSFWorkbook workbook;
try {
FileInputStream fin = new FileInputStream( filePath );
workbook = new HSSFWorkbook( fin );
} catch (IOException e) {
throw new RuntimeException(e);
}
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook(SS.FILENAME);
confirmReadMeSheet(workbook);
int nSheets = workbook.getNumberOfSheets();

View File

@ -14,42 +14,35 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.CellValue;
import org.apache.poi.hssf.util.CellReference;
/**
*
*/
public final class TestBug42464 extends TestCase {
String dirname;
protected void setUp() throws Exception {
super.setUp();
dirname = System.getProperty("HSSF.testdata.path");
}
public void testOKFile() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook(
new FileInputStream(new File(dirname,"42464-ExpPtg-ok.xls"))
);
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("42464-ExpPtg-ok.xls");
process(wb);
}
public void testExpSharedBadFile() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook(
new FileInputStream(new File(dirname,"42464-ExpPtg-bad.xls"))
);
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("42464-ExpPtg-bad.xls");
process(wb);
}
protected void process(HSSFWorkbook wb) {
private static void process(HSSFWorkbook wb) {
for(int i=0; i<wb.getNumberOfSheets(); i++) {
HSSFSheet s = wb.getSheetAt(i);
HSSFFormulaEvaluator eval =
@ -64,7 +57,7 @@ public final class TestBug42464 extends TestCase {
}
}
protected void process(HSSFRow row, HSSFFormulaEvaluator eval) {
private static void process(HSSFRow row, HSSFFormulaEvaluator eval) {
Iterator it = row.cellIterator();
while(it.hasNext()) {
HSSFCell cell = (HSSFCell)it.next();

File diff suppressed because it is too large Load Diff

View File

@ -14,28 +14,28 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.formula.AreaPtg;
import org.apache.poi.hssf.record.formula.FuncVarPtg;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.formula.AreaPtg;
import org.apache.poi.hssf.record.formula.FuncVarPtg;
/**
*
*/
public final class TestFormulaEvaluatorBugs extends TestCase {
private String dirName;
private String tmpDirName;
protected void setUp() throws Exception {
super.setUp();
dirName = System.getProperty("HSSF.testdata.path");
protected void setUp() {
tmpDirName = System.getProperty("java.io.tmpdir");
}
@ -51,8 +51,8 @@ public final class TestFormulaEvaluatorBugs extends TestCase {
public void test44636() throws Exception {
// Open the existing file, tweak one value and
// re-calculate
FileInputStream in = new FileInputStream(new File(dirName,"44636.xls"));
HSSFWorkbook wb = new HSSFWorkbook(in);
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("44636.xls");
HSSFSheet sheet = wb.getSheetAt (0);
HSSFRow row = sheet.getRow (0);
@ -99,10 +99,9 @@ public final class TestFormulaEvaluatorBugs extends TestCase {
*
* @author Yegor Kozlov
*/
public void test44297() throws IOException {
FileInputStream in = new FileInputStream(new File(dirName, "44297.xls"));
HSSFWorkbook wb = new HSSFWorkbook(in);
in.close();
public void test44297() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("44297.xls");
HSSFRow row;
HSSFCell cell;
@ -111,55 +110,55 @@ public final class TestFormulaEvaluatorBugs extends TestCase {
HSSFFormulaEvaluator eva = new HSSFFormulaEvaluator(sheet, wb);
row = (HSSFRow)sheet.getRow(0);
row = sheet.getRow(0);
cell = row.getCell((short)0);
assertEquals("31+46", cell.getCellFormula());
eva.setCurrentRow(row);
assertEquals(77, eva.evaluate(cell).getNumberValue(), 0);
row = (HSSFRow)sheet.getRow(1);
row = sheet.getRow(1);
cell = row.getCell((short)0);
assertEquals("30+53", cell.getCellFormula());
eva.setCurrentRow(row);
assertEquals(83, eva.evaluate(cell).getNumberValue(), 0);
row = (HSSFRow)sheet.getRow(2);
row = sheet.getRow(2);
cell = row.getCell((short)0);
assertEquals("SUM(A1:A2)", cell.getCellFormula());
eva.setCurrentRow(row);
assertEquals(160, eva.evaluate(cell).getNumberValue(), 0);
row = (HSSFRow)sheet.getRow(4);
row = sheet.getRow(4);
cell = row.getCell((short)0);
assertEquals("32767+32768", cell.getCellFormula());
eva.setCurrentRow(row);
assertEquals(65535, eva.evaluate(cell).getNumberValue(), 0);
row = (HSSFRow)sheet.getRow(7);
row = sheet.getRow(7);
cell = row.getCell((short)0);
assertEquals("32744+42333", cell.getCellFormula());
eva.setCurrentRow(row);
assertEquals(75077, eva.evaluate(cell).getNumberValue(), 0);
row = (HSSFRow)sheet.getRow(8);
row = sheet.getRow(8);
cell = row.getCell((short)0);
assertEquals("327680.0/32768", cell.getCellFormula());
eva.setCurrentRow(row);
assertEquals(10, eva.evaluate(cell).getNumberValue(), 0);
row = (HSSFRow)sheet.getRow(9);
row = sheet.getRow(9);
cell = row.getCell((short)0);
assertEquals("32767+32769", cell.getCellFormula());
eva.setCurrentRow(row);
assertEquals(65536, eva.evaluate(cell).getNumberValue(), 0);
row = (HSSFRow)sheet.getRow(10);
row = sheet.getRow(10);
cell = row.getCell((short)0);
assertEquals("35000+36000", cell.getCellFormula());
eva.setCurrentRow(row);
assertEquals(71000, eva.evaluate(cell).getNumberValue(), 0);
row = (HSSFRow)sheet.getRow(11);
row = sheet.getRow(11);
cell = row.getCell((short)0);
assertEquals("-1000000.0-3000000.0", cell.getCellFormula());
eva.setCurrentRow(row);
@ -172,21 +171,20 @@ public final class TestFormulaEvaluatorBugs extends TestCase {
*
* @author Nick Burch
*/
public void test44410() throws IOException {
FileInputStream in = new FileInputStream(new File(dirName, "SingleLetterRanges.xls"));
HSSFWorkbook wb = new HSSFWorkbook(in);
in.close();
public void test44410() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SingleLetterRanges.xls");
HSSFSheet sheet = wb.getSheetAt(0);
HSSFFormulaEvaluator eva = new HSSFFormulaEvaluator(sheet, wb);
// =index(C:C,2,1) -> 2
HSSFRow rowIDX = (HSSFRow)sheet.getRow(3);
HSSFRow rowIDX = sheet.getRow(3);
// =sum(C:C) -> 6
HSSFRow rowSUM = (HSSFRow)sheet.getRow(4);
HSSFRow rowSUM = sheet.getRow(4);
// =sum(C:D) -> 66
HSSFRow rowSUM2D = (HSSFRow)sheet.getRow(5);
HSSFRow rowSUM2D = sheet.getRow(5);
// Test the sum
HSSFCell cellSUM = rowSUM.getCell((short)0);

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,7 +15,6 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import java.io.File;
@ -27,6 +25,7 @@ import java.util.Date;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.util.TempFile;
@ -34,11 +33,10 @@ import org.apache.poi.util.TempFile;
* @author Andrew C. Oliver (acoliver at apache dot org)
* @author Avik Sengupta
*/
public final class TestFormulas extends TestCase {
public class TestFormulas
extends TestCase {
public TestFormulas(String s) {
super(s);
private static HSSFWorkbook openSample(String sampleFileName) {
return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
}
/**
@ -48,7 +46,6 @@ extends TestCase {
public void testBasicAddIntegers()
throws Exception {
short rownum = 0;
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -170,7 +167,6 @@ extends TestCase {
private void floatTest(String operator)
throws Exception {
short rownum = 0;
File file = TempFile.createTempFile("testFormulaFloat",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -210,7 +206,6 @@ extends TestCase {
private void floatVerify(String operator, File file)
throws Exception {
short rownum = 0;
FileInputStream in = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(in);
@ -334,7 +329,6 @@ extends TestCase {
*/
private void operationalRefVerify(String operator, File file)
throws Exception {
short rownum = 0;
FileInputStream in = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(in);
@ -451,7 +445,6 @@ extends TestCase {
*/
private void binomialOperator(String operator)
throws Exception {
short rownum = 0;
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -495,7 +488,6 @@ extends TestCase {
*/
private void binomialVerify(String operator, File file)
throws Exception {
short rownum = 0;
FileInputStream in = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(in);
@ -549,7 +541,6 @@ extends TestCase {
public void areaFunctionTest(String function)
throws Exception {
short rownum = 0;
File file = TempFile.createTempFile("testFormulaAreaFunction"+function,".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -587,7 +578,6 @@ extends TestCase {
public void refArrayFunctionTest(String function)
throws Exception {
short rownum = 0;
File file = TempFile.createTempFile("testFormulaArrayFunction"+function,".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -626,7 +616,6 @@ extends TestCase {
public void refAreaArrayFunctionTest(String function)
throws Exception {
short rownum = 0;
File file = TempFile.createTempFile("testFormulaAreaArrayFunction"+function,".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -711,8 +700,6 @@ extends TestCase {
public void testSheetFunctions()
throws IOException
{
String filename = System.getProperty("HSSF.testdata.path");
File file = TempFile.createTempFile("testSheetFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -785,8 +772,6 @@ extends TestCase {
public void testStringFormulas()
throws IOException
{
String readFilename = System.getProperty("HSSF.testdata.path");
File file = TempFile.createTempFile("testStringFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -801,17 +786,13 @@ extends TestCase {
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
FileInputStream in = new FileInputStream(readFilename+File.separator+"StringFormulas.xls");
wb = new HSSFWorkbook(in);
wb = openSample("StringFormulas.xls");
s = wb.getSheetAt(0);
r = s.getRow(0);
c = r.getCell((short)0);
assertTrue("expected: UPPER(\"xyz\") got "+c.getCellFormula(), ("UPPER(\"xyz\")").equals(c.getCellFormula()));
//c = r.getCell((short)1);
//assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
in.close();
}
@ -847,8 +828,6 @@ extends TestCase {
public void testDateFormulas()
throws IOException
{
String readFilename = System.getProperty("HSSF.testdata.path");
File file = TempFile.createTempFile("testDateFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -885,8 +864,6 @@ extends TestCase {
public void testIfFormulas()
throws IOException
{
String readFilename = System.getProperty("HSSF.testdata.path");
File file = TempFile.createTempFile("testIfFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@ -913,9 +890,7 @@ extends TestCase {
assertTrue("expected: IF(A1=D1,\"A1\",\"B1\") got "+c.getCellFormula(), ("IF(A1=D1,\"A1\",\"B1\")").equals(c.getCellFormula()));
in.close();
in = new FileInputStream(readFilename+File.separator+"IfFormulaTest.xls");
wb = new HSSFWorkbook(in);
wb = openSample("IfFormulaTest.xls");
s = wb.getSheetAt(0);
r = s.getRow(3);
c = r.getCell((short)0);
@ -975,13 +950,9 @@ extends TestCase {
public void testSumIf()
throws IOException
{
String readFilename = System.getProperty("HSSF.testdata.path");
String function ="SUMIF(A1:A5,\">4000\",B1:B5)";
File inFile = new File(readFilename+"/sumifformula.xls");
FileInputStream in = new FileInputStream(inFile);
HSSFWorkbook wb = new HSSFWorkbook(in);
in.close();
HSSFWorkbook wb = openSample("sumifformula.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = s.getRow(0);
@ -1025,16 +996,9 @@ extends TestCase {
assertTrue("sumif == 0 bytes", file.length() > 0);
}
public void testSquareMacro() throws IOException {
File dir = new File(System.getProperty("HSSF.testdata.path"));
File xls = new File(dir, "SquareMacro.xls");
FileInputStream in = new FileInputStream(xls);
HSSFWorkbook w;
try {
w = new HSSFWorkbook(in);
} finally {
in.close();
}
public void testSquareMacro() {
HSSFWorkbook w = openSample("SquareMacro.xls");
HSSFSheet s0 = w.getSheetAt(0);
HSSFRow[] r = {s0.getRow(0), s0.getRow(1)};
@ -1071,18 +1035,10 @@ extends TestCase {
assertEquals(4d, d2.getNumericCellValue(), 1e-9);
}
public void testStringFormulaRead() throws IOException {
File dir = new File(System.getProperty("HSSF.testdata.path"));
File xls = new File(dir, "StringFormulas.xls");
FileInputStream in = new FileInputStream(xls);
HSSFWorkbook w;
try {
w = new HSSFWorkbook(in);
} finally {
in.close();
}
public void testStringFormulaRead() {
HSSFWorkbook w = openSample("StringFormulas.xls");
HSSFCell c = w.getSheetAt(0).getRow(0).getCell((short)0);
assertEquals("String Cell value","XYZ",c.getStringCellValue());
assertEquals("String Cell value","XYZ",c.getRichStringCellValue().getString());
}
/** test for bug 34021*/
@ -1098,10 +1054,7 @@ extends TestCase {
/*Unknown Ptg 3C*/
public void test27272_1() throws Exception {
String readFilename = System.getProperty("HSSF.testdata.path");
File inFile = new File(readFilename+"/27272_1.xls");
FileInputStream in = new FileInputStream(inFile);
HSSFWorkbook wb = new HSSFWorkbook(in);
HSSFWorkbook wb = openSample("27272_1.xls");
wb.getSheetAt(0);
assertEquals("Reference for named range ", "#REF!",wb.getNameAt(0).getReference());
File outF = File.createTempFile("bug27272_1",".xls");
@ -1110,10 +1063,7 @@ extends TestCase {
}
/*Unknown Ptg 3D*/
public void test27272_2() throws Exception {
String readFilename = System.getProperty("HSSF.testdata.path");
File inFile = new File(readFilename+"/27272_2.xls");
FileInputStream in = new FileInputStream(inFile);
HSSFWorkbook wb = new HSSFWorkbook(in);
HSSFWorkbook wb = openSample("27272_2.xls");
assertEquals("Reference for named range ", "#REF!",wb.getNameAt(0).getReference());
File outF = File.createTempFile("bug27272_2",".xls");
wb.write(new FileOutputStream(outF));
@ -1127,11 +1077,8 @@ extends TestCase {
cell.setCellFormula("IF(A1=\"A\",1,)");
}
public void testSharedFormula() throws Exception {
String readFilename = System.getProperty("HSSF.testdata.path");
File inFile = new File(readFilename+"/SharedFormulaTest.xls");
FileInputStream fis = new FileInputStream(inFile);
HSSFWorkbook wb = new HSSFWorkbook(fis);
public void testSharedFormula() {
HSSFWorkbook wb = openSample("SharedFormulaTest.xls");
assertEquals("A$1*2", wb.getSheetAt(0).getRow(1).getCell((short)1).toString());
assertEquals("$A11*2", wb.getSheetAt(0).getRow(11).getCell((short)1).toString());
@ -1140,9 +1087,6 @@ extends TestCase {
}
public static void main(String [] args) {
System.out
.println("Testing org.apache.poi.hssf.usermodel.TestFormulas");
junit.textui.TestRunner.run(TestFormulas.class);
}
}

View File

@ -17,18 +17,21 @@
package org.apache.poi.hssf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.TempFile;
/**
@ -40,12 +43,26 @@ import org.apache.poi.util.TempFile;
*/
public final class TestHSSFCell extends TestCase {
private static HSSFWorkbook openSample(String sampleFileName) {
return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
}
private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
original.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
return new HSSFWorkbook(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* test that Boolean and Error types (BoolErrRecord) are supported properly.
*/
public void testBoolErr()
throws java.io.IOException {
String readFilename = System.getProperty("HSSF.testdata.path");
File file = TempFile.createTempFile("testBoolErr",".xls");
FileOutputStream out = new FileOutputStream(file);
@ -102,33 +119,24 @@ public final class TestHSSFCell extends TestCase {
public void testDateWindowingRead() throws Exception {
GregorianCalendar cal = new GregorianCalendar(2000,0,1); // Jan. 1, 2000
Date date = cal.getTime();
String path = System.getProperty("HSSF.testdata.path");
// first check a file with 1900 Date Windowing
String filename = path + "/1900DateWindowing.xls";
FileInputStream stream = new FileInputStream(filename);
POIFSFileSystem fs = new POIFSFileSystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFWorkbook workbook = openSample("1900DateWindowing.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
assertEquals("Date from file using 1900 Date Windowing",
date.getTime(),
sheet.getRow(0).getCell((short)0)
.getDateCellValue().getTime());
stream.close();
// now check a file with 1904 Date Windowing
filename = path + "/1904DateWindowing.xls";
stream = new FileInputStream(filename);
fs = new POIFSFileSystem(stream);
workbook = new HSSFWorkbook(fs);
workbook = openSample("1904DateWindowing.xls");
sheet = workbook.getSheetAt(0);
assertEquals("Date from file using 1904 Date Windowing",
date.getTime(),
sheet.getRow(0).getCell((short)0)
.getDateCellValue().getTime());
stream.close();
}
/**
@ -140,55 +148,39 @@ public final class TestHSSFCell extends TestCase {
public void testDateWindowingWrite() throws Exception {
GregorianCalendar cal = new GregorianCalendar(2000,0,1); // Jan. 1, 2000
Date date = cal.getTime();
String path = System.getProperty("HSSF.testdata.path");
// first check a file with 1900 Date Windowing
String filename = path + "/1900DateWindowing.xls";
writeCell(filename, 0, (short) 1, date);
HSSFWorkbook wb;
wb = openSample("1900DateWindowing.xls");
setCell(wb, 0, 1, date);
wb = writeOutAndReadBack(wb);
assertEquals("Date from file using 1900 Date Windowing",
date.getTime(),
readCell(filename, 0, (short) 1).getTime());
readCell(wb, 0, 1).getTime());
// now check a file with 1904 Date Windowing
filename = path + "/1904DateWindowing.xls";
writeCell(filename, 0, (short) 1, date);
wb = openSample("1904DateWindowing.xls");
setCell(wb, 0, 1, date);
wb = writeOutAndReadBack(wb);
assertEquals("Date from file using 1900 Date Windowing",
date.getTime(),
readCell(filename, 0, (short) 1).getTime());
readCell(wb, 0, 1).getTime());
}
/**
* Sets cell value and writes file.
*/
private void writeCell(String filename,
int rowIdx, short colIdx, Date date) throws Exception {
FileInputStream stream = new FileInputStream(filename);
POIFSFileSystem fs = new POIFSFileSystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
private static void setCell(HSSFWorkbook workbook, int rowIdx, int colIdx, Date date) {
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row = sheet.getRow(rowIdx);
HSSFCell cell = row.getCell(colIdx);
if (cell == null) {
cell = row.createCell(colIdx);
cell = row.createCell((short)colIdx);
}
cell.setCellValue(date);
// Write the file
stream.close();
FileOutputStream oStream = new FileOutputStream(filename);
workbook.write(oStream);
oStream.close();
}
/**
* Reads cell value from file.
*/
private Date readCell(String filename,
int rowIdx, short colIdx) throws Exception {
FileInputStream stream = new FileInputStream(filename);
POIFSFileSystem fs = new POIFSFileSystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
private static Date readCell(HSSFWorkbook workbook, int rowIdx, int colIdx) {
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row = sheet.getRow(rowIdx);
HSSFCell cell = row.getCell(colIdx);
@ -201,12 +193,7 @@ public final class TestHSSFCell extends TestCase {
public void testActiveCell() throws Exception
{
//read in sample
String dir = System.getProperty("HSSF.testdata.path");
File sample = new File(dir + "/Simple.xls");
assertTrue("Simple.xls exists and is readable", sample.canRead());
FileInputStream fis = new FileInputStream(sample);
HSSFWorkbook book = new HSSFWorkbook(fis);
fis.close();
HSSFWorkbook book = openSample("Simple.xls");
//check initial position
HSSFSheet umSheet = book.getSheetAt(0);
@ -225,14 +212,8 @@ public final class TestHSSFCell extends TestCase {
3, s.getActiveCellRow());
//write book to temp file; read and verify that position is serialized
File temp = TempFile.createTempFile("testActiveCell", ".xls");
FileOutputStream fos = new FileOutputStream(temp);
book.write(fos);
fos.close();
book = writeOutAndReadBack(book);
fis = new FileInputStream(temp);
book = new HSSFWorkbook(fis);
fis.close();
umSheet = book.getSheetAt(0);
s = umSheet.getSheet();
@ -245,12 +226,8 @@ public final class TestHSSFCell extends TestCase {
/**
* test that Cell Styles being applied to formulas remain intact
*/
public void testFormulaStyle()
throws java.io.IOException {
String readFilename = System.getProperty("HSSF.testdata.path");
public void testFormulaStyle() {
File file = TempFile.createTempFile("testFormulaStyle",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("testSheet1");
HSSFRow r = null;
@ -258,7 +235,7 @@ public final class TestHSSFCell extends TestCase {
HSSFCellStyle cs = wb.createCellStyle();
HSSFFont f = wb.createFont();
f.setFontHeightInPoints((short) 20);
f.setColor((short) HSSFColor.RED.index);
f.setColor(HSSFColor.RED.index);
f.setBoldweight(f.BOLDWEIGHT_BOLD);
f.setFontName("Arial Unicode MS");
cs.setFillBackgroundColor((short)3);
@ -273,13 +250,7 @@ public final class TestHSSFCell extends TestCase {
c.setCellStyle(cs);
c.setCellFormula("2*3");
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
wb = writeOutAndReadBack(wb);
s = wb.getSheetAt(0);
r = s.getRow(0);
c = r.getCell((short)0);
@ -293,17 +264,14 @@ public final class TestHSSFCell extends TestCase {
assertTrue("Left Border", (cs.getBorderLeft() == (short)1));
assertTrue("Right Border", (cs.getBorderRight() == (short)1));
assertTrue("Bottom Border", (cs.getBorderBottom() == (short)1));
in.close();
}
/**
* Test reading hyperlinks
*/
public void testWithHyperlink() throws Exception {
String dir = System.getProperty("HSSF.testdata.path");
File f = new File(dir, "WithHyperlink.xls");
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(f));
public void testWithHyperlink() {
HSSFWorkbook wb = openSample("WithHyperlink.xls");
HSSFSheet sheet = wb.getSheetAt(0);
HSSFCell cell = sheet.getRow(4).getCell((short)0);
@ -320,9 +288,8 @@ public final class TestHSSFCell extends TestCase {
* Test reading hyperlinks
*/
public void testWithTwoHyperlinks() throws Exception {
String dir = System.getProperty("HSSF.testdata.path");
File f = new File(dir, "WithTwoHyperLinks.xls");
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(f));
HSSFWorkbook wb = openSample("WithTwoHyperLinks.xls");
HSSFSheet sheet = wb.getSheetAt(0);
@ -354,7 +321,7 @@ public final class TestHSSFCell extends TestCase {
assertEquals("Boolean", "TRUE", c.toString());
c=r.createCell((short) 1); c.setCellValue(1.5);
assertEquals("Numeric", "1.5", c.toString());
c=r.createCell((short)(2)); c.setCellValue("Astring");
c=r.createCell((short)(2)); c.setCellValue(new HSSFRichTextString("Astring"));
assertEquals("String", "Astring", c.toString());
c=r.createCell((short) 3); c.setCellErrorValue((byte) 7);
assertEquals("Error", "#ERR7", c.toString());
@ -392,10 +359,7 @@ public final class TestHSSFCell extends TestCase {
}
public static void main(String [] args) {
System.out
.println("Testing org.apache.poi.hssf.usermodel.TestHSSFCell");
junit.textui.TestRunner.run(TestHSSFCell.class);
}
}

View File

@ -20,13 +20,14 @@ import junit.framework.TestCase;
import java.io.*;
import org.apache.poi.hssf.HSSFTestDataSamples;
/**
* Tests TestHSSFCellComment.
*
* @author Yegor Kozlov
*/
public class TestHSSFComment extends TestCase {
public final class TestHSSFComment extends TestCase {
/**
* Test that we can create cells and add comments to it.
@ -83,12 +84,9 @@ public class TestHSSFComment extends TestCase {
/**
* test that we can read cell comments from an existing workbook.
*/
public static void testReadComments() throws Exception {
public static void testReadComments() {
String dir = System.getProperty("HSSF.testdata.path");
FileInputStream is = new FileInputStream(new File(dir, "SimpleWithComments.xls"));
HSSFWorkbook wb = new HSSFWorkbook(is);
is.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithComments.xls");
HSSFSheet sheet = wb.getSheetAt(0);
@ -123,12 +121,9 @@ public class TestHSSFComment extends TestCase {
/**
* test that we can modify existing cell comments
*/
public static void testModifyComments() throws Exception {
public static void testModifyComments() throws IOException {
String dir = System.getProperty("HSSF.testdata.path");
FileInputStream is = new FileInputStream(new File(dir, "SimpleWithComments.xls"));
HSSFWorkbook wb = new HSSFWorkbook(is);
is.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithComments.xls");
HSSFSheet sheet = wb.getSheetAt(0);

View File

@ -19,16 +19,15 @@
package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* Class TestHSSFDateUtil
@ -40,10 +39,7 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
* @author Alex Jacoby (ajacoby at gmail.com)
* @version %I%, %G%
*/
public class TestHSSFDateUtil
extends TestCase
{
public class TestHSSFDateUtil extends TestCase {
public static final int CALENDAR_JANUARY = 0;
public static final int CALENDAR_FEBRUARY = 1;
@ -301,11 +297,8 @@ public class TestHSSFDateUtil
* correctly
*/
public void testOnARealFile() throws Exception {
String path = System.getProperty("HSSF.testdata.path");
String filename = path + "/DateFormats.xls";
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(filename));
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("DateFormats.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
Workbook wb = workbook.getWorkbook();

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,20 +15,16 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
/**
* Tests row shifting capabilities.
@ -37,30 +32,16 @@ import java.io.FileOutputStream;
*
* @author Shawn Laubach (slaubach at apache dot com)
*/
public class TestHSSFHeaderFooter extends TestCase {
/**
* Constructor for TestHeaderFooter.
* @param arg0
*/
public TestHSSFHeaderFooter(String arg0) {
super(arg0);
}
public final class TestHSSFHeaderFooter extends TestCase {
/**
* Tests that get header retreives the proper values.
*
* @author Shawn Laubach (slaubach at apache dot org)
*/
public void testRetrieveCorrectHeader() throws Exception
{
public void testRetrieveCorrectHeader() {
// Read initial file in
String filename = System.getProperty( "HSSF.testdata.path" );
filename = filename + "/EmbeddedChartHeaderTest.xls";
FileInputStream fin = new FileInputStream( filename );
HSSFWorkbook wb = new HSSFWorkbook( fin );
fin.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("EmbeddedChartHeaderTest.xls");
HSSFSheet s = wb.getSheetAt( 0 );
HSSFHeader head = s.getHeader();
@ -74,14 +55,9 @@ public class TestHSSFHeaderFooter extends TestCase {
*
* @author Shawn Laubach (slaubach at apache dot org)
*/
public void testRetrieveCorrectFooter() throws Exception
{
public void testRetrieveCorrectFooter() {
// Read initial file in
String filename = System.getProperty( "HSSF.testdata.path" );
filename = filename + "/EmbeddedChartHeaderTest.xls";
FileInputStream fin = new FileInputStream( filename );
HSSFWorkbook wb = new HSSFWorkbook( fin );
fin.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("EmbeddedChartHeaderTest.xls");
HSSFSheet s = wb.getSheetAt( 0 );
HSSFFooter foot = s.getFooter();
@ -93,7 +69,7 @@ public class TestHSSFHeaderFooter extends TestCase {
/**
* Testcase for Bug 17039 HSSFHeader doesnot support DBCS
*/
public void testHeaderHas16bitCharacter() throws Exception {
public void testHeaderHas16bitCharacter() {
HSSFWorkbook b = new HSSFWorkbook();
HSSFSheet s = b.createSheet("Test");
HSSFHeader h = s.getHeader();
@ -101,10 +77,7 @@ public class TestHSSFHeaderFooter extends TestCase {
h.setCenter("\u0392");
h.setRight("\u0393");
ByteArrayOutputStream out = new ByteArrayOutputStream();
b.write(out);
HSSFWorkbook b2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
HSSFWorkbook b2 = HSSFTestDataSamples.writeOutAndReadBack(b);
HSSFHeader h2 = b2.getSheet("Test").getHeader();
assertEquals(h2.getLeft(),"\u0391");
@ -115,7 +88,7 @@ public class TestHSSFHeaderFooter extends TestCase {
/**
* Testcase for Bug 17039 HSSFFooter doesnot support DBCS
*/
public void testFooterHas16bitCharacter() throws Exception{
public void testFooterHas16bitCharacter() {
HSSFWorkbook b = new HSSFWorkbook();
HSSFSheet s = b.createSheet("Test");
HSSFFooter f = s.getFooter();
@ -123,10 +96,7 @@ public class TestHSSFHeaderFooter extends TestCase {
f.setCenter("\u0392");
f.setRight("\u0393");
ByteArrayOutputStream out = new ByteArrayOutputStream();
b.write(out);
HSSFWorkbook b2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
HSSFWorkbook b2 = HSSFTestDataSamples.writeOutAndReadBack(b);
HSSFFooter f2 = b2.getSheet("Test").getFooter();
assertEquals(f2.getLeft(),"\u0391");
@ -134,10 +104,8 @@ public class TestHSSFHeaderFooter extends TestCase {
assertEquals(f2.getRight(),"\u0393");
}
public void testReadDBCSHeaderFooter() throws Exception{
String readFilename = System.getProperty("HSSF.testdata.path");
FileInputStream in = new FileInputStream(readFilename+File.separator+"DBCSHeader.xls");
HSSFWorkbook wb = new HSSFWorkbook(in);
public void testReadDBCSHeaderFooter() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("DBCSHeader.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFHeader h = s.getHeader();
assertEquals("Header Left " ,h.getLeft(),"\u090f\u0915");

View File

@ -14,28 +14,28 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
import java.io.*;
import org.apache.poi.hssf.HSSFTestDataSamples;
/**
* Tests HSSFHyperlink.
*
* @author Yegor Kozlov
*/
public class TestHSSFHyperlink extends TestCase {
protected String cwd = System.getProperty("HSSF.testdata.path");
public final class TestHSSFHyperlink extends TestCase {
/**
* Test that we can read hyperlinks.
*/
public void testRead() throws Exception {
public void testRead() {
FileInputStream is = new FileInputStream(new File(cwd, "HyperlinksOnManySheets.xls"));
HSSFWorkbook wb = new HSSFWorkbook(is);
is.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("HyperlinksOnManySheets.xls");
HSSFSheet sheet;
HSSFCell cell;
@ -74,9 +74,7 @@ public class TestHSSFHyperlink extends TestCase {
}
public void testModify() throws Exception {
FileInputStream is = new FileInputStream(new File(cwd, "HyperlinksOnManySheets.xls"));
HSSFWorkbook wb = new HSSFWorkbook(is);
is.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("HyperlinksOnManySheets.xls");
HSSFSheet sheet;
HSSFCell cell;
@ -168,10 +166,8 @@ public class TestHSSFHyperlink extends TestCase {
assertEquals("'Target Sheet'!A1", link.getAddress());
}
public void testCloneSheet() throws Exception {
FileInputStream is = new FileInputStream(new File(cwd, "HyperlinksOnManySheets.xls"));
HSSFWorkbook wb = new HSSFWorkbook(is);
is.close();
public void testCloneSheet() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("HyperlinksOnManySheets.xls");
HSSFCell cell;
HSSFHyperlink link;

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -25,6 +24,8 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.PaletteRecord;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.util.TempFile;
@ -32,15 +33,10 @@ import org.apache.poi.util.TempFile;
/**
* @author Brian Sanders (bsanders at risklabs dot com)
*/
public class TestHSSFPalette extends TestCase
{
public final class TestHSSFPalette extends TestCase {
private PaletteRecord palette;
private HSSFPalette hssfPalette;
public TestHSSFPalette(String name)
{
super(name);
}
public void setUp()
{
@ -54,12 +50,7 @@ public class TestHSSFPalette extends TestCase
public void testCustomPalette() throws IOException
{
//reading sample xls
String dir = System.getProperty("HSSF.testdata.path");
File sample = new File(dir + "/Simple.xls");
assertTrue("Simple.xls exists and is readable", sample.canRead());
FileInputStream fis = new FileInputStream(sample);
HSSFWorkbook book = new HSSFWorkbook(fis);
fis.close();
HSSFWorkbook book = HSSFTestDataSamples.openSampleWorkbook("Simple.xls");
//creating custom palette
HSSFPalette palette = book.getCustomPalette();
@ -72,7 +63,7 @@ public class TestHSSFPalette extends TestCase
book.write(fos);
fos.close();
fis = new FileInputStream(temp);
FileInputStream fis = new FileInputStream(temp);
book = new HSSFWorkbook(fis);
fis.close();
@ -98,13 +89,8 @@ public class TestHSSFPalette extends TestCase
/**
* Uses the palette from cell stylings
*/
public void testPaletteFromCellColours() throws Exception {
String dir = System.getProperty("HSSF.testdata.path");
File sample = new File(dir + "/SimpleWithColours.xls");
assertTrue("SimpleWithColours.xls exists and is readable", sample.canRead());
FileInputStream fis = new FileInputStream(sample);
HSSFWorkbook book = new HSSFWorkbook(fis);
fis.close();
public void testPaletteFromCellColours() {
HSSFWorkbook book = HSSFTestDataSamples.openSampleWorkbook("SimpleWithColours.xls");
HSSFPalette p = book.getCustomPalette();

View File

@ -17,13 +17,13 @@
package org.apache.poi.hssf.usermodel;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
/**
* Test <code>HSSFPicture</code>.
*
@ -58,9 +58,8 @@ public final class TestHSSFPicture extends TestCase{
private static byte[] getTestDataFileContent(String fileName) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String readFilename = System.getProperty("HSSF.testdata.path");
try {
InputStream fis = new FileInputStream(readFilename+File.separator+fileName);
InputStream fis = HSSFTestDataSamples.openSampleFileStream(fileName);
byte[] buf = new byte[512];
while(true) {

View File

@ -15,20 +15,19 @@
limitations under the License.
==================================================================== */
/*
* HSSFWorkbook.java
*
* Created on September 30, 2001, 3:37 PM
*/
package org.apache.poi.hssf.usermodel;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import junit.framework.TestCase;
import javax.imageio.ImageIO;
import java.io.*;
import java.util.*;
import java.awt.image.BufferedImage;
import org.apache.poi.hssf.HSSFTestDataSamples;
/**
* Test <code>HSSFPictureData</code>.
@ -37,14 +36,11 @@ import java.awt.image.BufferedImage;
* @author Yegor Kozlov (yegor at apache dot org)
* @author Trejkaz (trejkaz at trypticon dot org)
*/
public class TestHSSFPictureData extends TestCase{
public final class TestHSSFPictureData extends TestCase{
static String cwd = System.getProperty("HSSF.testdata.path");
public void testPictures() throws IOException {
FileInputStream is = new FileInputStream(new File(cwd, "SimpleWithImages.xls"));
HSSFWorkbook wb = new HSSFWorkbook(is);
is.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");
List lst = wb.getAllPictures();
//assertEquals(2, lst.size());
@ -69,6 +65,5 @@ public class TestHSSFPictureData extends TestCase{
//TODO: test code for PICT, WMF and EMF
}
}
}
}

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,7 +15,6 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import java.io.ByteArrayInputStream;
@ -27,6 +25,7 @@ import java.io.FileOutputStream;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.record.HCenterRecord;
import org.apache.poi.hssf.record.PasswordRecord;
@ -36,7 +35,6 @@ 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.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.TempFile;
/**
@ -46,13 +44,10 @@ import org.apache.poi.util.TempFile;
* @author Glen Stampoultzis (glens at apache.org)
* @author Andrew C. Oliver (acoliver apache org)
*/
public final class TestHSSFSheet extends TestCase {
public class TestHSSFSheet
extends TestCase
{
public TestHSSFSheet(String s)
{
super(s);
private static HSSFWorkbook openSample(String sampleFileName) {
return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
}
/**
@ -268,10 +263,7 @@ public class TestHSSFSheet
* Setting landscape and portrait stuff on existing sheets
*/
public void testPrintSetupLandscapeExisting() throws Exception {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/SimpleWithPageBreaks.xls";
HSSFWorkbook workbook =
new HSSFWorkbook(new FileInputStream(filename));
HSSFWorkbook workbook = openSample("SimpleWithPageBreaks.xls");
assertEquals(3, workbook.getNumberOfSheets());
@ -358,10 +350,7 @@ public class TestHSSFSheet
}
public void testGroupRowsExisting() throws Exception {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/NoGutsRecords.xls";
HSSFWorkbook workbook =
new HSSFWorkbook(new FileInputStream(filename));
HSSFWorkbook workbook = openSample("NoGutsRecords.xls");
HSSFSheet s = workbook.getSheetAt(0);
HSSFRow r1 = s.getRow(0);
@ -412,13 +401,8 @@ public class TestHSSFSheet
}
public void testGetDrawings() throws Exception {
String filename = System.getProperty("HSSF.testdata.path");
HSSFWorkbook wb1c = new HSSFWorkbook(
new FileInputStream(new File(filename,"WithChart.xls"))
);
HSSFWorkbook wb2c = new HSSFWorkbook(
new FileInputStream(new File(filename,"WithTwoCharts.xls"))
);
HSSFWorkbook wb1c = openSample("WithChart.xls");
HSSFWorkbook wb2c = openSample("WithTwoCharts.xls");
// 1 chart sheet -> data on 1st, chart on 2nd
assertNotNull(wb1c.getSheetAt(0).getDrawingPatriarch());
@ -597,15 +581,7 @@ public class TestHSSFSheet
*
*/
public void testPageBreakFiles() throws Exception{
FileInputStream fis = null;
HSSFWorkbook wb = null;
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/SimpleWithPageBreaks.xls";
fis = new FileInputStream(filename);
wb = new HSSFWorkbook(fis);
fis.close();
HSSFWorkbook wb = openSample("SimpleWithPageBreaks.xls");
HSSFSheet sheet = wb.getSheetAt(0);
assertNotNull(sheet);
@ -641,14 +617,7 @@ public class TestHSSFSheet
}
public void testDBCSName () throws Exception {
FileInputStream fis = null;
HSSFWorkbook wb = null;
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/DBCSSheetName.xls";
fis = new FileInputStream(filename);
wb = new HSSFWorkbook(fis);
HSSFWorkbook wb = openSample("DBCSSheetName.xls");
HSSFSheet s= wb.getSheetAt(1);
assertEquals ("DBCS Sheet Name 2", wb.getSheetName(1),"\u090f\u0915" );
assertEquals("DBCS Sheet Name 1", wb.getSheetName(0),"\u091c\u093e");
@ -659,17 +628,8 @@ public class TestHSSFSheet
* parameter to allow setting the toprow in the visible view
* of the sheet when it is first opened.
*/
public void testTopRow() throws Exception
{
FileInputStream fis = null;
HSSFWorkbook wb = null;
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/SimpleWithPageBreaks.xls";
fis = new FileInputStream(filename);
wb = new HSSFWorkbook(fis);
fis.close();
public void testTopRow() {
HSSFWorkbook wb = openSample("SimpleWithPageBreaks.xls");
HSSFSheet sheet = wb.getSheetAt(0);
assertNotNull(sheet);
@ -722,14 +682,9 @@ public class TestHSSFSheet
out.close();
workbook = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
assertTrue("No Exceptions while reading file", true);
//try adding empty rows in an existing worksheet
String cwd = System.getProperty("HSSF.testdata.path");
FileInputStream in = new FileInputStream(new File(cwd, "Simple.xls"));
workbook = new HSSFWorkbook(in);
in.close();
assertTrue("No Exceptions while reading file", true);
workbook = openSample("Simple.xls");
sheet = workbook.getSheetAt(0);
for (int i = 3; i < 10; i++) sheet.createRow(i);
@ -739,17 +694,11 @@ public class TestHSSFSheet
out.close();
workbook = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
assertTrue("No Exceptions while reading file", true);
}
public void testAutoSizeColumn() throws Exception {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/43902.xls";
HSSFWorkbook wb = openSample("43902.xls");
String sheetName = "my sheet";
FileInputStream is = new FileInputStream(filename);
POIFSFileSystem fs = new POIFSFileSystem(is);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheet(sheetName);
// Can't use literal numbers for column sizes, as
@ -798,9 +747,7 @@ public class TestHSSFSheet
* Setting ForceFormulaRecalculation on sheets
*/
public void testForceRecalculation() throws Exception {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/UncalcedRecord.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
HSSFWorkbook workbook = openSample("UncalcedRecord.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFSheet sheet2 = workbook.getSheetAt(0);
@ -874,8 +821,7 @@ public class TestHSSFSheet
public void testColumnWidth() throws Exception {
//check we can correctly read column widths from a reference workbook
String filename = System.getProperty("HSSF.testdata.path") + "/colwidth.xls";
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filename));
HSSFWorkbook wb = openSample("colwidth.xls");
//reference values
int[] ref = {365, 548, 731, 914, 1097, 1280, 1462, 1645, 1828, 2011, 2194, 2377, 2560, 2742, 2925, 3108, 3291, 3474, 3657};

View File

@ -14,28 +14,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.poi.hssf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import junit.framework.TestCase;
import junit.framework.*;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.NameRecord;
public class TestHSSFWorkbook extends TestCase
{
HSSFWorkbook hssfWorkbook;
String filename;
protected void setUp() throws Exception {
super.setUp();
filename = System.getProperty("HSSF.testdata.path");
/**
*
*/
public final class TestHSSFWorkbook extends TestCase {
private static HSSFWorkbook openSample(String sampleFileName) {
return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
}
public void testSetRepeatingRowsAndColumns() throws Exception
{
public void testSetRepeatingRowsAndColumns() {
// Test bug 29747
HSSFWorkbook b = new HSSFWorkbook( );
b.createSheet();
@ -46,9 +40,7 @@ public class TestHSSFWorkbook extends TestCase
assertEquals( 3, nameRecord.getIndexToSheet() );
}
public void testDuplicateNames()
throws Exception
{
public void testDuplicateNames() {
HSSFWorkbook b = new HSSFWorkbook( );
b.createSheet("Sheet1");
b.createSheet();
@ -105,14 +97,14 @@ public class TestHSSFWorkbook extends TestCase
public void testSheetSelection() {
HSSFWorkbook b = new HSSFWorkbook();
b.createSheet("Sheet One");
HSSFSheet s = b.createSheet("Sheet Two");
b.createSheet("Sheet Two");
b.setSelectedTab((short) 1);
b.setDisplayedTab((short) 1);
assertEquals(b.getSelectedTab(), 1);
assertEquals(b.getDisplayedTab(), 1);
}
public void testSheetClone() throws Exception {
public void testSheetClone() {
// First up, try a simple file
HSSFWorkbook b = new HSSFWorkbook();
assertEquals(0, b.getNumberOfSheets());
@ -124,22 +116,18 @@ public class TestHSSFWorkbook extends TestCase
assertEquals(3, b.getNumberOfSheets());
// Now try a problem one with drawing records in it
b = new HSSFWorkbook(
new FileInputStream(new File(filename,"SheetWithDrawing.xls"))
);
b = openSample("SheetWithDrawing.xls");
assertEquals(1, b.getNumberOfSheets());
b.cloneSheet(0);
assertEquals(2, b.getNumberOfSheets());
}
public void testReadWriteWithCharts() throws Exception {
public void testReadWriteWithCharts() {
HSSFWorkbook b;
HSSFSheet s;
// Single chart, two sheets
b = new HSSFWorkbook(
new FileInputStream(new File(filename,"44010-SingleChart.xls"))
);
b = openSample("44010-SingleChart.xls");
assertEquals(2, b.getNumberOfSheets());
s = b.getSheetAt(1);
assertEquals(0, s.getFirstRowNum());
@ -154,9 +142,7 @@ public class TestHSSFWorkbook extends TestCase
// We've now called getDrawingPatriarch() so
// everything will be all screwy
// So, start again
b = new HSSFWorkbook(
new FileInputStream(new File(filename,"44010-SingleChart.xls"))
);
b = openSample("44010-SingleChart.xls");
b = writeRead(b);
assertEquals(2, b.getNumberOfSheets());
@ -166,9 +152,7 @@ public class TestHSSFWorkbook extends TestCase
// Two charts, three sheets
b = new HSSFWorkbook(
new FileInputStream(new File(filename,"44010-TwoCharts.xls"))
);
b = openSample("44010-TwoCharts.xls");
assertEquals(3, b.getNumberOfSheets());
s = b.getSheetAt(1);
@ -188,9 +172,7 @@ public class TestHSSFWorkbook extends TestCase
// We've now called getDrawingPatriarch() so
// everything will be all screwy
// So, start again
b = new HSSFWorkbook(
new FileInputStream(new File(filename,"44010-TwoCharts.xls"))
);
b = openSample("44010-TwoCharts.xls");
b = writeRead(b);
assertEquals(3, b.getNumberOfSheets());
@ -203,11 +185,7 @@ public class TestHSSFWorkbook extends TestCase
assertEquals(0, s.getLastRowNum());
}
private HSSFWorkbook writeRead(HSSFWorkbook b) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.write(baos);
return new HSSFWorkbook(
new ByteArrayInputStream(baos.toByteArray())
);
private static HSSFWorkbook writeRead(HSSFWorkbook b) {
return HSSFTestDataSamples.writeOutAndReadBack(b);
}
}

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,16 +15,8 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.hssf.util.AreaReference;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.TempFile;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@ -33,6 +24,12 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.util.AreaReference;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.util.TempFile;
/**
*
@ -41,34 +38,13 @@ import java.io.IOException;
* @author Danny Mui (danny at muibros.com)
* @author Amol S. Deshmukh &lt; amol at ap ache dot org &gt;
*/
public class TestNamedRange
extends TestCase {
public final class TestNamedRange extends TestCase {
public TestNamedRange(String testName) {
super(testName);
private static HSSFWorkbook openSample(String sampleFileName) {
return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
}
public static void main(java.lang.String[] args) {
String filename = System.getProperty("HSSF.testdata.path");
// assume andy is running this in the debugger
if (filename == null)
{
if (args != null && args.length == 1) {
System.setProperty(
"HSSF.testdata.path",
args[0]);
} else {
System.err.println("Geesh, no HSSF.testdata.path system " +
"property, no command line arg with the path "+
"what do you expect me to do, guess where teh data " +
"files are? Sorry, I give up!");
}
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TestNamedRange.class);
}
@ -76,19 +52,7 @@ public class TestNamedRange
public void testNamedRange()
throws IOException
{
FileInputStream fis = null;
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Simple.xls";
fis = new FileInputStream(filename);
fs = new POIFSFileSystem(fis);
wb = new HSSFWorkbook(fs);
HSSFWorkbook wb = openSample("Simple.xls");
//Creating new Named Range
HSSFName newNamedRange = wb.createName();
@ -112,12 +76,11 @@ public class TestNamedRange
SanityChecker c = new SanityChecker();
c.checkHSSFWorkbook(wb);
File file = TempFile.createTempFile("testNamedRange",
".xls");
File file = TempFile.createTempFile("testNamedRange", ".xls");
FileOutputStream fileOut = new FileOutputStream(file);
wb.write(fileOut);
fis.close();
fileOut.close();
assertTrue("file exists",file.exists());
@ -136,20 +99,8 @@ public class TestNamedRange
* <p>
* Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=9632" target="_bug">#9632</a>
*/
public void testNamedRead() throws IOException
{
FileInputStream fis = null;
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/namedinput.xls";
fis = new FileInputStream(filename);
fs = new POIFSFileSystem(fis);
wb = new HSSFWorkbook(fs);
public void testNamedRead() {
HSSFWorkbook wb = openSample("namedinput.xls");
//Get index of the namedrange with the name = "NamedRangeName" , which was defined in input.xls as A1:D10
int NamedRangeIndex = wb.getNameIndex("NamedRangeName");
@ -161,15 +112,12 @@ public class TestNamedRange
//Getting its reference
String reference = namedRange1.getReference();
fis.close();
assertEquals(sheetName+"!$A$1:$D$10", reference);
HSSFName namedRange2 = wb.getNameAt(1);
assertEquals(sheetName+"!$D$17:$G$27", namedRange2.getReference());
assertEquals("SecondNamedRange", namedRange2.getNameName());
}
/**
@ -177,21 +125,8 @@ public class TestNamedRange
* <p>
* Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=16411" target="_bug">#16411</a>
*/
public void testNamedReadModify() throws IOException
{
FileInputStream fis = null;
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/namedinput.xls";
fis = new FileInputStream(filename);
fs = new POIFSFileSystem(fis);
wb = new HSSFWorkbook(fs);
public void testNamedReadModify() {
HSSFWorkbook wb = openSample("namedinput.xls");
HSSFName name = wb.getNameAt(0);
String sheetName = wb.getSheetName(0);
@ -203,7 +138,6 @@ public class TestNamedRange
name.setReference(newReference);
assertEquals(newReference, name.getReference());
}
/**
@ -378,35 +312,13 @@ public class TestNamedRange
/**
* Test to see if the print area can be retrieved from an excel created file
*/
public void testPrintAreaFileRead()
throws IOException
{
FileInputStream fis = null;
POIFSFileSystem fs = null;
HSSFWorkbook workbook = null;
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/SimpleWithPrintArea.xls";
try {
fis = new FileInputStream(filename);
fs = new POIFSFileSystem(fis);
workbook = new HSSFWorkbook(fs);
public void testPrintAreaFileRead() {
HSSFWorkbook workbook = openSample("SimpleWithPrintArea.xls");
String sheetName = workbook.getSheetName(0);
String reference = sheetName+"!$A$1:$C$5";
assertEquals(reference, workbook.getPrintArea(0));
} finally {
fis.close();
}
}
@ -621,6 +533,4 @@ public class TestNamedRange
String contents = c.getStringCellValue();
assertEquals("Contents of cell retrieved by its named reference", contents, cvalue);
}
}

View File

@ -17,36 +17,28 @@
package org.apache.poi.hssf.usermodel;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import junit.framework.TestCase;
public class TestOLE2Embeding extends TestCase {
public void testEmbeding() throws Exception {
String dirname = System.getProperty("HSSF.testdata.path");
String filename = dirname + "/ole2-embedding.xls";
import org.apache.poi.hssf.HSSFTestDataSamples;
File file = new File(filename);
FileInputStream in = new FileInputStream(file);
HSSFWorkbook workbook;
/**
*
*/
public final class TestOLE2Embeding extends TestCase {
public void testEmbeding() {
// This used to break, until bug #43116 was fixed
workbook = new HSSFWorkbook(in);
in.close();
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("ole2-embedding.xls");
// Check we can get at the Escher layer still
workbook.getAllPictures();
}
public void testEmbeddedObjects() throws Exception {
String dirname = System.getProperty("HSSF.testdata.path");
String filename = dirname + "/ole2-embedding.xls";
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("ole2-embedding.xls");
File file = new File(filename);
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
List objects = workbook.getAllEmbeddedObjects();
assertEquals("Wrong number of objects", 2, objects.size());
assertEquals("Wrong name for first object", "MBD06CAB431",
@ -56,6 +48,5 @@ public class TestOLE2Embeding extends TestCase {
((HSSFObjectData)
objects.get(1)).getDirectory().getName());
}
}

View File

@ -17,32 +17,31 @@
package org.apache.poi.hssf.usermodel;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.PropertySetFactory;
import java.io.FileInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import junit.framework.TestCase;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* Old-style setting of POIFS properties doesn't work with POI 3.0.2
*
* @author Yegor Kozlov
*/
public class TestPOIFSProperties extends TestCase{
protected String cwd = System.getProperty("HSSF.testdata.path");
protected String title = "Testing POIFS properties";
private static final String title = "Testing POIFS properties";
public void testFail() throws Exception {
FileInputStream is = new FileInputStream(new File(cwd, "Simple.xls"));
InputStream is = HSSFTestDataSamples.openSampleFileStream("Simple.xls");
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
HSSFWorkbook wb = new HSSFWorkbook(fs);
@ -62,19 +61,13 @@ public class TestPOIFSProperties extends TestCase{
POIFSFileSystem fs2 = new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
SummaryInformation summary2 = (SummaryInformation)PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
try {
//failing assertion
assertEquals(title, summary2.getTitle());
} catch (AssertionError e){
assertTrue(true);
}
}
public void testOK() throws Exception {
FileInputStream is = new FileInputStream(new File(cwd, "Simple.xls"));
InputStream is = HSSFTestDataSamples.openSampleFileStream("Simple.xls");
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
//set POIFS properties before constructing HSSFWorkbook
SummaryInformation summary1 = (SummaryInformation)PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,46 +15,28 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import java.util.GregorianCalendar;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.EOFRecord;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.FileInputStream;
import java.util.GregorianCalendar;
import java.util.List;
/**
* @author Glen Stampoultzis (glens at apache.org)
*/
public class TestReadWriteChart
extends TestCase
{
public TestReadWriteChart(String s)
{
super(s);
}
public final class TestReadWriteChart extends TestCase {
/**
* In the presence of a chart we need to make sure BOF/EOF records still exist.
*/
public void testBOFandEOFRecords()
throws Exception
{
//System.out.println("made it in testBOFandEOF");
String path = System.getProperty("HSSF.testdata.path");
String filename = path + "/SimpleChart.xls";
//System.out.println("path is "+path);
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(filename));
//System.out.println("opened file");
HSSFWorkbook workbook = new HSSFWorkbook(fs);
public void testBOFandEOFRecords() {
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("SimpleChart.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow firstRow = sheet.getRow(0);
HSSFCell firstCell = firstRow.getCell(( short ) 0);
@ -77,29 +58,7 @@ public class TestReadWriteChart
assertTrue(records.get(records.size() - 1) instanceof EOFRecord);
}
public static void main(String [] args)
{
String filename = System.getProperty("HSSF.testdata.path");
// assume andy is running this in the debugger
if (filename == null)
{
if (args != null && args[0].length() == 1) {
System.setProperty(
"HSSF.testdata.path",
args[0]);
} else {
System.err.println("Geesh, no HSSF.testdata.path system " +
"property, no command line arg with the path "+
"what do you expect me to do, guess where teh data " +
"files are? Sorry, I give up!");
}
}
System.out
.println("Testing org.apache.poi.hssf.usermodel.TestReadWriteChart");
public static void main(String [] args) {
junit.textui.TestRunner.run(TestReadWriteChart.class);
}
}

View File

@ -14,40 +14,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.poi.hssf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import junit.framework.TestCase;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.HSSFTestDataSamples;
/**
* Tests for how HSSFWorkbook behaves with XLS files
* with a WORKBOOK directory entry (instead of the more
* usual, Workbook)
*/
public class TestSheetHiding extends TestCase {
private String dirPath;
private String xlsHidden = "TwoSheetsOneHidden.xls";
private String xlsShown = "TwoSheetsNoneHidden.xls";
public final class TestSheetHiding extends TestCase {
private HSSFWorkbook wbH;
private HSSFWorkbook wbU;
protected void setUp() throws Exception {
super.setUp();
dirPath = System.getProperty("HSSF.testdata.path");
FileInputStream isH = new FileInputStream(dirPath + "/" + xlsHidden);
POIFSFileSystem fsH = new POIFSFileSystem(isH);
FileInputStream isU = new FileInputStream(dirPath + "/" + xlsShown);
POIFSFileSystem fsU = new POIFSFileSystem(isU);
wbH = new HSSFWorkbook(fsH);
wbU = new HSSFWorkbook(fsU);
protected void setUp() {
wbH = HSSFTestDataSamples.openSampleWorkbook("TwoSheetsOneHidden.xls");
wbU = HSSFTestDataSamples.openSampleWorkbook("TwoSheetsNoneHidden.xls");
}
/**

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,19 +15,14 @@
limitations under the License.
==================================================================== */
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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
/**
* Tests row shifting capabilities.
@ -37,16 +31,7 @@ import java.io.FileOutputStream;
* @author Shawn Laubach (slaubach at apache dot com)
* @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
*/
public class TestSheetShiftRows extends TestCase {
/**
* Constructor for TestSheetShiftRows.
* @param arg0
*/
public TestSheetShiftRows(String arg0) {
super(arg0);
}
public final class TestSheetShiftRows extends TestCase {
/**
* Tests the shiftRows function. Does three different shifts.
@ -59,25 +44,16 @@ public class TestSheetShiftRows extends TestCase {
public void testShiftRows() throws Exception
{
// Read initial file in
String filename = System.getProperty( "HSSF.testdata.path" );
filename = filename + "/SimpleMultiCell.xls";
FileInputStream fin = new FileInputStream( filename );
HSSFWorkbook wb = new HSSFWorkbook( fin );
fin.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleMultiCell.xls");
HSSFSheet s = wb.getSheetAt( 0 );
// Shift the second row down 1 and write to temp file
s.shiftRows( 1, 1, 1 );
File tempFile = TempFile.createTempFile( "shift", "test.xls" );
FileOutputStream fout = new FileOutputStream( tempFile );
wb.write( fout );
fout.close();
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
// Read from temp file and check the number of cells in each
// row (in original file each row was unique)
fin = new FileInputStream( tempFile );
wb = new HSSFWorkbook( fin );
fin.close();
s = wb.getSheetAt( 0 );
assertEquals( s.getRow( 0 ).getPhysicalNumberOfCells(), 1 );
@ -89,15 +65,9 @@ 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 = TempFile.createTempFile( "shift", "test.xls" );
fout = new FileOutputStream( tempFile );
wb.write( fout );
fout.close();
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
// Read and ensure things are where they should be
fin = new FileInputStream( tempFile );
wb = new HSSFWorkbook( fin );
fin.close();
s = wb.getSheetAt( 0 );
assertTrue( s.getRow( 0 ) == null || s.getRow( 0 ).getPhysicalNumberOfCells() == 0 );
assertTrue( s.getRow( 1 ) == null || s.getRow( 1 ).getPhysicalNumberOfCells() == 0 );
@ -107,22 +77,12 @@ public class TestSheetShiftRows extends TestCase {
assertEquals( s.getRow( 5 ).getPhysicalNumberOfCells(), 2 );
// Read the first file again
fin = new FileInputStream( filename );
wb = new HSSFWorkbook( fin );
fin.close();
wb = HSSFTestDataSamples.openSampleWorkbook("SimpleMultiCell.xls");
s = wb.getSheetAt( 0 );
// Shift rows 3 and 4 up and write to temp file
s.shiftRows( 2, 3, -2 );
tempFile = TempFile.createTempFile( "shift", "test.xls" );
fout = new FileOutputStream( tempFile );
wb.write( fout );
fout.close();
// Read file and test
fin = new FileInputStream( tempFile );
wb = new HSSFWorkbook( fin );
fin.close();
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
s = wb.getSheetAt( 0 );
assertEquals( s.getRow( 0 ).getPhysicalNumberOfCells(), 3 );
assertEquals( s.getRow( 1 ).getPhysicalNumberOfCells(), 4 );
@ -175,11 +135,7 @@ public class TestSheetShiftRows extends TestCase {
public void testShiftWithComments() throws Exception {
String filename = System.getProperty( "HSSF.testdata.path" );
filename = filename + "/comments.xls";
FileInputStream fin = new FileInputStream( filename );
HSSFWorkbook wb = new HSSFWorkbook( fin );
fin.close();
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("comments.xls");
HSSFSheet sheet = wb.getSheet("Sheet1");
assertEquals(3, sheet.getLastRowNum());
@ -241,12 +197,8 @@ public class TestSheetShiftRows extends TestCase {
/**
* See bug #34023
*/
public void testShiftWithFormulas() throws Exception {
String filename = System.getProperty( "HSSF.testdata.path" );
filename = filename + "/ForShifting.xls";
FileInputStream fin = new FileInputStream( filename );
HSSFWorkbook wb = new HSSFWorkbook( fin );
fin.close();
public void testShiftWithFormulas() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ForShifting.xls");
HSSFSheet sheet = wb.getSheet("Sheet1");
assertEquals(19, sheet.getLastRowNum());

View File

@ -14,43 +14,36 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.RecordFormatException;
/**
* @author aviks
*
* This testcase contains tests for bugs that are yet to be fixed.
* Therefore, the standard ant test target does not run these tests.
* Run this testcase with the single-test target.
* The names of the tests usually correspond to the Bugzilla id's
* PLEASE MOVE tests from this class to TestBugs once the bugs are fixed,
* so that they are then run automatically.
* This testcase contains tests for bugs that are yet to be fixed. Therefore,
* the standard ant test target does not run these tests. Run this testcase with
* the single-test target. The names of the tests usually correspond to the
* Bugzilla id's PLEASE MOVE tests from this class to TestBugs once the bugs are
* fixed, so that they are then run automatically.
*/
public class TestUnfixedBugs extends TestCase {
public final class TestUnfixedBugs extends TestCase {
public TestUnfixedBugs(String arg0) {
super(arg0);
}
protected String cwd = System.getProperty("HSSF.testdata.path");
public void test43493() throws Exception {
// Has crazy corrup subrecords on
public void test43493() {
// Has crazy corrupt sub-records on
// a EmbeddedObjectRefSubRecord
File f = new File(cwd, "43493.xls");
HSSFWorkbook wb = new HSSFWorkbook(
new FileInputStream(f)
);
try {
HSSFTestDataSamples.openSampleWorkbook("43493.xls");
} catch (RecordFormatException e) {
if (e.getCause().getCause() instanceof ArrayIndexOutOfBoundsException) {
throw new AssertionFailedError("Identified bug 43493");
}
throw e;
}
}
}

View File

@ -14,37 +14,33 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.poi.hssf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.InputStream;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* Tests for how HSSFWorkbook behaves with XLS files
* with a WORKBOOK directory entry (instead of the more
* usual, Workbook)
*/
public class TestUppercaseWorkbook extends TestCase {
private String dirPath;
public final class TestUppercaseWorkbook extends TestCase {
private String xlsA = "WORKBOOK_in_capitals.xls";
protected void setUp() throws Exception {
super.setUp();
dirPath = System.getProperty("HSSF.testdata.path");
}
/**
* Test that we can open a file with WORKBOOK
*/
public void testOpen() throws Exception {
FileInputStream is = new FileInputStream(dirPath + "/" + xlsA);
InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
POIFSFileSystem fs = new POIFSFileSystem(is);
@ -68,7 +64,7 @@ public class TestUppercaseWorkbook extends TestCase {
* Test that when we write out, we go back to the correct case
*/
public void testWrite() throws Exception {
FileInputStream is = new FileInputStream(dirPath + "/" + xlsA);
InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
POIFSFileSystem fs = new POIFSFileSystem(is);
// Open the workbook, not preserving nodes
@ -96,7 +92,7 @@ public class TestUppercaseWorkbook extends TestCase {
* correct case
*/
public void testWritePreserve() throws Exception {
FileInputStream is = new FileInputStream(dirPath + "/" + xlsA);
InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
POIFSFileSystem fs = new POIFSFileSystem(is);
// Open the workbook, not preserving nodes

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,10 +15,17 @@
limitations under the License.
==================================================================== */
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.HSSFTestDataSamples;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.BackupRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
@ -29,12 +35,6 @@ 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
*
@ -42,10 +42,7 @@ import java.util.Iterator;
* @author Greg Merrill
* @author Siggi Cherem
*/
public class TestWorkbook
extends TestCase
{
public class TestWorkbook extends TestCase {
private static final String LAST_NAME_KEY = "lastName";
private static final String FIRST_NAME_KEY = "firstName";
private static final String SSN_KEY = "ssn";
@ -58,15 +55,9 @@ public class TestWorkbook
private static final String SSN_VALUE = "555555555";
private SanityChecker sanityChecker = new SanityChecker();
/**
* Constructor TestWorkbook
*
* @param name
*/
public TestWorkbook(String name)
{
super(name);
private static HSSFWorkbook openSample(String sampleFileName) {
return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
}
/**
@ -178,21 +169,12 @@ public class TestWorkbook
*
*/
public void testReadSimple()
throws IOException
{
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Simple.xls";
FileInputStream stream = new FileInputStream(filename);
POIFSFileSystem fs = new POIFSFileSystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
public void testReadSimple() {
HSSFWorkbook workbook = openSample("Simple.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
assertEquals(REPLACE_ME,
sheet.getRow(( short ) 0).getCell(( short ) 0)
.getStringCellValue());
stream.close();
HSSFCell cell = sheet.getRow(0).getCell(0);
assertEquals(REPLACE_ME, cell .getRichStringCellValue().getString());
}
/**
@ -204,24 +186,15 @@ public class TestWorkbook
*
*/
public void testReadSimpleWithDataFormat()
throws IOException
{
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/SimpleWithDataFormat.xls";
FileInputStream stream = new FileInputStream(filename);
POIFSFileSystem fs = new POIFSFileSystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
public void testReadSimpleWithDataFormat() {
HSSFWorkbook workbook = openSample("SimpleWithDataFormat.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFDataFormat format = workbook.createDataFormat();
HSSFCell cell =
sheet.getRow(( short ) 0).getCell(( short ) 0);
HSSFCell cell = sheet.getRow(0).getCell(0);
assertEquals(1.25,cell.getNumericCellValue(), 1e-10);
assertEquals(format.getFormat(cell.getCellStyle().getDataFormat()), "0.0");
stream.close();
}
/**
@ -283,30 +256,14 @@ public class TestWorkbook
*
*/
public void testReadEmployeeSimple()
throws IOException
{
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Employee.xls";
FileInputStream stream = new FileInputStream(filename);
POIFSFileSystem fs = new POIFSFileSystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
public void testReadEmployeeSimple() {
HSSFWorkbook workbook = openSample("Employee.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
assertEquals(EMPLOYEE_INFORMATION,
sheet.getRow(1).getCell(( short ) 1)
.getStringCellValue());
assertEquals(LAST_NAME_KEY,
sheet.getRow(3).getCell(( short ) 2)
.getStringCellValue());
assertEquals(FIRST_NAME_KEY,
sheet.getRow(4).getCell(( short ) 2)
.getStringCellValue());
assertEquals(SSN_KEY,
sheet.getRow(5).getCell(( short ) 2)
.getStringCellValue());
stream.close();
assertEquals(EMPLOYEE_INFORMATION, sheet.getRow(1).getCell(1).getStringCellValue());
assertEquals(LAST_NAME_KEY, sheet.getRow(3).getCell(2).getStringCellValue());
assertEquals(FIRST_NAME_KEY, sheet.getRow(4).getCell(2).getStringCellValue());
assertEquals(SSN_KEY, sheet.getRow(5).getCell(2).getStringCellValue());
}
/**
@ -322,33 +279,17 @@ public class TestWorkbook
*
*/
public void testModifySimple()
throws IOException
{
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Simple.xls";
FileInputStream instream = new FileInputStream(filename);
POIFSFileSystem fsin = new POIFSFileSystem(instream);
HSSFWorkbook workbook = new HSSFWorkbook(fsin);
public void testModifySimple() {
HSSFWorkbook workbook = openSample("Simple.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCell cell =
sheet.getRow(( short ) 0).getCell(( short ) 0);
HSSFCell cell = sheet.getRow(0).getCell(0);
cell.setCellValue(REPLACED);
File destination = TempFile.createTempFile("SimpleResult",
".xls");
FileOutputStream outstream = new FileOutputStream(destination);
cell.setCellValue(new HSSFRichTextString(REPLACED));
workbook.write(outstream);
instream.close();
outstream.close();
instream = new FileInputStream(destination);
workbook = new HSSFWorkbook(new POIFSFileSystem(instream));
workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook);
sheet = workbook.getSheetAt(0);
cell = sheet.getRow(( short ) 0).getCell(( short ) 0);
assertEquals(REPLACED, cell.getStringCellValue());
instream.close();
cell = sheet.getRow(0).getCell(0);
assertEquals(REPLACED, cell.getRichStringCellValue().getString());
}
/**
@ -364,42 +305,26 @@ public class TestWorkbook
* or is incorrect. <P>
*
*/
public void testModifySimpleWithSkip()
throws IOException
{
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/SimpleWithSkip.xls";
FileInputStream instream = new FileInputStream(filename);
POIFSFileSystem fsin = new POIFSFileSystem(instream);
HSSFWorkbook workbook = new HSSFWorkbook(fsin);
public void testModifySimpleWithSkip() {
HSSFWorkbook workbook = openSample("SimpleWithSkip.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCell cell =
sheet.getRow(( short ) 0).getCell(( short ) 1);
HSSFCell cell = sheet.getRow(0).getCell(1);
cell.setCellValue(REPLACED);
cell = sheet.getRow(( short ) 1).getCell(( short ) 0);
cell = sheet.getRow(1).getCell(0);
cell.setCellValue(REPLACED);
File destination =
TempFile.createTempFile("SimpleWithSkipResult", ".xls");
FileOutputStream outstream = new FileOutputStream(destination);
workbook.write(outstream);
instream.close();
outstream.close();
instream = new FileInputStream(destination);
workbook = new HSSFWorkbook(new POIFSFileSystem(instream));
workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook);
sheet = workbook.getSheetAt(0);
cell = sheet.getRow(( short ) 0).getCell(( short ) 1);
cell = sheet.getRow(0).getCell(1);
assertEquals(REPLACED, cell.getStringCellValue());
cell = sheet.getRow(( short ) 0).getCell(( short ) 0);
cell = sheet.getRow(0).getCell(0);
assertEquals(DO_NOT_REPLACE, cell.getStringCellValue());
cell = sheet.getRow(( short ) 1).getCell(( short ) 0);
cell = sheet.getRow(1).getCell(0);
assertEquals(REPLACED, cell.getStringCellValue());
cell = sheet.getRow(( short ) 1).getCell(( short ) 1);
cell = sheet.getRow(1).getCell(1);
assertEquals(DO_NOT_REPLACE, cell.getStringCellValue());
instream.close();
}
/**
@ -415,41 +340,26 @@ public class TestWorkbook
* is incorrect or has not been replaced. <P>
*
*/
public void testModifySimpleWithStyling()
throws IOException
{
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/SimpleWithStyling.xls";
FileInputStream instream = new FileInputStream(filename);
POIFSFileSystem fsin = new POIFSFileSystem(instream);
HSSFWorkbook workbook = new HSSFWorkbook(fsin);
public void testModifySimpleWithStyling() {
HSSFWorkbook workbook = openSample("SimpleWithStyling.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
for (int k = 0; k < 4; k++)
{
HSSFCell cell = sheet.getRow(( short ) k).getCell(( short ) 0);
cell.setCellValue(REPLACED);
cell.setCellValue(new HSSFRichTextString(REPLACED));
}
File destination =
TempFile.createTempFile("SimpleWithStylingResult", ".xls");
FileOutputStream outstream = new FileOutputStream(destination);
workbook.write(outstream);
instream.close();
outstream.close();
instream = new FileInputStream(destination);
workbook = new HSSFWorkbook(new POIFSFileSystem(instream));
workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook);
sheet = workbook.getSheetAt(0);
for (int k = 0; k < 4; k++)
{
HSSFCell cell = sheet.getRow(( short ) k).getCell(( short ) 0);
assertEquals(REPLACED, cell.getStringCellValue());
assertEquals(REPLACED, cell.getRichStringCellValue().getString());
}
instream.close();
}
/**
@ -465,48 +375,23 @@ public class TestWorkbook
* is incorrect or has not been replaced. <P>
*
*/
public void testModifyEmployee()
throws IOException
{
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Employee.xls";
FileInputStream instream = new FileInputStream(filename);
POIFSFileSystem fsin = new POIFSFileSystem(instream);
HSSFWorkbook workbook = new HSSFWorkbook(fsin);
public void testModifyEmployee() {
HSSFWorkbook workbook = openSample("Employee.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCell cell =
sheet.getRow(( short ) 3).getCell(( short ) 2);
HSSFCell cell = sheet.getRow(3).getCell(2);
cell.setCellValue(LAST_NAME_VALUE);
cell = sheet.getRow(( short ) 4).getCell(( short ) 2);
cell = sheet.getRow(4).getCell(2);
cell.setCellValue(FIRST_NAME_VALUE);
cell = sheet.getRow(( short ) 5).getCell(( short ) 2);
cell = sheet.getRow(5).getCell(2);
cell.setCellValue(SSN_VALUE);
File destination = TempFile.createTempFile("EmployeeResult",
".xls");
FileOutputStream outstream = new FileOutputStream(destination);
workbook.write(outstream);
instream.close();
outstream.close();
instream = new FileInputStream(destination);
workbook = new HSSFWorkbook(new POIFSFileSystem(instream));
workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook);
sheet = workbook.getSheetAt(0);
assertEquals(EMPLOYEE_INFORMATION,
sheet.getRow(1).getCell(( short ) 1)
.getStringCellValue());
assertEquals(LAST_NAME_VALUE,
sheet.getRow(3).getCell(( short ) 2)
.getStringCellValue());
assertEquals(FIRST_NAME_VALUE,
sheet.getRow(4).getCell(( short ) 2)
.getStringCellValue());
assertEquals(SSN_VALUE,
sheet.getRow(5).getCell(( short ) 2)
.getStringCellValue());
instream.close();
assertEquals(EMPLOYEE_INFORMATION, sheet.getRow(1).getCell(1).getStringCellValue());
assertEquals(LAST_NAME_VALUE, sheet.getRow(3).getCell(2).getStringCellValue());
assertEquals(FIRST_NAME_VALUE, sheet.getRow(4).getCell(2).getStringCellValue());
assertEquals(SSN_VALUE, sheet.getRow(5).getCell(2).getStringCellValue());
}
/**
@ -517,21 +402,10 @@ public class TestWorkbook
* FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly indentifies the cell<P>
*
*/
public void testReadSheetWithRK()
throws IOException
{
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/rk.xls";
// a.xls has a value on position (0,0)
FileInputStream in = new FileInputStream(filename);
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook h = new HSSFWorkbook(fs);
public void testReadSheetWithRK() {
HSSFWorkbook h = openSample("rk.xls");
HSSFSheet s = h.getSheetAt(0);
HSSFRow r = s.getRow(0);
HSSFCell c = r.getCell(( short ) 0);
HSSFCell c = s.getRow(0).getCell(0);
int a = c.getCellType();
assertEquals(a, c.CELL_TYPE_NUMERIC);
@ -564,7 +438,6 @@ public class TestWorkbook
{
r = s.createRow(rownum);
// r.setRowNum(( short ) rownum);
for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)
{
c = r.createCell(cellnum);
@ -572,7 +445,7 @@ public class TestWorkbook
+ ((( double ) rownum / 1000)
+ (( double ) cellnum / 10000)));
c = r.createCell(( short ) (cellnum + 1));
c.setCellValue("TEST");
c.setCellValue(new HSSFRichTextString("TEST"));
}
}
s.addMergedRegion(new Region(( short ) 0, ( short ) 0, ( short ) 10,
@ -632,7 +505,7 @@ public class TestWorkbook
HSSFRow row = sheet.createRow(( short ) 2);
HSSFCell cell = row.createCell(( short ) 1);
cell.setCellValue("Class");
cell.setCellValue(new HSSFRichTextString("Class"));
cell = row.createCell(( short ) 2);
// workbook.write(new FileOutputStream("/a2.xls"));
@ -695,12 +568,11 @@ public class TestWorkbook
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Test Print Titles");
String sheetName = workbook.getSheetName(0);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short)1);
cell.setCellValue("hi");
cell.setCellValue(new HSSFRichTextString("hi"));
workbook.setRepeatingRowsAndColumns(0, 0, 1, 0, 0);
@ -712,24 +584,11 @@ public class TestWorkbook
fileOut.close();
assertTrue("file exists",file.exists());
}
public static void main(String [] ignored_args)
{
String filename = System.getProperty("HSSF.testdata.path");
// assume this is relative to basedir
if (filename == null)
{
System.setProperty(
"HSSF.testdata.path",
"src/testcases/org/apache/poi/hssf/data");
}
System.out
.println("Testing org.apache.poi.hssf.usermodel.HSSFWorkbook");
junit.textui.TestRunner.run(TestWorkbook.class);
}
}

View File

@ -15,23 +15,28 @@
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.util;
import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.formula.MemFuncPtg;
import org.apache.poi.hssf.record.formula.Area3DPtg;
import org.apache.poi.hssf.record.formula.UnionPtg;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.formula.Area3DPtg;
import org.apache.poi.hssf.record.formula.MemFuncPtg;
import org.apache.poi.hssf.record.formula.UnionPtg;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFName;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
*/
public final class TestAreaReference extends TestCase {
public void testAreaRef1() {
@ -99,7 +104,7 @@ public final class TestAreaReference extends TestCase {
TestCellReference.confirmCell(allCells[2], "Tabelle1", 6, 1, true, true, "Tabelle1!$B$7");
}
private static class HSSFWB extends HSSFWorkbook {
private static final class HSSFWB extends HSSFWorkbook {
public HSSFWB(InputStream in) throws IOException {
super(in);
}
@ -182,12 +187,9 @@ public final class TestAreaReference extends TestCase {
}
public void testDiscontinousReference() throws Exception {
String filename = System.getProperty( "HSSF.testdata.path" );
filename = filename + "/44167.xls";
FileInputStream fin = new FileInputStream( filename );
HSSFWB wb = new HSSFWB( fin );
InputStream is = HSSFTestDataSamples.openSampleFileStream("44167.xls");
HSSFWB wb = new HSSFWB(is);
Workbook workbook = wb.getWorkbook();
fin.close();
assertEquals(1, wb.getNumberOfNames());
String sheetName = "Tabelle1";
@ -269,5 +271,4 @@ public final class TestAreaReference extends TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run(TestAreaReference.class);
}
}

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -16,57 +15,55 @@
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.filesystem;
import junit.framework.TestCase;
import java.io.*;
import org.apache.poi.hssf.HSSFTestDataSamples;
/**
* Class to test that POIFS complains when given an Office 2007 XML document
*
* @author Marc Johnson
*/
public class TestOffice2007XMLException extends TestCase {
public class TestOffice2007XMLException extends TestCase
{
public String dirname;
public void setUp() {
dirname = System.getProperty("HSSF.testdata.path");
private static final InputStream openSampleStream(String sampleFileName) {
return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
}
public void testXMLException() throws IOException
{
FileInputStream in = new FileInputStream(dirname + "/sample.xlsx");
InputStream in = openSampleStream("sample.xlsx");
try {
new POIFSFileSystem(in);
fail();
fail("expected exception was not thrown");
} catch(OfficeXmlFileException e) {
// Good
// expected during successful test
assertTrue(e.getMessage().indexOf("POI only supports OLE2 Office documents") > 0);
}
}
public void testDetectAsPOIFS() throws IOException {
InputStream in;
public void testDetectAsPOIFS() {
// ooxml file isn't
in = new PushbackInputStream(
new FileInputStream(dirname + "/SampleSS.xlsx"), 10
);
assertFalse(POIFSFileSystem.hasPOIFSHeader(in));
confirmIsPOIFS("SampleSS.xlsx", false);
// xls file is
in = new PushbackInputStream(
new FileInputStream(dirname + "/SampleSS.xls"), 10
);
assertTrue(POIFSFileSystem.hasPOIFSHeader(in));
confirmIsPOIFS("SampleSS.xls", true);
// text file isn't
in = new PushbackInputStream(
new FileInputStream(dirname + "/SampleSS.txt"), 10
);
assertFalse(POIFSFileSystem.hasPOIFSHeader(in));
confirmIsPOIFS("SampleSS.txt", false);
}
private void confirmIsPOIFS(String sampleFileName, boolean expectedResult) {
InputStream in = new PushbackInputStream(openSampleStream(sampleFileName), 10);
boolean actualResult;
try {
actualResult = POIFSFileSystem.hasPOIFSHeader(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
assertEquals(expectedResult, actualResult);
}
}

View File

@ -17,14 +17,13 @@
package org.apache.poi.poifs.filesystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
/**
* Tests for POIFSFileSystem
*
@ -32,10 +31,6 @@ import junit.framework.TestCase;
*/
public final class TestPOIFSFileSystem extends TestCase {
public TestPOIFSFileSystem(String testName) {
super(testName);
}
/**
* Mock exception used to ensure correct error handling
*/
@ -121,16 +116,7 @@ public final class TestPOIFSFileSystem extends TestCase {
}
private static InputStream openSampleStream(String sampleName) {
String dataDirName = System.getProperty("HSSF.testdata.path");
if(dataDirName == null) {
throw new RuntimeException("Missing system property '" + "HSSF.testdata.path" + "'");
}
File f = new File(dataDirName + "/" + sampleName);
try {
return new FileInputStream(f);
} catch (FileNotFoundException e) {
throw new RuntimeException("Sample file '" + f.getAbsolutePath() + "' not found");
}
private static InputStream openSampleStream(String sampleFileName) {
return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
}
}

View File

@ -18,12 +18,21 @@
package org.apache.poi.poifs.filesystem;
import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import junit.framework.ComparisonFailure;
import junit.framework.TestCase;
import java.io.*;
import java.util.*;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.property.DirectoryProperty;
import org.apache.poi.poifs.property.Property;
@ -36,45 +45,46 @@ import org.apache.poi.poifs.property.Property;
*
* @author Yegor Kozlov
*/
public class TestPropertySorter extends TestCase {
public final class TestPropertySorter extends TestCase {
//the correct order of entries in the test file
protected static final String[] _entries = {
private static final String[] _entries = {
"dir", "JML", "UTIL", "Loader", "Sheet1", "Sheet2", "Sheet3",
"__SRP_0", "__SRP_1", "__SRP_2", "__SRP_3", "__SRP_4", "__SRP_5",
"ThisWorkbook", "_VBA_PROJECT",
};
protected File testFile;
public void setUp(){
String home = System.getProperty("HSSF.testdata.path");
testFile = new File(home + "/39234.xls");
private static POIFSFileSystem openSampleFS() {
InputStream is = HSSFTestDataSamples.openSampleFileStream("39234.xls");
try {
return new POIFSFileSystem(is);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Test sorting of properties in <code>DirectoryProperty</code>
*/
public void testSortProperties() throws IOException {
InputStream is = new FileInputStream(testFile);
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
POIFSFileSystem fs = openSampleFS();
Property[] props = getVBAProperties(fs);
assertEquals(_entries.length, props.length);
// (1). See that there is a problem with the old case-sensitive property comparartor
Arrays.sort(props, new CaseSensitivePropertyComparator());
// (1). See that there is a problem with the old case-sensitive property comparator
Arrays.sort(props, OldCaseSensitivePropertyComparator);
try {
for (int i = 0; i < props.length; i++) {
assertEquals(_entries[i], props[i].getName());
}
fail("case-sensitive property comparator returns properties in wrong order");
fail("expected old case-sensitive property comparator to return properties in wrong order");
} catch (ComparisonFailure e){
; // as expected
// expected during successful test
assertNotNull(e.getMessage());
}
// (2) Verify that the fixed proeprty comparator works right
// (2) Verify that the fixed property comparator works right
Arrays.sort(props, new DirectoryProperty.PropertyComparator());
for (int i = 0; i < props.length; i++) {
assertEquals(_entries[i], props[i].getName());
@ -85,14 +95,12 @@ public class TestPropertySorter extends TestCase {
* Serialize file system and verify that the order of properties is the same as in the original file.
*/
public void testSerialization() throws IOException {
InputStream is = new FileInputStream(testFile);
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
POIFSFileSystem fs = openSampleFS();
ByteArrayOutputStream out = new ByteArrayOutputStream();
fs.writeFilesystem(out);
out.close();
is = new ByteArrayInputStream(out.toByteArray());
InputStream is = new ByteArrayInputStream(out.toByteArray());
fs = new POIFSFileSystem(is);
is.close();
Property[] props = getVBAProperties(fs);
@ -128,26 +136,17 @@ public class TestPropertySorter extends TestCase {
/**
* Old version of case-sensitive PropertyComparator to demonstrate the problem
*/
private class CaseSensitivePropertyComparator implements Comparator
{
private static final Comparator OldCaseSensitivePropertyComparator = new Comparator() {
public boolean equals(Object o)
{
return this == o;
}
public int compare(Object o1, Object o2)
{
public int compare(Object o1, Object o2) {
String name1 = (( Property ) o1).getName();
String name2 = (( Property ) o2).getName();
int result = name1.length() - name2.length();
if (result == 0)
{
if (result == 0) {
result = name1.compareTo(name2);
}
return result;
}
}
};
}