aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/poifs
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2018-11-25 21:56:43 +0000
committerDominik Stadler <centic@apache.org>2018-11-25 21:56:43 +0000
commitd28720afd08d1998a7784f09e6cfd5ed9bec2dc9 (patch)
tree98e3741a997796f090353cb473a4602973c90d93 /src/testcases/org/apache/poi/poifs
parent061db56f2d5026b9c2e698ccf8773153b8942d68 (diff)
downloadpoi-d28720afd08d1998a7784f09e6cfd5ed9bec2dc9.tar.gz
poi-d28720afd08d1998a7784f09e6cfd5ed9bec2dc9.zip
Add test for FileMagic
JavaDoc warning fixes Remove some IDE warnings Reformat code of sample application git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1847437 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/poifs')
-rw-r--r--src/testcases/org/apache/poi/poifs/filesystem/TestFileMagic.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestFileMagic.java b/src/testcases/org/apache/poi/poifs/filesystem/TestFileMagic.java
new file mode 100644
index 0000000000..6587547b95
--- /dev/null
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestFileMagic.java
@@ -0,0 +1,80 @@
+/* ====================================================================
+ 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.
+==================================================================== */
+
+package org.apache.poi.poifs.filesystem;
+
+import org.apache.poi.POIDataSamples;
+import org.junit.Test;
+
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.junit.Assert.*;
+
+public class TestFileMagic {
+ @Test
+ public void testFileMagic() {
+ assertEquals(FileMagic.XML, FileMagic.valueOf("XML"));
+ assertEquals(FileMagic.XML, FileMagic.valueOf("<?xml".getBytes()));
+
+ assertEquals(FileMagic.HTML, FileMagic.valueOf("HTML"));
+ assertEquals(FileMagic.HTML, FileMagic.valueOf("<!DOCTYP".getBytes()));
+ assertEquals(FileMagic.HTML, FileMagic.valueOf("<!DOCTYPE".getBytes()));
+ assertEquals(FileMagic.HTML, FileMagic.valueOf("<html".getBytes()));
+
+ try {
+ FileMagic.valueOf("some string");
+ fail("Should catch exception here");
+ } catch (IllegalArgumentException e) {
+ // expected here
+ }
+ }
+
+ @Test
+ public void testFileMagicFile() throws IOException {
+ assertEquals(FileMagic.OLE2, FileMagic.valueOf(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls")));
+ assertEquals(FileMagic.OOXML, FileMagic.valueOf(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xlsx")));
+ }
+
+ @Test
+ public void testFileMagicStream() throws IOException {
+ try (InputStream stream = new BufferedInputStream(new FileInputStream(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls")))) {
+ assertEquals(FileMagic.OLE2, FileMagic.valueOf(stream));
+ }
+ try (InputStream stream = new BufferedInputStream(new FileInputStream(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xlsx")))) {
+ assertEquals(FileMagic.OOXML, FileMagic.valueOf(stream));
+ }
+ }
+
+ @Test
+ public void testPrepare() throws IOException {
+ try (InputStream stream = new BufferedInputStream(new FileInputStream(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xlsx")))) {
+ assertSame(stream, FileMagic.prepareToCheckMagic(stream));
+ }
+
+ try (InputStream stream = new InputStream() {
+ @Override
+ public int read() {
+ return 0;
+ }
+ }) {
+ assertNotSame(stream, FileMagic.prepareToCheckMagic(stream));
+ }
+ }
+}