summaryrefslogtreecommitdiffstats
path: root/src/testcases
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2018-11-26 21:27:30 +0000
committerAndreas Beeker <kiwiwings@apache.org>2018-11-26 21:27:30 +0000
commit12baa02efaa4f3ee2ac7ac875718eb2d68c14513 (patch)
tree40b69c1945b9d2238365e3e3455b5dd4db43e7f2 /src/testcases
parentd9b2b80668e17b454ad747a085754adedb705154 (diff)
parent42f7b8b8472080ee8ab50fa9f528ff0d76232e6f (diff)
downloadpoi-REL_4_0_1.tar.gz
poi-REL_4_0_1.zip
tag r1847494 as 4.0.1REL_4_0_1
git-svn-id: https://svn.apache.org/repos/asf/poi/tags/REL_4_0_1@1847495 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
-rw-r--r--src/testcases/org/apache/poi/poifs/filesystem/TestFileMagic.java81
-rw-r--r--src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java15
-rw-r--r--src/testcases/org/apache/poi/ss/formula/SheetRangeAndWorkbookIndexFormatterTest.java66
3 files changed, 162 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..4dba721b1f
--- /dev/null
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestFileMagic.java
@@ -0,0 +1,81 @@
+/* ====================================================================
+ 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.commons.codec.Charsets;
+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(Charsets.UTF_8)));
+
+ assertEquals(FileMagic.HTML, FileMagic.valueOf("HTML"));
+ assertEquals(FileMagic.HTML, FileMagic.valueOf("<!DOCTYP".getBytes(Charsets.UTF_8)));
+ assertEquals(FileMagic.HTML, FileMagic.valueOf("<!DOCTYPE".getBytes(Charsets.UTF_8)));
+ assertEquals(FileMagic.HTML, FileMagic.valueOf("<html".getBytes(Charsets.UTF_8)));
+
+ 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));
+ }
+ }
+}
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
index 83b085bfd3..1fffceafeb 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
@@ -17,6 +17,7 @@
package org.apache.poi.poifs.filesystem;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -278,4 +279,18 @@ public final class TestPOIFSFileSystem {
private static InputStream openSampleStream(String sampleFileName) {
return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
}
+
+ @Test
+ public void fileMagics() {
+ for (FileMagic fm : FileMagic.values()) {
+ if (fm == FileMagic.UNKNOWN) {
+ continue;
+ }
+ for (byte[] b : fm.magic) {
+ assertEquals(fm, FileMagic.valueOf(b));
+ }
+ }
+
+ assertEquals(FileMagic.UNKNOWN, FileMagic.valueOf("foobaa".getBytes(UTF_8)));
+ }
}
diff --git a/src/testcases/org/apache/poi/ss/formula/SheetRangeAndWorkbookIndexFormatterTest.java b/src/testcases/org/apache/poi/ss/formula/SheetRangeAndWorkbookIndexFormatterTest.java
new file mode 100644
index 0000000000..43dd236d38
--- /dev/null
+++ b/src/testcases/org/apache/poi/ss/formula/SheetRangeAndWorkbookIndexFormatterTest.java
@@ -0,0 +1,66 @@
+/* ====================================================================
+ 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.ss.formula;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class SheetRangeAndWorkbookIndexFormatterTest {
+ @Test
+ public void noDelimiting_ifASingleSheetNameDoesntNeedDelimiting() {
+ StringBuilder sb = new StringBuilder();
+ String result = SheetRangeAndWorkbookIndexFormatter.format(sb, 0, "noDelimiting", null);
+ assertEquals("[0]noDelimiting", result);
+ }
+
+ @Test
+ public void everythingIsScreened_ifASingleSheetNameNeedsDelimiting() {
+ StringBuilder sb = new StringBuilder();
+ String result = SheetRangeAndWorkbookIndexFormatter.format(sb, 0, "1delimiting", null);
+ assertEquals("'[0]1delimiting'", result);
+ }
+
+ @Test
+ public void noDelimiting_ifBothSheetNamesDontNeedDelimiting() {
+ StringBuilder sb = new StringBuilder();
+ String result = SheetRangeAndWorkbookIndexFormatter.format(sb, 0, "noDelimiting1", "noDelimiting2");
+ assertEquals("[0]noDelimiting1:noDelimiting2", result);
+ }
+
+ @Test
+ public void everythingIsScreened_ifFirstSheetNamesNeedsDelimiting() {
+ StringBuilder sb = new StringBuilder();
+ String result = SheetRangeAndWorkbookIndexFormatter.format(sb, 0, "1delimiting", "noDelimiting");
+ assertEquals("'[0]1delimiting:noDelimiting'", result);
+ }
+
+ @Test
+ public void everythingIsScreened_ifLastSheetNamesNeedsDelimiting() {
+ StringBuilder sb = new StringBuilder();
+ String result = SheetRangeAndWorkbookIndexFormatter.format(sb, 0, "noDelimiting", "1delimiting");
+ assertEquals("'[0]noDelimiting:1delimiting'", result);
+ }
+
+ @Test
+ public void everythingIsScreened_ifBothSheetNamesNeedDelimiting() {
+ StringBuilder sb = new StringBuilder();
+ String result = SheetRangeAndWorkbookIndexFormatter.format(sb, 0, "1delimiting", "2delimiting");
+ assertEquals("'[0]1delimiting:2delimiting'", result);
+ }
+}