001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.poi.hssf.usermodel;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023:
024: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * Tests for how HSSFWorkbook behaves with XLS files
030: * with a WORKBOOK directory entry (instead of the more
031: * usual, Workbook)
032: */
033: public class TestUppercaseWorkbook extends TestCase {
034: private String dirPath;
035: private String xlsA = "WORKBOOK_in_capitals.xls";
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039:
040: dirPath = System.getProperty("HSSF.testdata.path");
041: }
042:
043: /**
044: * Test that we can open a file with WORKBOOK
045: */
046: public void testOpen() throws Exception {
047: FileInputStream is = new FileInputStream(dirPath + "/" + xlsA);
048:
049: POIFSFileSystem fs = new POIFSFileSystem(is);
050:
051: // Ensure that we have a WORKBOOK entry
052: fs.getRoot().getEntry("WORKBOOK");
053: // And a summary
054: fs.getRoot().getEntry("\005SummaryInformation");
055: assertTrue(true);
056:
057: // But not a Workbook one
058: try {
059: fs.getRoot().getEntry("Workbook");
060: fail();
061: } catch (FileNotFoundException e) {
062: }
063:
064: // Try to open the workbook
065: HSSFWorkbook wb = new HSSFWorkbook(fs);
066: }
067:
068: /**
069: * Test that when we write out, we go back to the correct case
070: */
071: public void testWrite() throws Exception {
072: FileInputStream is = new FileInputStream(dirPath + "/" + xlsA);
073: POIFSFileSystem fs = new POIFSFileSystem(is);
074:
075: // Open the workbook, not preserving nodes
076: HSSFWorkbook wb = new HSSFWorkbook(fs);
077: ByteArrayOutputStream out = new ByteArrayOutputStream();
078: wb.write(out);
079:
080: // Check now
081: ByteArrayInputStream in = new ByteArrayInputStream(out
082: .toByteArray());
083: POIFSFileSystem fs2 = new POIFSFileSystem(in);
084:
085: // Check that we have the new entries
086: fs2.getRoot().getEntry("Workbook");
087: try {
088: fs2.getRoot().getEntry("WORKBOOK");
089: fail();
090: } catch (FileNotFoundException e) {
091: }
092:
093: // And it can be opened
094: HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
095: }
096:
097: /**
098: * Test that when we write out preserving nodes, we go back to the
099: * correct case
100: */
101: public void testWritePreserve() throws Exception {
102: FileInputStream is = new FileInputStream(dirPath + "/" + xlsA);
103: POIFSFileSystem fs = new POIFSFileSystem(is);
104:
105: // Open the workbook, not preserving nodes
106: HSSFWorkbook wb = new HSSFWorkbook(fs, true);
107:
108: ByteArrayOutputStream out = new ByteArrayOutputStream();
109: wb.write(out);
110:
111: // Check now
112: ByteArrayInputStream in = new ByteArrayInputStream(out
113: .toByteArray());
114: POIFSFileSystem fs2 = new POIFSFileSystem(in);
115:
116: // Check that we have the new entries
117: fs2.getRoot().getEntry("Workbook");
118: try {
119: fs2.getRoot().getEntry("WORKBOOK");
120: fail();
121: } catch (FileNotFoundException e) {
122: }
123:
124: // As we preserved, should also have a few other streams
125: fs2.getRoot().getEntry("\005SummaryInformation");
126:
127: // And it can be opened
128: HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
129: }
130: }
|