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.

TestOfficeXMLException.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.poifs.filesystem;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.PushbackInputStream;
  20. import java.util.Arrays;
  21. import org.apache.poi.hssf.HSSFTestDataSamples;
  22. import junit.framework.TestCase;
  23. /**
  24. * Class to test that POIFS complains when given an Office 2003 XML
  25. * of Office Open XML (OOXML, 2007+) document
  26. */
  27. public class TestOfficeXMLException extends TestCase {
  28. private static final InputStream openSampleStream(String sampleFileName) {
  29. return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
  30. }
  31. public void testOOXMLException() throws IOException
  32. {
  33. InputStream in = openSampleStream("sample.xlsx");
  34. try {
  35. new POIFSFileSystem(in).close();
  36. fail("expected exception was not thrown");
  37. } catch(OfficeXmlFileException e) {
  38. // expected during successful test
  39. assertTrue(e.getMessage().indexOf("The supplied data appears to be in the Office 2007+ XML") > -1);
  40. assertTrue(e.getMessage().indexOf("You are calling the part of POI that deals with OLE2 Office Documents") > -1);
  41. }
  42. }
  43. public void test2003XMLException() throws IOException
  44. {
  45. InputStream in = openSampleStream("SampleSS.xml");
  46. try {
  47. new POIFSFileSystem(in).close();
  48. fail("expected exception was not thrown");
  49. } catch(NotOLE2FileException e) {
  50. // expected during successful test
  51. assertTrue(e.getMessage().indexOf("The supplied data appears to be a raw XML file") > -1);
  52. assertTrue(e.getMessage().indexOf("Formats such as Office 2003 XML") > -1);
  53. }
  54. }
  55. public void testDetectAsPOIFS() throws IOException {
  56. // ooxml file isn't
  57. confirmIsPOIFS("SampleSS.xlsx", false);
  58. // 2003 xml file isn't
  59. confirmIsPOIFS("SampleSS.xml", false);
  60. // xls file is
  61. confirmIsPOIFS("SampleSS.xls", true);
  62. // text file isn't
  63. confirmIsPOIFS("SampleSS.txt", false);
  64. }
  65. private void confirmIsPOIFS(String sampleFileName, boolean expectedResult) throws IOException {
  66. InputStream in = new PushbackInputStream(openSampleStream(sampleFileName), 10);
  67. try {
  68. boolean actualResult;
  69. try {
  70. actualResult = POIFSFileSystem.hasPOIFSHeader(in);
  71. } catch (IOException e) {
  72. throw new RuntimeException(e);
  73. }
  74. assertEquals(expectedResult, actualResult);
  75. } finally {
  76. in.close();
  77. }
  78. }
  79. public void testFileCorruption() throws Exception {
  80. // create test InputStream
  81. byte[] testData = { (byte)1, (byte)2, (byte)3 };
  82. InputStream testInput = new ByteArrayInputStream(testData);
  83. // detect header
  84. InputStream in = new PushbackInputStream(testInput, 10);
  85. assertFalse(POIFSFileSystem.hasPOIFSHeader(in));
  86. // check if InputStream is still intact
  87. byte[] test = new byte[3];
  88. in.read(test);
  89. assertTrue(Arrays.equals(testData, test));
  90. assertEquals(-1, in.read());
  91. }
  92. public void testFileCorruptionOPOIFS() throws Exception {
  93. // create test InputStream
  94. byte[] testData = { (byte)1, (byte)2, (byte)3 };
  95. InputStream testInput = new ByteArrayInputStream(testData);
  96. // detect header
  97. InputStream in = new PushbackInputStream(testInput, 10);
  98. assertFalse(OPOIFSFileSystem.hasPOIFSHeader(in));
  99. // check if InputStream is still intact
  100. byte[] test = new byte[3];
  101. in.read(test);
  102. assertTrue(Arrays.equals(testData, test));
  103. assertEquals(-1, in.read());
  104. }
  105. }