您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TestXSSFImportFromXML.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import java.io.IOException;
  22. import java.text.DateFormatSymbols;
  23. import java.text.ParseException;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Date;
  26. import java.util.Locale;
  27. import javax.xml.xpath.XPathExpressionException;
  28. import org.apache.poi.ss.usermodel.CellType;
  29. import org.apache.poi.xssf.XSSFTestDataSamples;
  30. import org.apache.poi.xssf.usermodel.XSSFMap;
  31. import org.apache.poi.xssf.usermodel.XSSFRow;
  32. import org.apache.poi.xssf.usermodel.XSSFSheet;
  33. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  34. import org.junit.Test;
  35. import org.xml.sax.SAXException;
  36. public class TestXSSFImportFromXML {
  37. @Test
  38. public void testImportFromXML() throws IOException, XPathExpressionException, SAXException{
  39. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx")) {
  40. String name = "name";
  41. String teacher = "teacher";
  42. String tutor = "tutor";
  43. String cdl = "cdl";
  44. String duration = "duration";
  45. String topic = "topic";
  46. String project = "project";
  47. String credits = "credits";
  48. String testXML = "<CORSO>" +
  49. "<NOME>" + name + "</NOME>" +
  50. "<DOCENTE>" + teacher + "</DOCENTE>" +
  51. "<TUTOR>" + tutor + "</TUTOR>" +
  52. "<CDL>" + cdl + "</CDL>" +
  53. "<DURATA>" + duration + "</DURATA>" +
  54. "<ARGOMENTO>" + topic + "</ARGOMENTO>" +
  55. "<PROGETTO>" + project + "</PROGETTO>" +
  56. "<CREDITI>" + credits + "</CREDITI>" +
  57. "</CORSO>\u0000";
  58. XSSFMap map = wb.getMapInfo().getXSSFMapByName("CORSO_mapping");
  59. assertNotNull(map);
  60. XSSFImportFromXML importer = new XSSFImportFromXML(map);
  61. importer.importFromXML(testXML);
  62. XSSFSheet sheet = wb.getSheetAt(0);
  63. XSSFRow row = sheet.getRow(0);
  64. assertTrue(row.getCell(0).getStringCellValue().equals(name));
  65. assertTrue(row.getCell(1).getStringCellValue().equals(teacher));
  66. assertTrue(row.getCell(2).getStringCellValue().equals(tutor));
  67. assertTrue(row.getCell(3).getStringCellValue().equals(cdl));
  68. assertTrue(row.getCell(4).getStringCellValue().equals(duration));
  69. assertTrue(row.getCell(5).getStringCellValue().equals(topic));
  70. assertTrue(row.getCell(6).getStringCellValue().equals(project));
  71. assertTrue(row.getCell(7).getStringCellValue().equals(credits));
  72. }
  73. }
  74. @Test(timeout=60000)
  75. public void testMultiTable() throws IOException, XPathExpressionException, SAXException{
  76. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx")) {
  77. String cellC6 = "c6";
  78. String cellC7 = "c7";
  79. String cellC8 = "c8";
  80. String cellC9 = "c9";
  81. StringBuilder testXML = new StringBuilder("<ns1:MapInfo xmlns:ns1=\"" + NS_SPREADSHEETML + "\" SelectionNamespaces=\"\">" +
  82. "<ns1:Schema ID=\"" + cellC6 + "\" SchemaRef=\"a\" />" +
  83. "<ns1:Schema ID=\"" + cellC7 + "\" SchemaRef=\"b\" />" +
  84. "<ns1:Schema ID=\"" + cellC8 + "\" SchemaRef=\"c\" />" +
  85. "<ns1:Schema ID=\"" + cellC9 + "\" SchemaRef=\"d\" />");
  86. int cellOffset = 10; // cell C10
  87. for (int i = 0; i < 10000; i++) {
  88. testXML.append("<ns1:Schema ID=\"c").append(i + cellOffset).append("\" SchemaRef=\"d\" />");
  89. }
  90. testXML.append("<ns1:Map ID=\"1\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\" AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">" + "<ns1:DataBinding DataBindingLoadMode=\"\" />" + "</ns1:Map>" + "<ns1:Map ID=\"2\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\" AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">" + "<ns1:DataBinding DataBindingLoadMode=\"\" />" + "</ns1:Map>" + "<ns1:Map ID=\"3\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\" AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">" + "<ns1:DataBinding DataBindingLoadMode=\"\" />" + "</ns1:Map>" + "</ns1:MapInfo>\u0000");
  91. XSSFMap map = wb.getMapInfo().getXSSFMapByName("MapInfo_mapping");
  92. assertNotNull(map);
  93. XSSFImportFromXML importer = new XSSFImportFromXML(map);
  94. importer.importFromXML(testXML.toString());
  95. //Check for Schema element
  96. XSSFSheet sheet = wb.getSheetAt(1);
  97. // check table size (+1 for the header row)
  98. assertEquals(3 + 1, wb.getTable("Tabella1").getRowCount());
  99. assertEquals(10004 + 1, wb.getTable("Tabella2").getRowCount());
  100. // table1 size was reduced, check that former table cells have been cleared
  101. assertEquals(CellType.BLANK, wb.getSheetAt(0).getRow(8).getCell(5).getCellType());
  102. // table2 size was increased, check that new table cells have been cleared
  103. assertEquals(CellType.BLANK, sheet.getRow(10).getCell(3).getCellType());
  104. assertEquals(cellC6, sheet.getRow(5).getCell(2).getStringCellValue());
  105. assertEquals(cellC7, sheet.getRow(6).getCell(2).getStringCellValue());
  106. assertEquals(cellC8, sheet.getRow(7).getCell(2).getStringCellValue());
  107. assertEquals(cellC9, sheet.getRow(8).getCell(2).getStringCellValue());
  108. assertEquals("c5001", sheet.getRow(5000).getCell(2).getStringCellValue());
  109. }
  110. }
  111. @Test
  112. public void testSingleAttributeCellWithNamespace() throws IOException, XPathExpressionException, SAXException{
  113. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx")) {
  114. int id = 1;
  115. String displayName = "dispName";
  116. String ref = "19";
  117. int count = 21;
  118. String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>" +
  119. "<ns1:table xmlns:ns1=\"" + NS_SPREADSHEETML + "\" id=\"" + id + "\" displayName=\"" + displayName + "\" ref=\"" + ref + "\">" +
  120. "<ns1:tableColumns count=\"" + count + "\" />" +
  121. "</ns1:table>\u0000";
  122. XSSFMap map = wb.getMapInfo().getXSSFMapByName("table_mapping");
  123. assertNotNull(map);
  124. XSSFImportFromXML importer = new XSSFImportFromXML(map);
  125. importer.importFromXML(testXML);
  126. //Check for Schema element
  127. XSSFSheet sheet = wb.getSheetAt(0);
  128. assertEquals(new Double(id), sheet.getRow(28).getCell(1).getNumericCellValue(), 0);
  129. assertEquals(displayName, sheet.getRow(11).getCell(5).getStringCellValue());
  130. assertEquals(ref, sheet.getRow(14).getCell(7).getStringCellValue());
  131. assertEquals(new Double(count), sheet.getRow(18).getCell(3).getNumericCellValue(), 0);
  132. }
  133. }
  134. @Test
  135. public void testOptionalFields_Bugzilla_55864() throws IOException, XPathExpressionException, SAXException {
  136. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55864.xlsx")) {
  137. String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
  138. "<PersonInfoRoot>" +
  139. "<PersonData>" +
  140. "<FirstName>Albert</FirstName>" +
  141. "<LastName>Einstein</LastName>" +
  142. "<BirthDate>1879-03-14</BirthDate>" +
  143. "</PersonData>" +
  144. "</PersonInfoRoot>";
  145. XSSFMap map = wb.getMapInfo().getXSSFMapByName("PersonInfoRoot_Map");
  146. assertNotNull(map);
  147. XSSFImportFromXML importer = new XSSFImportFromXML(map);
  148. importer.importFromXML(testXML);
  149. XSSFSheet sheet = wb.getSheetAt(0);
  150. XSSFRow rowHeadings = sheet.getRow(0);
  151. XSSFRow rowData = sheet.getRow(1);
  152. assertEquals("FirstName", rowHeadings.getCell(0).getStringCellValue());
  153. assertEquals("Albert", rowData.getCell(0).getStringCellValue());
  154. assertEquals("LastName", rowHeadings.getCell(1).getStringCellValue());
  155. assertEquals("Einstein", rowData.getCell(1).getStringCellValue());
  156. assertEquals("BirthDate", rowHeadings.getCell(2).getStringCellValue());
  157. assertEquals("1879-03-14", rowData.getCell(2).getStringCellValue());
  158. // Value for OptionalRating is declared optional (minOccurs=0) in 55864.xlsx
  159. assertEquals("OptionalRating", rowHeadings.getCell(3).getStringCellValue());
  160. assertNull("", rowData.getCell(3));
  161. }
  162. }
  163. @Test
  164. public void testOptionalFields_Bugzilla_57890() throws IOException, ParseException, XPathExpressionException, SAXException {
  165. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57890.xlsx");
  166. String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<TestInfoRoot>"
  167. + "<TestData>" + "<Int>" + Integer.MIN_VALUE + "</Int>" + "<UnsignedInt>12345</UnsignedInt>"
  168. + "<double>1.0000123</double>" + "<Date>1991-03-14</Date>" + "</TestData>" + "</TestInfoRoot>";
  169. XSSFMap map = wb.getMapInfo().getXSSFMapByName("TestInfoRoot_Map");
  170. assertNotNull(map);
  171. XSSFImportFromXML importer = new XSSFImportFromXML(map);
  172. importer.importFromXML(testXML);
  173. XSSFSheet sheet = wb.getSheetAt(0);
  174. XSSFRow rowHeadings = sheet.getRow(0);
  175. XSSFRow rowData = sheet.getRow(1);
  176. assertEquals("Date", rowHeadings.getCell(0).getStringCellValue());
  177. Date date = new SimpleDateFormat("yyyy-MM-dd", DateFormatSymbols.getInstance(Locale.ROOT)).parse("1991-3-14");
  178. assertEquals(date, rowData.getCell(0).getDateCellValue());
  179. assertEquals("Amount Int", rowHeadings.getCell(1).getStringCellValue());
  180. assertEquals(new Double(Integer.MIN_VALUE), rowData.getCell(1).getNumericCellValue(), 0);
  181. assertEquals("Amount Double", rowHeadings.getCell(2).getStringCellValue());
  182. assertEquals(1.0000123, rowData.getCell(2).getNumericCellValue(), 0);
  183. assertEquals("Amount UnsignedInt", rowHeadings.getCell(3).getStringCellValue());
  184. assertEquals(new Double(12345), rowData.getCell(3).getNumericCellValue(), 0);
  185. wb.close();
  186. }
  187. }