You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSSFExportToXML.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.extractor;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertThrows;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.IOException;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Collection;
  25. import java.util.Date;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;
  28. import javax.xml.parsers.DocumentBuilder;
  29. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  30. import org.apache.poi.ooxml.POIXMLDocumentPart;
  31. import org.apache.poi.ooxml.util.DocumentHelper;
  32. import org.apache.poi.ss.usermodel.Cell;
  33. import org.apache.poi.ss.usermodel.FormulaError;
  34. import org.apache.poi.ss.usermodel.Row;
  35. import org.apache.poi.ss.usermodel.Sheet;
  36. import org.apache.poi.util.XMLHelper;
  37. import org.apache.poi.xssf.XSSFTestDataSamples;
  38. import org.apache.poi.xssf.model.MapInfo;
  39. import org.apache.poi.xssf.usermodel.XSSFMap;
  40. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  41. import org.junit.jupiter.api.Test;
  42. import org.xml.sax.SAXException;
  43. import org.xml.sax.SAXParseException;
  44. public final class TestXSSFExportToXML {
  45. @Test
  46. void testExportToXML() throws Exception {
  47. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx")) {
  48. boolean found = false;
  49. for (POIXMLDocumentPart p : wb.getRelations()) {
  50. if (!(p instanceof MapInfo)) {
  51. continue;
  52. }
  53. MapInfo mapInfo = (MapInfo) p;
  54. XSSFMap map = mapInfo.getXSSFMapById(1);
  55. XSSFExportToXml exporter = new XSSFExportToXml(map);
  56. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  57. exporter.exportToXML(os, true);
  58. String xml = os.toString("UTF-8");
  59. assertNotNull(xml);
  60. assertFalse(xml.isEmpty());
  61. String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
  62. String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
  63. String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
  64. String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
  65. String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
  66. String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
  67. String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
  68. String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
  69. assertEquals("ro", docente);
  70. assertEquals("ro", nome);
  71. assertEquals("ds", tutor);
  72. assertEquals("gs", cdl);
  73. assertEquals("g", durata);
  74. assertEquals("gvvv", argomento);
  75. assertEquals("aaaa", progetto);
  76. assertEquals("aa", crediti);
  77. parseXML(xml);
  78. found = true;
  79. }
  80. assertTrue(found);
  81. }
  82. }
  83. @Test
  84. void testExportToXMLInverseOrder() throws Exception {
  85. try (XSSFWorkbook wb = XSSFTestDataSamples
  86. .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx")) {
  87. boolean found = false;
  88. for (POIXMLDocumentPart p : wb.getRelations()) {
  89. if (!(p instanceof MapInfo)) {
  90. continue;
  91. }
  92. MapInfo mapInfo = (MapInfo) p;
  93. XSSFMap map = mapInfo.getXSSFMapById(1);
  94. XSSFExportToXml exporter = new XSSFExportToXml(map);
  95. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  96. exporter.exportToXML(os, true);
  97. String xml = os.toString("UTF-8");
  98. assertNotNull(xml);
  99. assertFalse(xml.isEmpty());
  100. String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
  101. String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
  102. String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
  103. String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
  104. String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
  105. String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
  106. String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
  107. String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
  108. assertEquals("aa", nome);
  109. assertEquals("aaaa", docente);
  110. assertEquals("gvvv", tutor);
  111. assertEquals("g", cdl);
  112. assertEquals("gs", durata);
  113. assertEquals("ds", argomento);
  114. assertEquals("ro", progetto);
  115. assertEquals("ro", crediti);
  116. parseXML(xml);
  117. found = true;
  118. }
  119. assertTrue(found);
  120. }
  121. }
  122. @Test
  123. void testXPathOrdering() throws IOException {
  124. try (XSSFWorkbook wb = XSSFTestDataSamples
  125. .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx")) {
  126. boolean found = false;
  127. for (POIXMLDocumentPart p : wb.getRelations()) {
  128. if (p instanceof MapInfo) {
  129. MapInfo mapInfo = (MapInfo) p;
  130. XSSFMap map = mapInfo.getXSSFMapById(1);
  131. XSSFExportToXml exporter = new XSSFExportToXml(map);
  132. assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME"));
  133. assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE"));
  134. }
  135. found = true;
  136. }
  137. assertTrue(found);
  138. }
  139. }
  140. @Test
  141. void testMultiTable() throws Exception {
  142. try (XSSFWorkbook wb = XSSFTestDataSamples
  143. .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx")) {
  144. boolean found = false;
  145. for (POIXMLDocumentPart p : wb.getRelations()) {
  146. if (p instanceof MapInfo) {
  147. MapInfo mapInfo = (MapInfo) p;
  148. XSSFMap map = mapInfo.getXSSFMapById(2);
  149. assertNotNull(map);
  150. XSSFExportToXml exporter = new XSSFExportToXml(map);
  151. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  152. exporter.exportToXML(os, true);
  153. String xml = os.toString("UTF-8");
  154. assertNotNull(xml);
  155. String[] regexConditions = {
  156. "<MapInfo", "</MapInfo>",
  157. "<Schema ID=\"1\" Namespace=\"\" SchemaRef=\"\"/>",
  158. "<Schema ID=\"4\" Namespace=\"\" SchemaRef=\"\"/>",
  159. "DataBinding",
  160. "Map Append=\"false\" AutoFit=\"false\" ID=\"1\"",
  161. "Map Append=\"false\" AutoFit=\"false\" ID=\"5\"",
  162. };
  163. for (String condition : regexConditions) {
  164. Pattern pattern = Pattern.compile(condition);
  165. Matcher matcher = pattern.matcher(xml);
  166. assertTrue(matcher.find());
  167. }
  168. }
  169. found = true;
  170. }
  171. assertTrue(found);
  172. }
  173. }
  174. @Test
  175. void testExportToXMLSingleAttributeNamespace() throws Exception {
  176. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx")) {
  177. for (XSSFMap map : wb.getCustomXMLMappings()) {
  178. XSSFExportToXml exporter = new XSSFExportToXml(map);
  179. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  180. SAXParseException ex = assertThrows(SAXParseException.class, () -> exporter.exportToXML(os, true));
  181. assertEquals("schema_reference: Failed to read schema document 'Schema11', because 'file' access is not allowed due to restriction set by the accessExternalSchema property.", ex.getMessage().trim());
  182. }
  183. }
  184. }
  185. @Test
  186. void test55850ComplexXmlExport() throws Exception {
  187. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55850.xlsx")) {
  188. boolean found = false;
  189. for (POIXMLDocumentPart p : wb.getRelations()) {
  190. if (!(p instanceof MapInfo)) {
  191. continue;
  192. }
  193. MapInfo mapInfo = (MapInfo) p;
  194. XSSFMap map = mapInfo.getXSSFMapById(2);
  195. assertNotNull(map, "XSSFMap is null");
  196. XSSFExportToXml exporter = new XSSFExportToXml(map);
  197. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  198. exporter.exportToXML(os, true);
  199. String xmlData = os.toString("UTF-8");
  200. assertNotNull(xmlData);
  201. assertFalse(xmlData.isEmpty());
  202. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  203. String b = a.split("<B>")[1].split("</B>")[0].trim();
  204. String c = b.split("<C>")[1].split("</C>")[0].trim();
  205. String d = c.split("<D>")[1].split("</Dd>")[0].trim();
  206. String e = d.split("<E>")[1].split("</EA>")[0].trim();
  207. String euro = e.split("<EUR>")[1].split("</EUR>")[0].trim();
  208. String chf = e.split("<CHF>")[1].split("</CHF>")[0].trim();
  209. assertEquals("15", euro);
  210. assertEquals("19", chf);
  211. parseXML(xmlData);
  212. found = true;
  213. }
  214. assertTrue(found);
  215. }
  216. }
  217. @Test
  218. void testFormulaCells_Bugzilla_55927() throws Exception {
  219. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx")) {
  220. boolean found = false;
  221. for (POIXMLDocumentPart p : wb.getRelations()) {
  222. if (!(p instanceof MapInfo)) {
  223. continue;
  224. }
  225. MapInfo mapInfo = (MapInfo) p;
  226. XSSFMap map = mapInfo.getXSSFMapById(1);
  227. assertNotNull(map, "XSSFMap is null");
  228. XSSFExportToXml exporter = new XSSFExportToXml(map);
  229. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  230. exporter.exportToXML(os, true);
  231. String xmlData = os.toString("UTF-8");
  232. assertNotNull(xmlData);
  233. assertFalse(xmlData.isEmpty());
  234. assertEquals("2012-01-13", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
  235. assertEquals("2012-02-16", xmlData.split("<FORMULA_DATE>")[1].split("</FORMULA_DATE>")[0].trim());
  236. parseXML(xmlData);
  237. found = true;
  238. }
  239. assertTrue(found);
  240. }
  241. }
  242. @Test
  243. void testFormulaCells_Bugzilla_55926() throws Exception {
  244. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx")) {
  245. boolean found = false;
  246. for (POIXMLDocumentPart p : wb.getRelations()) {
  247. if (!(p instanceof MapInfo)) {
  248. continue;
  249. }
  250. MapInfo mapInfo = (MapInfo) p;
  251. XSSFMap map = mapInfo.getXSSFMapById(1);
  252. assertNotNull(map, "XSSFMap is null");
  253. XSSFExportToXml exporter = new XSSFExportToXml(map);
  254. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  255. exporter.exportToXML(os, true);
  256. String xmlData = os.toString("UTF-8");
  257. assertNotNull(xmlData);
  258. assertFalse(xmlData.isEmpty());
  259. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  260. String doubleValue = a.split("<DOUBLE>")[1].split("</DOUBLE>")[0].trim();
  261. String stringValue = a.split("<STRING>")[1].split("</STRING>")[0].trim();
  262. assertEquals("Hello World", stringValue);
  263. assertEquals("5.1", doubleValue);
  264. parseXML(xmlData);
  265. found = true;
  266. }
  267. assertTrue(found);
  268. }
  269. }
  270. @Test
  271. void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception {
  272. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx")) {
  273. boolean found = false;
  274. for (POIXMLDocumentPart p : wb.getRelations()) {
  275. if (!(p instanceof MapInfo)) {
  276. continue;
  277. }
  278. MapInfo mapInfo = (MapInfo) p;
  279. XSSFMap map = mapInfo.getXSSFMapById(1);
  280. assertNotNull(map, "XSSFMap is null");
  281. XSSFExportToXml exporter = new XSSFExportToXml(map);
  282. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  283. exporter.exportToXML(os, true);
  284. String xmlData = os.toString("UTF-8");
  285. assertNotNull(xmlData);
  286. assertFalse(xmlData.isEmpty());
  287. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  288. String euro = a.split("<EUR>")[1].split("</EUR>")[0].trim();
  289. assertEquals("1", euro);
  290. parseXML(xmlData);
  291. found = true;
  292. }
  293. assertTrue(found);
  294. }
  295. }
  296. @Test
  297. void testXmlExportSchemaWithXSAllTag_Bugzilla_56169() throws Exception {
  298. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx")) {
  299. for (XSSFMap map : wb.getCustomXMLMappings()) {
  300. XSSFExportToXml exporter = new XSSFExportToXml(map);
  301. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  302. exporter.exportToXML(os, true);
  303. String xmlData = os.toString("UTF-8");
  304. assertNotNull(xmlData);
  305. assertFalse(xmlData.isEmpty());
  306. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  307. String a_b = a.split("<B>")[1].split("</B>")[0].trim();
  308. String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
  309. String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
  310. String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
  311. String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
  312. assertEquals("1", a_b_c_e_euro);
  313. assertEquals("2", a_b_c_e_chf);
  314. String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
  315. String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
  316. String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
  317. String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
  318. assertEquals("3", a_b_d_e_euro);
  319. assertEquals("4", a_b_d_e_chf);
  320. }
  321. }
  322. }
  323. @SuppressWarnings("EqualsWithItself")
  324. @Test
  325. void testXmlExportCompare_Bug_55923() throws Exception {
  326. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) {
  327. boolean found = false;
  328. for (POIXMLDocumentPart p : wb.getRelations()) {
  329. if (!(p instanceof MapInfo)) {
  330. continue;
  331. }
  332. MapInfo mapInfo = (MapInfo) p;
  333. XSSFMap map = mapInfo.getXSSFMapById(4);
  334. assertNotNull(map, "XSSFMap is null");
  335. XSSFExportToXml exporter = new XSSFExportToXml(map);
  336. assertEquals(0, exporter.compare("", ""));
  337. assertEquals(0, exporter.compare("/", "/"));
  338. assertEquals(0, exporter.compare("//", "//"));
  339. assertEquals(0, exporter.compare("/a/", "/b/"));
  340. assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR",
  341. "/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:CHF"));
  342. found = true;
  343. }
  344. assertTrue(found);
  345. }
  346. }
  347. @Test
  348. void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception {
  349. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) {
  350. boolean found = false;
  351. for (POIXMLDocumentPart p : wb.getRelations()) {
  352. if (!(p instanceof MapInfo)) {
  353. continue;
  354. }
  355. MapInfo mapInfo = (MapInfo) p;
  356. XSSFMap map = mapInfo.getXSSFMapById(4);
  357. assertNotNull(map, "XSSFMap is null");
  358. XSSFExportToXml exporter = new XSSFExportToXml(map);
  359. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  360. exporter.exportToXML(os, true);
  361. String xmlData = os.toString("UTF-8");
  362. assertNotNull(xmlData);
  363. assertFalse(xmlData.isEmpty());
  364. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  365. String a_b = a.split("<B>")[1].split("</B>")[0].trim();
  366. String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
  367. String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
  368. String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
  369. String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
  370. assertEquals("1", a_b_c_e_euro);
  371. assertEquals("2", a_b_c_e_chf);
  372. String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
  373. String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
  374. String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
  375. String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
  376. assertEquals("3", a_b_d_e_euro);
  377. assertEquals("4", a_b_d_e_chf);
  378. found = true;
  379. }
  380. assertTrue(found);
  381. }
  382. }
  383. private void parseXML(String xmlData) throws IOException, SAXException {
  384. DocumentBuilder docBuilder = XMLHelper.newDocumentBuilder();
  385. docBuilder.parse(new ByteArrayInputStream(xmlData.getBytes(StandardCharsets.UTF_8)));
  386. }
  387. @Test
  388. void testExportDataTypes() throws Exception {
  389. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) {
  390. Sheet sheet = wb.getSheetAt(0);
  391. Row row = sheet.getRow(0);
  392. Cell cString = row.createCell(0);
  393. cString.setCellValue("somestring");
  394. Cell cBoolean = row.createCell(1);
  395. cBoolean.setCellValue(true);
  396. Cell cError = row.createCell(2);
  397. cError.setCellErrorValue(FormulaError.NUM.getCode());
  398. Cell cFormulaString = row.createCell(3);
  399. cFormulaString.setCellFormula("A1");
  400. Cell cFormulaNumeric = row.createCell(4);
  401. cFormulaNumeric.setCellFormula("F1");
  402. Cell cNumeric = row.createCell(5);
  403. cNumeric.setCellValue(1.2);
  404. Cell cDate = row.createCell(6);
  405. cDate.setCellValue(new Date());
  406. boolean found = false;
  407. for (POIXMLDocumentPart p : wb.getRelations()) {
  408. if (!(p instanceof MapInfo)) {
  409. continue;
  410. }
  411. MapInfo mapInfo = (MapInfo) p;
  412. XSSFMap map = mapInfo.getXSSFMapById(4);
  413. assertNotNull(map, "XSSFMap is null");
  414. XSSFExportToXml exporter = new XSSFExportToXml(map);
  415. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  416. exporter.exportToXML(os, true);
  417. String xmlData = os.toString("UTF-8");
  418. assertNotNull(xmlData);
  419. assertFalse(xmlData.isEmpty());
  420. parseXML(xmlData);
  421. found = true;
  422. }
  423. assertTrue(found);
  424. }
  425. }
  426. @Test
  427. void testValidateFalse() throws Exception {
  428. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) {
  429. boolean found = false;
  430. for (POIXMLDocumentPart p : wb.getRelations()) {
  431. if (!(p instanceof MapInfo)) {
  432. continue;
  433. }
  434. MapInfo mapInfo = (MapInfo) p;
  435. XSSFMap map = mapInfo.getXSSFMapById(4);
  436. assertNotNull(map, "XSSFMap is null");
  437. XSSFExportToXml exporter = new XSSFExportToXml(map);
  438. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  439. exporter.exportToXML(os, false);
  440. String xmlData = os.toString("UTF-8");
  441. assertNotNull(xmlData);
  442. assertFalse(xmlData.isEmpty());
  443. parseXML(xmlData);
  444. found = true;
  445. }
  446. assertTrue(found);
  447. }
  448. }
  449. @Test
  450. void testRefElementsInXmlSchema_Bugzilla_56730() throws Exception {
  451. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.xlsx")) {
  452. boolean found = false;
  453. for (POIXMLDocumentPart p : wb.getRelations()) {
  454. if (!(p instanceof MapInfo)) {
  455. continue;
  456. }
  457. MapInfo mapInfo = (MapInfo) p;
  458. XSSFMap map = mapInfo.getXSSFMapById(1);
  459. assertNotNull(map, "XSSFMap is null");
  460. XSSFExportToXml exporter = new XSSFExportToXml(map);
  461. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  462. exporter.exportToXML(os, true);
  463. String xmlData = os.toString("UTF-8");
  464. assertNotNull(xmlData);
  465. assertFalse(xmlData.isEmpty());
  466. assertEquals("2014-12-31", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
  467. assertEquals("12.5", xmlData.split("<REFELEMENT>")[1].split("</REFELEMENT>")[0].trim());
  468. parseXML(xmlData);
  469. found = true;
  470. }
  471. assertTrue(found);
  472. }
  473. }
  474. @Test
  475. void testBug59026() throws Exception {
  476. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx")) {
  477. Collection<XSSFMap> mappings = wb.getCustomXMLMappings();
  478. assertTrue(mappings.size() > 0);
  479. for (XSSFMap map : mappings) {
  480. XSSFExportToXml exporter = new XSSFExportToXml(map);
  481. UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
  482. exporter.exportToXML(os, false);
  483. assertNotNull(os.toString("UTF-8"));
  484. }
  485. }
  486. }
  487. @Test
  488. void testExportTableWithNonMappedColumn_Bugzilla_61281() throws Exception {
  489. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61281.xlsx")) {
  490. for (XSSFMap map : wb.getCustomXMLMappings()) {
  491. XSSFExportToXml exporter = new XSSFExportToXml(map);
  492. UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
  493. exporter.exportToXML(bos, true);
  494. assertNotNull(DocumentHelper.readDocument(bos.toInputStream()));
  495. String exportedXml = bos.toString("UTF-8");
  496. assertEquals("<Test><Test>1</Test></Test>", exportedXml.replaceAll("\\s+", ""));
  497. }
  498. }
  499. }
  500. @Test
  501. void testXXEInSchema() throws Exception {
  502. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("xxe_in_schema.xlsx")) {
  503. for (XSSFMap map : wb.getCustomXMLMappings()) {
  504. XSSFExportToXml exporter = new XSSFExportToXml(map);
  505. UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
  506. assertThrows(SAXParseException.class, () -> exporter.exportToXML(bos, true));
  507. }
  508. }
  509. }
  510. }