2008-01-16 07:46:43 -05:00
|
|
|
|
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
|
2008-03-09 10:21:34 -04:00
|
|
|
package org.apache.poi;
|
2008-01-16 07:46:43 -05:00
|
|
|
|
2015-07-20 10:03:35 -04:00
|
|
|
import java.io.ByteArrayInputStream;
|
2014-12-28 04:16:57 -05:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.PushbackInputStream;
|
2015-07-20 10:03:35 -04:00
|
|
|
import java.util.Arrays;
|
2014-12-28 04:16:57 -05:00
|
|
|
|
2008-01-16 07:46:43 -05:00
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
2009-08-19 14:51:44 -04:00
|
|
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
|
|
|
import org.apache.poi.openxml4j.opc.OPCPackage;
|
|
|
|
|
2008-01-16 07:46:43 -05:00
|
|
|
/**
|
|
|
|
* Class to test that HXF correctly detects OOXML
|
|
|
|
* documents
|
|
|
|
*/
|
|
|
|
public class TestDetectAsOOXML extends TestCase
|
|
|
|
{
|
|
|
|
public void testOpensProperly() throws Exception
|
|
|
|
{
|
2009-08-19 14:51:44 -04:00
|
|
|
OPCPackage.open(HSSFTestDataSamples.openSampleFileStream("sample.xlsx"));
|
2008-01-16 07:46:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testDetectAsPOIFS() throws Exception {
|
|
|
|
InputStream in;
|
|
|
|
|
|
|
|
// ooxml file is
|
|
|
|
in = new PushbackInputStream(
|
2009-08-19 14:51:44 -04:00
|
|
|
HSSFTestDataSamples.openSampleFileStream("SampleSS.xlsx"), 10
|
2008-01-16 07:46:43 -05:00
|
|
|
);
|
2008-03-09 10:21:34 -04:00
|
|
|
assertTrue(POIXMLDocument.hasOOXMLHeader(in));
|
2014-12-28 04:16:57 -05:00
|
|
|
in.close();
|
2008-01-16 07:46:43 -05:00
|
|
|
|
|
|
|
// xls file isn't
|
|
|
|
in = new PushbackInputStream(
|
2009-08-19 14:51:44 -04:00
|
|
|
HSSFTestDataSamples.openSampleFileStream("SampleSS.xls"), 10
|
2008-01-16 07:46:43 -05:00
|
|
|
);
|
2008-03-09 10:21:34 -04:00
|
|
|
assertFalse(POIXMLDocument.hasOOXMLHeader(in));
|
2014-12-28 04:16:57 -05:00
|
|
|
in.close();
|
2008-01-16 07:46:43 -05:00
|
|
|
|
|
|
|
// text file isn't
|
|
|
|
in = new PushbackInputStream(
|
2009-08-19 14:51:44 -04:00
|
|
|
HSSFTestDataSamples.openSampleFileStream("SampleSS.txt"), 10
|
2008-01-16 07:46:43 -05:00
|
|
|
);
|
2008-03-09 10:21:34 -04:00
|
|
|
assertFalse(POIXMLDocument.hasOOXMLHeader(in));
|
2014-12-28 04:16:57 -05:00
|
|
|
in.close();
|
2008-01-16 07:46:43 -05:00
|
|
|
}
|
2015-07-20 10:03:35 -04:00
|
|
|
|
|
|
|
public void testFileCorruption() throws Exception {
|
|
|
|
|
|
|
|
// create test InputStream
|
|
|
|
byte[] testData = { (byte)1, (byte)2, (byte)3 };
|
|
|
|
ByteArrayInputStream testInput = new ByteArrayInputStream(testData);
|
|
|
|
|
|
|
|
// detect header
|
|
|
|
InputStream in = new PushbackInputStream(testInput, 10);
|
|
|
|
assertFalse(POIXMLDocument.hasOOXMLHeader(in));
|
|
|
|
|
|
|
|
// check if InputStream is still intact
|
|
|
|
byte[] test = new byte[3];
|
|
|
|
in.read(test);
|
|
|
|
assertTrue(Arrays.equals(testData, test));
|
|
|
|
assertEquals(-1, in.read());
|
|
|
|
}
|
|
|
|
|
2008-01-16 07:46:43 -05:00
|
|
|
}
|