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.

TestReadOnlySharedStringsTable.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xssf.eventusermodel;
  20. import java.io.IOException;
  21. import java.util.List;
  22. import java.util.regex.Pattern;
  23. import junit.framework.TestCase;
  24. import org.apache.poi.POIDataSamples;
  25. import org.apache.poi.openxml4j.opc.OPCPackage;
  26. import org.apache.poi.openxml4j.opc.PackagePart;
  27. import org.apache.poi.xssf.model.SharedStringsTable;
  28. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
  30. import org.xml.sax.SAXException;
  31. /**
  32. * Tests for {@link org.apache.poi.xssf.eventusermodel.XSSFReader}
  33. */
  34. public final class TestReadOnlySharedStringsTable extends TestCase {
  35. private static POIDataSamples _ssTests = POIDataSamples.getSpreadSheetInstance();
  36. public void testParse() throws Exception {
  37. OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("SampleSS.xlsx"));
  38. List<PackagePart> parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.xml"));
  39. assertEquals(1, parts.size());
  40. SharedStringsTable stbl = new SharedStringsTable(parts.get(0));
  41. ReadOnlySharedStringsTable rtbl = new ReadOnlySharedStringsTable(parts.get(0));
  42. assertEquals(stbl.getCount(), rtbl.getCount());
  43. assertEquals(stbl.getUniqueCount(), rtbl.getUniqueCount());
  44. assertEquals(stbl.getItems().size(), stbl.getUniqueCount());
  45. assertEquals(rtbl.getItems().size(), rtbl.getUniqueCount());
  46. for(int i=0; i < stbl.getUniqueCount(); i++){
  47. CTRst i1 = stbl.getEntryAt(i);
  48. String i2 = rtbl.getEntryAt(i);
  49. assertEquals(i1.getT(), i2);
  50. }
  51. }
  52. public void testPhoneticRuns() throws Exception {
  53. OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("51519.xlsx"));
  54. List<PackagePart> parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.xml"));
  55. assertEquals(1, parts.size());
  56. ReadOnlySharedStringsTable rtbl = new ReadOnlySharedStringsTable(parts.get(0));
  57. List<String> strings = rtbl.getItems();
  58. assertEquals(49, strings.size());
  59. assertEquals("\u30B3\u30E1\u30F3\u30C8", rtbl.getEntryAt(0));
  60. assertNull(rtbl.getPhoneticStringAt(0));
  61. assertEquals("\u65E5\u672C\u30AA\u30E9\u30AF\u30EB", rtbl.getEntryAt(3));
  62. assertEquals("\u30CB\u30DB\u30F3", rtbl.getPhoneticStringAt(3));
  63. }
  64. public void testEmptySSTOnPackageObtainedViaWorkbook() throws Exception {
  65. XSSFWorkbook wb = new XSSFWorkbook(_ssTests.openResourceAsStream("noSharedStringTable.xlsx"));
  66. OPCPackage pkg = wb.getPackage();
  67. assertEmptySST(pkg);
  68. wb.close();
  69. }
  70. public void testEmptySSTOnPackageDirect() throws Exception {
  71. OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("noSharedStringTable.xlsx"));
  72. assertEmptySST(pkg);
  73. }
  74. private void assertEmptySST(OPCPackage pkg) throws IOException, SAXException {
  75. ReadOnlySharedStringsTable sst = new ReadOnlySharedStringsTable(pkg);
  76. assertEquals(0, sst.getCount());
  77. assertEquals(0, sst.getUniqueCount());
  78. assertNull(sst.getItems()); // same state it's left in if fed a package which has no SST part.
  79. }
  80. }