import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Date;
import java.util.regex.Matcher;
@Test
public void testExportToXML() throws Exception {
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx")) {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(1);
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xml = os.toString("UTF-8");
-
- assertNotNull(xml);
- assertFalse(xml.isEmpty());
-
- String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
- String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
- String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
- String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
- String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
- String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
- String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
- String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
-
- assertEquals("ro", docente);
- assertEquals("ro", nome);
- assertEquals("ds", tutor);
- assertEquals("gs", cdl);
- assertEquals("g", durata);
- assertEquals("gvvv", argomento);
- assertEquals("aaaa", progetto);
- assertEquals("aa", crediti);
-
- parseXML(xml);
-
- found = true;
- }
- assertTrue(found);
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
+
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
+
+ XSSFMap map = mapInfo.getXSSFMapById(1);
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xml = os.toString("UTF-8");
+
+ assertNotNull(xml);
+ assertFalse(xml.isEmpty());
+
+ String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
+ String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
+ String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
+ String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
+ String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
+ String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
+ String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
+ String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
+
+ assertEquals("ro", docente);
+ assertEquals("ro", nome);
+ assertEquals("ds", tutor);
+ assertEquals("gs", cdl);
+ assertEquals("g", durata);
+ assertEquals("gvvv", argomento);
+ assertEquals("aaaa", progetto);
+ assertEquals("aa", crediti);
+
+ parseXML(xml);
+
+ found = true;
+ }
+ assertTrue(found);
+ }
}
@Test
public void testExportToXMLInverseOrder() throws Exception {
+ try (XSSFWorkbook wb = XSSFTestDataSamples
+ .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx")) {
- XSSFWorkbook wb = XSSFTestDataSamples
- .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(1);
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xml = os.toString("UTF-8");
-
- assertNotNull(xml);
- assertFalse(xml.isEmpty());
-
- String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
- String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
- String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
- String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
- String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
- String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
- String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
- String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
-
- assertEquals("aa", nome);
- assertEquals("aaaa", docente);
- assertEquals("gvvv", tutor);
- assertEquals("g", cdl);
- assertEquals("gs", durata);
- assertEquals("ds", argomento);
- assertEquals("ro", progetto);
- assertEquals("ro", crediti);
-
- parseXML(xml);
-
- found = true;
- }
- assertTrue(found);
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
+
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
+
+ XSSFMap map = mapInfo.getXSSFMapById(1);
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xml = os.toString("UTF-8");
+
+ assertNotNull(xml);
+ assertFalse(xml.isEmpty());
+
+ String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
+ String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
+ String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
+ String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
+ String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
+ String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
+ String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
+ String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
+
+ assertEquals("aa", nome);
+ assertEquals("aaaa", docente);
+ assertEquals("gvvv", tutor);
+ assertEquals("g", cdl);
+ assertEquals("gs", durata);
+ assertEquals("ds", argomento);
+ assertEquals("ro", progetto);
+ assertEquals("ro", crediti);
+
+ parseXML(xml);
+
+ found = true;
+ }
+ assertTrue(found);
+ }
}
@Test
- public void testXPathOrdering() {
+ public void testXPathOrdering() throws IOException {
+ try (XSSFWorkbook wb = XSSFTestDataSamples
+ .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx")) {
- XSSFWorkbook wb = XSSFTestDataSamples
- .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ if (p instanceof MapInfo) {
+ MapInfo mapInfo = (MapInfo) p;
- if (p instanceof MapInfo) {
- MapInfo mapInfo = (MapInfo) p;
+ XSSFMap map = mapInfo.getXSSFMapById(1);
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
- XSSFMap map = mapInfo.getXSSFMapById(1);
- XSSFExportToXml exporter = new XSSFExportToXml(map);
+ assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME"));
+ assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE"));
+ }
- assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME"));
- assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE"));
- }
-
- found = true;
- }
- assertTrue(found);
+ found = true;
+ }
+ assertTrue(found);
+ }
}
@Test
public void testMultiTable() throws Exception {
+ try (XSSFWorkbook wb = XSSFTestDataSamples
+ .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx")) {
- XSSFWorkbook wb = XSSFTestDataSamples
- .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ if (p instanceof MapInfo) {
+ MapInfo mapInfo = (MapInfo) p;
- if (p instanceof MapInfo) {
- MapInfo mapInfo = (MapInfo) p;
+ XSSFMap map = mapInfo.getXSSFMapById(2);
- XSSFMap map = mapInfo.getXSSFMapById(2);
+ assertNotNull(map);
- assertNotNull(map);
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xml = os.toString("UTF-8");
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xml = os.toString("UTF-8");
+ assertNotNull(xml);
- assertNotNull(xml);
+ String[] regexConditions = {
+ "<MapInfo", "</MapInfo>",
+ "<Schema ID=\"1\" Namespace=\"\" SchemaRef=\"\"/>",
+ "<Schema ID=\"4\" Namespace=\"\" SchemaRef=\"\"/>",
+ "DataBinding",
+ "Map Append=\"false\" AutoFit=\"false\" ID=\"1\"",
+ "Map Append=\"false\" AutoFit=\"false\" ID=\"5\"",
+ };
- String[] regexConditions = {
- "<MapInfo", "</MapInfo>",
- "<Schema ID=\"1\" Namespace=\"\" SchemaRef=\"\"/>",
- "<Schema ID=\"4\" Namespace=\"\" SchemaRef=\"\"/>",
- "DataBinding",
- "Map Append=\"false\" AutoFit=\"false\" ID=\"1\"",
- "Map Append=\"false\" AutoFit=\"false\" ID=\"5\"",
- };
+ for (String condition : regexConditions) {
+ Pattern pattern = Pattern.compile(condition);
+ Matcher matcher = pattern.matcher(xml);
+ assertTrue(matcher.find());
+ }
+ }
- for (String condition : regexConditions) {
- Pattern pattern = Pattern.compile(condition);
- Matcher matcher = pattern.matcher(xml);
- assertTrue(matcher.find());
- }
- }
-
- found = true;
- }
- assertTrue(found);
+ found = true;
+ }
+ assertTrue(found);
+ }
}
@Test
@Ignore(value="Fails, but I don't know if it is ok or not...")
public void testExportToXMLSingleAttributeNamespace() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx");
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx")) {
- for (XSSFMap map : wb.getCustomXMLMappings()) {
- XSSFExportToXml exporter = new XSSFExportToXml(map);
+ for (XSSFMap map : wb.getCustomXMLMappings()) {
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ }
}
}
@Test
public void test55850ComplexXmlExport() throws Exception {
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55850.xlsx")) {
- XSSFWorkbook wb = XSSFTestDataSamples
- .openSampleWorkbook("55850.xlsx");
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
+ XSSFMap map = mapInfo.getXSSFMapById(2);
+
+ assertNotNull("XSSFMap is null", map);
- XSSFMap map = mapInfo.getXSSFMapById(2);
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xmlData = os.toString("UTF-8");
- assertNotNull("XSSFMap is null", map);
+ assertNotNull(xmlData);
+ assertFalse(xmlData.isEmpty());
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
+ String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
+ String b = a.split("<B>")[1].split("</B>")[0].trim();
+ String c = b.split("<C>")[1].split("</C>")[0].trim();
+ String d = c.split("<D>")[1].split("</Dd>")[0].trim();
+ String e = d.split("<E>")[1].split("</EA>")[0].trim();
- assertNotNull(xmlData);
- assertFalse(xmlData.isEmpty());
+ String euro = e.split("<EUR>")[1].split("</EUR>")[0].trim();
+ String chf = e.split("<CHF>")[1].split("</CHF>")[0].trim();
- String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
- String b = a.split("<B>")[1].split("</B>")[0].trim();
- String c = b.split("<C>")[1].split("</C>")[0].trim();
- String d = c.split("<D>")[1].split("</Dd>")[0].trim();
- String e = d.split("<E>")[1].split("</EA>")[0].trim();
+ assertEquals("15", euro);
+ assertEquals("19", chf);
- String euro = e.split("<EUR>")[1].split("</EUR>")[0].trim();
- String chf = e.split("<CHF>")[1].split("</CHF>")[0].trim();
+ parseXML(xmlData);
- assertEquals("15", euro);
- assertEquals("19", chf);
-
- parseXML(xmlData);
-
- found = true;
+ found = true;
+ }
+ assertTrue(found);
}
- assertTrue(found);
}
@Test
public void testFormulaCells_Bugzilla_55927() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx");
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx")) {
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
- XSSFMap map = mapInfo.getXSSFMapById(1);
+ XSSFMap map = mapInfo.getXSSFMapById(1);
- assertNotNull("XSSFMap is null", map);
+ assertNotNull("XSSFMap is null", map);
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xmlData = os.toString("UTF-8");
- assertNotNull(xmlData);
- assertFalse(xmlData.isEmpty());
+ assertNotNull(xmlData);
+ assertFalse(xmlData.isEmpty());
- assertEquals("2012-01-13", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
- assertEquals("2012-02-16", xmlData.split("<FORMULA_DATE>")[1].split("</FORMULA_DATE>")[0].trim());
+ assertEquals("2012-01-13", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
+ assertEquals("2012-02-16", xmlData.split("<FORMULA_DATE>")[1].split("</FORMULA_DATE>")[0].trim());
- parseXML(xmlData);
+ parseXML(xmlData);
- found = true;
+ found = true;
+ }
+ assertTrue(found);
}
- assertTrue(found);
}
@Test
public void testFormulaCells_Bugzilla_55926() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx");
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx")) {
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
- XSSFMap map = mapInfo.getXSSFMapById(1);
+ XSSFMap map = mapInfo.getXSSFMapById(1);
- assertNotNull("XSSFMap is null", map);
+ assertNotNull("XSSFMap is null", map);
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xmlData = os.toString("UTF-8");
- assertNotNull(xmlData);
- assertFalse(xmlData.isEmpty());
+ assertNotNull(xmlData);
+ assertFalse(xmlData.isEmpty());
- String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
- String doubleValue = a.split("<DOUBLE>")[1].split("</DOUBLE>")[0].trim();
- String stringValue = a.split("<STRING>")[1].split("</STRING>")[0].trim();
+ String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
+ String doubleValue = a.split("<DOUBLE>")[1].split("</DOUBLE>")[0].trim();
+ String stringValue = a.split("<STRING>")[1].split("</STRING>")[0].trim();
- assertEquals("Hello World", stringValue);
- assertEquals("5.1", doubleValue);
+ assertEquals("Hello World", stringValue);
+ assertEquals("5.1", doubleValue);
- parseXML(xmlData);
+ parseXML(xmlData);
- found = true;
+ found = true;
+ }
+ assertTrue(found);
}
- assertTrue(found);
}
@Test
public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception {
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx")) {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx");
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
+ XSSFMap map = mapInfo.getXSSFMapById(1);
+
+ assertNotNull("XSSFMap is null", map);
- XSSFMap map = mapInfo.getXSSFMapById(1);
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xmlData = os.toString("UTF-8");
- assertNotNull("XSSFMap is null", map);
+ assertNotNull(xmlData);
+ assertFalse(xmlData.isEmpty());
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
+ String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
+ String euro = a.split("<EUR>")[1].split("</EUR>")[0].trim();
+ assertEquals("1", euro);
- assertNotNull(xmlData);
- assertFalse(xmlData.isEmpty());
+ parseXML(xmlData);
- String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
- String euro = a.split("<EUR>")[1].split("</EUR>")[0].trim();
- assertEquals("1",euro);
-
- parseXML(xmlData);
-
- found = true;
+ found = true;
+ }
+ assertTrue(found);
}
- assertTrue(found);
}
@Test
public void testXmlExportSchemaWithXSAllTag_Bugzilla_56169() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx");
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx")) {
- for (XSSFMap map : wb.getCustomXMLMappings()) {
- XSSFExportToXml exporter = new XSSFExportToXml(map);
+ for (XSSFMap map : wb.getCustomXMLMappings()) {
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xmlData = os.toString("UTF-8");
- assertNotNull(xmlData);
- assertTrue(!xmlData.isEmpty());
+ assertNotNull(xmlData);
+ assertTrue(!xmlData.isEmpty());
- String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
- String a_b = a.split("<B>")[1].split("</B>")[0].trim();
- String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
- String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
- String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
- String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
+ String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
+ String a_b = a.split("<B>")[1].split("</B>")[0].trim();
+ String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
+ String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
+ String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
+ String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
- assertEquals("1", a_b_c_e_euro);
- assertEquals("2", a_b_c_e_chf);
+ assertEquals("1", a_b_c_e_euro);
+ assertEquals("2", a_b_c_e_chf);
- String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
- String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
+ String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
+ String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
- String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
- String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
+ String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
+ String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
- assertEquals("3", a_b_d_e_euro);
- assertEquals("4", a_b_d_e_chf);
+ assertEquals("3", a_b_d_e_euro);
+ assertEquals("4", a_b_d_e_chf);
+ }
}
}
@Test
public void testXmlExportCompare_Bug_55923() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) {
+
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
+
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ XSSFMap map = mapInfo.getXSSFMapById(4);
- if (!(p instanceof MapInfo)) {
- continue;
+ assertNotNull("XSSFMap is null", map);
+
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ assertEquals(0, exporter.compare("", ""));
+ assertEquals(0, exporter.compare("/", "/"));
+ assertEquals(0, exporter.compare("//", "//"));
+ assertEquals(0, exporter.compare("/a/", "/b/"));
+
+ assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR",
+ "/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:CHF"));
+
+ found = true;
}
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(4);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- assertEquals(0, exporter.compare("", ""));
- assertEquals(0, exporter.compare("/", "/"));
- assertEquals(0, exporter.compare("//", "//"));
- assertEquals(0, exporter.compare("/a/", "/b/"));
-
- assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR",
- "/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:CHF"));
-
- found = true;
+ assertTrue(found);
}
- assertTrue(found);
}
@Test
public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) {
+
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
+
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
+
+ XSSFMap map = mapInfo.getXSSFMapById(4);
+
+ assertNotNull("XSSFMap is null", map);
+
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xmlData = os.toString("UTF-8");
+
+ assertNotNull(xmlData);
+ assertFalse(xmlData.isEmpty());
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
+ String a_b = a.split("<B>")[1].split("</B>")[0].trim();
+ String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
+ String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
+ String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
+ String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
- if (!(p instanceof MapInfo)) {
- continue;
+ assertEquals("1", a_b_c_e_euro);
+ assertEquals("2", a_b_c_e_chf);
+
+ String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
+ String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
+
+ String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
+ String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
+
+ assertEquals("3", a_b_d_e_euro);
+ assertEquals("4", a_b_d_e_chf);
+
+ found = true;
}
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(4);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
-
- assertNotNull(xmlData);
- assertFalse(xmlData.isEmpty());
-
- String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
- String a_b = a.split("<B>")[1].split("</B>")[0].trim();
- String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
- String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
- String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
- String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
-
- assertEquals("1",a_b_c_e_euro);
- assertEquals("2",a_b_c_e_chf);
-
- String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
- String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
-
- String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
- String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
-
- assertEquals("3",a_b_d_e_euro);
- assertEquals("4",a_b_d_e_chf);
-
- found = true;
+ assertTrue(found);
}
- assertTrue(found);
}
private void parseXML(String xmlData) throws IOException, SAXException, ParserConfigurationException {
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.setEntityResolver(new DummyEntityResolver());
- docBuilder.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8")));
+ docBuilder.parse(new ByteArrayInputStream(xmlData.getBytes(StandardCharsets.UTF_8)));
}
- private static class DummyEntityResolver implements EntityResolver
- {
+ private static class DummyEntityResolver implements EntityResolver {
@Override
- public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
- {
+ public InputSource resolveEntity(String publicId, String systemId) {
return null;
}
}
@Test
public void testExportDataTypes() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
-
- Sheet sheet = wb.getSheetAt(0);
- Row row = sheet.getRow(0);
-
- Cell cString = row.createCell(0);
- cString.setCellValue("somestring");
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) {
- Cell cBoolean = row.createCell(1);
- cBoolean.setCellValue(true);
+ Sheet sheet = wb.getSheetAt(0);
+ Row row = sheet.getRow(0);
- Cell cError = row.createCell(2);
- cError.setCellErrorValue(FormulaError.NUM.getCode());
-
- Cell cFormulaString = row.createCell(3);
- cFormulaString.setCellFormula("A1");
+ Cell cString = row.createCell(0);
+ cString.setCellValue("somestring");
- Cell cFormulaNumeric = row.createCell(4);
- cFormulaNumeric.setCellFormula("F1");
+ Cell cBoolean = row.createCell(1);
+ cBoolean.setCellValue(true);
- Cell cNumeric = row.createCell(5);
- cNumeric.setCellValue(1.2);
+ Cell cError = row.createCell(2);
+ cError.setCellErrorValue(FormulaError.NUM.getCode());
- Cell cDate = row.createCell(6);
- cDate.setCellValue(new Date());
+ Cell cFormulaString = row.createCell(3);
+ cFormulaString.setCellFormula("A1");
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ Cell cFormulaNumeric = row.createCell(4);
+ cFormulaNumeric.setCellFormula("F1");
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
+ Cell cNumeric = row.createCell(5);
+ cNumeric.setCellValue(1.2);
+
+ Cell cDate = row.createCell(6);
+ cDate.setCellValue(new Date());
+
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
+
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
+
+ XSSFMap map = mapInfo.getXSSFMapById(4);
- XSSFMap map = mapInfo.getXSSFMapById(4);
+ assertNotNull("XSSFMap is null", map);
- assertNotNull("XSSFMap is null", map);
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xmlData = os.toString("UTF-8");
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
+ assertNotNull(xmlData);
+ assertFalse(xmlData.isEmpty());
- assertNotNull(xmlData);
- assertFalse(xmlData.isEmpty());
-
- parseXML(xmlData);
-
- found = true;
+ parseXML(xmlData);
+
+ found = true;
+ }
+ assertTrue(found);
}
- assertTrue(found);
}
@Test
public void testValidateFalse() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) {
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
+ XSSFMap map = mapInfo.getXSSFMapById(4);
- XSSFMap map = mapInfo.getXSSFMapById(4);
+ assertNotNull("XSSFMap is null", map);
- assertNotNull("XSSFMap is null", map);
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, false);
+ String xmlData = os.toString("UTF-8");
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, false);
- String xmlData = os.toString("UTF-8");
+ assertNotNull(xmlData);
+ assertFalse(xmlData.isEmpty());
- assertNotNull(xmlData);
- assertFalse(xmlData.isEmpty());
-
- parseXML(xmlData);
-
- found = true;
+ parseXML(xmlData);
+
+ found = true;
+ }
+ assertTrue(found);
}
- assertTrue(found);
}
@Test
public void testRefElementsInXmlSchema_Bugzilla_56730() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.xlsx")) {
+
+ boolean found = false;
+ for (POIXMLDocumentPart p : wb.getRelations()) {
+
+ if (!(p instanceof MapInfo)) {
+ continue;
+ }
+ MapInfo mapInfo = (MapInfo) p;
+
+ XSSFMap map = mapInfo.getXSSFMapById(1);
+
+ assertNotNull("XSSFMap is null", map);
+
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, true);
+ String xmlData = os.toString("UTF-8");
+
+ assertNotNull(xmlData);
+ assertFalse(xmlData.isEmpty());
+
+ assertEquals("2014-12-31", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
+ assertEquals("12.5", xmlData.split("<REFELEMENT>")[1].split("</REFELEMENT>")[0].trim());
+
+ parseXML(xmlData);
+
+ found = true;
}
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(1);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
-
- assertNotNull(xmlData);
- assertFalse(xmlData.isEmpty());
-
- assertEquals("2014-12-31", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
- assertEquals("12.5", xmlData.split("<REFELEMENT>")[1].split("</REFELEMENT>")[0].trim());
-
- parseXML(xmlData);
-
- found = true;
+ assertTrue(found);
}
- assertTrue(found);
}
@Test
public void testBug59026() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx");
-
- Collection<XSSFMap> mappings = wb.getCustomXMLMappings();
- assertTrue(mappings.size() > 0);
- for (XSSFMap map : mappings) {
- XSSFExportToXml exporter = new XSSFExportToXml(map);
-
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, false);
- assertNotNull(os.toString("UTF-8"));
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx")) {
+ Collection<XSSFMap> mappings = wb.getCustomXMLMappings();
+ assertTrue(mappings.size() > 0);
+ for (XSSFMap map : mappings) {
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ exporter.exportToXML(os, false);
+ assertNotNull(os.toString("UTF-8"));
+ }
}
}
@Test
public void testExportTableWithNonMappedColumn_Bugzilla_61281() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61281.xlsx");
- for (XSSFMap map : wb.getCustomXMLMappings()) {
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- exporter.exportToXML(bos, true);
- assertNotNull(DocumentHelper.readDocument(new ByteArrayInputStream(bos.toByteArray())));
- String exportedXml = bos.toString("UTF-8");
- assertEquals("<Test><Test>1</Test></Test>", exportedXml.replaceAll("\\s+", ""));
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61281.xlsx")) {
+ for (XSSFMap map : wb.getCustomXMLMappings()) {
+ XSSFExportToXml exporter = new XSSFExportToXml(map);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ exporter.exportToXML(bos, true);
+ assertNotNull(DocumentHelper.readDocument(new ByteArrayInputStream(bos.toByteArray())));
+ String exportedXml = bos.toString("UTF-8");
+ assertEquals("<Test><Test>1</Test></Test>", exportedXml.replaceAll("\\s+", ""));
+ }
}
}
}
private static final String TEST_AUTHOR = "test author";
@Test
- public void findAuthor() throws Exception {
+ public void findAuthor() {
CommentsTable sheetComments = new CommentsTable();
assertEquals(1, sheetComments.getNumberOfAuthors());
assertEquals(0, sheetComments.findAuthor(""));
}
@Test
- public void getCellComment() throws Exception {
+ public void getCellComment() {
CommentsTable sheetComments = new CommentsTable();
CTComments comments = sheetComments.getCTComments();
@Test
- public void existing() {
- Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
- Sheet sheet1 = workbook.getSheetAt(0);
- Sheet sheet2 = workbook.getSheetAt(1);
-
- assertTrue( ((XSSFSheet)sheet1).hasComments() );
- assertFalse( ((XSSFSheet)sheet2).hasComments() );
-
- // Comments should be in C5 and C7
- Row r5 = sheet1.getRow(4);
- Row r7 = sheet1.getRow(6);
- assertNotNull( r5.getCell(2).getCellComment() );
- assertNotNull( r7.getCell(2).getCellComment() );
-
- // Check they have what we expect
- // TODO: Rich text formatting
- Comment cc5 = r5.getCell(2).getCellComment();
- Comment cc7 = r7.getCell(2).getCellComment();
-
- assertEquals("Nick Burch", cc5.getAuthor());
- assertEquals("Nick Burch:\nThis is a comment", cc5.getString().getString());
- assertEquals(4, cc5.getRow());
- assertEquals(2, cc5.getColumn());
-
- assertEquals("Nick Burch", cc7.getAuthor());
- assertEquals("Nick Burch:\nComment #1\n", cc7.getString().getString());
- assertEquals(6, cc7.getRow());
- assertEquals(2, cc7.getColumn());
+ public void existing() throws IOException {
+ try (Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx")) {
+ Sheet sheet1 = workbook.getSheetAt(0);
+ Sheet sheet2 = workbook.getSheetAt(1);
+
+ assertTrue(((XSSFSheet) sheet1).hasComments());
+ assertFalse(((XSSFSheet) sheet2).hasComments());
+
+ // Comments should be in C5 and C7
+ Row r5 = sheet1.getRow(4);
+ Row r7 = sheet1.getRow(6);
+ assertNotNull(r5.getCell(2).getCellComment());
+ assertNotNull(r7.getCell(2).getCellComment());
+
+ // Check they have what we expect
+ // TODO: Rich text formatting
+ Comment cc5 = r5.getCell(2).getCellComment();
+ Comment cc7 = r7.getCell(2).getCellComment();
+
+ assertEquals("Nick Burch", cc5.getAuthor());
+ assertEquals("Nick Burch:\nThis is a comment", cc5.getString().getString());
+ assertEquals(4, cc5.getRow());
+ assertEquals(2, cc5.getColumn());
+
+ assertEquals("Nick Burch", cc7.getAuthor());
+ assertEquals("Nick Burch:\nComment #1\n", cc7.getString().getString());
+ assertEquals(6, cc7.getRow());
+ assertEquals(2, cc7.getColumn());
+ }
}
@Test
- public void writeRead() {
- XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
- XSSFSheet sheet1 = workbook.getSheetAt(0);
- XSSFSheet sheet2 = workbook.getSheetAt(1);
-
- assertTrue( sheet1.hasComments() );
- assertFalse( sheet2.hasComments() );
-
- // Change on comment on sheet 1, and add another into
- // sheet 2
- Row r5 = sheet1.getRow(4);
- Comment cc5 = r5.getCell(2).getCellComment();
- cc5.setAuthor("Apache POI");
- cc5.setString(new XSSFRichTextString("Hello!"));
-
- Row r2s2 = sheet2.createRow(2);
- Cell c1r2s2 = r2s2.createCell(1);
- assertNull(c1r2s2.getCellComment());
-
- Drawing<?> dg = sheet2.createDrawingPatriarch();
- Comment cc2 = dg.createCellComment(new XSSFClientAnchor());
- cc2.setAuthor("Also POI");
- cc2.setString(new XSSFRichTextString("A new comment"));
- c1r2s2.setCellComment(cc2);
-
-
- // Save, and re-load the file
- workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
-
- // Check we still have comments where we should do
- sheet1 = workbook.getSheetAt(0);
- sheet2 = workbook.getSheetAt(1);
- assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
- assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
- assertNotNull(sheet2.getRow(2).getCell(1).getCellComment());
-
- // And check they still have the contents they should do
- assertEquals("Apache POI",
- sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
- assertEquals("Nick Burch",
- sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
- assertEquals("Also POI",
- sheet2.getRow(2).getCell(1).getCellComment().getAuthor());
-
- assertEquals("Hello!",
- sheet1.getRow(4).getCell(2).getCellComment().getString().getString());
+ public void writeRead() throws IOException {
+ try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx")) {
+ XSSFSheet sheet1 = workbook.getSheetAt(0);
+ XSSFSheet sheet2 = workbook.getSheetAt(1);
+
+ assertTrue(sheet1.hasComments());
+ assertFalse(sheet2.hasComments());
+
+ // Change on comment on sheet 1, and add another into
+ // sheet 2
+ Row r5 = sheet1.getRow(4);
+ Comment cc5 = r5.getCell(2).getCellComment();
+ cc5.setAuthor("Apache POI");
+ cc5.setString(new XSSFRichTextString("Hello!"));
+
+ Row r2s2 = sheet2.createRow(2);
+ Cell c1r2s2 = r2s2.createCell(1);
+ assertNull(c1r2s2.getCellComment());
+
+ Drawing<?> dg = sheet2.createDrawingPatriarch();
+ Comment cc2 = dg.createCellComment(new XSSFClientAnchor());
+ cc2.setAuthor("Also POI");
+ cc2.setString(new XSSFRichTextString("A new comment"));
+ c1r2s2.setCellComment(cc2);
+
+ // Save, and re-load the file
+ try (XSSFWorkbook workbookBack = XSSFTestDataSamples.writeOutAndReadBack(workbook)) {
+ // Check we still have comments where we should do
+ sheet1 = workbookBack.getSheetAt(0);
+ sheet2 = workbookBack.getSheetAt(1);
+ assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
+ assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
+ assertNotNull(sheet2.getRow(2).getCell(1).getCellComment());
+
+ // And check they still have the contents they should do
+ assertEquals("Apache POI",
+ sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
+ assertEquals("Nick Burch",
+ sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
+ assertEquals("Also POI",
+ sheet2.getRow(2).getCell(1).getCellComment().getAuthor());
+
+ assertEquals("Hello!",
+ sheet1.getRow(4).getCell(2).getCellComment().getString().getString());
+ }
+ }
}
@Test
- public void readWriteMultipleAuthors() {
- XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx");
- XSSFSheet sheet1 = workbook.getSheetAt(0);
- XSSFSheet sheet2 = workbook.getSheetAt(1);
-
- assertTrue( sheet1.hasComments() );
- assertFalse( sheet2.hasComments() );
-
- assertEquals("Nick Burch",
- sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
- assertEquals("Nick Burch",
- sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
- assertEquals("Torchbox",
- sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
-
- // Save, and re-load the file
- workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
-
- // Check we still have comments where we should do
- sheet1 = workbook.getSheetAt(0);
- assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
- assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
- assertNotNull(sheet1.getRow(12).getCell(2).getCellComment());
-
- // And check they still have the contents they should do
- assertEquals("Nick Burch",
- sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
- assertEquals("Nick Burch",
- sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
- assertEquals("Torchbox",
- sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
-
- // Todo - check text too, once bug fixed
+ public void readWriteMultipleAuthors() throws IOException {
+ try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx")) {
+ XSSFSheet sheet1 = workbook.getSheetAt(0);
+ XSSFSheet sheet2 = workbook.getSheetAt(1);
+
+ assertTrue(sheet1.hasComments());
+ assertFalse(sheet2.hasComments());
+
+ assertEquals("Nick Burch",
+ sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
+ assertEquals("Nick Burch",
+ sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
+ assertEquals("Torchbox",
+ sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
+
+ // Save, and re-load the file
+ try (XSSFWorkbook workbookBack = XSSFTestDataSamples.writeOutAndReadBack(workbook)) {
+
+ // Check we still have comments where we should do
+ sheet1 = workbookBack.getSheetAt(0);
+ assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
+ assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
+ assertNotNull(sheet1.getRow(12).getCell(2).getCellComment());
+
+ // And check they still have the contents they should do
+ assertEquals("Nick Burch",
+ sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
+ assertEquals("Nick Burch",
+ sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
+ assertEquals("Torchbox",
+ sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
+
+ // Todo - check text too, once bug fixed
+ }
+ }
}
@Test
- public void removeComment() throws Exception {
+ public void removeComment() {
final CellAddress addrA1 = new CellAddress("A1");
final CellAddress addrA2 = new CellAddress("A2");
final CellAddress addrA3 = new CellAddress("A3");
Cell A1 = getCell(sheet, 0, 0);
//Cell A1 = getCell(sheet, 2, 2);
Drawing<?> drawing = sheet.createDrawingPatriarch();
- setComment(sheet, A1, drawing, "for A1", helper, anchor);
+ setComment(A1, drawing, "for A1", helper, anchor);
// find comment in A1 before we set the comment in B2
Comment commentA1 = A1.getCellComment();
// place comment in B2, according to Bug 54920 this removes the comment in A1!
Cell B2 = getCell(sheet, 1, 1);
- setComment(sheet, B2, drawing, "for B2", helper, anchor);
+ setComment(B2, drawing, "for B2", helper, anchor);
// find comment in A1
Comment commentB2 = B2.getCellComment();
// Set the comment on a sheet
//
- private static void setComment(Sheet sheet, Cell cell, Drawing<?> drawing, String commentText, CreationHelper helper, ClientAnchor anchor) {
+ private static void setComment(Cell cell, Drawing<?> drawing, String commentText, CreationHelper helper, ClientAnchor anchor) {
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex());
anchor.setRow1(cell.getRowIndex());