unfinished release guide. It would be nice to have a html version.
[poi.git] / src / scratchpad / ooxml-testcases / org / apache / poi / hssf / TestHSSFXML.java
blob97453265c447b8aa638b61084204c8052d32cfbd
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
17 package org.apache.poi.hssf;
19 import java.io.File;
21 import org.apache.poi.hssf.model.SharedStringsTable;
22 import org.apache.poi.hxf.HXFDocument;
23 import org.openxml4j.opc.Package;
24 import org.openxml4j.opc.PackagePart;
25 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
27 import junit.framework.TestCase;
29 public class TestHSSFXML extends TestCase {
30 /**
31 * Uses the old style schemas.microsoft.com schema uri
33 private File sampleFileBeta;
34 /**
35 * Uses the new style schemas.openxmlformats.org schema uri
37 private File sampleFile;
39 protected void setUp() throws Exception {
40 super.setUp();
42 sampleFile = new File(
43 System.getProperty("HSSF.testdata.path") +
44 File.separator + "sample.xlsx"
46 sampleFileBeta = new File(
47 System.getProperty("HSSF.testdata.path") +
48 File.separator + "sample-beta.xlsx"
52 public void testContainsMainContentType() throws Exception {
53 Package pack = HXFDocument.openPackage(sampleFile);
55 boolean found = false;
56 for(PackagePart part : pack.getParts()) {
57 if(part.getContentType().equals(HSSFXML.MAIN_CONTENT_TYPE)) {
58 found = true;
60 System.out.println(part);
62 assertTrue(found);
65 public void testOpen() throws Exception {
66 HXFDocument.openPackage(sampleFile);
67 HXFDocument.openPackage(sampleFileBeta);
69 HSSFXML xml;
71 // With an old-style uri, as found in a file produced
72 // with the office 2007 beta, will fail, as we don't
73 // translate things
74 try {
75 xml = new HSSFXML(
76 HXFDocument.openPackage(sampleFileBeta)
78 fail();
79 } catch(Exception e) {}
81 // With the finalised uri, should be fine
82 xml = new HSSFXML(
83 HXFDocument.openPackage(sampleFile)
86 // Check it has a workbook
87 assertNotNull(xml.getWorkbook());
90 public void testSheetBasics() throws Exception {
91 HSSFXML xml = new HSSFXML(
92 HXFDocument.openPackage(sampleFile)
95 // Should have three sheets
96 assertEquals(3, xml.getSheetReferences().sizeOfSheetArray());
97 assertEquals(3, xml.getSheetReferences().getSheetArray().length);
99 // Check they're as expected
100 CTSheet[] sheets = xml.getSheetReferences().getSheetArray();
101 assertEquals("Sheet1", sheets[0].getName());
102 assertEquals("Sheet2", sheets[1].getName());
103 assertEquals("Sheet3", sheets[2].getName());
104 assertEquals("rId1", sheets[0].getId());
105 assertEquals("rId2", sheets[1].getId());
106 assertEquals("rId3", sheets[2].getId());
108 // Now get those objects
109 assertNotNull(xml.getSheet(sheets[0]));
110 assertNotNull(xml.getSheet(sheets[1]));
111 assertNotNull(xml.getSheet(sheets[2]));
114 public void testMetadataBasics() throws Exception {
115 HSSFXML xml = new HSSFXML(
116 HXFDocument.openPackage(sampleFile)
118 assertNotNull(xml.getCoreProperties());
119 assertNotNull(xml.getExtendedProperties());
121 assertEquals("Microsoft Excel", xml.getExtendedProperties().getApplication());
122 assertEquals(0, xml.getExtendedProperties().getCharacters());
123 assertEquals(0, xml.getExtendedProperties().getLines());
125 assertEquals(null, xml.getCoreProperties().getTitleProperty().getValue());
126 assertEquals(null, xml.getCoreProperties().getSubjectProperty().getValue());
129 public void testSharedStringBasics() throws Exception {
130 HSSFXML xml = new HSSFXML(
131 HXFDocument.openPackage(sampleFile)
133 assertNotNull(xml._getSharedStringsTable());
135 SharedStringsTable sst = xml._getSharedStringsTable();
136 assertEquals(10, sst.size());
138 assertEquals("Lorem", sst.get(0));
139 for(int i=0; i<sst.size(); i++) {
140 assertEquals(sst.get(i), xml.getSharedString(i));
143 // Add a few more, then save and reload, checking
144 // changes have been kept
145 sst.add("Foo");
146 sst.add("Bar");
147 sst.set(0, "LoremLorem");
149 sst.write();
151 xml = new HSSFXML(xml.getPackage());
152 sst = xml._getSharedStringsTable();
153 assertEquals(12, sst.size());
155 assertEquals("LoremLorem", sst.get(0));
156 for(int i=0; i<sst.size(); i++) {
157 assertEquals(sst.get(i), xml.getSharedString(i));