-\r
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
- \r
-package org.apache.poi.ddf;\r
-\r
-import junit.framework.TestCase;\r
-import org.apache.poi.util.HexRead;\r
-import org.apache.poi.util.HexDump;\r
-\r
-import java.io.IOException;\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.util.Iterator;\r
-import java.util.Arrays;\r
-\r
-/**\r
- * Test read/serialize of escher blip records\r
- *\r
- * @author Yegor Kozlov\r
- */\r
-public class TestEscherBlipRecord extends TestCase\r
-{\r
- protected String cwd = System.getProperty("DDF.testdata.path");\r
-\r
- //test reading/serializing of a PNG blip\r
- public void testReadPNG() throws IOException {\r
- //provided in bug-44886\r
- byte[] data = read(new File(cwd, "Container.dat"));\r
-\r
- EscherContainerRecord record = new EscherContainerRecord();\r
- record.fillFields(data, 0, new DefaultEscherRecordFactory());\r
- EscherContainerRecord bstore = (EscherContainerRecord)record.getChild(1);\r
- EscherBSERecord bse1 = (EscherBSERecord)bstore.getChild(0);\r
- assertEquals(EscherBSERecord.BT_PNG, bse1.getBlipTypeWin32());\r
- assertEquals(EscherBSERecord.BT_PNG, bse1.getBlipTypeMacOS());\r
- assertTrue(Arrays.equals(new byte[]{\r
- 0x65, 0x07, 0x4A, (byte)0x8D, 0x3E, 0x42, (byte)0x8B, (byte)0xAC,\r
- 0x1D, (byte)0x89, 0x35, 0x4F, 0x48, (byte)0xFA, 0x37, (byte)0xC2\r
- }, bse1.getUid()));\r
- assertEquals(255, bse1.getTag());\r
- assertEquals(32308, bse1.getSize());\r
-\r
- EscherBitmapBlip blip1 = (EscherBitmapBlip)bse1.getBlipRecord();\r
- assertEquals(0x6E00, blip1.getOptions());\r
- assertEquals(EscherBitmapBlip.RECORD_ID_PNG, blip1.getRecordId());\r
- assertTrue(Arrays.equals(new byte[]{\r
- 0x65, 0x07, 0x4A, (byte)0x8D, 0x3E, 0x42, (byte)0x8B, (byte)0xAC,\r
- 0x1D, (byte)0x89, 0x35, 0x4F, 0x48, (byte)0xFA, 0x37, (byte)0xC2\r
- }, blip1.getUID()));\r
-\r
- //serialize and read again\r
- byte[] ser = bse1.serialize();\r
- EscherBSERecord bse2 = new EscherBSERecord();\r
- bse2.fillFields(ser, 0, new DefaultEscherRecordFactory());\r
- assertEquals(bse1.getRecordId(), bse2.getRecordId());\r
- assertEquals(bse1.getBlipTypeWin32(), bse2.getBlipTypeWin32());\r
- assertEquals(bse1.getBlipTypeMacOS(), bse2.getBlipTypeMacOS());\r
- assertTrue(Arrays.equals(bse1.getUid(), bse2.getUid()));\r
- assertEquals(bse1.getTag(), bse2.getTag());\r
- assertEquals(bse1.getSize(), bse2.getSize());\r
-\r
- EscherBitmapBlip blip2 = (EscherBitmapBlip)bse1.getBlipRecord();\r
- assertEquals(blip1.getOptions(), blip2.getOptions());\r
- assertEquals(blip1.getRecordId(), blip2.getRecordId());\r
- assertEquals(blip1.getUID(), blip2.getUID());\r
-\r
- assertTrue(Arrays.equals(blip1.getPicturedata(), blip1.getPicturedata()));\r
- }\r
-\r
- //test reading/serializing of a PICT metafile\r
- public void testReadPICT() throws IOException {\r
- //provided in bug-44886\r
- byte[] data = read(new File(cwd, "Container.dat"));\r
-\r
- EscherContainerRecord record = new EscherContainerRecord();\r
- record.fillFields(data, 0, new DefaultEscherRecordFactory());\r
- EscherContainerRecord bstore = (EscherContainerRecord)record.getChild(1);\r
- EscherBSERecord bse1 = (EscherBSERecord)bstore.getChild(1);\r
- //System.out.println(bse1);\r
- assertEquals(EscherBSERecord.BT_WMF, bse1.getBlipTypeWin32());\r
- assertEquals(EscherBSERecord.BT_PICT, bse1.getBlipTypeMacOS());\r
- assertTrue(Arrays.equals(new byte[]{\r
- (byte)0xC7, 0x15, 0x69, 0x2D, (byte)0xE5, (byte)0x89, (byte)0xA3, 0x6F,\r
- 0x66, 0x03, (byte)0xD6, 0x24, (byte)0xF7, (byte)0xDB, 0x1D, 0x13\r
- }, bse1.getUid()));\r
- assertEquals(255, bse1.getTag());\r
- assertEquals(1133, bse1.getSize());\r
-\r
- EscherMetafileBlip blip1 = (EscherMetafileBlip)bse1.getBlipRecord();\r
- assertEquals(0x5430, blip1.getOptions());\r
- assertEquals(EscherMetafileBlip.RECORD_ID_PICT, blip1.getRecordId());\r
- assertTrue(Arrays.equals(new byte[]{\r
- 0x57, 0x32, 0x7B, (byte)0x91, 0x23, 0x5D, (byte)0xDB, 0x36,\r
- 0x7A, (byte)0xDB, (byte)0xFF, 0x17, (byte)0xFE, (byte)0xF3, (byte)0xA7, 0x05\r
- }, blip1.getUID()));\r
- assertTrue(Arrays.equals(new byte[]{\r
- (byte)0xC7, 0x15, 0x69, 0x2D, (byte)0xE5, (byte)0x89, (byte)0xA3, 0x6F,\r
- 0x66, 0x03, (byte)0xD6, 0x24, (byte)0xF7, (byte)0xDB, 0x1D, 0x13\r
- }, blip1.getPrimaryUID()));\r
-\r
- //serialize and read again\r
- byte[] ser = bse1.serialize();\r
- EscherBSERecord bse2 = new EscherBSERecord();\r
- bse2.fillFields(ser, 0, new DefaultEscherRecordFactory());\r
- assertEquals(bse1.getRecordId(), bse2.getRecordId());\r
- assertEquals(bse1.getOptions(), bse2.getOptions());\r
- assertEquals(bse1.getBlipTypeWin32(), bse2.getBlipTypeWin32());\r
- assertEquals(bse1.getBlipTypeMacOS(), bse2.getBlipTypeMacOS());\r
- assertTrue(Arrays.equals(bse1.getUid(), bse2.getUid()));\r
- assertEquals(bse1.getTag(), bse2.getTag());\r
- assertEquals(bse1.getSize(), bse2.getSize());\r
-\r
- EscherMetafileBlip blip2 = (EscherMetafileBlip)bse1.getBlipRecord();\r
- assertEquals(blip1.getOptions(), blip2.getOptions());\r
- assertEquals(blip1.getRecordId(), blip2.getRecordId());\r
- assertEquals(blip1.getUID(), blip2.getUID());\r
- assertEquals(blip1.getPrimaryUID(), blip2.getPrimaryUID());\r
-\r
- assertTrue(Arrays.equals(blip1.getPicturedata(), blip1.getPicturedata()));\r
- }\r
-\r
- //integral test: check that the read-write-read round trip is consistent\r
- public void testContainer() throws IOException {\r
- byte[] data = read(new File(cwd, "Container.dat"));\r
-\r
- EscherContainerRecord record = new EscherContainerRecord();\r
- record.fillFields(data, 0, new DefaultEscherRecordFactory());\r
-\r
- byte[] ser = record.serialize();\r
- assertTrue(Arrays.equals(data, ser));\r
- }\r
-\r
- private byte[] read(File file) throws IOException {\r
- byte[] data = new byte[(int)file.length()];\r
- FileInputStream is = new FileInputStream(file);\r
- is.read(data);\r
- is.close();\r
- return data;\r
- }\r
-\r
-}\r
+/* ====================================================================
+ 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.ddf;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+/**
+ * Test read/serialize of escher blip records
+ *
+ * @author Yegor Kozlov
+ */
+public final class TestEscherBlipRecord extends TestCase {
+ protected String cwd = System.getProperty("DDF.testdata.path");
+
+ //test reading/serializing of a PNG blip
+ public void testReadPNG() {
+ //provided in bug-44886
+ byte[] data = read(new File(cwd, "Container.dat"));
+
+ EscherContainerRecord record = new EscherContainerRecord();
+ record.fillFields(data, 0, new DefaultEscherRecordFactory());
+ EscherContainerRecord bstore = (EscherContainerRecord)record.getChild(1);
+ EscherBSERecord bse1 = (EscherBSERecord)bstore.getChild(0);
+ assertEquals(EscherBSERecord.BT_PNG, bse1.getBlipTypeWin32());
+ assertEquals(EscherBSERecord.BT_PNG, bse1.getBlipTypeMacOS());
+ assertTrue(Arrays.equals(new byte[]{
+ 0x65, 0x07, 0x4A, (byte)0x8D, 0x3E, 0x42, (byte)0x8B, (byte)0xAC,
+ 0x1D, (byte)0x89, 0x35, 0x4F, 0x48, (byte)0xFA, 0x37, (byte)0xC2
+ }, bse1.getUid()));
+ assertEquals(255, bse1.getTag());
+ assertEquals(32308, bse1.getSize());
+
+ EscherBitmapBlip blip1 = (EscherBitmapBlip)bse1.getBlipRecord();
+ assertEquals(0x6E00, blip1.getOptions());
+ assertEquals(EscherBitmapBlip.RECORD_ID_PNG, blip1.getRecordId());
+ assertTrue(Arrays.equals(new byte[]{
+ 0x65, 0x07, 0x4A, (byte)0x8D, 0x3E, 0x42, (byte)0x8B, (byte)0xAC,
+ 0x1D, (byte)0x89, 0x35, 0x4F, 0x48, (byte)0xFA, 0x37, (byte)0xC2
+ }, blip1.getUID()));
+
+ //serialize and read again
+ byte[] ser = bse1.serialize();
+ EscherBSERecord bse2 = new EscherBSERecord();
+ bse2.fillFields(ser, 0, new DefaultEscherRecordFactory());
+ assertEquals(bse1.getRecordId(), bse2.getRecordId());
+ assertEquals(bse1.getBlipTypeWin32(), bse2.getBlipTypeWin32());
+ assertEquals(bse1.getBlipTypeMacOS(), bse2.getBlipTypeMacOS());
+ assertTrue(Arrays.equals(bse1.getUid(), bse2.getUid()));
+ assertEquals(bse1.getTag(), bse2.getTag());
+ assertEquals(bse1.getSize(), bse2.getSize());
+
+ EscherBitmapBlip blip2 = (EscherBitmapBlip)bse1.getBlipRecord();
+ assertEquals(blip1.getOptions(), blip2.getOptions());
+ assertEquals(blip1.getRecordId(), blip2.getRecordId());
+ assertEquals(blip1.getUID(), blip2.getUID());
+
+ assertTrue(Arrays.equals(blip1.getPicturedata(), blip1.getPicturedata()));
+ }
+
+ //test reading/serializing of a PICT metafile
+ public void testReadPICT() {
+ //provided in bug-44886
+ byte[] data = read(new File(cwd, "Container.dat"));
+
+ EscherContainerRecord record = new EscherContainerRecord();
+ record.fillFields(data, 0, new DefaultEscherRecordFactory());
+ EscherContainerRecord bstore = (EscherContainerRecord)record.getChild(1);
+ EscherBSERecord bse1 = (EscherBSERecord)bstore.getChild(1);
+ //System.out.println(bse1);
+ assertEquals(EscherBSERecord.BT_WMF, bse1.getBlipTypeWin32());
+ assertEquals(EscherBSERecord.BT_PICT, bse1.getBlipTypeMacOS());
+ assertTrue(Arrays.equals(new byte[]{
+ (byte)0xC7, 0x15, 0x69, 0x2D, (byte)0xE5, (byte)0x89, (byte)0xA3, 0x6F,
+ 0x66, 0x03, (byte)0xD6, 0x24, (byte)0xF7, (byte)0xDB, 0x1D, 0x13
+ }, bse1.getUid()));
+ assertEquals(255, bse1.getTag());
+ assertEquals(1133, bse1.getSize());
+
+ EscherMetafileBlip blip1 = (EscherMetafileBlip)bse1.getBlipRecord();
+ assertEquals(0x5430, blip1.getOptions());
+ assertEquals(EscherMetafileBlip.RECORD_ID_PICT, blip1.getRecordId());
+ assertTrue(Arrays.equals(new byte[]{
+ 0x57, 0x32, 0x7B, (byte)0x91, 0x23, 0x5D, (byte)0xDB, 0x36,
+ 0x7A, (byte)0xDB, (byte)0xFF, 0x17, (byte)0xFE, (byte)0xF3, (byte)0xA7, 0x05
+ }, blip1.getUID()));
+ assertTrue(Arrays.equals(new byte[]{
+ (byte)0xC7, 0x15, 0x69, 0x2D, (byte)0xE5, (byte)0x89, (byte)0xA3, 0x6F,
+ 0x66, 0x03, (byte)0xD6, 0x24, (byte)0xF7, (byte)0xDB, 0x1D, 0x13
+ }, blip1.getPrimaryUID()));
+
+ //serialize and read again
+ byte[] ser = bse1.serialize();
+ EscherBSERecord bse2 = new EscherBSERecord();
+ bse2.fillFields(ser, 0, new DefaultEscherRecordFactory());
+ assertEquals(bse1.getRecordId(), bse2.getRecordId());
+ assertEquals(bse1.getOptions(), bse2.getOptions());
+ assertEquals(bse1.getBlipTypeWin32(), bse2.getBlipTypeWin32());
+ assertEquals(bse1.getBlipTypeMacOS(), bse2.getBlipTypeMacOS());
+ assertTrue(Arrays.equals(bse1.getUid(), bse2.getUid()));
+ assertEquals(bse1.getTag(), bse2.getTag());
+ assertEquals(bse1.getSize(), bse2.getSize());
+
+ EscherMetafileBlip blip2 = (EscherMetafileBlip)bse1.getBlipRecord();
+ assertEquals(blip1.getOptions(), blip2.getOptions());
+ assertEquals(blip1.getRecordId(), blip2.getRecordId());
+ assertEquals(blip1.getUID(), blip2.getUID());
+ assertEquals(blip1.getPrimaryUID(), blip2.getPrimaryUID());
+
+ assertTrue(Arrays.equals(blip1.getPicturedata(), blip1.getPicturedata()));
+ }
+
+ //integral test: check that the read-write-read round trip is consistent
+ public void testContainer() {
+ byte[] data = read(new File(cwd, "Container.dat"));
+
+ EscherContainerRecord record = new EscherContainerRecord();
+ record.fillFields(data, 0, new DefaultEscherRecordFactory());
+
+ byte[] ser = record.serialize();
+ assertTrue(Arrays.equals(data, ser));
+ }
+
+ private byte[] read(File file) {
+ byte[] data = new byte[(int)file.length()];
+ try {
+ FileInputStream is = new FileInputStream(file);
+ is.read(data);
+ is.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ return data;
+ }
+
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf;\r
-\r
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
-import org.apache.poi.ss.ITestDataProvider;\r
-import org.apache.poi.ss.SpreadsheetVersion;\r
-import org.apache.poi.ss.usermodel.Workbook;\r
-\r
-/**\r
- * @author Yegor Kozlov\r
- */\r
-public final class HSSFITestDataProvider implements ITestDataProvider {\r
-\r
- public HSSFWorkbook openSampleWorkbook(String sampleFileName) {\r
- return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);\r
- }\r
-\r
- public HSSFWorkbook writeOutAndReadBack(Workbook original) {\r
- if(!(original instanceof HSSFWorkbook)) {\r
- throw new IllegalArgumentException("Expected an instance of HSSFWorkbook");\r
- }\r
-\r
- return HSSFTestDataSamples.writeOutAndReadBack((HSSFWorkbook)original);\r
- }\r
-\r
- public HSSFWorkbook createWorkbook(){\r
- return new HSSFWorkbook();\r
- }\r
-\r
- public byte[] getTestDataFileContent(String fileName) {\r
- return HSSFTestDataSamples.getTestDataFileContent(fileName);\r
- }\r
- \r
- public SpreadsheetVersion getSpreadsheetVersion(){\r
- return SpreadsheetVersion.EXCEL97;\r
- }\r
-\r
- private HSSFITestDataProvider(){}\r
- private static HSSFITestDataProvider inst = new HSSFITestDataProvider();\r
- public static HSSFITestDataProvider getInstance(){\r
- return inst;\r
- }\r
-}
\ No newline at end of file
+/* ====================================================================
+ 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.hssf;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.ss.usermodel.Workbook;
+
+/**
+ * @author Yegor Kozlov
+ */
+public final class HSSFITestDataProvider implements ITestDataProvider {
+
+ public HSSFWorkbook openSampleWorkbook(String sampleFileName) {
+ return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
+ }
+
+ public HSSFWorkbook writeOutAndReadBack(Workbook original) {
+ if(!(original instanceof HSSFWorkbook)) {
+ throw new IllegalArgumentException("Expected an instance of HSSFWorkbook");
+ }
+
+ return HSSFTestDataSamples.writeOutAndReadBack((HSSFWorkbook)original);
+ }
+
+ public HSSFWorkbook createWorkbook(){
+ return new HSSFWorkbook();
+ }
+
+ public byte[] getTestDataFileContent(String fileName) {
+ return HSSFTestDataSamples.getTestDataFileContent(fileName);
+ }
+
+ public SpreadsheetVersion getSpreadsheetVersion(){
+ return SpreadsheetVersion.EXCEL97;
+ }
+
+ private HSSFITestDataProvider(){}
+ private static HSSFITestDataProvider inst = new HSSFITestDataProvider();
+ public static HSSFITestDataProvider getInstance(){
+ return inst;
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileNotFoundException;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-\r
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
-\r
-/**\r
- * Centralises logic for finding/opening sample files in the src/testcases/org/apache/poi/hssf/hssf/data folder. \r
- * \r
- * @author Josh Micich\r
- */\r
-public final class HSSFTestDataSamples {\r
-\r
- private static final String TEST_DATA_DIR_SYS_PROPERTY_NAME = "HSSF.testdata.path";\r
-\r
- private static boolean _isInitialised;\r
- private static File _resolvedDataDir;\r
- /** <code>true</code> if standard system propery is not set, \r
- * but the data is available on the test runtime classpath */\r
- private static boolean _sampleDataIsAvaliableOnClassPath;\r
-\r
- /**\r
- * Opens a sample file from the standard HSSF test data directory\r
- * \r
- * @return an open <tt>InputStream</tt> for the specified sample file\r
- */\r
- public static InputStream openSampleFileStream(String sampleFileName) {\r
- \r
- if(!_isInitialised) {\r
- try {\r
- initialise();\r
- } finally {\r
- _isInitialised = true;\r
- }\r
- }\r
- if (_sampleDataIsAvaliableOnClassPath) {\r
- InputStream result = openClasspathResource(sampleFileName);\r
- if(result == null) {\r
- throw new RuntimeException("specified test sample file '" + sampleFileName \r
- + "' not found on the classpath");\r
- }\r
-// System.out.println("opening cp: " + sampleFileName);\r
- // wrap to avoid temp warning method about auto-closing input stream\r
- return new NonSeekableInputStream(result);\r
- }\r
- if (_resolvedDataDir == null) {\r
- throw new RuntimeException("Must set system property '"\r
- + TEST_DATA_DIR_SYS_PROPERTY_NAME\r
- + "' properly before running tests");\r
- }\r
- \r
- File f = new File(_resolvedDataDir, sampleFileName);\r
- if (!f.exists()) {\r
- throw new RuntimeException("Sample file '" + sampleFileName\r
- + "' not found in data dir '" + _resolvedDataDir.getAbsolutePath() + "'");\r
- }\r
-// System.out.println("opening " + f.getAbsolutePath());\r
- try {\r
- return new FileInputStream(f);\r
- } catch (FileNotFoundException e) {\r
- throw new RuntimeException(e);\r
- }\r
- }\r
-\r
- private static void initialise() {\r
- String dataDirName = System.getProperty(TEST_DATA_DIR_SYS_PROPERTY_NAME);\r
- if (dataDirName == null) {\r
- // check to see if we can just get the resources from the classpath\r
- InputStream is = openClasspathResource("SampleSS.xls"); \r
- if(is != null) {\r
- try {\r
- is.close(); // be nice\r
- } catch (IOException e) {\r
- throw new RuntimeException(e);\r
- }\r
- _sampleDataIsAvaliableOnClassPath = true;\r
- return;\r
- }\r
- \r
- \r
- throw new RuntimeException("Must set system property '"\r
- + TEST_DATA_DIR_SYS_PROPERTY_NAME\r
- + "' before running tests");\r
- }\r
- File dataDir = new File(dataDirName);\r
- if (!dataDir.exists()) {\r
- throw new RuntimeException("Data dir '" + dataDirName\r
- + "' specified by system property '"\r
- + TEST_DATA_DIR_SYS_PROPERTY_NAME + "' does not exist");\r
- }\r
- // convert to canonical file, to make any subsequent error messages clearer.\r
- try {\r
- _resolvedDataDir = dataDir.getCanonicalFile();\r
- } catch (IOException e) {\r
- throw new RuntimeException(e);\r
- }\r
- }\r
-\r
- /**\r
- * Opens a test sample file from the 'data' sub-package of this class's package. \r
- * @return <code>null</code> if the sample file is not deployed on the classpath.\r
- */\r
- private static InputStream openClasspathResource(String sampleFileName) {\r
- return HSSFTestDataSamples.class.getResourceAsStream("data/" + sampleFileName);\r
- }\r
- \r
- private static final class NonSeekableInputStream extends InputStream {\r
-\r
- private final InputStream _is;\r
-\r
- public NonSeekableInputStream(InputStream is) {\r
- _is = is;\r
- }\r
-\r
- public int read() throws IOException {\r
- return _is.read();\r
- }\r
- public int read(byte[] b, int off, int len) throws IOException {\r
- return _is.read(b, off, len);\r
- }\r
- public boolean markSupported() {\r
- return false;\r
- }\r
- public void close() throws IOException {\r
- _is.close();\r
- }\r
- }\r
-\r
- public static HSSFWorkbook openSampleWorkbook(String sampleFileName) {\r
- try {\r
- return new HSSFWorkbook(openSampleFileStream(sampleFileName));\r
- } catch (IOException e) {\r
- throw new RuntimeException(e);\r
- }\r
- }\r
- /**\r
- * Writes a spreadsheet to a <tt>ByteArrayOutputStream</tt> and reads it back\r
- * from a <tt>ByteArrayInputStream</tt>.<p/>\r
- * Useful for verifying that the serialisation round trip\r
- */\r
- public static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {\r
- \r
- try {\r
- ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);\r
- original.write(baos);\r
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());\r
- return new HSSFWorkbook(bais);\r
- } catch (IOException e) {\r
- throw new RuntimeException(e);\r
- }\r
- }\r
-\r
- /**\r
- * @return byte array of sample file content from file found in standard hssf test data dir \r
- */\r
- public static byte[] getTestDataFileContent(String fileName) {\r
- ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
-\r
- try {\r
- InputStream fis = HSSFTestDataSamples.openSampleFileStream(fileName);\r
-\r
- byte[] buf = new byte[512];\r
- while (true) {\r
- int bytesRead = fis.read(buf);\r
- if (bytesRead < 1) {\r
- break;\r
- }\r
- bos.write(buf, 0, bytesRead);\r
- }\r
- fis.close();\r
- } catch (IOException e) {\r
- throw new RuntimeException(e);\r
- }\r
- return bos.toByteArray();\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+
+/**
+ * Centralises logic for finding/opening sample files in the src/testcases/org/apache/poi/hssf/hssf/data folder.
+ *
+ * @author Josh Micich
+ */
+public final class HSSFTestDataSamples {
+
+ private static final String TEST_DATA_DIR_SYS_PROPERTY_NAME = "HSSF.testdata.path";
+
+ private static boolean _isInitialised;
+ private static File _resolvedDataDir;
+ /** <code>true</code> if standard system propery is not set,
+ * but the data is available on the test runtime classpath */
+ private static boolean _sampleDataIsAvaliableOnClassPath;
+
+ /**
+ * Opens a sample file from the standard HSSF test data directory
+ *
+ * @return an open <tt>InputStream</tt> for the specified sample file
+ */
+ public static InputStream openSampleFileStream(String sampleFileName) {
+
+ if(!_isInitialised) {
+ try {
+ initialise();
+ } finally {
+ _isInitialised = true;
+ }
+ }
+ if (_sampleDataIsAvaliableOnClassPath) {
+ InputStream result = openClasspathResource(sampleFileName);
+ if(result == null) {
+ throw new RuntimeException("specified test sample file '" + sampleFileName
+ + "' not found on the classpath");
+ }
+// System.out.println("opening cp: " + sampleFileName);
+ // wrap to avoid temp warning method about auto-closing input stream
+ return new NonSeekableInputStream(result);
+ }
+ if (_resolvedDataDir == null) {
+ throw new RuntimeException("Must set system property '"
+ + TEST_DATA_DIR_SYS_PROPERTY_NAME
+ + "' properly before running tests");
+ }
+
+ File f = new File(_resolvedDataDir, sampleFileName);
+ if (!f.exists()) {
+ throw new RuntimeException("Sample file '" + sampleFileName
+ + "' not found in data dir '" + _resolvedDataDir.getAbsolutePath() + "'");
+ }
+// System.out.println("opening " + f.getAbsolutePath());
+ try {
+ return new FileInputStream(f);
+ } catch (FileNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static void initialise() {
+ String dataDirName = System.getProperty(TEST_DATA_DIR_SYS_PROPERTY_NAME);
+ if (dataDirName == null) {
+ // check to see if we can just get the resources from the classpath
+ InputStream is = openClasspathResource("SampleSS.xls");
+ if (is != null) {
+ try {
+ is.close(); // be nice
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ _sampleDataIsAvaliableOnClassPath = true;
+ return;
+ }
+
+ throw new RuntimeException("Must set system property '"
+ + TEST_DATA_DIR_SYS_PROPERTY_NAME + "' before running tests");
+ }
+ File dataDir = new File(dataDirName);
+ if (!dataDir.exists()) {
+ throw new RuntimeException("Data dir '" + dataDirName
+ + "' specified by system property '" + TEST_DATA_DIR_SYS_PROPERTY_NAME
+ + "' does not exist");
+ }
+ // convert to canonical file, to make any subsequent error messages
+ // clearer.
+ try {
+ _resolvedDataDir = dataDir.getCanonicalFile();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Opens a test sample file from the 'data' sub-package of this class's package.
+ * @return <code>null</code> if the sample file is not deployed on the classpath.
+ */
+ private static InputStream openClasspathResource(String sampleFileName) {
+ return HSSFTestDataSamples.class.getResourceAsStream("data/" + sampleFileName);
+ }
+
+ private static final class NonSeekableInputStream extends InputStream {
+
+ private final InputStream _is;
+
+ public NonSeekableInputStream(InputStream is) {
+ _is = is;
+ }
+
+ public int read() throws IOException {
+ return _is.read();
+ }
+ public int read(byte[] b, int off, int len) throws IOException {
+ return _is.read(b, off, len);
+ }
+ public boolean markSupported() {
+ return false;
+ }
+ public void close() throws IOException {
+ _is.close();
+ }
+ }
+
+ public static HSSFWorkbook openSampleWorkbook(String sampleFileName) {
+ try {
+ return new HSSFWorkbook(openSampleFileStream(sampleFileName));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ /**
+ * Writes a spreadsheet to a <tt>ByteArrayOutputStream</tt> and reads it back
+ * from a <tt>ByteArrayInputStream</tt>.<p/>
+ * Useful for verifying that the serialisation round trip
+ */
+ public static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
+
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
+ original.write(baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ return new HSSFWorkbook(bais);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * @return byte array of sample file content from file found in standard hssf test data dir
+ */
+ public static byte[] getTestDataFileContent(String fileName) {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+ try {
+ InputStream fis = HSSFTestDataSamples.openSampleFileStream(fileName);
+
+ byte[] buf = new byte[512];
+ while (true) {
+ int bytesRead = fis.read(buf);
+ if (bytesRead < 1) {
+ break;
+ }
+ bos.write(buf, 0, bytesRead);
+ }
+ fis.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ return bos.toByteArray();
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.model;\r
-\r
-import java.util.Arrays;\r
-\r
-import junit.framework.AssertionFailedError;\r
-import junit.framework.TestCase;\r
-\r
-import org.apache.poi.hssf.record.NumberRecord;\r
-import org.apache.poi.hssf.record.Record;\r
-import org.apache.poi.hssf.record.RowRecord;\r
-import org.apache.poi.hssf.record.UnknownRecord;\r
-import org.apache.poi.hssf.record.WindowTwoRecord;\r
-\r
-/**\r
- * Tests for {@link RowBlocksReader}\r
- * \r
- * @author Josh Micich\r
- */\r
-public final class TestRowBlocksReader extends TestCase {\r
- public void testAbnormalPivotTableRecords_bug46280() {\r
- int SXVIEW_SID = 0x00B0;\r
- Record[] inRecs = {\r
- new RowRecord(0),\r
- new NumberRecord(),\r
- // normally MSODRAWING(0x00EC) would come here before SXVIEW\r
- new UnknownRecord(SXVIEW_SID, "dummydata (SXVIEW: View Definition)".getBytes()),\r
- new WindowTwoRecord(),\r
- };\r
- RecordStream rs = new RecordStream(Arrays.asList(inRecs), 0);\r
- RowBlocksReader rbr = new RowBlocksReader(rs);\r
- if (rs.peekNextClass() == WindowTwoRecord.class) {\r
- // Should have stopped at the SXVIEW record \r
- throw new AssertionFailedError("Identified bug 46280b");\r
- }\r
- RecordStream rbStream = rbr.getPlainRecordStream();\r
- assertEquals(inRecs[0], rbStream.getNext());\r
- assertEquals(inRecs[1], rbStream.getNext());\r
- assertFalse(rbStream.hasNext());\r
- assertTrue(rs.hasNext());\r
- assertEquals(inRecs[2], rs.getNext());\r
- assertEquals(inRecs[3], rs.getNext());\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf.model;
+
+import java.util.Arrays;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.record.NumberRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.RowRecord;
+import org.apache.poi.hssf.record.UnknownRecord;
+import org.apache.poi.hssf.record.WindowTwoRecord;
+import org.apache.poi.hssf.record.pivottable.ViewDefinitionRecord;
+
+/**
+ * Tests for {@link RowBlocksReader}
+ *
+ * @author Josh Micich
+ */
+public final class TestRowBlocksReader extends TestCase {
+ public void testAbnormalPivotTableRecords_bug46280() {
+ int SXVIEW_SID = ViewDefinitionRecord.sid;
+ Record[] inRecs = {
+ new RowRecord(0),
+ new NumberRecord(),
+ // normally MSODRAWING(0x00EC) would come here before SXVIEW
+ new UnknownRecord(SXVIEW_SID, "dummydata (SXVIEW: View Definition)".getBytes()),
+ new WindowTwoRecord(),
+ };
+ RecordStream rs = new RecordStream(Arrays.asList(inRecs), 0);
+ RowBlocksReader rbr = new RowBlocksReader(rs);
+ if (rs.peekNextClass() == WindowTwoRecord.class) {
+ // Should have stopped at the SXVIEW record
+ throw new AssertionFailedError("Identified bug 46280b");
+ }
+ RecordStream rbStream = rbr.getPlainRecordStream();
+ assertEquals(inRecs[0], rbStream.getNext());
+ assertEquals(inRecs[1], rbStream.getNext());
+ assertFalse(rbStream.hasNext());
+ assertTrue(rs.hasNext());
+ assertEquals(inRecs[2], rs.getNext());
+ assertEquals(inRecs[3], rs.getNext());
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.record;\r
-\r
-import java.util.Arrays;\r
-\r
-import junit.framework.AssertionFailedError;\r
-import junit.framework.TestCase;\r
-\r
-import org.apache.poi.util.HexRead;\r
-\r
-/**\r
- * Tests the serialization and deserialization of the TestEmbeddedObjectRefSubRecord\r
- * class works correctly. Test data taken directly from a real\r
- * Excel file.\r
- *\r
- * @author Yegor Kozlov\r
- */\r
-public final class TestEmbeddedObjectRefSubRecord extends TestCase {\r
-\r
- private static final short EORSR_SID = EmbeddedObjectRefSubRecord.sid;\r
-\r
- public void testStore() {\r
- String data1\r
- = "20 00 05 00 FC 10 76 01 02 24 14 DF 00 03 10 00 "\r
- + "00 46 6F 72 6D 73 2E 43 68 65 63 6B 42 6F 78 2E "\r
- + "31 00 00 00 00 00 70 00 00 00 00 00 00 00 00 00 "\r
- + "00 00";\r
-\r
- byte[] src = hr(data1);\r
-\r
- RecordInputStream in = TestcaseRecordInputStream.create(EORSR_SID, src);\r
-\r
- EmbeddedObjectRefSubRecord record1 = new EmbeddedObjectRefSubRecord(in, src.length);\r
-\r
- byte[] ser = record1.serialize();\r
-\r
- RecordInputStream in2 = TestcaseRecordInputStream.create(ser);\r
- EmbeddedObjectRefSubRecord record2 = new EmbeddedObjectRefSubRecord(in2, ser.length-4);\r
-\r
- confirmData(src, ser);\r
- assertEquals(record1.getOLEClassName(), record2.getOLEClassName());\r
-\r
- byte[] ser2 = record1.serialize();\r
- assertTrue(Arrays.equals(ser, ser2));\r
- }\r
-\r
- /**\r
- * @param expectedData does not include sid & size\r
- * @param actualFullRecordData includes sid & size\r
- */\r
- private static void confirmData(byte[] expectedData, byte[] actualFullRecordData) {\r
- assertEquals(expectedData.length, actualFullRecordData.length-4);\r
- for (int i = 0; i < expectedData.length; i++) {\r
- if(expectedData[i] != actualFullRecordData[i+4]) {\r
- throw new AssertionFailedError("Difference at offset (" + i + ")");\r
- }\r
- }\r
- }\r
-\r
- public void testCreate() {\r
-\r
- EmbeddedObjectRefSubRecord record1 = new EmbeddedObjectRefSubRecord();\r
-\r
- byte[] ser = record1.serialize();\r
- RecordInputStream in2 = TestcaseRecordInputStream.create(ser);\r
- EmbeddedObjectRefSubRecord record2 = new EmbeddedObjectRefSubRecord(in2, ser.length-4);\r
-\r
- assertEquals(record1.getOLEClassName(), record2.getOLEClassName());\r
- assertEquals(record1.getStreamId(), record2.getStreamId());\r
-\r
- byte[] ser2 = record1.serialize();\r
- assertTrue(Arrays.equals(ser, ser2));\r
- }\r
-\r
- public void testCameraTool_bug45912() {\r
- /**\r
- * taken from ftPictFmla sub-record in attachment 22645 (offset 0x40AB).\r
- */\r
- byte[] data45912 = hr(\r
- "12 00 0B 00 F8 02 88 04 3B 00 " +\r
- "00 00 00 01 00 00 00 01 " +\r
- "00 00");\r
- RecordInputStream in = TestcaseRecordInputStream.create(EORSR_SID, data45912);\r
-\r
- EmbeddedObjectRefSubRecord rec = new EmbeddedObjectRefSubRecord(in, data45912.length);\r
- byte[] ser2 = rec.serialize();\r
- confirmData(data45912, ser2);\r
- }\r
-\r
- private static byte[] hr(String string) {\r
- return HexRead.readFromString(string);\r
- }\r
-\r
- /**\r
- * tests various examples of OLE controls\r
- */\r
- public void testVarious() {\r
- String[] rawData = {\r
- "12 00 0B 00 70 95 0B 05 3B 01 00 36 00 40 00 18 00 19 00 18",\r
- "12 00 0B 00 B0 4D 3E 03 3B 00 00 00 00 01 00 00 80 01 C0 00",\r
- "0C 00 05 00 60 AF 3B 03 24 FD FF FE C0 FE",\r
- "24 00 05 00 40 42 3E 03 02 80 CD B4 04 03 15 00 00 46 6F 72 6D 73 2E 43 6F 6D 6D 61 6E 64 42 75 74 74 6F 6E 2E 31 00 00 00 00 54 00 00 00 00 00 00 00 00 00 00 00",\r
- "22 00 05 00 10 4E 3E 03 02 00 4C CC 04 03 12 00 00 46 6F 72 6D 73 2E 53 70 69 6E 42 75 74 74 6F 6E 2E 31 00 54 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00",\r
- "20 00 05 00 E0 41 3E 03 02 00 FC 0B 05 03 10 00 00 46 6F 72 6D 73 2E 43 6F 6D 62 6F 42 6F 78 2E 31 00 74 00 00 00 4C 00 00 00 00 00 00 00 00 00 00 00",\r
- "24 00 05 00 00 4C AF 03 02 80 E1 93 05 03 14 00 00 46 6F 72 6D 73 2E 4F 70 74 69 6F 6E 42 75 74 74 6F 6E 2E 31 00 C0 00 00 00 70 00 00 00 00 00 00 00 00 00 00 00",\r
- "20 00 05 00 E0 A4 28 04 02 80 EA 93 05 03 10 00 00 46 6F 72 6D 73 2E 43 68 65 63 6B 42 6F 78 2E 31 00 30 01 00 00 6C 00 00 00 00 00 00 00 00 00 00 00",\r
- "1C 00 05 00 30 40 3E 03 02 00 CC B4 04 03 0D 00 00 46 6F 72 6D 73 2E 4C 61 62 65 6C 2E 31 9C 01 00 00 54 00 00 00 00 00 00 00 00 00 00 00",\r
- "1E 00 05 00 B0 A4 28 04 02 00 D0 0A 05 03 0F 00 00 46 6F 72 6D 73 2E 4C 69 73 74 42 6F 78 2E 31 F0 01 00 00 48 00 00 00 00 00 00 00 00 00 00 00",\r
- "24 00 05 00 C0 AF 3B 03 02 80 D1 0A 05 03 14 00 00 46 6F 72 6D 73 2E 54 6F 67 67 6C 65 42 75 74 74 6F 6E 2E 31 00 38 02 00 00 6C 00 00 00 00 00 00 00 00 00 00 00",\r
- "1E 00 05 00 90 AF 3B 03 02 80 D4 0A 05 03 0F 00 00 46 6F 72 6D 73 2E 54 65 78 74 42 6F 78 2E 31 A4 02 00 00 48 00 00 00 00 00 00 00 00 00 00 00",\r
- "24 00 05 00 60 40 3E 03 02 00 D6 0A 05 03 14 00 00 46 6F 72 6D 73 2E 54 6F 67 67 6C 65 42 75 74 74 6F 6E 2E 31 00 EC 02 00 00 6C 00 00 00 00 00 00 00 00 00 00 00",\r
- "20 00 05 00 20 4D 3E 03 02 00 D9 0A 05 03 11 00 00 46 6F 72 6D 73 2E 53 63 72 6F 6C 6C 42 61 72 2E 31 58 03 00 00 20 00 00 00 00 00 00 00 00 00 00 00",\r
- "20 00 05 00 00 AF 28 04 02 80 31 AC 04 03 10 00 00 53 68 65 6C 6C 2E 45 78 70 6C 6F 72 65 72 2E 32 00 78 03 00 00 AC 00 00 00 00 00 00 00 00 00 00 00",\r
- };\r
-\r
- for (int i = 0; i < rawData.length; i++) {\r
- confirmRead(hr(rawData[i]), i);\r
- }\r
- }\r
-\r
- private static void confirmRead(byte[] data, int i) {\r
- RecordInputStream in = TestcaseRecordInputStream.create(EORSR_SID, data);\r
-\r
- EmbeddedObjectRefSubRecord rec = new EmbeddedObjectRefSubRecord(in, data.length);\r
- byte[] ser2 = rec.serialize();\r
- TestcaseRecordInputStream.confirmRecordEncoding("Test record " + i, EORSR_SID, data, ser2);\r
- }\r
- \r
- public void testVisioDrawing_bug46199() {\r
- /**\r
- * taken from ftPictFmla sub-record in attachment 22860 (stream offset 0x768F).<br/>\r
- * Note that the since the string length is zero, there is no unicode flag byte\r
- */\r
- byte[] data46199 = hr(\r
- "0E 00 "\r
- + "05 00 "\r
- + "28 25 A3 01 "\r
- + "02 6C D1 34 02 "\r
- + "03 00 00 "\r
- + "0F CB E8 00");\r
- RecordInputStream in = TestcaseRecordInputStream.create(EORSR_SID, data46199);\r
-\r
- EmbeddedObjectRefSubRecord rec;\r
- try {\r
- rec = new EmbeddedObjectRefSubRecord(in, data46199.length);\r
- } catch (RecordFormatException e) {\r
- if (e.getMessage().equals("Not enough data (3) to read requested (4) bytes")) {\r
- throw new AssertionFailedError("Identified bug 22860");\r
- }\r
- throw e;\r
- }\r
- byte[] ser2 = rec.serialize();\r
- TestcaseRecordInputStream.confirmRecordEncoding(EORSR_SID, data46199, ser2);\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf.record;
+
+import java.util.Arrays;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+import org.apache.poi.util.HexRead;
+
+/**
+ * Tests the serialization and deserialization of the TestEmbeddedObjectRefSubRecord
+ * class works correctly. Test data taken directly from a real
+ * Excel file.
+ *
+ * @author Yegor Kozlov
+ */
+public final class TestEmbeddedObjectRefSubRecord extends TestCase {
+
+ private static final short EORSR_SID = EmbeddedObjectRefSubRecord.sid;
+
+ public void testStore() {
+ String data1
+ = "20 00 05 00 FC 10 76 01 02 24 14 DF 00 03 10 00 "
+ + "00 46 6F 72 6D 73 2E 43 68 65 63 6B 42 6F 78 2E "
+ + "31 00 00 00 00 00 70 00 00 00 00 00 00 00 00 00 "
+ + "00 00";
+
+ byte[] src = hr(data1);
+
+ RecordInputStream in = TestcaseRecordInputStream.create(EORSR_SID, src);
+
+ EmbeddedObjectRefSubRecord record1 = new EmbeddedObjectRefSubRecord(in, src.length);
+
+ byte[] ser = record1.serialize();
+
+ RecordInputStream in2 = TestcaseRecordInputStream.create(ser);
+ EmbeddedObjectRefSubRecord record2 = new EmbeddedObjectRefSubRecord(in2, ser.length-4);
+
+ confirmData(src, ser);
+ assertEquals(record1.getOLEClassName(), record2.getOLEClassName());
+
+ byte[] ser2 = record1.serialize();
+ assertTrue(Arrays.equals(ser, ser2));
+ }
+
+ /**
+ * @param expectedData does not include sid & size
+ * @param actualFullRecordData includes sid & size
+ */
+ private static void confirmData(byte[] expectedData, byte[] actualFullRecordData) {
+ assertEquals(expectedData.length, actualFullRecordData.length-4);
+ for (int i = 0; i < expectedData.length; i++) {
+ if(expectedData[i] != actualFullRecordData[i+4]) {
+ throw new AssertionFailedError("Difference at offset (" + i + ")");
+ }
+ }
+ }
+
+ public void testCreate() {
+
+ EmbeddedObjectRefSubRecord record1 = new EmbeddedObjectRefSubRecord();
+
+ byte[] ser = record1.serialize();
+ RecordInputStream in2 = TestcaseRecordInputStream.create(ser);
+ EmbeddedObjectRefSubRecord record2 = new EmbeddedObjectRefSubRecord(in2, ser.length-4);
+
+ assertEquals(record1.getOLEClassName(), record2.getOLEClassName());
+ assertEquals(record1.getStreamId(), record2.getStreamId());
+
+ byte[] ser2 = record1.serialize();
+ assertTrue(Arrays.equals(ser, ser2));
+ }
+
+ public void testCameraTool_bug45912() {
+ /**
+ * taken from ftPictFmla sub-record in attachment 22645 (offset 0x40AB).
+ */
+ byte[] data45912 = hr(
+ "12 00 0B 00 F8 02 88 04 3B 00 " +
+ "00 00 00 01 00 00 00 01 " +
+ "00 00");
+ RecordInputStream in = TestcaseRecordInputStream.create(EORSR_SID, data45912);
+
+ EmbeddedObjectRefSubRecord rec = new EmbeddedObjectRefSubRecord(in, data45912.length);
+ byte[] ser2 = rec.serialize();
+ confirmData(data45912, ser2);
+ }
+
+ private static byte[] hr(String string) {
+ return HexRead.readFromString(string);
+ }
+
+ /**
+ * tests various examples of OLE controls
+ */
+ public void testVarious() {
+ String[] rawData = {
+ "12 00 0B 00 70 95 0B 05 3B 01 00 36 00 40 00 18 00 19 00 18",
+ "12 00 0B 00 B0 4D 3E 03 3B 00 00 00 00 01 00 00 80 01 C0 00",
+ "0C 00 05 00 60 AF 3B 03 24 FD FF FE C0 FE",
+ "24 00 05 00 40 42 3E 03 02 80 CD B4 04 03 15 00 00 46 6F 72 6D 73 2E 43 6F 6D 6D 61 6E 64 42 75 74 74 6F 6E 2E 31 00 00 00 00 54 00 00 00 00 00 00 00 00 00 00 00",
+ "22 00 05 00 10 4E 3E 03 02 00 4C CC 04 03 12 00 00 46 6F 72 6D 73 2E 53 70 69 6E 42 75 74 74 6F 6E 2E 31 00 54 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00",
+ "20 00 05 00 E0 41 3E 03 02 00 FC 0B 05 03 10 00 00 46 6F 72 6D 73 2E 43 6F 6D 62 6F 42 6F 78 2E 31 00 74 00 00 00 4C 00 00 00 00 00 00 00 00 00 00 00",
+ "24 00 05 00 00 4C AF 03 02 80 E1 93 05 03 14 00 00 46 6F 72 6D 73 2E 4F 70 74 69 6F 6E 42 75 74 74 6F 6E 2E 31 00 C0 00 00 00 70 00 00 00 00 00 00 00 00 00 00 00",
+ "20 00 05 00 E0 A4 28 04 02 80 EA 93 05 03 10 00 00 46 6F 72 6D 73 2E 43 68 65 63 6B 42 6F 78 2E 31 00 30 01 00 00 6C 00 00 00 00 00 00 00 00 00 00 00",
+ "1C 00 05 00 30 40 3E 03 02 00 CC B4 04 03 0D 00 00 46 6F 72 6D 73 2E 4C 61 62 65 6C 2E 31 9C 01 00 00 54 00 00 00 00 00 00 00 00 00 00 00",
+ "1E 00 05 00 B0 A4 28 04 02 00 D0 0A 05 03 0F 00 00 46 6F 72 6D 73 2E 4C 69 73 74 42 6F 78 2E 31 F0 01 00 00 48 00 00 00 00 00 00 00 00 00 00 00",
+ "24 00 05 00 C0 AF 3B 03 02 80 D1 0A 05 03 14 00 00 46 6F 72 6D 73 2E 54 6F 67 67 6C 65 42 75 74 74 6F 6E 2E 31 00 38 02 00 00 6C 00 00 00 00 00 00 00 00 00 00 00",
+ "1E 00 05 00 90 AF 3B 03 02 80 D4 0A 05 03 0F 00 00 46 6F 72 6D 73 2E 54 65 78 74 42 6F 78 2E 31 A4 02 00 00 48 00 00 00 00 00 00 00 00 00 00 00",
+ "24 00 05 00 60 40 3E 03 02 00 D6 0A 05 03 14 00 00 46 6F 72 6D 73 2E 54 6F 67 67 6C 65 42 75 74 74 6F 6E 2E 31 00 EC 02 00 00 6C 00 00 00 00 00 00 00 00 00 00 00",
+ "20 00 05 00 20 4D 3E 03 02 00 D9 0A 05 03 11 00 00 46 6F 72 6D 73 2E 53 63 72 6F 6C 6C 42 61 72 2E 31 58 03 00 00 20 00 00 00 00 00 00 00 00 00 00 00",
+ "20 00 05 00 00 AF 28 04 02 80 31 AC 04 03 10 00 00 53 68 65 6C 6C 2E 45 78 70 6C 6F 72 65 72 2E 32 00 78 03 00 00 AC 00 00 00 00 00 00 00 00 00 00 00",
+ };
+
+ for (int i = 0; i < rawData.length; i++) {
+ confirmRead(hr(rawData[i]), i);
+ }
+ }
+
+ private static void confirmRead(byte[] data, int i) {
+ RecordInputStream in = TestcaseRecordInputStream.create(EORSR_SID, data);
+
+ EmbeddedObjectRefSubRecord rec = new EmbeddedObjectRefSubRecord(in, data.length);
+ byte[] ser2 = rec.serialize();
+ TestcaseRecordInputStream.confirmRecordEncoding("Test record " + i, EORSR_SID, data, ser2);
+ }
+
+ public void testVisioDrawing_bug46199() {
+ /**
+ * taken from ftPictFmla sub-record in attachment 22860 (stream offset 0x768F).<br/>
+ * Note that the since the string length is zero, there is no unicode flag byte
+ */
+ byte[] data46199 = hr(
+ "0E 00 "
+ + "05 00 "
+ + "28 25 A3 01 "
+ + "02 6C D1 34 02 "
+ + "03 00 00 "
+ + "0F CB E8 00");
+ RecordInputStream in = TestcaseRecordInputStream.create(EORSR_SID, data46199);
+
+ EmbeddedObjectRefSubRecord rec;
+ try {
+ rec = new EmbeddedObjectRefSubRecord(in, data46199.length);
+ } catch (RecordFormatException e) {
+ if (e.getMessage().equals("Not enough data (3) to read requested (4) bytes")) {
+ throw new AssertionFailedError("Identified bug 22860");
+ }
+ throw e;
+ }
+ byte[] ser2 = rec.serialize();
+ TestcaseRecordInputStream.confirmRecordEncoding(EORSR_SID, data46199, ser2);
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.record;\r
-\r
-import junit.framework.AssertionFailedError;\r
-import junit.framework.TestCase;\r
-/**\r
- * \r
- * @author Josh Micich\r
- */\r
-public final class TestExternalNameRecord extends TestCase {\r
-\r
- private static final byte[] dataFDS = {\r
- 0, 0, 0, 0, 0, 0, 3, 0, 70, 68, 83, 0, 0,\r
- };\r
- \r
- // data taken from bugzilla 44774 att 21790\r
- private static final byte[] dataAutoDocName = {\r
- -22, 127, 0, 0, 0, 0, 29, 0, 39, 49, 57, 49, 50, 49, 57, 65, 87, 52, 32, 67, 111, 114,\r
- 112, 44, 91, 87, 79, 82, 75, 79, 85, 84, 95, 80, 88, 93, 39,\r
- };\r
- \r
- // data taken from bugzilla 44774 att 21790\r
- private static final byte[] dataPlainName = {\r
- 0, 0, 0, 0, 0, 0, 9, 0, 82, 97, 116, 101, 95, 68, 97, 116, 101, 9, 0, 58, 0, 0, 0, 0, 4, 0, 8, 0\r
- // TODO - the last 2 bytes of formula data (8,0) seem weird. They encode to ConcatPtg, UnknownPtg\r
- // UnknownPtg is otherwise not created by any other test cases\r
- };\r
- \r
- private static ExternalNameRecord createSimpleENR(byte[] data) {\r
- return new ExternalNameRecord(TestcaseRecordInputStream.create(0x0023, data));\r
- }\r
- public void testBasicDeserializeReserialize() {\r
- \r
- ExternalNameRecord enr = createSimpleENR(dataFDS);\r
- assertEquals("FDS", enr.getText());\r
- \r
- try {\r
- TestcaseRecordInputStream.confirmRecordEncoding(0x0023, dataFDS, enr.serialize());\r
- } catch (ArrayIndexOutOfBoundsException e) {\r
- if(e.getMessage().equals("15")) {\r
- throw new AssertionFailedError("Identified bug 44695");\r
- }\r
- }\r
- }\r
-\r
- public void testBasicSize() {\r
- ExternalNameRecord enr = createSimpleENR(dataFDS);\r
- if(enr.getRecordSize() == 13) {\r
- throw new AssertionFailedError("Identified bug 44695");\r
- }\r
- assertEquals(17, enr.getRecordSize());\r
- }\r
- \r
- public void testAutoStdDocName() {\r
-\r
- ExternalNameRecord enr;\r
- try {\r
- enr = createSimpleENR(dataAutoDocName);\r
- } catch (ArrayIndexOutOfBoundsException e) {\r
- if(e.getMessage() == null) {\r
- throw new AssertionFailedError("Identified bug XXXX");\r
- }\r
- throw e;\r
- }\r
- assertEquals("'191219AW4 Corp,[WORKOUT_PX]'", enr.getText());\r
- assertTrue(enr.isAutomaticLink());\r
- assertFalse(enr.isBuiltInName());\r
- assertFalse(enr.isIconifiedPictureLink());\r
- assertFalse(enr.isOLELink());\r
- assertFalse(enr.isPicureLink());\r
- assertTrue(enr.isStdDocumentNameIdentifier());\r
-\r
- TestcaseRecordInputStream.confirmRecordEncoding(0x0023, dataAutoDocName, enr.serialize());\r
- }\r
-\r
- public void testPlainName() {\r
-\r
- ExternalNameRecord enr = createSimpleENR(dataPlainName);\r
- assertEquals("Rate_Date", enr.getText());\r
- assertFalse(enr.isAutomaticLink());\r
- assertFalse(enr.isBuiltInName());\r
- assertFalse(enr.isIconifiedPictureLink());\r
- assertFalse(enr.isOLELink());\r
- assertFalse(enr.isPicureLink());\r
- assertFalse(enr.isStdDocumentNameIdentifier());\r
-\r
- TestcaseRecordInputStream.confirmRecordEncoding(0x0023, dataPlainName, enr.serialize());\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf.record;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+/**
+ *
+ * @author Josh Micich
+ */
+public final class TestExternalNameRecord extends TestCase {
+
+ private static final byte[] dataFDS = {
+ 0, 0, 0, 0, 0, 0, 3, 0, 70, 68, 83, 0, 0,
+ };
+
+ // data taken from bugzilla 44774 att 21790
+ private static final byte[] dataAutoDocName = {
+ -22, 127, 0, 0, 0, 0, 29, 0, 39, 49, 57, 49, 50, 49, 57, 65, 87, 52, 32, 67, 111, 114,
+ 112, 44, 91, 87, 79, 82, 75, 79, 85, 84, 95, 80, 88, 93, 39,
+ };
+
+ // data taken from bugzilla 44774 att 21790
+ private static final byte[] dataPlainName = {
+ 0, 0, 0, 0, 0, 0, 9, 0, 82, 97, 116, 101, 95, 68, 97, 116, 101, 9, 0, 58, 0, 0, 0, 0, 4, 0, 8, 0
+ // TODO - the last 2 bytes of formula data (8,0) seem weird. They encode to ConcatPtg, UnknownPtg
+ // UnknownPtg is otherwise not created by any other test cases
+ };
+
+ private static ExternalNameRecord createSimpleENR(byte[] data) {
+ return new ExternalNameRecord(TestcaseRecordInputStream.create(0x0023, data));
+ }
+ public void testBasicDeserializeReserialize() {
+
+ ExternalNameRecord enr = createSimpleENR(dataFDS);
+ assertEquals("FDS", enr.getText());
+
+ try {
+ TestcaseRecordInputStream.confirmRecordEncoding(0x0023, dataFDS, enr.serialize());
+ } catch (ArrayIndexOutOfBoundsException e) {
+ if(e.getMessage().equals("15")) {
+ throw new AssertionFailedError("Identified bug 44695");
+ }
+ }
+ }
+
+ public void testBasicSize() {
+ ExternalNameRecord enr = createSimpleENR(dataFDS);
+ if(enr.getRecordSize() == 13) {
+ throw new AssertionFailedError("Identified bug 44695");
+ }
+ assertEquals(17, enr.getRecordSize());
+ }
+
+ public void testAutoStdDocName() {
+
+ ExternalNameRecord enr;
+ try {
+ enr = createSimpleENR(dataAutoDocName);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ if(e.getMessage() == null) {
+ throw new AssertionFailedError("Identified bug XXXX");
+ }
+ throw e;
+ }
+ assertEquals("'191219AW4 Corp,[WORKOUT_PX]'", enr.getText());
+ assertTrue(enr.isAutomaticLink());
+ assertFalse(enr.isBuiltInName());
+ assertFalse(enr.isIconifiedPictureLink());
+ assertFalse(enr.isOLELink());
+ assertFalse(enr.isPicureLink());
+ assertTrue(enr.isStdDocumentNameIdentifier());
+
+ TestcaseRecordInputStream.confirmRecordEncoding(0x0023, dataAutoDocName, enr.serialize());
+ }
+
+ public void testPlainName() {
+
+ ExternalNameRecord enr = createSimpleENR(dataPlainName);
+ assertEquals("Rate_Date", enr.getText());
+ assertFalse(enr.isAutomaticLink());
+ assertFalse(enr.isBuiltInName());
+ assertFalse(enr.isIconifiedPictureLink());
+ assertFalse(enr.isOLELink());
+ assertFalse(enr.isPicureLink());
+ assertFalse(enr.isStdDocumentNameIdentifier());
+
+ TestcaseRecordInputStream.confirmRecordEncoding(0x0023, dataPlainName, enr.serialize());
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.record;\r
-\r
-import junit.framework.AssertionFailedError;\r
-import junit.framework.TestCase;\r
-\r
-import org.apache.poi.util.HexRead;\r
-\r
-/**\r
- * Tests for {@link StyleRecord}\r
- */\r
-public final class TestStyleRecord extends TestCase {\r
- public void testUnicodeReadName() {\r
- byte[] data = HexRead.readFromString(\r
- "11 00 09 00 01 38 5E C4 89 5F 00 53 00 68 00 65 00 65 00 74 00 31 00");\r
- RecordInputStream in = TestcaseRecordInputStream.create(StyleRecord.sid, data);\r
- StyleRecord sr = new StyleRecord(in);\r
- assertEquals("\u5E38\u89C4_Sheet1", sr.getName()); // "<Conventional>_Sheet1"\r
- byte[] ser;\r
- try {\r
- ser = sr.serialize();\r
- } catch (IllegalStateException e) {\r
- if (e.getMessage().equals("Incorrect number of bytes written - expected 27 but got 18")) {\r
- throw new AssertionFailedError("Identified bug 46385");\r
- }\r
- throw e;\r
- }\r
- TestcaseRecordInputStream.confirmRecordEncoding(StyleRecord.sid, data, ser);\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf.record;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+import org.apache.poi.util.HexRead;
+
+/**
+ * Tests for {@link StyleRecord}
+ */
+public final class TestStyleRecord extends TestCase {
+ public void testUnicodeReadName() {
+ byte[] data = HexRead.readFromString(
+ "11 00 09 00 01 38 5E C4 89 5F 00 53 00 68 00 65 00 65 00 74 00 31 00");
+ RecordInputStream in = TestcaseRecordInputStream.create(StyleRecord.sid, data);
+ StyleRecord sr = new StyleRecord(in);
+ assertEquals("\u5E38\u89C4_Sheet1", sr.getName()); // "<Conventional>_Sheet1"
+ byte[] ser;
+ try {
+ ser = sr.serialize();
+ } catch (IllegalStateException e) {
+ if (e.getMessage().equals("Incorrect number of bytes written - expected 27 but got 18")) {
+ throw new AssertionFailedError("Identified bug 46385");
+ }
+ throw e;
+ }
+ TestcaseRecordInputStream.confirmRecordEncoding(StyleRecord.sid, data, ser);
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.record;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.util.Arrays;\r
-\r
-import junit.framework.TestCase;\r
-\r
-import org.apache.poi.hssf.record.formula.Ptg;\r
-import org.apache.poi.hssf.record.formula.RefPtg;\r
-import org.apache.poi.hssf.usermodel.HSSFRichTextString;\r
-import org.apache.poi.util.HexRead;\r
-import org.apache.poi.util.LittleEndian;\r
-\r
-/**\r
- * Tests that serialization and deserialization of the TextObjectRecord .\r
- * Test data taken directly from a real Excel file.\r
- *\r
- * @author Yegor Kozlov\r
- */\r
-public final class TestTextObjectRecord extends TestCase {\r
-\r
- private static final byte[] simpleData = HexRead.readFromString(\r
- "B6 01 12 00 " +\r
- "12 02 00 00 00 00 00 00" +\r
- "00 00 0D 00 08 00 00 00" +\r
- "00 00 " +\r
- "3C 00 0E 00 " +\r
- "00 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21 " +\r
- "3C 00 08 " +\r
- "00 0D 00 00 00 00 00 00 00"\r
- );\r
-\r
-\r
- public void testRead() {\r
-\r
- RecordInputStream is =TestcaseRecordInputStream.create(simpleData);\r
- TextObjectRecord record = new TextObjectRecord(is);\r
-\r
- assertEquals(TextObjectRecord.sid, record.getSid());\r
- assertEquals(TextObjectRecord.HORIZONTAL_TEXT_ALIGNMENT_LEFT_ALIGNED, record.getHorizontalTextAlignment());\r
- assertEquals(TextObjectRecord.VERTICAL_TEXT_ALIGNMENT_TOP, record.getVerticalTextAlignment());\r
- assertEquals(TextObjectRecord.TEXT_ORIENTATION_NONE, record.getTextOrientation());\r
- assertEquals("Hello, World!", record.getStr().getString());\r
- }\r
-\r
- public void testWrite() {\r
- HSSFRichTextString str = new HSSFRichTextString("Hello, World!");\r
-\r
- TextObjectRecord record = new TextObjectRecord();\r
- record.setStr(str);\r
- record.setHorizontalTextAlignment( TextObjectRecord.HORIZONTAL_TEXT_ALIGNMENT_LEFT_ALIGNED );\r
- record.setVerticalTextAlignment( TextObjectRecord.VERTICAL_TEXT_ALIGNMENT_TOP );\r
- record.setTextLocked( true );\r
- record.setTextOrientation( TextObjectRecord.TEXT_ORIENTATION_NONE );\r
-\r
- byte [] ser = record.serialize();\r
- assertEquals(ser.length , simpleData.length);\r
-\r
- assertTrue(Arrays.equals(simpleData, ser));\r
-\r
- //read again\r
- RecordInputStream is = TestcaseRecordInputStream.create(simpleData);\r
- record = new TextObjectRecord(is);\r
- }\r
-\r
- /**\r
- * Zero {@link ContinueRecord}s follow a {@link TextObjectRecord} if the text is empty\r
- */\r
- public void testWriteEmpty() {\r
- HSSFRichTextString str = new HSSFRichTextString("");\r
-\r
- TextObjectRecord record = new TextObjectRecord();\r
- record.setStr(str);\r
-\r
- byte [] ser = record.serialize();\r
-\r
- int formatDataLen = LittleEndian.getUShort(ser, 16);\r
- assertEquals("formatDataLength", 0, formatDataLen);\r
-\r
- assertEquals(22, ser.length); // just the TXO record\r
-\r
- //read again\r
- RecordInputStream is = TestcaseRecordInputStream.create(ser);\r
- record = new TextObjectRecord(is);\r
- assertEquals(0, record.getStr().length());\r
- }\r
-\r
- /**\r
- * Test that TextObjectRecord serializes logs records properly.\r
- */\r
- public void testLongRecords() {\r
- int[] length = {1024, 2048, 4096, 8192, 16384}; //test against strings of different length\r
- for (int i = 0; i < length.length; i++) {\r
- StringBuffer buff = new StringBuffer(length[i]);\r
- for (int j = 0; j < length[i]; j++) {\r
- buff.append("x");\r
- }\r
- HSSFRichTextString str = new HSSFRichTextString(buff.toString());\r
-\r
- TextObjectRecord obj = new TextObjectRecord();\r
- obj.setStr(str);\r
-\r
- byte [] data = obj.serialize();\r
- RecordInputStream is = new RecordInputStream(new ByteArrayInputStream(data));\r
- is.nextRecord();\r
- TextObjectRecord record = new TextObjectRecord(is);\r
- str = record.getStr();\r
-\r
- assertEquals(buff.length(), str.length());\r
- assertEquals(buff.toString(), str.getString());\r
- }\r
-\r
- }\r
-\r
- /**\r
- * Test cloning\r
- */\r
- public void testClone() {\r
- String text = "Hello, World";\r
- HSSFRichTextString str = new HSSFRichTextString(text);\r
-\r
- TextObjectRecord obj = new TextObjectRecord();\r
- obj.setStr( str );\r
-\r
-\r
- TextObjectRecord cloned = (TextObjectRecord)obj.clone();\r
- assertEquals(obj.getRecordSize(), cloned.getRecordSize());\r
- assertEquals(obj.getHorizontalTextAlignment(), cloned.getHorizontalTextAlignment());\r
- assertEquals(obj.getStr().getString(), cloned.getStr().getString());\r
-\r
- //finally check that the serialized data is the same\r
- byte[] src = obj.serialize();\r
- byte[] cln = cloned.serialize();\r
- assertTrue(Arrays.equals(src, cln));\r
- }\r
-\r
- /** similar to {@link #simpleData} but with link formula at end of TXO rec*/\r
- private static final byte[] linkData = HexRead.readFromString(\r
- "B6 01 " + // TextObjectRecord.sid\r
- "1E 00 " + // size 18\r
- "44 02 02 00 00 00 00 00" +\r
- "00 00 " +\r
- "02 00 " + // strLen 2\r
- "10 00 " + // 16 bytes for 2 format runs\r
- "00 00 00 00 " +\r
-\r
- "05 00 " + // formula size\r
- "D4 F0 8A 03 " + // unknownInt\r
- "24 01 00 13 C0 " + //tRef(T2)\r
- "13 " + // ??\r
-\r
- "3C 00 " + // ContinueRecord.sid\r
- "03 00 " + // size 3\r
- "00 " + // unicode compressed\r
- "41 42 " + // 'AB'\r
- "3C 00 " + // ContinueRecord.sid\r
- "10 00 " + // size 16\r
- "00 00 18 00 00 00 00 00 " +\r
- "02 00 00 00 00 00 00 00 "\r
- );\r
-\r
-\r
- public void testLinkFormula() {\r
- RecordInputStream is = new RecordInputStream(new ByteArrayInputStream(linkData));\r
- is.nextRecord();\r
- TextObjectRecord rec = new TextObjectRecord(is);\r
-\r
- Ptg ptg = rec.getLinkRefPtg();\r
- assertNotNull(ptg);\r
- assertEquals(RefPtg.class, ptg.getClass());\r
- RefPtg rptg = (RefPtg) ptg;\r
- assertEquals("T2", rptg.toFormulaString());\r
-\r
- byte [] data2 = rec.serialize();\r
- assertEquals(linkData.length, data2.length);\r
- assertTrue(Arrays.equals(linkData, data2));\r
- }\r
-\r
-}\r
+/* ====================================================================
+ 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.hssf.record;
+
+import java.io.ByteArrayInputStream;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.hssf.record.formula.RefPtg;
+import org.apache.poi.hssf.usermodel.HSSFRichTextString;
+import org.apache.poi.util.HexRead;
+import org.apache.poi.util.LittleEndian;
+
+/**
+ * Tests that serialization and deserialization of the TextObjectRecord .
+ * Test data taken directly from a real Excel file.
+ *
+ * @author Yegor Kozlov
+ */
+public final class TestTextObjectRecord extends TestCase {
+
+ private static final byte[] simpleData = HexRead.readFromString(
+ "B6 01 12 00 " +
+ "12 02 00 00 00 00 00 00" +
+ "00 00 0D 00 08 00 00 00" +
+ "00 00 " +
+ "3C 00 0E 00 " +
+ "00 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21 " +
+ "3C 00 08 " +
+ "00 0D 00 00 00 00 00 00 00"
+ );
+
+
+ public void testRead() {
+
+ RecordInputStream is =TestcaseRecordInputStream.create(simpleData);
+ TextObjectRecord record = new TextObjectRecord(is);
+
+ assertEquals(TextObjectRecord.sid, record.getSid());
+ assertEquals(TextObjectRecord.HORIZONTAL_TEXT_ALIGNMENT_LEFT_ALIGNED, record.getHorizontalTextAlignment());
+ assertEquals(TextObjectRecord.VERTICAL_TEXT_ALIGNMENT_TOP, record.getVerticalTextAlignment());
+ assertEquals(TextObjectRecord.TEXT_ORIENTATION_NONE, record.getTextOrientation());
+ assertEquals("Hello, World!", record.getStr().getString());
+ }
+
+ public void testWrite() {
+ HSSFRichTextString str = new HSSFRichTextString("Hello, World!");
+
+ TextObjectRecord record = new TextObjectRecord();
+ record.setStr(str);
+ record.setHorizontalTextAlignment( TextObjectRecord.HORIZONTAL_TEXT_ALIGNMENT_LEFT_ALIGNED );
+ record.setVerticalTextAlignment( TextObjectRecord.VERTICAL_TEXT_ALIGNMENT_TOP );
+ record.setTextLocked( true );
+ record.setTextOrientation( TextObjectRecord.TEXT_ORIENTATION_NONE );
+
+ byte [] ser = record.serialize();
+ assertEquals(ser.length , simpleData.length);
+
+ assertTrue(Arrays.equals(simpleData, ser));
+
+ //read again
+ RecordInputStream is = TestcaseRecordInputStream.create(simpleData);
+ record = new TextObjectRecord(is);
+ }
+
+ /**
+ * Zero {@link ContinueRecord}s follow a {@link TextObjectRecord} if the text is empty
+ */
+ public void testWriteEmpty() {
+ HSSFRichTextString str = new HSSFRichTextString("");
+
+ TextObjectRecord record = new TextObjectRecord();
+ record.setStr(str);
+
+ byte [] ser = record.serialize();
+
+ int formatDataLen = LittleEndian.getUShort(ser, 16);
+ assertEquals("formatDataLength", 0, formatDataLen);
+
+ assertEquals(22, ser.length); // just the TXO record
+
+ //read again
+ RecordInputStream is = TestcaseRecordInputStream.create(ser);
+ record = new TextObjectRecord(is);
+ assertEquals(0, record.getStr().length());
+ }
+
+ /**
+ * Test that TextObjectRecord serializes logs records properly.
+ */
+ public void testLongRecords() {
+ int[] length = {1024, 2048, 4096, 8192, 16384}; //test against strings of different length
+ for (int i = 0; i < length.length; i++) {
+ StringBuffer buff = new StringBuffer(length[i]);
+ for (int j = 0; j < length[i]; j++) {
+ buff.append("x");
+ }
+ HSSFRichTextString str = new HSSFRichTextString(buff.toString());
+
+ TextObjectRecord obj = new TextObjectRecord();
+ obj.setStr(str);
+
+ byte [] data = obj.serialize();
+ RecordInputStream is = new RecordInputStream(new ByteArrayInputStream(data));
+ is.nextRecord();
+ TextObjectRecord record = new TextObjectRecord(is);
+ str = record.getStr();
+
+ assertEquals(buff.length(), str.length());
+ assertEquals(buff.toString(), str.getString());
+ }
+ }
+
+ /**
+ * Test cloning
+ */
+ public void testClone() {
+ String text = "Hello, World";
+ HSSFRichTextString str = new HSSFRichTextString(text);
+
+ TextObjectRecord obj = new TextObjectRecord();
+ obj.setStr( str );
+
+
+ TextObjectRecord cloned = (TextObjectRecord)obj.clone();
+ assertEquals(obj.getRecordSize(), cloned.getRecordSize());
+ assertEquals(obj.getHorizontalTextAlignment(), cloned.getHorizontalTextAlignment());
+ assertEquals(obj.getStr().getString(), cloned.getStr().getString());
+
+ //finally check that the serialized data is the same
+ byte[] src = obj.serialize();
+ byte[] cln = cloned.serialize();
+ assertTrue(Arrays.equals(src, cln));
+ }
+
+ /** similar to {@link #simpleData} but with link formula at end of TXO rec*/
+ private static final byte[] linkData = HexRead.readFromString(
+ "B6 01 " + // TextObjectRecord.sid
+ "1E 00 " + // size 18
+ "44 02 02 00 00 00 00 00" +
+ "00 00 " +
+ "02 00 " + // strLen 2
+ "10 00 " + // 16 bytes for 2 format runs
+ "00 00 00 00 " +
+
+ "05 00 " + // formula size
+ "D4 F0 8A 03 " + // unknownInt
+ "24 01 00 13 C0 " + //tRef(T2)
+ "13 " + // ??
+
+ "3C 00 " + // ContinueRecord.sid
+ "03 00 " + // size 3
+ "00 " + // unicode compressed
+ "41 42 " + // 'AB'
+ "3C 00 " + // ContinueRecord.sid
+ "10 00 " + // size 16
+ "00 00 18 00 00 00 00 00 " +
+ "02 00 00 00 00 00 00 00 "
+ );
+
+
+ public void testLinkFormula() {
+ RecordInputStream is = new RecordInputStream(new ByteArrayInputStream(linkData));
+ is.nextRecord();
+ TextObjectRecord rec = new TextObjectRecord(is);
+
+ Ptg ptg = rec.getLinkRefPtg();
+ assertNotNull(ptg);
+ assertEquals(RefPtg.class, ptg.getClass());
+ RefPtg rptg = (RefPtg) ptg;
+ assertEquals("T2", rptg.toFormulaString());
+
+ byte [] data2 = rec.serialize();
+ assertEquals(linkData.length, data2.length);
+ assertTrue(Arrays.equals(linkData, data2));
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
- \r
-package org.apache.poi.hssf.record;\r
-\r
-import junit.framework.AssertionFailedError;\r
-import junit.framework.TestCase;\r
-\r
-import org.apache.poi.util.HexRead;\r
-\r
-/**\r
- * Tests for {@link WriteAccessRecord}\r
- *\r
- * @author Josh Micich\r
- */\r
-public final class TestWriteAccessRecord extends TestCase {\r
- \r
- private static final String HEX_SIXTYFOUR_SPACES = ""\r
- + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "\r
- + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "\r
- + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "\r
- + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20";\r
- \r
- \r
- public void testMissingStringHeader_bug47001a() {\r
- /*\r
- * Data taken from offset 0x0224 in\r
- * attachment 23468 from bugzilla 47001\r
- */\r
- byte[] data = HexRead.readFromString(""\r
- + "5C 00 70 00 "\r
- + "4A 61 76 61 20 45 78 63 65 6C 20 41 50 49 20 76 "\r
- + "32 2E 36 2E 34"\r
- + "20 20 20 20 20 20 20 20 20 20 20 "\r
- + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "\r
- + HEX_SIXTYFOUR_SPACES);\r
-\r
- RecordInputStream in = TestcaseRecordInputStream.create(data);\r
-\r
- WriteAccessRecord rec;\r
- try {\r
- rec = new WriteAccessRecord(in);\r
- } catch (RecordFormatException e) {\r
- if (e.getMessage().equals("Not enough data (0) to read requested (1) bytes")) {\r
- throw new AssertionFailedError("Identified bug 47001a");\r
- }\r
- throw e;\r
- }\r
- assertEquals("Java Excel API v2.6.4", rec.getUsername());\r
- \r
- \r
- byte[] expectedEncoding = HexRead.readFromString(""\r
- + "15 00 00 4A 61 76 61 20 45 78 63 65 6C 20 41 50 "\r
- + "49 20 76 32 2E 36 2E 34"\r
- + "20 20 20 20 20 20 20 20 "\r
- + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "\r
- + HEX_SIXTYFOUR_SPACES);\r
- \r
- TestcaseRecordInputStream.confirmRecordEncoding(WriteAccessRecord.sid, expectedEncoding, rec.serialize());\r
- }\r
- \r
- public void testShortRecordWrittenByMSAccess() {\r
- /*\r
- * Data taken from two example files\r
- * ex42564-21435.xls \r
- * bug_42794.xls (from bug 42794 attachment 20429)\r
- * In both cases, this data is found at offset 0x0C1C.\r
- */\r
- byte[] data = HexRead.readFromString(""\r
- + "5C 00 39 00 "\r
- + "36 00 00 41 20 73 61 74 69 73 66 69 65 64 20 4D "\r
- + "69 63 72 6F 73 6F 66 74 20 4F 66 66 69 63 65 39 "\r
- + "20 55 73 65 72"\r
- + "20 20 20 20 20 20 20 20 20 20 20 "\r
- + "20 20 20 20 20 20 20 20 20");\r
-\r
- RecordInputStream in = TestcaseRecordInputStream.create(data);\r
- WriteAccessRecord rec = new WriteAccessRecord(in);\r
- assertEquals("A satisfied Microsoft Office9 User", rec.getUsername());\r
- byte[] expectedEncoding = HexRead.readFromString(""\r
- + "22 00 00 41 20 73 61 74 69 73 66 69 65 64 20 4D "\r
- + "69 63 72 6F 73 6F 66 74 20 4F 66 66 69 63 65 39 "\r
- + "20 55 73 65 72"\r
- + "20 20 20 20 20 20 20 20 20 20 20 "\r
- + HEX_SIXTYFOUR_SPACES);\r
- \r
- TestcaseRecordInputStream.confirmRecordEncoding(WriteAccessRecord.sid, expectedEncoding, rec.serialize());\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf.record;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+import org.apache.poi.util.HexRead;
+
+/**
+ * Tests for {@link WriteAccessRecord}
+ *
+ * @author Josh Micich
+ */
+public final class TestWriteAccessRecord extends TestCase {
+
+ private static final String HEX_SIXTYFOUR_SPACES = ""
+ + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
+ + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
+ + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
+ + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20";
+
+
+ public void testMissingStringHeader_bug47001a() {
+ /*
+ * Data taken from offset 0x0224 in
+ * attachment 23468 from bugzilla 47001
+ */
+ byte[] data = HexRead.readFromString(""
+ + "5C 00 70 00 "
+ + "4A 61 76 61 20 45 78 63 65 6C 20 41 50 49 20 76 "
+ + "32 2E 36 2E 34"
+ + "20 20 20 20 20 20 20 20 20 20 20 "
+ + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
+ + HEX_SIXTYFOUR_SPACES);
+
+ RecordInputStream in = TestcaseRecordInputStream.create(data);
+
+ WriteAccessRecord rec;
+ try {
+ rec = new WriteAccessRecord(in);
+ } catch (RecordFormatException e) {
+ if (e.getMessage().equals("Not enough data (0) to read requested (1) bytes")) {
+ throw new AssertionFailedError("Identified bug 47001a");
+ }
+ throw e;
+ }
+ assertEquals("Java Excel API v2.6.4", rec.getUsername());
+
+
+ byte[] expectedEncoding = HexRead.readFromString(""
+ + "15 00 00 4A 61 76 61 20 45 78 63 65 6C 20 41 50 "
+ + "49 20 76 32 2E 36 2E 34"
+ + "20 20 20 20 20 20 20 20 "
+ + "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
+ + HEX_SIXTYFOUR_SPACES);
+
+ TestcaseRecordInputStream.confirmRecordEncoding(WriteAccessRecord.sid, expectedEncoding, rec.serialize());
+ }
+
+ public void testShortRecordWrittenByMSAccess() {
+ /*
+ * Data taken from two example files
+ * ex42564-21435.xls
+ * bug_42794.xls (from bug 42794 attachment 20429)
+ * In both cases, this data is found at offset 0x0C1C.
+ */
+ byte[] data = HexRead.readFromString(""
+ + "5C 00 39 00 "
+ + "36 00 00 41 20 73 61 74 69 73 66 69 65 64 20 4D "
+ + "69 63 72 6F 73 6F 66 74 20 4F 66 66 69 63 65 39 "
+ + "20 55 73 65 72"
+ + "20 20 20 20 20 20 20 20 20 20 20 "
+ + "20 20 20 20 20 20 20 20 20");
+
+ RecordInputStream in = TestcaseRecordInputStream.create(data);
+ WriteAccessRecord rec = new WriteAccessRecord(in);
+ assertEquals("A satisfied Microsoft Office9 User", rec.getUsername());
+ byte[] expectedEncoding = HexRead.readFromString(""
+ + "22 00 00 41 20 73 61 74 69 73 66 69 65 64 20 4D "
+ + "69 63 72 6F 73 6F 66 74 20 4F 66 66 69 63 65 39 "
+ + "20 55 73 65 72"
+ + "20 20 20 20 20 20 20 20 20 20 20 "
+ + HEX_SIXTYFOUR_SPACES);
+
+ TestcaseRecordInputStream.confirmRecordEncoding(WriteAccessRecord.sid, expectedEncoding, rec.serialize());
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.record.chart;\r
-\r
-import java.util.Arrays;\r
-\r
-import org.apache.poi.hssf.record.RecordInputStream;\r
-import org.apache.poi.hssf.record.TestcaseRecordInputStream;\r
-import org.apache.poi.util.HexRead;\r
-\r
-import junit.framework.AssertionFailedError;\r
-import junit.framework.TestCase;\r
-\r
-/**\r
- * Tests for {@link ChartFormatRecord} Test data taken directly from a real\r
- * Excel file.\r
- * \r
- * @author Josh Micich\r
- */\r
-public final class TestChartFormatRecord extends TestCase {\r
- /**\r
- * This rather uninteresting data came from attachment 23347 of bug 46693 at\r
- * offsets 0x6BB2 and 0x7BAF\r
- */\r
- private static final byte[] data = HexRead.readFromString(\r
- "14 10 14 00 " // BIFF header\r
- + "00 00 00 00 00 00 00 00 "\r
- + "00 00 00 00 00 00 00 00 "\r
- + "00 00 00 00");\r
-\r
- /**\r
- * The correct size of a {@link ChartFormatRecord} is 20 bytes (not including header).\r
- */\r
- public void testLoad() {\r
- RecordInputStream in = TestcaseRecordInputStream.create(data);\r
- ChartFormatRecord record = new ChartFormatRecord(in);\r
- if (in.remaining() == 2) {\r
- throw new AssertionFailedError("Identified bug 44693d");\r
- }\r
- assertEquals(0, in.remaining());\r
- assertEquals(24, record.getRecordSize());\r
-\r
- byte[] data2 = record.serialize();\r
- assertTrue(Arrays.equals(data, data2));\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf.record.chart;
+
+import java.util.Arrays;
+
+import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.hssf.record.TestcaseRecordInputStream;
+import org.apache.poi.util.HexRead;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+/**
+ * Tests for {@link ChartFormatRecord} Test data taken directly from a real
+ * Excel file.
+ *
+ * @author Josh Micich
+ */
+public final class TestChartFormatRecord extends TestCase {
+ /**
+ * This rather uninteresting data came from attachment 23347 of bug 46693 at
+ * offsets 0x6BB2 and 0x7BAF
+ */
+ private static final byte[] data = HexRead.readFromString(
+ "14 10 14 00 " // BIFF header
+ + "00 00 00 00 00 00 00 00 "
+ + "00 00 00 00 00 00 00 00 "
+ + "00 00 00 00");
+
+ /**
+ * The correct size of a {@link ChartFormatRecord} is 20 bytes (not including header).
+ */
+ public void testLoad() {
+ RecordInputStream in = TestcaseRecordInputStream.create(data);
+ ChartFormatRecord record = new ChartFormatRecord(in);
+ if (in.remaining() == 2) {
+ throw new AssertionFailedError("Identified bug 44693d");
+ }
+ assertEquals(0, in.remaining());
+ assertEquals(24, record.getRecordSize());
+
+ byte[] data2 = record.serialize();
+ assertTrue(Arrays.equals(data, data2));
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.record.formula.eval;\r
-\r
-import junit.framework.AssertionFailedError;\r
-import junit.framework.TestCase;\r
-\r
-import org.apache.poi.hssf.record.formula.functions.EvalFactory;\r
-\r
-/**\r
- * Test for {@link EqualEval}\r
- *\r
- * @author Josh Micich\r
- */\r
-public final class TestEqualEval extends TestCase {\r
-\r
- /**\r
- * Test for bug observable at svn revision 692218 (Sep 2008)<br/>\r
- * The value from a 1x1 area should be taken immediately, regardless of srcRow and srcCol\r
- */\r
- public void test1x1AreaOperand() {\r
- \r
- ValueEval[] values = { BoolEval.FALSE, };\r
- Eval[] args = {\r
- EvalFactory.createAreaEval("B1:B1", values),\r
- BoolEval.FALSE,\r
- };\r
- Eval result = EqualEval.instance.evaluate(args, 10, (short)20);\r
- if (result instanceof ErrorEval) {\r
- if (result == ErrorEval.VALUE_INVALID) {\r
- throw new AssertionFailedError("Identified bug in evaluation of 1x1 area");\r
- }\r
- }\r
- assertEquals(BoolEval.class, result.getClass());\r
- assertTrue(((BoolEval)result).getBooleanValue());\r
- }\r
- /**\r
- * Empty string is equal to blank\r
- */\r
- public void testBlankEqualToEmptyString() {\r
- \r
- Eval[] args = {\r
- new StringEval(""),\r
- BlankEval.INSTANCE,\r
- };\r
- Eval result = EqualEval.instance.evaluate(args, 10, (short)20);\r
- assertEquals(BoolEval.class, result.getClass());\r
- BoolEval be = (BoolEval) result;\r
- if (!be.getBooleanValue()) {\r
- throw new AssertionFailedError("Identified bug blank/empty string equality");\r
- }\r
- assertTrue(be.getBooleanValue());\r
- }\r
- \r
- /**\r
- * Test for bug 46613 (observable at svn r737248)\r
- */\r
- public void testStringInsensitive_bug46613() {\r
- if (!evalStringCmp("abc", "aBc", EqualEval.instance)) {\r
- throw new AssertionFailedError("Identified bug 46613");\r
- }\r
- assertTrue(evalStringCmp("abc", "aBc", EqualEval.instance));\r
- assertTrue(evalStringCmp("ABC", "azz", LessThanEval.instance));\r
- assertTrue(evalStringCmp("abc", "AZZ", LessThanEval.instance));\r
- assertTrue(evalStringCmp("ABC", "aaa", GreaterThanEval.instance));\r
- assertTrue(evalStringCmp("abc", "AAA", GreaterThanEval.instance));\r
- }\r
-\r
- private static boolean evalStringCmp(String a, String b, OperationEval cmpOp) {\r
- Eval[] args = {\r
- new StringEval(a),\r
- new StringEval(b),\r
- };\r
- Eval result = cmpOp.evaluate(args, 10, (short)20);\r
- assertEquals(BoolEval.class, result.getClass());\r
- BoolEval be = (BoolEval) result;\r
- return be.getBooleanValue();\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf.record.formula.eval;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.record.formula.functions.EvalFactory;
+
+/**
+ * Test for {@link EqualEval}
+ *
+ * @author Josh Micich
+ */
+public final class TestEqualEval extends TestCase {
+
+ /**
+ * Test for bug observable at svn revision 692218 (Sep 2008)<br/>
+ * The value from a 1x1 area should be taken immediately, regardless of srcRow and srcCol
+ */
+ public void test1x1AreaOperand() {
+
+ ValueEval[] values = { BoolEval.FALSE, };
+ Eval[] args = {
+ EvalFactory.createAreaEval("B1:B1", values),
+ BoolEval.FALSE,
+ };
+ Eval result = EqualEval.instance.evaluate(args, 10, (short)20);
+ if (result instanceof ErrorEval) {
+ if (result == ErrorEval.VALUE_INVALID) {
+ throw new AssertionFailedError("Identified bug in evaluation of 1x1 area");
+ }
+ }
+ assertEquals(BoolEval.class, result.getClass());
+ assertTrue(((BoolEval)result).getBooleanValue());
+ }
+ /**
+ * Empty string is equal to blank
+ */
+ public void testBlankEqualToEmptyString() {
+
+ Eval[] args = {
+ new StringEval(""),
+ BlankEval.INSTANCE,
+ };
+ Eval result = EqualEval.instance.evaluate(args, 10, (short)20);
+ assertEquals(BoolEval.class, result.getClass());
+ BoolEval be = (BoolEval) result;
+ if (!be.getBooleanValue()) {
+ throw new AssertionFailedError("Identified bug blank/empty string equality");
+ }
+ assertTrue(be.getBooleanValue());
+ }
+
+ /**
+ * Test for bug 46613 (observable at svn r737248)
+ */
+ public void testStringInsensitive_bug46613() {
+ if (!evalStringCmp("abc", "aBc", EqualEval.instance)) {
+ throw new AssertionFailedError("Identified bug 46613");
+ }
+ assertTrue(evalStringCmp("abc", "aBc", EqualEval.instance));
+ assertTrue(evalStringCmp("ABC", "azz", LessThanEval.instance));
+ assertTrue(evalStringCmp("abc", "AZZ", LessThanEval.instance));
+ assertTrue(evalStringCmp("ABC", "aaa", GreaterThanEval.instance));
+ assertTrue(evalStringCmp("abc", "AAA", GreaterThanEval.instance));
+ }
+
+ private static boolean evalStringCmp(String a, String b, OperationEval cmpOp) {
+ Eval[] args = {
+ new StringEval(a),
+ new StringEval(b),
+ };
+ Eval result = cmpOp.evaluate(args, 10, (short)20);
+ assertEquals(BoolEval.class, result.getClass());
+ BoolEval be = (BoolEval) result;
+ return be.getBooleanValue();
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.usermodel;\r
-\r
-import junit.framework.TestCase;\r
-\r
-import java.io.*;\r
-\r
-import org.apache.poi.hssf.HSSFTestDataSamples;\r
-\r
-/**\r
- * Tests HSSFHyperlink.\r
- *\r
- * @author Yegor Kozlov\r
- */\r
-public final class TestHSSFHyperlink extends TestCase {\r
-\r
- /**\r
- * Test that we can read hyperlinks.\r
- */\r
- public void testRead() {\r
-\r
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("HyperlinksOnManySheets.xls");\r
-\r
- HSSFSheet sheet;\r
- HSSFCell cell;\r
- HSSFHyperlink link;\r
-\r
- sheet = wb.getSheet("WebLinks");\r
- cell = sheet.getRow(4).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("POI", link.getLabel());\r
- assertEquals("POI", cell.getRichStringCellValue().getString());\r
- assertEquals("http://poi.apache.org/", link.getAddress());\r
-\r
- cell = sheet.getRow(8).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("HSSF", link.getLabel());\r
- assertEquals("HSSF", cell.getRichStringCellValue().getString());\r
- assertEquals("http://poi.apache.org/hssf/", link.getAddress());\r
-\r
- sheet = wb.getSheet("Emails");\r
- cell = sheet.getRow(4).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("dev", link.getLabel());\r
- assertEquals("dev", cell.getRichStringCellValue().getString());\r
- assertEquals("mailto:dev@poi.apache.org", link.getAddress());\r
-\r
- sheet = wb.getSheet("Internal");\r
- cell = sheet.getRow(4).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("Link To First Sheet", link.getLabel());\r
- assertEquals("Link To First Sheet", cell.getRichStringCellValue().getString());\r
- assertEquals("WebLinks!A1", link.getTextMark());\r
- }\r
-\r
- public void testModify() throws Exception {\r
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("HyperlinksOnManySheets.xls");\r
-\r
- HSSFSheet sheet;\r
- HSSFCell cell;\r
- HSSFHyperlink link;\r
-\r
- sheet = wb.getSheet("WebLinks");\r
- cell = sheet.getRow(4).getCell(0);\r
- link = cell.getHyperlink();\r
- //modify the link\r
- link.setAddress("www.apache.org");\r
-\r
- //serialize and read again\r
- ByteArrayOutputStream out = new ByteArrayOutputStream();\r
- wb.write(out);\r
-\r
- wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));\r
- sheet = wb.getSheet("WebLinks");\r
- cell = sheet.getRow(4).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("www.apache.org", link.getAddress());\r
-\r
- }\r
-\r
- public void testCreate() throws Exception {\r
- HSSFWorkbook wb = new HSSFWorkbook();\r
-\r
- HSSFCell cell;\r
- HSSFSheet sheet = wb.createSheet("Hyperlinks");\r
-\r
- //URL\r
- cell = sheet.createRow(0).createCell(0);\r
- cell.setCellValue("URL Link");\r
- HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);\r
- link.setAddress("http://poi.apache.org/");\r
- cell.setHyperlink(link);\r
-\r
- //link to a file in the current directory\r
- cell = sheet.createRow(1).createCell(0);\r
- cell.setCellValue("File Link");\r
- link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE);\r
- link.setAddress("link1.xls");\r
- cell.setHyperlink(link);\r
-\r
- //e-mail link\r
- cell = sheet.createRow(2).createCell(0);\r
- cell.setCellValue("Email Link");\r
- link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL);\r
- //note, if subject contains white spaces, make sure they are url-encoded\r
- link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");\r
- cell.setHyperlink(link);\r
-\r
- //link to a place in this workbook\r
-\r
- //create a target sheet and cell\r
- HSSFSheet sheet2 = wb.createSheet("Target Sheet");\r
- sheet2.createRow(0).createCell(0).setCellValue("Target Cell");\r
-\r
- cell = sheet.createRow(3).createCell(0);\r
- cell.setCellValue("Worksheet Link");\r
- link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);\r
- link.setTextMark("'Target Sheet'!A1");\r
- cell.setHyperlink(link);\r
-\r
- //serialize and read again\r
- ByteArrayOutputStream out = new ByteArrayOutputStream();\r
- wb.write(out);\r
-\r
- wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));\r
- sheet = wb.getSheet("Hyperlinks");\r
- cell = sheet.getRow(0).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("http://poi.apache.org/", link.getAddress());\r
-\r
- cell = sheet.getRow(1).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("link1.xls", link.getAddress());\r
-\r
- cell = sheet.getRow(2).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("mailto:poi@apache.org?subject=Hyperlinks", link.getAddress());\r
-\r
- cell = sheet.getRow(3).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("'Target Sheet'!A1", link.getTextMark());\r
- }\r
-\r
- public void testCloneSheet() {\r
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("HyperlinksOnManySheets.xls");\r
-\r
- HSSFCell cell;\r
- HSSFHyperlink link;\r
-\r
- HSSFSheet sheet = wb.cloneSheet(0);\r
-\r
- cell = sheet.getRow(4).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("http://poi.apache.org/", link.getAddress());\r
-\r
- cell = sheet.getRow(8).getCell(0);\r
- link = cell.getHyperlink();\r
- assertNotNull(link);\r
- assertEquals("http://poi.apache.org/hssf/", link.getAddress());\r
- }\r
-\r
- /**\r
- * Test that HSSFSheet#shiftRows moves hyperlinks,\r
- * see bugs #46445 and #29957\r
- */\r
- public void testShiftRows(){\r
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("46445.xls");\r
-\r
-\r
- HSSFSheet sheet = wb.getSheetAt(0);\r
-\r
- //verify existing hyperlink in A3\r
- HSSFCell cell1 = sheet.getRow(2).getCell(0);\r
- HSSFHyperlink link1 = cell1.getHyperlink();\r
- assertNotNull(link1);\r
- assertEquals(2, link1.getFirstRow());\r
- assertEquals(2, link1.getLastRow());\r
-\r
- //assign a hyperlink to A4\r
- HSSFHyperlink link2 = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);\r
- link2.setAddress("Sheet2!A2");\r
- HSSFCell cell2 = sheet.getRow(3).getCell(0);\r
- cell2.setHyperlink(link2);\r
- assertEquals(3, link2.getFirstRow());\r
- assertEquals(3, link2.getLastRow());\r
-\r
- //move the 3rd row two rows down\r
- sheet.shiftRows(sheet.getFirstRowNum(), sheet.getLastRowNum(), 2);\r
-\r
- //cells A3 and A4 don't contain hyperlinks anymore\r
- assertNull(sheet.getRow(2).getCell(0).getHyperlink());\r
- assertNull(sheet.getRow(3).getCell(0).getHyperlink());\r
-\r
- //the first hypelink now belongs to A5\r
- HSSFHyperlink link1_shifted = sheet.getRow(2+2).getCell(0).getHyperlink();\r
- assertNotNull(link1_shifted);\r
- assertEquals(4, link1_shifted.getFirstRow());\r
- assertEquals(4, link1_shifted.getLastRow());\r
-\r
- //the second hypelink now belongs to A6\r
- HSSFHyperlink link2_shifted = sheet.getRow(3+2).getCell(0).getHyperlink();\r
- assertNotNull(link2_shifted);\r
- assertEquals(5, link2_shifted.getFirstRow());\r
- assertEquals(5, link2_shifted.getLastRow());\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf.usermodel;
+
+import junit.framework.TestCase;
+
+import java.io.*;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
+
+/**
+ * Tests HSSFHyperlink.
+ *
+ * @author Yegor Kozlov
+ */
+public final class TestHSSFHyperlink extends TestCase {
+
+ /**
+ * Test that we can read hyperlinks.
+ */
+ public void testRead() {
+
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("HyperlinksOnManySheets.xls");
+
+ HSSFSheet sheet;
+ HSSFCell cell;
+ HSSFHyperlink link;
+
+ sheet = wb.getSheet("WebLinks");
+ cell = sheet.getRow(4).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("POI", link.getLabel());
+ assertEquals("POI", cell.getRichStringCellValue().getString());
+ assertEquals("http://poi.apache.org/", link.getAddress());
+
+ cell = sheet.getRow(8).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("HSSF", link.getLabel());
+ assertEquals("HSSF", cell.getRichStringCellValue().getString());
+ assertEquals("http://poi.apache.org/hssf/", link.getAddress());
+
+ sheet = wb.getSheet("Emails");
+ cell = sheet.getRow(4).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("dev", link.getLabel());
+ assertEquals("dev", cell.getRichStringCellValue().getString());
+ assertEquals("mailto:dev@poi.apache.org", link.getAddress());
+
+ sheet = wb.getSheet("Internal");
+ cell = sheet.getRow(4).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("Link To First Sheet", link.getLabel());
+ assertEquals("Link To First Sheet", cell.getRichStringCellValue().getString());
+ assertEquals("WebLinks!A1", link.getTextMark());
+ }
+
+ public void testModify() throws Exception {
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("HyperlinksOnManySheets.xls");
+
+ HSSFSheet sheet;
+ HSSFCell cell;
+ HSSFHyperlink link;
+
+ sheet = wb.getSheet("WebLinks");
+ cell = sheet.getRow(4).getCell(0);
+ link = cell.getHyperlink();
+ //modify the link
+ link.setAddress("www.apache.org");
+
+ //serialize and read again
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ wb.write(out);
+
+ wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+ sheet = wb.getSheet("WebLinks");
+ cell = sheet.getRow(4).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("www.apache.org", link.getAddress());
+
+ }
+
+ public void testCreate() throws Exception {
+ HSSFWorkbook wb = new HSSFWorkbook();
+
+ HSSFCell cell;
+ HSSFSheet sheet = wb.createSheet("Hyperlinks");
+
+ //URL
+ cell = sheet.createRow(0).createCell(0);
+ cell.setCellValue("URL Link");
+ HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
+ link.setAddress("http://poi.apache.org/");
+ cell.setHyperlink(link);
+
+ //link to a file in the current directory
+ cell = sheet.createRow(1).createCell(0);
+ cell.setCellValue("File Link");
+ link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE);
+ link.setAddress("link1.xls");
+ cell.setHyperlink(link);
+
+ //e-mail link
+ cell = sheet.createRow(2).createCell(0);
+ cell.setCellValue("Email Link");
+ link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL);
+ //note, if subject contains white spaces, make sure they are url-encoded
+ link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
+ cell.setHyperlink(link);
+
+ //link to a place in this workbook
+
+ //create a target sheet and cell
+ HSSFSheet sheet2 = wb.createSheet("Target Sheet");
+ sheet2.createRow(0).createCell(0).setCellValue("Target Cell");
+
+ cell = sheet.createRow(3).createCell(0);
+ cell.setCellValue("Worksheet Link");
+ link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
+ link.setTextMark("'Target Sheet'!A1");
+ cell.setHyperlink(link);
+
+ //serialize and read again
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ wb.write(out);
+
+ wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+ sheet = wb.getSheet("Hyperlinks");
+ cell = sheet.getRow(0).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("http://poi.apache.org/", link.getAddress());
+
+ cell = sheet.getRow(1).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("link1.xls", link.getAddress());
+
+ cell = sheet.getRow(2).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("mailto:poi@apache.org?subject=Hyperlinks", link.getAddress());
+
+ cell = sheet.getRow(3).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("'Target Sheet'!A1", link.getTextMark());
+ }
+
+ public void testCloneSheet() {
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("HyperlinksOnManySheets.xls");
+
+ HSSFCell cell;
+ HSSFHyperlink link;
+
+ HSSFSheet sheet = wb.cloneSheet(0);
+
+ cell = sheet.getRow(4).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("http://poi.apache.org/", link.getAddress());
+
+ cell = sheet.getRow(8).getCell(0);
+ link = cell.getHyperlink();
+ assertNotNull(link);
+ assertEquals("http://poi.apache.org/hssf/", link.getAddress());
+ }
+
+ /**
+ * Test that HSSFSheet#shiftRows moves hyperlinks,
+ * see bugs #46445 and #29957
+ */
+ public void testShiftRows(){
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("46445.xls");
+
+
+ HSSFSheet sheet = wb.getSheetAt(0);
+
+ //verify existing hyperlink in A3
+ HSSFCell cell1 = sheet.getRow(2).getCell(0);
+ HSSFHyperlink link1 = cell1.getHyperlink();
+ assertNotNull(link1);
+ assertEquals(2, link1.getFirstRow());
+ assertEquals(2, link1.getLastRow());
+
+ //assign a hyperlink to A4
+ HSSFHyperlink link2 = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
+ link2.setAddress("Sheet2!A2");
+ HSSFCell cell2 = sheet.getRow(3).getCell(0);
+ cell2.setHyperlink(link2);
+ assertEquals(3, link2.getFirstRow());
+ assertEquals(3, link2.getLastRow());
+
+ //move the 3rd row two rows down
+ sheet.shiftRows(sheet.getFirstRowNum(), sheet.getLastRowNum(), 2);
+
+ //cells A3 and A4 don't contain hyperlinks anymore
+ assertNull(sheet.getRow(2).getCell(0).getHyperlink());
+ assertNull(sheet.getRow(3).getCell(0).getHyperlink());
+
+ //the first hypelink now belongs to A5
+ HSSFHyperlink link1_shifted = sheet.getRow(2+2).getCell(0).getHyperlink();
+ assertNotNull(link1_shifted);
+ assertEquals(4, link1_shifted.getFirstRow());
+ assertEquals(4, link1_shifted.getLastRow());
+
+ //the second hypelink now belongs to A6
+ HSSFHyperlink link2_shifted = sheet.getRow(3+2).getCell(0).getHyperlink();
+ assertNotNull(link2_shifted);
+ assertEquals(5, link2_shifted.getFirstRow());
+ assertEquals(5, link2_shifted.getLastRow());
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.usermodel;\r
-\r
-import org.apache.poi.hssf.HSSFTestDataSamples;\r
-\r
-import junit.framework.AssertionFailedError;\r
-import junit.framework.TestCase;\r
-\r
-/**\r
- * @author Josh Micich\r
- */\r
-public final class TestHSSFPatriarch extends TestCase {\r
-\r
- public void testBasic() {\r
-\r
- HSSFWorkbook wb = new HSSFWorkbook();\r
- HSSFSheet sheet = wb.createSheet();\r
-\r
- HSSFPatriarch patr = sheet.createDrawingPatriarch();\r
-\r
- assertNotNull(patr);\r
-\r
- // assert something more interesting\r
- }\r
-\r
- // TODO - fix bug 44916 (1-May-2008)\r
- public void DISABLED_test44916() {\r
-\r
- HSSFWorkbook wb = new HSSFWorkbook();\r
- HSSFSheet sheet = wb.createSheet();\r
-\r
- // 1. Create drawing patriarch\r
- HSSFPatriarch patr = sheet.createDrawingPatriarch();\r
-\r
- // 2. Try to re-get the patriarch\r
- HSSFPatriarch existingPatr;\r
- try {\r
- existingPatr = sheet.getDrawingPatriarch();\r
- } catch (NullPointerException e) {\r
- throw new AssertionFailedError("Identified bug 44916");\r
- }\r
-\r
- // 3. Use patriarch\r
- HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 600, 245, (short) 1, 1, (short) 1, 2);\r
- anchor.setAnchorType(3);\r
- byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("logoKarmokar4.png");\r
- int idx1 = wb.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);\r
- patr.createPicture(anchor, idx1);\r
-\r
- // 4. Try to re-use patriarch later\r
- existingPatr = sheet.getDrawingPatriarch();\r
- assertNotNull(existingPatr);\r
- }\r
-\r
-}\r
+/* ====================================================================
+ 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.hssf.usermodel;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+/**
+ * @author Josh Micich
+ */
+public final class TestHSSFPatriarch extends TestCase {
+
+ public void testBasic() {
+
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+
+ HSSFPatriarch patr = sheet.createDrawingPatriarch();
+ assertNotNull(patr);
+
+ // assert something more interesting
+ }
+
+ // TODO - fix bug 44916 (1-May-2008)
+ public void DISABLED_test44916() {
+
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+
+ // 1. Create drawing patriarch
+ HSSFPatriarch patr = sheet.createDrawingPatriarch();
+
+ // 2. Try to re-get the patriarch
+ HSSFPatriarch existingPatr;
+ try {
+ existingPatr = sheet.getDrawingPatriarch();
+ } catch (NullPointerException e) {
+ throw new AssertionFailedError("Identified bug 44916");
+ }
+
+ // 3. Use patriarch
+ HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 600, 245, (short) 1, 1, (short) 1, 2);
+ anchor.setAnchorType(3);
+ byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("logoKarmokar4.png");
+ int idx1 = wb.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
+ patr.createPicture(anchor, idx1);
+
+ // 4. Try to re-use patriarch later
+ existingPatr = sheet.getDrawingPatriarch();
+ assertNotNull(existingPatr);
+ }
+}
-/*\r
-* Licensed to the Apache Software Foundation (ASF) under one or more\r
-* contributor license agreements. See the NOTICE file distributed with\r
-* this work for additional information regarding copyright ownership.\r
-* The ASF licenses this file to You under the Apache License, Version 2.0\r
-* (the "License"); you may not use this file except in compliance with\r
-* the License. You may obtain a copy of the License at\r
-*\r
-* http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*/\r
-package org.apache.poi.hssf.usermodel;\r
-\r
-import junit.framework.TestCase;\r
-\r
-import org.apache.poi.hssf.HSSFTestDataSamples;\r
-import org.apache.poi.hssf.HSSFITestDataProvider;\r
-import org.apache.poi.ss.usermodel.BaseTestPicture;\r
-\r
-/**\r
- * Test <code>HSSFPicture</code>.\r
- *\r
- * @author Yegor Kozlov (yegor at apache.org)\r
- */\r
-public final class TestHSSFPicture extends BaseTestPicture {\r
-\r
- @Override\r
- protected HSSFITestDataProvider getTestDataProvider(){\r
- return HSSFITestDataProvider.getInstance();\r
- }\r
-\r
- public void testResize() {\r
- baseTestResize(new HSSFClientAnchor(0, 0, 848, 240, (short)0, 0, (short)1, 9));\r
- }\r
-\r
- /**\r
- * Bug # 45829 reported ArithmeticException (/ by zero) when resizing png with zero DPI.\r
- */\r
- public void test45829() {\r
- HSSFWorkbook wb = new HSSFWorkbook();\r
- HSSFSheet sh1 = wb.createSheet();\r
- HSSFPatriarch p1 = sh1.createDrawingPatriarch();\r
-\r
- byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("45829.png");\r
- int idx1 = wb.addPicture( pictureData, HSSFWorkbook.PICTURE_TYPE_PNG );\r
- HSSFPicture pic = p1.createPicture(new HSSFClientAnchor(), idx1);\r
- pic.resize();\r
- }\r
-}\r
+/*
+* 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.hssf.usermodel;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.HSSFITestDataProvider;
+import org.apache.poi.ss.usermodel.BaseTestPicture;
+
+/**
+ * Test <code>HSSFPicture</code>.
+ *
+ * @author Yegor Kozlov (yegor at apache.org)
+ */
+public final class TestHSSFPicture extends BaseTestPicture {
+
+ @Override
+ protected HSSFITestDataProvider getTestDataProvider(){
+ return HSSFITestDataProvider.getInstance();
+ }
+
+ public void testResize() {
+ baseTestResize(new HSSFClientAnchor(0, 0, 848, 240, (short)0, 0, (short)1, 9));
+ }
+
+ /**
+ * Bug # 45829 reported ArithmeticException (/ by zero) when resizing png with zero DPI.
+ */
+ public void test45829() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh1 = wb.createSheet();
+ HSSFPatriarch p1 = sh1.createDrawingPatriarch();
+
+ byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("45829.png");
+ int idx1 = wb.addPicture( pictureData, HSSFWorkbook.PICTURE_TYPE_PNG );
+ HSSFPicture pic = p1.createPicture(new HSSFClientAnchor(), idx1);
+ pic.resize();
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.hssf.usermodel;\r
-\r
-import java.awt.image.BufferedImage;\r
-import java.io.ByteArrayInputStream;\r
-import java.io.IOException;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-\r
-import javax.imageio.ImageIO;\r
-\r
-import junit.framework.TestCase;\r
-\r
-import org.apache.poi.hssf.HSSFTestDataSamples;\r
-\r
-/**\r
- * Test <code>HSSFPictureData</code>.\r
- * The code to retrieve images from a workbook provided by Trejkaz (trejkaz at trypticon dot org) in Bug 41223.\r
- *\r
- * @author Yegor Kozlov (yegor at apache dot org)\r
- * @author Trejkaz (trejkaz at trypticon dot org)\r
- */\r
-public final class TestHSSFPictureData extends TestCase{\r
-\r
-\r
- public void testPictures() throws IOException {\r
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");\r
-\r
- List lst = wb.getAllPictures();\r
- //assertEquals(2, lst.size());\r
-\r
- for (Iterator it = lst.iterator(); it.hasNext(); ) {\r
- HSSFPictureData pict = (HSSFPictureData)it.next();\r
- String ext = pict.suggestFileExtension();\r
- byte[] data = pict.getData();\r
- if (ext.equals("jpeg")){\r
- //try to read image data using javax.imageio.* (JDK 1.4+)\r
- BufferedImage jpg = ImageIO.read(new ByteArrayInputStream(data));\r
- assertNotNull(jpg);\r
- assertEquals(192, jpg.getWidth());\r
- assertEquals(176, jpg.getHeight());\r
- assertEquals(HSSFWorkbook.PICTURE_TYPE_JPEG, pict.getFormat());\r
- } else if (ext.equals("png")){\r
- //try to read image data using javax.imageio.* (JDK 1.4+)\r
- BufferedImage png = ImageIO.read(new ByteArrayInputStream(data));\r
- assertNotNull(png);\r
- assertEquals(300, png.getWidth());\r
- assertEquals(300, png.getHeight());\r
- assertEquals(HSSFWorkbook.PICTURE_TYPE_PNG, pict.getFormat());\r
- } else {\r
- //TODO: test code for PICT, WMF and EMF\r
- }\r
- }\r
- }\r
-}\r
+/* ====================================================================
+ 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.hssf.usermodel;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.imageio.ImageIO;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
+
+/**
+ * Test <code>HSSFPictureData</code>.
+ * The code to retrieve images from a workbook provided by Trejkaz (trejkaz at trypticon dot org) in Bug 41223.
+ *
+ * @author Yegor Kozlov (yegor at apache dot org)
+ * @author Trejkaz (trejkaz at trypticon dot org)
+ */
+public final class TestHSSFPictureData extends TestCase{
+
+
+ public void testPictures() throws IOException {
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");
+
+ @SuppressWarnings("unchecked") // TODO - add getFormat() to interface PictureData and genericise wb.getAllPictures()
+ List<HSSFPictureData> lst = (List<HSSFPictureData>)(List<?>)wb.getAllPictures();
+ //assertEquals(2, lst.size());
+
+ for (Iterator it = lst.iterator(); it.hasNext(); ) {
+ HSSFPictureData pict = (HSSFPictureData)it.next();
+ String ext = pict.suggestFileExtension();
+ byte[] data = pict.getData();
+ if (ext.equals("jpeg")){
+ //try to read image data using javax.imageio.* (JDK 1.4+)
+ BufferedImage jpg = ImageIO.read(new ByteArrayInputStream(data));
+ assertNotNull(jpg);
+ assertEquals(192, jpg.getWidth());
+ assertEquals(176, jpg.getHeight());
+ assertEquals(HSSFWorkbook.PICTURE_TYPE_JPEG, pict.getFormat());
+ } else if (ext.equals("png")){
+ //try to read image data using javax.imageio.* (JDK 1.4+)
+ BufferedImage png = ImageIO.read(new ByteArrayInputStream(data));
+ assertNotNull(png);
+ assertEquals(300, png.getWidth());
+ assertEquals(300, png.getHeight());
+ assertEquals(HSSFWorkbook.PICTURE_TYPE_PNG, pict.getFormat());
+ } else {
+ //TODO: test code for PICT, WMF and EMF
+ }
+ }
+ }
+}
-/*\r
-* Licensed to the Apache Software Foundation (ASF) under one or more\r
-* contributor license agreements. See the NOTICE file distributed with\r
-* this work for additional information regarding copyright ownership.\r
-* The ASF licenses this file to You under the Apache License, Version 2.0\r
-* (the "License"); you may not use this file except in compliance with\r
-* the License. You may obtain a copy of the License at\r
-*\r
-* http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*/\r
-package org.apache.poi.hssf.usermodel;\r
-\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-\r
-import junit.framework.TestCase;\r
-\r
-/**\r
- * Test <code>HSSFTextbox</code>.\r
- *\r
- * @author Yegor Kozlov (yegor at apache.org)\r
- */\r
-public final class TestHSSFTextbox extends TestCase{\r
-\r
- /**\r
- * Test that accessors to horizontal and vertical alignment work properly\r
- */\r
- public void testAlignment() {\r
- HSSFWorkbook wb = new HSSFWorkbook();\r
- HSSFSheet sh1 = wb.createSheet();\r
- HSSFPatriarch patriarch = sh1.createDrawingPatriarch();\r
-\r
- HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 6, 4));\r
- HSSFRichTextString str = new HSSFRichTextString("Hello, World");\r
- textbox.setString(str);\r
- textbox.setHorizontalAlignment(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED);\r
- textbox.setVerticalAlignment(HSSFTextbox.VERTICAL_ALIGNMENT_CENTER);\r
-\r
- assertEquals(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED, textbox.getHorizontalAlignment());\r
- assertEquals(HSSFTextbox.VERTICAL_ALIGNMENT_CENTER, textbox.getVerticalAlignment());\r
- }\r
-\r
- /**\r
- * Excel requires at least one format run in HSSFTextbox.\r
- * When inserting text make sure that if font is not set we must set the default one.\r
- */\r
- public void testSetDeafultTextFormat() {\r
- HSSFWorkbook wb = new HSSFWorkbook();\r
- HSSFSheet sheet = wb.createSheet();\r
- HSSFPatriarch patriarch = sheet.createDrawingPatriarch();\r
-\r
- HSSFTextbox textbox1 = patriarch.createTextbox(new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)3,3));\r
- HSSFRichTextString rt1 = new HSSFRichTextString("Hello, World!");\r
- assertEquals(0, rt1.numFormattingRuns());\r
- textbox1.setString(rt1);\r
-\r
- HSSFRichTextString rt2 = textbox1.getString();\r
- assertEquals(1, rt2.numFormattingRuns());\r
- assertEquals(HSSFRichTextString.NO_FONT, rt2.getFontOfFormattingRun(0));\r
- }\r
-\r
- }\r
+/* ====================================================================
+ 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.hssf.usermodel;
+
+import junit.framework.TestCase;
+
+/**
+ * Test <code>HSSFTextbox</code>.
+ *
+ * @author Yegor Kozlov (yegor at apache.org)
+ */
+public final class TestHSSFTextbox extends TestCase{
+
+ /**
+ * Test that accessors to horizontal and vertical alignment work properly
+ */
+ public void testAlignment() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh1 = wb.createSheet();
+ HSSFPatriarch patriarch = sh1.createDrawingPatriarch();
+
+ HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 6, 4));
+ HSSFRichTextString str = new HSSFRichTextString("Hello, World");
+ textbox.setString(str);
+ textbox.setHorizontalAlignment(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED);
+ textbox.setVerticalAlignment(HSSFTextbox.VERTICAL_ALIGNMENT_CENTER);
+
+ assertEquals(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED, textbox.getHorizontalAlignment());
+ assertEquals(HSSFTextbox.VERTICAL_ALIGNMENT_CENTER, textbox.getVerticalAlignment());
+ }
+
+ /**
+ * Excel requires at least one format run in HSSFTextbox.
+ * When inserting text make sure that if font is not set we must set the default one.
+ */
+ public void testSetDeafultTextFormat() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+
+ HSSFTextbox textbox1 = patriarch.createTextbox(new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)3,3));
+ HSSFRichTextString rt1 = new HSSFRichTextString("Hello, World!");
+ assertEquals(0, rt1.numFormattingRuns());
+ textbox1.setString(rt1);
+
+ HSSFRichTextString rt2 = textbox1.getString();
+ assertEquals(1, rt2.numFormattingRuns());
+ assertEquals(HSSFRichTextString.NO_FONT, rt2.getFontOfFormattingRun(0));
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.ss;\r
-\r
-import org.apache.poi.ss.usermodel.Workbook;\r
-\r
-/**\r
- * Incapsulates a provider of test data for common HSSF / XSSF tests.\r
- */\r
-public interface ITestDataProvider {\r
- /**\r
- * Override to provide HSSF / XSSF specific way for re-serialising a workbook\r
- *\r
- * @param wb the workbook to re-serialize\r
- * @return the re-serialized workbook\r
- */\r
- Workbook writeOutAndReadBack(Workbook wb);\r
-\r
- /**\r
- * Override to provide way of loading HSSF / XSSF sample workbooks\r
- *\r
- * @param sampleFileName the file name to load\r
- * @return an instance of Workbook loaded from the supplied file name\r
- */\r
- Workbook openSampleWorkbook(String sampleFileName);\r
-\r
- /**\r
- * Override to provide way of creating HSSF / XSSF workbooks\r
- * @return an instance of Workbook\r
- */\r
- Workbook createWorkbook();\r
-\r
- /**\r
- * Opens a sample file from the standard HSSF test data directory\r
- *\r
- * @return an open <tt>InputStream</tt> for the specified sample file\r
- */\r
- byte[] getTestDataFileContent(String fileName);\r
-\r
- SpreadsheetVersion getSpreadsheetVersion();\r
-}\r
+/* ====================================================================
+ 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;
+
+import org.apache.poi.ss.usermodel.Workbook;
+
+/**
+ * Encapsulates a provider of test data for common HSSF / XSSF tests.
+ */
+public interface ITestDataProvider {
+ /**
+ * Override to provide HSSF / XSSF specific way for re-serialising a workbook
+ *
+ * @param wb the workbook to re-serialize
+ * @return the re-serialized workbook
+ */
+ Workbook writeOutAndReadBack(Workbook wb);
+
+ /**
+ * Override to provide way of loading HSSF / XSSF sample workbooks
+ *
+ * @param sampleFileName the file name to load
+ * @return an instance of Workbook loaded from the supplied file name
+ */
+ Workbook openSampleWorkbook(String sampleFileName);
+
+ /**
+ * Override to provide way of creating HSSF / XSSF workbooks
+ * @return an instance of Workbook
+ */
+ Workbook createWorkbook();
+
+ /**
+ * Opens a sample file from the standard HSSF test data directory
+ *
+ * @return an open <tt>InputStream</tt> for the specified sample file
+ */
+ byte[] getTestDataFileContent(String fileName);
+
+ SpreadsheetVersion getSpreadsheetVersion();
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.ss;\r
-\r
-import junit.framework.TestCase;\r
-\r
-/**\r
- * Check that all enum values are properly set\r
- *\r
- * @author Yegor Kozlov\r
- */\r
-public class TestSpreadsheetVersion extends TestCase {\r
-\r
- public void testExcel97(){\r
- SpreadsheetVersion v = SpreadsheetVersion.EXCEL97;\r
- assertEquals(1 << 8, v.getMaxColumns());\r
- assertEquals(v.getMaxColumns() - 1, v.getLastColumnIndex());\r
- assertEquals(1 << 16, v.getMaxRows());\r
- assertEquals(v.getMaxRows() - 1, v.getLastRowIndex());\r
- assertEquals(30, v.getMaxFunctionArgs());\r
- assertEquals(3, v.getMaxConditionalFormats());\r
- assertEquals("IV", v.getLastColumnName());\r
- }\r
-\r
- public void testExcel2007(){\r
- SpreadsheetVersion v = SpreadsheetVersion.EXCEL2007;\r
- assertEquals(1 << 14, v.getMaxColumns());\r
- assertEquals(v.getMaxColumns() - 1, v.getLastColumnIndex());\r
- assertEquals(1 << 20, v.getMaxRows());\r
- assertEquals(v.getMaxRows() - 1, v.getLastRowIndex());\r
- assertEquals(255, v.getMaxFunctionArgs());\r
- assertEquals(Integer.MAX_VALUE, v.getMaxConditionalFormats());\r
- assertEquals("XFD", v.getLastColumnName());\r
- }\r
-}\r
+/* ====================================================================
+ 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;
+
+import junit.framework.TestCase;
+
+/**
+ * Check that all enum values are properly set
+ *
+ * @author Yegor Kozlov
+ */
+public final class TestSpreadsheetVersion extends TestCase {
+
+ public void testExcel97(){
+ SpreadsheetVersion v = SpreadsheetVersion.EXCEL97;
+ assertEquals(1 << 8, v.getMaxColumns());
+ assertEquals(v.getMaxColumns() - 1, v.getLastColumnIndex());
+ assertEquals(1 << 16, v.getMaxRows());
+ assertEquals(v.getMaxRows() - 1, v.getLastRowIndex());
+ assertEquals(30, v.getMaxFunctionArgs());
+ assertEquals(3, v.getMaxConditionalFormats());
+ assertEquals("IV", v.getLastColumnName());
+ }
+
+ public void testExcel2007(){
+ SpreadsheetVersion v = SpreadsheetVersion.EXCEL2007;
+ assertEquals(1 << 14, v.getMaxColumns());
+ assertEquals(v.getMaxColumns() - 1, v.getLastColumnIndex());
+ assertEquals(1 << 20, v.getMaxRows());
+ assertEquals(v.getMaxRows() - 1, v.getLastRowIndex());
+ assertEquals(255, v.getMaxFunctionArgs());
+ assertEquals(Integer.MAX_VALUE, v.getMaxConditionalFormats());
+ assertEquals("XFD", v.getLastColumnName());
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.ss.formula;\r
-\r
-import junit.framework.Assert;\r
-import junit.framework.AssertionFailedError;\r
-\r
-import org.apache.poi.ss.formula.FormulaParser.FormulaParseException;\r
-/**\r
- * Avoids making {@link FormulaParseException} public\r
- * \r
- * @author Josh Micich\r
- */\r
-public class FormulaParserTestHelper {\r
- /**\r
- * @throws AssertionFailedError if <tt>e</tt> is not a formula parser exception \r
- * or if the exception message doesn't match.\r
- */\r
- public static void confirmParseException(RuntimeException e, String expectedMessage) {\r
- checkType(e);\r
- Assert.assertEquals(expectedMessage, e.getMessage());\r
- }\r
- /**\r
- * @throws AssertionFailedError if <tt>e</tt> is not a formula parser exception \r
- * or if <tt>e</tt> has no message.\r
- */\r
- public static void confirmParseException(RuntimeException e) {\r
- checkType(e);\r
- Assert.assertNotNull(e.getMessage());\r
- }\r
- private static void checkType(RuntimeException e) throws AssertionFailedError {\r
- if (!(e instanceof FormulaParseException)) {\r
- String failMsg = "Expected FormulaParseException, but got (" \r
- + e.getClass().getName() + "):";\r
- System.err.println(failMsg);\r
- e.printStackTrace();\r
- throw new AssertionFailedError(failMsg);\r
- }\r
- }\r
-}\r
+/* ====================================================================
+ 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 junit.framework.Assert;
+import junit.framework.AssertionFailedError;
+
+import org.apache.poi.ss.formula.FormulaParser.FormulaParseException;
+/**
+ * Avoids making {@link FormulaParseException} public
+ *
+ * @author Josh Micich
+ */
+public class FormulaParserTestHelper {
+ /**
+ * @throws AssertionFailedError if <tt>e</tt> is not a formula parser exception
+ * or if the exception message doesn't match.
+ */
+ public static void confirmParseException(RuntimeException e, String expectedMessage) {
+ checkType(e);
+ Assert.assertEquals(expectedMessage, e.getMessage());
+ }
+ /**
+ * @throws AssertionFailedError if <tt>e</tt> is not a formula parser exception
+ * or if <tt>e</tt> has no message.
+ */
+ public static void confirmParseException(RuntimeException e) {
+ checkType(e);
+ Assert.assertNotNull(e.getMessage());
+ }
+ private static void checkType(RuntimeException e) throws AssertionFailedError {
+ if (!(e instanceof FormulaParseException)) {
+ String failMsg = "Expected FormulaParseException, but got ("
+ + e.getClass().getName() + "):";
+ System.err.println(failMsg);
+ e.printStackTrace();
+ throw new AssertionFailedError(failMsg);
+ }
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.ss.usermodel;\r
-\r
-import junit.framework.TestCase;\r
-import org.apache.poi.ss.ITestDataProvider;\r
-import org.apache.poi.ss.SpreadsheetVersion;\r
-import org.apache.poi.ss.util.CellRangeAddress;\r
-\r
-/**\r
- * A base class for bugzilla issues that can be described in terms of common ss interfaces.\r
- *\r
- * @author Yegor Kozlov\r
- */\r
-public abstract class BaseTestBugzillaIssues extends TestCase {\r
-\r
- protected abstract ITestDataProvider getTestDataProvider();\r
-\r
- /**\r
- *\r
- * Test writing a hyperlink\r
- * Open resulting sheet in Excel and check that A1 contains a hyperlink\r
- *\r
- * Also tests bug 15353 (problems with hyperlinks to Google)\r
- */\r
- public void test23094() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet s = wb.createSheet();\r
- Row r = s.createRow(0);\r
- r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")");\r
- r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");\r
-\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- r = wb.getSheetAt(0).getRow(0);\r
-\r
- Cell cell_0 = r.getCell(0);\r
- assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula());\r
- Cell cell_1 = r.getCell(1);\r
- assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula());\r
- }\r
-\r
- /**\r
- * test writing a file with large number of unique strings,\r
- * open resulting file in Excel to check results!\r
- * @param num the number of strings to generate\r
- */\r
- public void baseTest15375(int num) {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet sheet = wb.createSheet();\r
- CreationHelper factory = wb.getCreationHelper();\r
-\r
- String tmp1 = null;\r
- String tmp2 = null;\r
- String tmp3 = null;\r
-\r
- for (int i = 0; i < num; i++) {\r
- tmp1 = "Test1" + i;\r
- tmp2 = "Test2" + i;\r
- tmp3 = "Test3" + i;\r
-\r
- Row row = sheet.createRow(i);\r
-\r
- Cell cell = row.createCell(0);\r
- cell.setCellValue(factory.createRichTextString(tmp1));\r
- cell = row.createCell(1);\r
- cell.setCellValue(factory.createRichTextString(tmp2));\r
- cell = row.createCell(2);\r
- cell.setCellValue(factory.createRichTextString(tmp3));\r
- }\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- for (int i = 0; i < num; i++) {\r
- tmp1 = "Test1" + i;\r
- tmp2 = "Test2" + i;\r
- tmp3 = "Test3" + i;\r
-\r
- Row row = sheet.getRow(i);\r
-\r
- assertEquals(tmp1, row.getCell(0).getStringCellValue());\r
- assertEquals(tmp2, row.getCell(1).getStringCellValue());\r
- assertEquals(tmp3, row.getCell(2).getStringCellValue());\r
- }\r
- }\r
-\r
- /**\r
- * Merged regions were being removed from the parent in cloned sheets\r
- */\r
- public void test22720() {\r
- Workbook workBook = getTestDataProvider().createWorkbook();\r
- workBook.createSheet("TEST");\r
- Sheet template = workBook.getSheetAt(0);\r
-\r
- template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));\r
- template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));\r
-\r
- Sheet clone = workBook.cloneSheet(0);\r
- int originalMerged = template.getNumMergedRegions();\r
- assertEquals("2 merged regions", 2, originalMerged);\r
-\r
- //remove merged regions from clone\r
- for (int i=template.getNumMergedRegions()-1; i>=0; i--) {\r
- clone.removeMergedRegion(i);\r
- }\r
-\r
- assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());\r
- //check if template's merged regions are OK\r
- if (template.getNumMergedRegions()>0) {\r
- // fetch the first merged region...EXCEPTION OCCURS HERE\r
- template.getMergedRegion(0);\r
- }\r
- //make sure we dont exception\r
-\r
- }\r
-\r
- public void test28031() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet sheet = wb.createSheet();\r
- wb.setSheetName(0, "Sheet1");\r
-\r
- Row row = sheet.createRow(0);\r
- Cell cell = row.createCell(0);\r
- String formulaText =\r
- "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";\r
- cell.setCellFormula(formulaText);\r
-\r
- assertEquals(formulaText, cell.getCellFormula());\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- cell = wb.getSheetAt(0).getRow(0).getCell(0);\r
- assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula());\r
- }\r
-\r
- /**\r
- * Bug 21334: "File error: data may have been lost" with a file\r
- * that contains macros and this formula:\r
- * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}\r
- */\r
- public void test21334() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet sh = wb.createSheet();\r
- Cell cell = sh.createRow(0).createCell(0);\r
- String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";\r
- cell.setCellFormula(formula);\r
-\r
- Workbook wb_sv = getTestDataProvider().writeOutAndReadBack(wb);\r
- Cell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0);\r
- assertEquals(formula, cell_sv.getCellFormula());\r
- }\r
-\r
- /** another test for the number of unique strings issue\r
- *test opening the resulting file in Excel*/\r
- public void test22568() {\r
- int r=2000;int c=3;\r
-\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet sheet = wb.createSheet("ExcelTest") ;\r
-\r
- int col_cnt=0, rw_cnt=0 ;\r
-\r
- col_cnt = c;\r
- rw_cnt = r;\r
-\r
- Row rw ;\r
- rw = sheet.createRow(0) ;\r
- //Header row\r
- for(int j=0; j<col_cnt; j++){\r
- Cell cell = rw.createCell(j) ;\r
- cell.setCellValue("Col " + (j+1));\r
- }\r
-\r
- for(int i=1; i<rw_cnt; i++){\r
- rw = sheet.createRow(i) ;\r
- for(int j=0; j<col_cnt; j++){\r
- Cell cell = rw.createCell(j) ;\r
- cell.setCellValue("Row:" + (i+1) + ",Column:" + (j+1));\r
- }\r
- }\r
-\r
- sheet.setDefaultColumnWidth(18) ;\r
-\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- sheet = wb.getSheetAt(0);\r
- rw = sheet.getRow(0);\r
- //Header row\r
- for(int j=0; j<col_cnt; j++){\r
- Cell cell = rw.getCell(j) ;\r
- assertEquals("Col " + (j+1), cell.getStringCellValue());\r
- }\r
- for(int i=1; i<rw_cnt; i++){\r
- rw = sheet.getRow(i) ;\r
- for(int j=0; j<col_cnt; j++){\r
- Cell cell = rw.getCell(j) ;\r
- assertEquals("Row:" + (i+1) + ",Column:" + (j+1), cell.getStringCellValue());\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69\r
- */\r
- public void test42448(){\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Cell cell = wb.createSheet().createRow(0).createCell(0);\r
- cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");\r
- assertTrue("no errors parsing formula", true);\r
- }\r
-\r
- /**\r
- * HSSFRichTextString.length() returns negative for really\r
- * long strings\r
- */\r
- public void test46368() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet s = wb.createSheet();\r
- Row r = s.createRow(0);\r
- for(int i=0; i<15; i++) {\r
- int len = 32760 + i;\r
- Cell c = r.createCell(i);\r
-\r
- StringBuffer sb = new StringBuffer();\r
- for(int j=0; j<len; j++) {\r
- sb.append("x");\r
- }\r
- RichTextString rtr = wb.getCreationHelper().createRichTextString(sb.toString());\r
- assertEquals(len, rtr.length());\r
- c.setCellValue(rtr);\r
- }\r
-\r
- // Save and reload\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- s = wb.getSheetAt(0);\r
- r = s.getRow(0);\r
- for(int i=0; i<15; i++) {\r
- int len = 32760 + i;\r
- Cell c = r.getCell(i);\r
- assertEquals(len, c.getRichStringCellValue().length());\r
- }\r
- }\r
-\r
- public void test18800() {\r
- Workbook book = getTestDataProvider().createWorkbook();\r
- book.createSheet("TEST");\r
- Sheet sheet = book.cloneSheet(0);\r
- book.setSheetName(1,"CLONE");\r
- sheet.createRow(0).createCell(0).setCellValue("Test");\r
-\r
- book = getTestDataProvider().writeOutAndReadBack(book);\r
- sheet = book.getSheet("CLONE");\r
- Row row = sheet.getRow(0);\r
- Cell cell = row.getCell(0);\r
- assertEquals("Test", cell.getRichStringCellValue().getString());\r
- }\r
-\r
- private static void addNewSheetWithCellsA1toD4(Workbook book, int sheet) {\r
-\r
- Sheet sht = book .createSheet("s" + sheet);\r
- for (int r=0; r < 4; r++) {\r
-\r
- Row row = sht.createRow (r);\r
- for (int c=0; c < 4; c++) {\r
-\r
- Cell cel = row.createCell(c);\r
- cel.setCellValue(sheet*100 + r*10 + c);\r
- }\r
- }\r
- }\r
-\r
- public void testBug43093() {\r
- Workbook xlw = getTestDataProvider().createWorkbook();\r
-\r
- addNewSheetWithCellsA1toD4(xlw, 1);\r
- addNewSheetWithCellsA1toD4(xlw, 2);\r
- addNewSheetWithCellsA1toD4(xlw, 3);\r
- addNewSheetWithCellsA1toD4(xlw, 4);\r
-\r
- Sheet s2 = xlw.getSheet("s2");\r
- Row s2r3 = s2.getRow(3);\r
- Cell s2E4 = s2r3.createCell(4);\r
- s2E4.setCellFormula("SUM(s3!B2:C3)");\r
-\r
- FormulaEvaluator eva = xlw.getCreationHelper().createFormulaEvaluator();\r
- double d = eva.evaluate(s2E4).getNumberValue();\r
-\r
- assertEquals(d, (311+312+321+322), 0.0000001);\r
- }\r
-\r
- public void testMaxFunctionArguments_bug46729(){\r
- String[] func = {"COUNT", "AVERAGE", "MAX", "MIN", "OR", "SUBTOTAL", "SKEW"};\r
-\r
- SpreadsheetVersion ssVersion = getTestDataProvider().getSpreadsheetVersion();\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Cell cell = wb.createSheet().createRow(0).createCell(0);\r
-\r
- String fmla;\r
- for (String name : func) {\r
-\r
- fmla = createFunction(name, 5);\r
- cell.setCellFormula(fmla);\r
-\r
- fmla = createFunction(name, ssVersion.getMaxFunctionArgs());\r
- cell.setCellFormula(fmla);\r
-\r
- try {\r
- fmla = createFunction(name, ssVersion.getMaxFunctionArgs() + 1);\r
- cell.setCellFormula(fmla);\r
- fail("Expected FormulaParseException");\r
- } catch (RuntimeException e){\r
- assertTrue(e.getMessage().startsWith("Too many arguments to function '"+name+"'"));\r
- }\r
- }\r
- }\r
-\r
- private String createFunction(String name, int maxArgs){\r
- StringBuffer fmla = new StringBuffer();\r
- fmla.append(name);\r
- fmla.append("(");\r
- for(int i=0; i < maxArgs; i++){\r
- if(i > 0) fmla.append(',');\r
- fmla.append("A1");\r
- }\r
- fmla.append(")");\r
- return fmla.toString();\r
- }\r
-}
\ No newline at end of file
+/* ====================================================================
+ 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.usermodel;
+
+import junit.framework.TestCase;
+import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.ss.util.CellRangeAddress;
+
+/**
+ * A base class for bugzilla issues that can be described in terms of common ss interfaces.
+ *
+ * @author Yegor Kozlov
+ */
+public abstract class BaseTestBugzillaIssues extends TestCase {
+
+ protected abstract ITestDataProvider getTestDataProvider();
+
+ /**
+ *
+ * Test writing a hyperlink
+ * Open resulting sheet in Excel and check that A1 contains a hyperlink
+ *
+ * Also tests bug 15353 (problems with hyperlinks to Google)
+ */
+ public void test23094() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet s = wb.createSheet();
+ Row r = s.createRow(0);
+ r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")");
+ r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
+
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ r = wb.getSheetAt(0).getRow(0);
+
+ Cell cell_0 = r.getCell(0);
+ assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula());
+ Cell cell_1 = r.getCell(1);
+ assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula());
+ }
+
+ /**
+ * test writing a file with large number of unique strings,
+ * open resulting file in Excel to check results!
+ * @param num the number of strings to generate
+ */
+ public void baseTest15375(int num) {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet sheet = wb.createSheet();
+ CreationHelper factory = wb.getCreationHelper();
+
+ String tmp1 = null;
+ String tmp2 = null;
+ String tmp3 = null;
+
+ for (int i = 0; i < num; i++) {
+ tmp1 = "Test1" + i;
+ tmp2 = "Test2" + i;
+ tmp3 = "Test3" + i;
+
+ Row row = sheet.createRow(i);
+
+ Cell cell = row.createCell(0);
+ cell.setCellValue(factory.createRichTextString(tmp1));
+ cell = row.createCell(1);
+ cell.setCellValue(factory.createRichTextString(tmp2));
+ cell = row.createCell(2);
+ cell.setCellValue(factory.createRichTextString(tmp3));
+ }
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ for (int i = 0; i < num; i++) {
+ tmp1 = "Test1" + i;
+ tmp2 = "Test2" + i;
+ tmp3 = "Test3" + i;
+
+ Row row = sheet.getRow(i);
+
+ assertEquals(tmp1, row.getCell(0).getStringCellValue());
+ assertEquals(tmp2, row.getCell(1).getStringCellValue());
+ assertEquals(tmp3, row.getCell(2).getStringCellValue());
+ }
+ }
+
+ /**
+ * Merged regions were being removed from the parent in cloned sheets
+ */
+ public void test22720() {
+ Workbook workBook = getTestDataProvider().createWorkbook();
+ workBook.createSheet("TEST");
+ Sheet template = workBook.getSheetAt(0);
+
+ template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
+ template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));
+
+ Sheet clone = workBook.cloneSheet(0);
+ int originalMerged = template.getNumMergedRegions();
+ assertEquals("2 merged regions", 2, originalMerged);
+
+ //remove merged regions from clone
+ for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
+ clone.removeMergedRegion(i);
+ }
+
+ assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
+ //check if template's merged regions are OK
+ if (template.getNumMergedRegions()>0) {
+ // fetch the first merged region...EXCEPTION OCCURS HERE
+ template.getMergedRegion(0);
+ }
+ //make sure we dont exception
+
+ }
+
+ public void test28031() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet sheet = wb.createSheet();
+ wb.setSheetName(0, "Sheet1");
+
+ Row row = sheet.createRow(0);
+ Cell cell = row.createCell(0);
+ String formulaText =
+ "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";
+ cell.setCellFormula(formulaText);
+
+ assertEquals(formulaText, cell.getCellFormula());
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ cell = wb.getSheetAt(0).getRow(0).getCell(0);
+ assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula());
+ }
+
+ /**
+ * Bug 21334: "File error: data may have been lost" with a file
+ * that contains macros and this formula:
+ * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}
+ */
+ public void test21334() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet sh = wb.createSheet();
+ Cell cell = sh.createRow(0).createCell(0);
+ String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";
+ cell.setCellFormula(formula);
+
+ Workbook wb_sv = getTestDataProvider().writeOutAndReadBack(wb);
+ Cell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0);
+ assertEquals(formula, cell_sv.getCellFormula());
+ }
+
+ /** another test for the number of unique strings issue
+ *test opening the resulting file in Excel*/
+ public void test22568() {
+ int r=2000;int c=3;
+
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet sheet = wb.createSheet("ExcelTest") ;
+
+ int col_cnt=0, rw_cnt=0 ;
+
+ col_cnt = c;
+ rw_cnt = r;
+
+ Row rw ;
+ rw = sheet.createRow(0) ;
+ //Header row
+ for(int j=0; j<col_cnt; j++){
+ Cell cell = rw.createCell(j) ;
+ cell.setCellValue("Col " + (j+1));
+ }
+
+ for(int i=1; i<rw_cnt; i++){
+ rw = sheet.createRow(i) ;
+ for(int j=0; j<col_cnt; j++){
+ Cell cell = rw.createCell(j) ;
+ cell.setCellValue("Row:" + (i+1) + ",Column:" + (j+1));
+ }
+ }
+
+ sheet.setDefaultColumnWidth(18) ;
+
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ rw = sheet.getRow(0);
+ //Header row
+ for(int j=0; j<col_cnt; j++){
+ Cell cell = rw.getCell(j) ;
+ assertEquals("Col " + (j+1), cell.getStringCellValue());
+ }
+ for(int i=1; i<rw_cnt; i++){
+ rw = sheet.getRow(i) ;
+ for(int j=0; j<col_cnt; j++){
+ Cell cell = rw.getCell(j) ;
+ assertEquals("Row:" + (i+1) + ",Column:" + (j+1), cell.getStringCellValue());
+ }
+ }
+ }
+
+ /**
+ * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69
+ */
+ public void test42448(){
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Cell cell = wb.createSheet().createRow(0).createCell(0);
+ cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");
+ assertTrue("no errors parsing formula", true);
+ }
+
+ /**
+ * HSSFRichTextString.length() returns negative for really
+ * long strings
+ */
+ public void test46368() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet s = wb.createSheet();
+ Row r = s.createRow(0);
+ for(int i=0; i<15; i++) {
+ int len = 32760 + i;
+ Cell c = r.createCell(i);
+
+ StringBuffer sb = new StringBuffer();
+ for(int j=0; j<len; j++) {
+ sb.append("x");
+ }
+ RichTextString rtr = wb.getCreationHelper().createRichTextString(sb.toString());
+ assertEquals(len, rtr.length());
+ c.setCellValue(rtr);
+ }
+
+ // Save and reload
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ s = wb.getSheetAt(0);
+ r = s.getRow(0);
+ for(int i=0; i<15; i++) {
+ int len = 32760 + i;
+ Cell c = r.getCell(i);
+ assertEquals(len, c.getRichStringCellValue().length());
+ }
+ }
+
+ public void test18800() {
+ Workbook book = getTestDataProvider().createWorkbook();
+ book.createSheet("TEST");
+ Sheet sheet = book.cloneSheet(0);
+ book.setSheetName(1,"CLONE");
+ sheet.createRow(0).createCell(0).setCellValue("Test");
+
+ book = getTestDataProvider().writeOutAndReadBack(book);
+ sheet = book.getSheet("CLONE");
+ Row row = sheet.getRow(0);
+ Cell cell = row.getCell(0);
+ assertEquals("Test", cell.getRichStringCellValue().getString());
+ }
+
+ private static void addNewSheetWithCellsA1toD4(Workbook book, int sheet) {
+
+ Sheet sht = book .createSheet("s" + sheet);
+ for (int r=0; r < 4; r++) {
+
+ Row row = sht.createRow (r);
+ for (int c=0; c < 4; c++) {
+
+ Cell cel = row.createCell(c);
+ cel.setCellValue(sheet*100 + r*10 + c);
+ }
+ }
+ }
+
+ public void testBug43093() {
+ Workbook xlw = getTestDataProvider().createWorkbook();
+
+ addNewSheetWithCellsA1toD4(xlw, 1);
+ addNewSheetWithCellsA1toD4(xlw, 2);
+ addNewSheetWithCellsA1toD4(xlw, 3);
+ addNewSheetWithCellsA1toD4(xlw, 4);
+
+ Sheet s2 = xlw.getSheet("s2");
+ Row s2r3 = s2.getRow(3);
+ Cell s2E4 = s2r3.createCell(4);
+ s2E4.setCellFormula("SUM(s3!B2:C3)");
+
+ FormulaEvaluator eva = xlw.getCreationHelper().createFormulaEvaluator();
+ double d = eva.evaluate(s2E4).getNumberValue();
+
+ assertEquals(d, (311+312+321+322), 0.0000001);
+ }
+
+ public void testMaxFunctionArguments_bug46729(){
+ String[] func = {"COUNT", "AVERAGE", "MAX", "MIN", "OR", "SUBTOTAL", "SKEW"};
+
+ SpreadsheetVersion ssVersion = getTestDataProvider().getSpreadsheetVersion();
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Cell cell = wb.createSheet().createRow(0).createCell(0);
+
+ String fmla;
+ for (String name : func) {
+
+ fmla = createFunction(name, 5);
+ cell.setCellFormula(fmla);
+
+ fmla = createFunction(name, ssVersion.getMaxFunctionArgs());
+ cell.setCellFormula(fmla);
+
+ try {
+ fmla = createFunction(name, ssVersion.getMaxFunctionArgs() + 1);
+ cell.setCellFormula(fmla);
+ fail("Expected FormulaParseException");
+ } catch (RuntimeException e){
+ assertTrue(e.getMessage().startsWith("Too many arguments to function '"+name+"'"));
+ }
+ }
+ }
+
+ private String createFunction(String name, int maxArgs){
+ StringBuffer fmla = new StringBuffer();
+ fmla.append(name);
+ fmla.append("(");
+ for(int i=0; i < maxArgs; i++){
+ if(i > 0) fmla.append(',');
+ fmla.append("A1");
+ }
+ fmla.append(")");
+ return fmla.toString();
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.ss.usermodel;\r
-\r
-import junit.framework.TestCase;\r
-import junit.framework.AssertionFailedError;\r
-import org.apache.poi.ss.ITestDataProvider;\r
-import org.apache.poi.ss.util.CellRangeAddress;\r
-\r
-/**\r
- * @author Yegor Kozlov\r
- */\r
-public abstract class BaseTestFont extends TestCase {\r
-\r
- protected abstract ITestDataProvider getTestDataProvider();\r
-\r
- public void baseTestDefaultFont(String defaultName, short defaultSize, short defaultColor){\r
- //get default font and check against default value\r
- Workbook workbook = getTestDataProvider().createWorkbook();\r
- Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, defaultColor, defaultSize, defaultName, false, false, Font.SS_NONE, Font.U_NONE);\r
- assertNotNull(fontFind);\r
-\r
- //get default font, then change 2 values and check against different values (height changes)\r
- Font font=workbook.createFont();\r
- font.setBoldweight(Font.BOLDWEIGHT_BOLD);\r
- assertEquals(Font.BOLDWEIGHT_BOLD, font.getBoldweight());\r
- font.setUnderline(Font.U_DOUBLE);\r
- assertEquals(Font.U_DOUBLE, font.getUnderline());\r
- font.setFontHeightInPoints((short)15);\r
- assertEquals(15*20, font.getFontHeight());\r
- assertEquals(15, font.getFontHeightInPoints());\r
- fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, defaultColor, (short)(15*20), defaultName, false, false, Font.SS_NONE, Font.U_DOUBLE);\r
- assertNotNull(fontFind);\r
- }\r
-\r
- public void testGetNumberOfFonts(){\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- int num0 = wb.getNumberOfFonts();\r
-\r
- Font f1=wb.createFont();\r
- f1.setBoldweight(Font.BOLDWEIGHT_BOLD);\r
- short idx1 = f1.getIndex();\r
- wb.createCellStyle().setFont(f1);\r
-\r
- Font f2=wb.createFont();\r
- f2.setUnderline(Font.U_DOUBLE);\r
- short idx2 = f2.getIndex();\r
- wb.createCellStyle().setFont(f2);\r
-\r
- Font f3=wb.createFont();\r
- f3.setFontHeightInPoints((short)23);\r
- short idx3 = f3.getIndex();\r
- wb.createCellStyle().setFont(f3);\r
-\r
- assertEquals(num0 + 3,wb.getNumberOfFonts());\r
- assertEquals(Font.BOLDWEIGHT_BOLD,wb.getFontAt(idx1).getBoldweight());\r
- assertEquals(Font.U_DOUBLE,wb.getFontAt(idx2).getUnderline());\r
- assertEquals(23,wb.getFontAt(idx3).getFontHeightInPoints());\r
- }\r
-\r
- /**\r
- * Tests that we can define fonts to a new\r
- * file, save, load, and still see them\r
- * @throws Exception\r
- */\r
- public void testCreateSave() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet s1 = wb.createSheet();\r
- Row r1 = s1.createRow(0);\r
- Cell r1c1 = r1.createCell(0);\r
- r1c1.setCellValue(2.2);\r
-\r
- int num0 = wb.getNumberOfFonts();\r
-\r
- Font font=wb.createFont();\r
- font.setBoldweight(Font.BOLDWEIGHT_BOLD);\r
- font.setStrikeout(true);\r
- font.setColor(IndexedColors.YELLOW.getIndex());\r
- font.setFontName("Courier");\r
- short font1Idx = font.getIndex();\r
- wb.createCellStyle().setFont(font);\r
- assertEquals(num0 + 1, wb.getNumberOfFonts());\r
-\r
- CellStyle cellStyleTitle=wb.createCellStyle();\r
- cellStyleTitle.setFont(font);\r
- r1c1.setCellStyle(cellStyleTitle);\r
-\r
- // Save and re-load\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- s1 = wb.getSheetAt(0);\r
-\r
- assertEquals(num0 + 1, wb.getNumberOfFonts());\r
- short idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex();\r
- Font fnt = wb.getFontAt(idx);\r
- assertNotNull(fnt);\r
- assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor());\r
- assertEquals("Courier", fnt.getFontName());\r
-\r
- // Now add an orphaned one\r
- Font font2 = wb.createFont();\r
- font2.setItalic(true);\r
- font2.setFontHeightInPoints((short)15);\r
- short font2Idx = font2.getIndex();\r
- wb.createCellStyle().setFont(font2);\r
- assertEquals(num0 + 2, wb.getNumberOfFonts());\r
-\r
- // Save and re-load\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- s1 = wb.getSheetAt(0);\r
-\r
- assertEquals(num0 + 2, wb.getNumberOfFonts());\r
- assertNotNull(wb.getFontAt(font1Idx));\r
- assertNotNull(wb.getFontAt(font2Idx));\r
-\r
- assertEquals(15, wb.getFontAt(font2Idx).getFontHeightInPoints());\r
- assertEquals(true, wb.getFontAt(font2Idx).getItalic());\r
- }\r
-\r
-\r
-\r
- /**\r
- * Test that fonts get added properly\r
- *\r
- * @see org.apache.poi.hssf.usermodel.TestBugs#test45338()\r
- */\r
- public void test45338() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- int num0 = wb.getNumberOfFonts();\r
-\r
- Sheet s = wb.createSheet();\r
- s.createRow(0);\r
- s.createRow(1);\r
- s.getRow(0).createCell(0);\r
- s.getRow(1).createCell(0);\r
-\r
- //default font\r
- Font f1 = wb.getFontAt((short)0);\r
- assertEquals(Font.BOLDWEIGHT_NORMAL, f1.getBoldweight());\r
-\r
- // Check that asking for the same font\r
- // multiple times gives you the same thing.\r
- // Otherwise, our tests wouldn't work!\r
- assertSame(wb.getFontAt((short)0), wb.getFontAt((short)0));\r
-\r
- // Look for a new font we have\r
- // yet to add\r
- assertNull(\r
- wb.findFont(\r
- Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),\r
- "Thingy", false, true, (short)2, (byte)2\r
- )\r
- );\r
-\r
- Font nf = wb.createFont();\r
- short nfIdx = nf.getIndex();\r
- assertEquals(num0 + 1, wb.getNumberOfFonts());\r
-\r
- assertSame(nf, wb.getFontAt(nfIdx));\r
-\r
- nf.setBoldweight(Font.BOLDWEIGHT_BOLD);\r
- nf.setColor((short)123);\r
- nf.setFontHeightInPoints((short)22);\r
- nf.setFontName("Thingy");\r
- nf.setItalic(false);\r
- nf.setStrikeout(true);\r
- nf.setTypeOffset((short)2);\r
- nf.setUnderline((byte)2);\r
-\r
- assertEquals(num0 + 1, wb.getNumberOfFonts());\r
- assertEquals(nf, wb.getFontAt(nfIdx));\r
-\r
- assertEquals(wb.getFontAt(nfIdx), wb.getFontAt(nfIdx));\r
- assertTrue(wb.getFontAt((short)0) != wb.getFontAt(nfIdx));\r
-\r
- // Find it now\r
- assertNotNull(\r
- wb.findFont(\r
- Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),\r
- "Thingy", false, true, (short)2, (byte)2\r
- )\r
- );\r
- assertSame(nf,\r
- wb.findFont(\r
- Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),\r
- "Thingy", false, true, (short)2, (byte)2\r
- )\r
- );\r
- }\r
-\r
-}
\ No newline at end of file
+/* ====================================================================
+ 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.usermodel;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.ss.ITestDataProvider;
+
+/**
+ * @author Yegor Kozlov
+ */
+public abstract class BaseTestFont extends TestCase {
+
+ protected abstract ITestDataProvider getTestDataProvider();
+
+ public void baseTestDefaultFont(String defaultName, short defaultSize, short defaultColor){
+ //get default font and check against default value
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, defaultColor, defaultSize, defaultName, false, false, Font.SS_NONE, Font.U_NONE);
+ assertNotNull(fontFind);
+
+ //get default font, then change 2 values and check against different values (height changes)
+ Font font=workbook.createFont();
+ font.setBoldweight(Font.BOLDWEIGHT_BOLD);
+ assertEquals(Font.BOLDWEIGHT_BOLD, font.getBoldweight());
+ font.setUnderline(Font.U_DOUBLE);
+ assertEquals(Font.U_DOUBLE, font.getUnderline());
+ font.setFontHeightInPoints((short)15);
+ assertEquals(15*20, font.getFontHeight());
+ assertEquals(15, font.getFontHeightInPoints());
+ fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, defaultColor, (short)(15*20), defaultName, false, false, Font.SS_NONE, Font.U_DOUBLE);
+ assertNotNull(fontFind);
+ }
+
+ public void testGetNumberOfFonts(){
+ Workbook wb = getTestDataProvider().createWorkbook();
+ int num0 = wb.getNumberOfFonts();
+
+ Font f1=wb.createFont();
+ f1.setBoldweight(Font.BOLDWEIGHT_BOLD);
+ short idx1 = f1.getIndex();
+ wb.createCellStyle().setFont(f1);
+
+ Font f2=wb.createFont();
+ f2.setUnderline(Font.U_DOUBLE);
+ short idx2 = f2.getIndex();
+ wb.createCellStyle().setFont(f2);
+
+ Font f3=wb.createFont();
+ f3.setFontHeightInPoints((short)23);
+ short idx3 = f3.getIndex();
+ wb.createCellStyle().setFont(f3);
+
+ assertEquals(num0 + 3,wb.getNumberOfFonts());
+ assertEquals(Font.BOLDWEIGHT_BOLD,wb.getFontAt(idx1).getBoldweight());
+ assertEquals(Font.U_DOUBLE,wb.getFontAt(idx2).getUnderline());
+ assertEquals(23,wb.getFontAt(idx3).getFontHeightInPoints());
+ }
+
+ /**
+ * Tests that we can define fonts to a new
+ * file, save, load, and still see them
+ */
+ public void testCreateSave() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet s1 = wb.createSheet();
+ Row r1 = s1.createRow(0);
+ Cell r1c1 = r1.createCell(0);
+ r1c1.setCellValue(2.2);
+
+ int num0 = wb.getNumberOfFonts();
+
+ Font font=wb.createFont();
+ font.setBoldweight(Font.BOLDWEIGHT_BOLD);
+ font.setStrikeout(true);
+ font.setColor(IndexedColors.YELLOW.getIndex());
+ font.setFontName("Courier");
+ short font1Idx = font.getIndex();
+ wb.createCellStyle().setFont(font);
+ assertEquals(num0 + 1, wb.getNumberOfFonts());
+
+ CellStyle cellStyleTitle=wb.createCellStyle();
+ cellStyleTitle.setFont(font);
+ r1c1.setCellStyle(cellStyleTitle);
+
+ // Save and re-load
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ s1 = wb.getSheetAt(0);
+
+ assertEquals(num0 + 1, wb.getNumberOfFonts());
+ short idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex();
+ Font fnt = wb.getFontAt(idx);
+ assertNotNull(fnt);
+ assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor());
+ assertEquals("Courier", fnt.getFontName());
+
+ // Now add an orphaned one
+ Font font2 = wb.createFont();
+ font2.setItalic(true);
+ font2.setFontHeightInPoints((short)15);
+ short font2Idx = font2.getIndex();
+ wb.createCellStyle().setFont(font2);
+ assertEquals(num0 + 2, wb.getNumberOfFonts());
+
+ // Save and re-load
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ s1 = wb.getSheetAt(0);
+
+ assertEquals(num0 + 2, wb.getNumberOfFonts());
+ assertNotNull(wb.getFontAt(font1Idx));
+ assertNotNull(wb.getFontAt(font2Idx));
+
+ assertEquals(15, wb.getFontAt(font2Idx).getFontHeightInPoints());
+ assertEquals(true, wb.getFontAt(font2Idx).getItalic());
+ }
+
+ /**
+ * Test that fonts get added properly
+ *
+ * @see org.apache.poi.hssf.usermodel.TestBugs#test45338()
+ */
+ public void test45338() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ int num0 = wb.getNumberOfFonts();
+
+ Sheet s = wb.createSheet();
+ s.createRow(0);
+ s.createRow(1);
+ s.getRow(0).createCell(0);
+ s.getRow(1).createCell(0);
+
+ //default font
+ Font f1 = wb.getFontAt((short)0);
+ assertEquals(Font.BOLDWEIGHT_NORMAL, f1.getBoldweight());
+
+ // Check that asking for the same font
+ // multiple times gives you the same thing.
+ // Otherwise, our tests wouldn't work!
+ assertSame(wb.getFontAt((short)0), wb.getFontAt((short)0));
+
+ // Look for a new font we have
+ // yet to add
+ assertNull(
+ wb.findFont(
+ Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),
+ "Thingy", false, true, (short)2, (byte)2
+ )
+ );
+
+ Font nf = wb.createFont();
+ short nfIdx = nf.getIndex();
+ assertEquals(num0 + 1, wb.getNumberOfFonts());
+
+ assertSame(nf, wb.getFontAt(nfIdx));
+
+ nf.setBoldweight(Font.BOLDWEIGHT_BOLD);
+ nf.setColor((short)123);
+ nf.setFontHeightInPoints((short)22);
+ nf.setFontName("Thingy");
+ nf.setItalic(false);
+ nf.setStrikeout(true);
+ nf.setTypeOffset((short)2);
+ nf.setUnderline((byte)2);
+
+ assertEquals(num0 + 1, wb.getNumberOfFonts());
+ assertEquals(nf, wb.getFontAt(nfIdx));
+
+ assertEquals(wb.getFontAt(nfIdx), wb.getFontAt(nfIdx));
+ assertTrue(wb.getFontAt((short)0) != wb.getFontAt(nfIdx));
+
+ // Find it now
+ assertNotNull(
+ wb.findFont(
+ Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),
+ "Thingy", false, true, (short)2, (byte)2
+ )
+ );
+ assertSame(nf,
+ wb.findFont(
+ Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),
+ "Thingy", false, true, (short)2, (byte)2
+ )
+ );
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.ss.usermodel;\r
-\r
-import junit.framework.TestCase;\r
-import org.apache.poi.ss.ITestDataProvider;\r
-\r
-/**\r
- * @author Yegor Kozlov\r
- */\r
-public abstract class BaseTestPicture extends TestCase {\r
-\r
- protected abstract ITestDataProvider getTestDataProvider();\r
-\r
- public void baseTestResize(ClientAnchor referenceAnchor) {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet sh1 = wb.createSheet();\r
- Drawing p1 = sh1.createDrawingPatriarch();\r
- CreationHelper factory = wb.getCreationHelper();\r
-\r
- byte[] pictureData = getTestDataProvider().getTestDataFileContent("logoKarmokar4.png");\r
- int idx1 = wb.addPicture( pictureData, Workbook.PICTURE_TYPE_PNG );\r
- Picture picture = p1.createPicture(factory.createClientAnchor(), idx1);\r
- picture.resize();\r
- ClientAnchor anchor1 = picture.getPreferredSize();\r
-\r
- //assert against what would BiffViewer print if we insert the image in xls and dump the file\r
- assertEquals(referenceAnchor.getCol1(), anchor1.getCol1());\r
- assertEquals(referenceAnchor.getRow1(), anchor1.getRow1());\r
- assertEquals(referenceAnchor.getCol2(), anchor1.getCol2());\r
- assertEquals(referenceAnchor.getRow2(), anchor1.getRow2());\r
- assertEquals(referenceAnchor.getDx1(), anchor1.getDx1());\r
- assertEquals(referenceAnchor.getDy1(), anchor1.getDy1());\r
- assertEquals(referenceAnchor.getDx2(), anchor1.getDx2());\r
- assertEquals(referenceAnchor.getDy2(), anchor1.getDy2());\r
- }\r
-\r
-\r
-}
\ No newline at end of file
+/* ====================================================================
+ 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.usermodel;
+
+import junit.framework.TestCase;
+import org.apache.poi.ss.ITestDataProvider;
+
+/**
+ * @author Yegor Kozlov
+ */
+public abstract class BaseTestPicture extends TestCase {
+
+ protected abstract ITestDataProvider getTestDataProvider();
+
+ public void baseTestResize(ClientAnchor referenceAnchor) {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet sh1 = wb.createSheet();
+ Drawing p1 = sh1.createDrawingPatriarch();
+ CreationHelper factory = wb.getCreationHelper();
+
+ byte[] pictureData = getTestDataProvider().getTestDataFileContent("logoKarmokar4.png");
+ int idx1 = wb.addPicture( pictureData, Workbook.PICTURE_TYPE_PNG );
+ Picture picture = p1.createPicture(factory.createClientAnchor(), idx1);
+ picture.resize();
+ ClientAnchor anchor1 = picture.getPreferredSize();
+
+ //assert against what would BiffViewer print if we insert the image in xls and dump the file
+ assertEquals(referenceAnchor.getCol1(), anchor1.getCol1());
+ assertEquals(referenceAnchor.getRow1(), anchor1.getRow1());
+ assertEquals(referenceAnchor.getCol2(), anchor1.getCol2());
+ assertEquals(referenceAnchor.getRow2(), anchor1.getRow2());
+ assertEquals(referenceAnchor.getDx1(), anchor1.getDx1());
+ assertEquals(referenceAnchor.getDy1(), anchor1.getDy1());
+ assertEquals(referenceAnchor.getDx2(), anchor1.getDx2());
+ assertEquals(referenceAnchor.getDy2(), anchor1.getDy2());
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.ss.usermodel;\r
-\r
-import junit.framework.TestCase;\r
-import org.apache.poi.ss.ITestDataProvider;\r
-import org.apache.poi.ss.util.CellRangeAddress;\r
-\r
-/**\r
- * Tests row shifting capabilities.\r
- *\r
- * @author Shawn Laubach (slaubach at apache dot com)\r
- * @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)\r
- */\r
-public abstract class BaseTestSheetShiftRows extends TestCase {\r
-\r
- /**\r
- * @return an object that provides test data in HSSF / XSSF specific way\r
- */\r
- protected abstract ITestDataProvider getTestDataProvider();\r
-\r
- /**\r
- * Tests the shiftRows function. Does three different shifts.\r
- * After each shift, writes the workbook to file and reads back to\r
- * check. This ensures that if some changes code that breaks\r
- * writing or what not, they realize it.\r
- *\r
- * @param sampleName the sample file to test against\r
- */\r
- public final void baseTestShiftRows(String sampleName){\r
- // Read initial file in\r
- Workbook wb = getTestDataProvider().openSampleWorkbook(sampleName);\r
- Sheet s = wb.getSheetAt( 0 );\r
-\r
- // Shift the second row down 1 and write to temp file\r
- s.shiftRows( 1, 1, 1 );\r
-\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
-\r
- // Read from temp file and check the number of cells in each\r
- // row (in original file each row was unique)\r
- s = wb.getSheetAt( 0 );\r
-\r
- assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 1);\r
- confirmEmptyRow(s, 1);\r
- assertEquals(s.getRow(2).getPhysicalNumberOfCells(), 2);\r
- assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 4);\r
- assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);\r
-\r
- // Shift rows 1-3 down 3 in the current one. This tests when\r
- // 1 row is blank. Write to a another temp file\r
- s.shiftRows( 0, 2, 3 );\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
-\r
- // Read and ensure things are where they should be\r
- s = wb.getSheetAt(0);\r
- confirmEmptyRow(s, 0);\r
- confirmEmptyRow(s, 1);\r
- confirmEmptyRow(s, 2);\r
- assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 1);\r
- confirmEmptyRow(s, 4);\r
- assertEquals(s.getRow(5).getPhysicalNumberOfCells(), 2);\r
-\r
- // Read the first file again\r
- wb = getTestDataProvider().openSampleWorkbook(sampleName);\r
- s = wb.getSheetAt( 0 );\r
-\r
- // Shift rows 3 and 4 up and write to temp file\r
- s.shiftRows( 2, 3, -2 );\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- s = wb.getSheetAt( 0 );\r
- assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 3);\r
- assertEquals(s.getRow(1).getPhysicalNumberOfCells(), 4);\r
- confirmEmptyRow(s, 2);\r
- confirmEmptyRow(s, 3);\r
- assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);\r
- }\r
- private static void confirmEmptyRow(Sheet s, int rowIx) {\r
- Row row = s.getRow(rowIx);\r
- assertTrue(row == null || row.getPhysicalNumberOfCells() == 0);\r
- }\r
-\r
- /**\r
- * Tests when rows are null.\r
- */\r
- public final void baseTestShiftRow() {\r
- Workbook b = getTestDataProvider().createWorkbook();\r
- Sheet s = b.createSheet();\r
- s.createRow(0).createCell(0).setCellValue("TEST1");\r
- s.createRow(3).createCell(0).setCellValue("TEST2");\r
- s.shiftRows(0,4,1);\r
- }\r
-\r
- /**\r
- * Tests when shifting the first row.\r
- */\r
- public final void baseTestActiveCell() {\r
- Workbook b = getTestDataProvider().createWorkbook();\r
- Sheet s = b.createSheet();\r
-\r
- s.createRow(0).createCell(0).setCellValue("TEST1");\r
- s.createRow(3).createCell(0).setCellValue("TEST2");\r
- s.shiftRows(0,4,1);\r
- }\r
-\r
- /**\r
- * When shifting rows, the page breaks should go with it\r
- *\r
- */\r
- public final void baseTestShiftRowBreaks() {\r
- Workbook b = getTestDataProvider().createWorkbook();\r
- Sheet s = b.createSheet();\r
- Row row = s.createRow(4);\r
- row.createCell(0).setCellValue("test");\r
- s.setRowBreak(4);\r
-\r
- s.shiftRows(4, 4, 2);\r
- assertTrue("Row number 6 should have a pagebreak", s.isRowBroken(6));\r
- }\r
-\r
-\r
- public final void baseTestShiftWithComments(String sampleName) {\r
- Workbook wb = getTestDataProvider().openSampleWorkbook(sampleName);\r
-\r
- Sheet sheet = wb.getSheet("Sheet1");\r
- assertEquals(3, sheet.getLastRowNum());\r
-\r
- // Verify comments are in the position expected\r
- assertNotNull(sheet.getCellComment(0,0));\r
- assertNull(sheet.getCellComment(1,0));\r
- assertNotNull(sheet.getCellComment(2,0));\r
- assertNotNull(sheet.getCellComment(3,0));\r
-\r
- String comment1 = sheet.getCellComment(0,0).getString().getString();\r
- assertEquals(comment1,"comment top row1 (index0)\n");\r
- String comment3 = sheet.getCellComment(2,0).getString().getString();\r
- assertEquals(comment3,"comment top row3 (index2)\n");\r
- String comment4 = sheet.getCellComment(3,0).getString().getString();\r
- assertEquals(comment4,"comment top row4 (index3)\n");\r
-\r
- // Shifting all but first line down to test comments shifting\r
- sheet.shiftRows(1, sheet.getLastRowNum(), 1, true, true);\r
-\r
- // Test that comments were shifted as expected\r
- assertEquals(4, sheet.getLastRowNum());\r
- assertNotNull(sheet.getCellComment(0,0));\r
- assertNull(sheet.getCellComment(1,0));\r
- assertNull(sheet.getCellComment(2,0));\r
- assertNotNull(sheet.getCellComment(3,0));\r
- assertNotNull(sheet.getCellComment(4,0));\r
-\r
- String comment1_shifted = sheet.getCellComment(0,0).getString().getString();\r
- assertEquals(comment1,comment1_shifted);\r
- String comment3_shifted = sheet.getCellComment(3,0).getString().getString();\r
- assertEquals(comment3,comment3_shifted);\r
- String comment4_shifted = sheet.getCellComment(4,0).getString().getString();\r
- assertEquals(comment4,comment4_shifted);\r
-\r
- // Write out and read back in again\r
- // Ensure that the changes were persisted\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- sheet = wb.getSheet("Sheet1");\r
- assertEquals(4, sheet.getLastRowNum());\r
-\r
- // Verify comments are in the position expected after the shift\r
- assertNotNull(sheet.getCellComment(0,0));\r
- assertNull(sheet.getCellComment(1,0));\r
- assertNull(sheet.getCellComment(2,0));\r
- assertNotNull(sheet.getCellComment(3,0));\r
- assertNotNull(sheet.getCellComment(4,0));\r
-\r
- comment1_shifted = sheet.getCellComment(0,0).getString().getString();\r
- assertEquals(comment1,comment1_shifted);\r
- comment3_shifted = sheet.getCellComment(3,0).getString().getString();\r
- assertEquals(comment3,comment3_shifted);\r
- comment4_shifted = sheet.getCellComment(4,0).getString().getString();\r
- assertEquals(comment4,comment4_shifted);\r
- }\r
-\r
- public final void baseTestShiftWithNames() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet sheet1 = wb.createSheet("Sheet1");\r
- Sheet sheet2 = wb.createSheet("Sheet2");\r
- Row row = sheet1.createRow(0);\r
- row.createCell(0).setCellValue(1.1);\r
- row.createCell(1).setCellValue(2.2);\r
-\r
- Name name1 = wb.createName();\r
- name1.setNameName("name1");\r
- name1.setRefersToFormula("Sheet1!$A$1+Sheet1!$B$1");\r
-\r
- Name name2 = wb.createName();\r
- name2.setNameName("name2");\r
- name2.setRefersToFormula("Sheet1!$A$1");\r
-\r
- //refers to A1 but on Sheet2. Should stay unaffected.\r
- Name name3 = wb.createName();\r
- name3.setNameName("name3");\r
- name3.setRefersToFormula("Sheet2!$A$1");\r
-\r
- //The scope of this one is Sheet2. Should stay unaffected.\r
- Name name4 = wb.createName();\r
- name4.setNameName("name4");\r
- name4.setRefersToFormula("A1");\r
- name4.setSheetIndex(1);\r
-\r
- sheet1.shiftRows(0, 1, 2); //shift down the top row on Sheet1.\r
- name1 = wb.getNameAt(0);\r
- assertEquals("Sheet1!$A$3+Sheet1!$B$3", name1.getRefersToFormula());\r
-\r
- name2 = wb.getNameAt(1);\r
- assertEquals("Sheet1!$A$3", name2.getRefersToFormula()); \r
-\r
- //name3 and name4 refer to Sheet2 and should not be affected\r
- name3 = wb.getNameAt(2);\r
- assertEquals("Sheet2!$A$1", name3.getRefersToFormula());\r
-\r
- name4 = wb.getNameAt(3);\r
- assertEquals("A1", name4.getRefersToFormula());\r
- }\r
-\r
- public final void baseTestShiftWithMergedRegions() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- Sheet sheet = wb.createSheet();\r
- Row row = sheet.createRow(0);\r
- row.createCell(0).setCellValue(1.1);\r
- row.createCell(1).setCellValue(2.2);\r
- CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);\r
- assertEquals("A1:C1", region.formatAsString());\r
-\r
- sheet.addMergedRegion(region);\r
-\r
- sheet.shiftRows(0, 1, 2);\r
- region = sheet.getMergedRegion(0);\r
- assertEquals("A3:C3", region.formatAsString());\r
- }\r
-\r
- /**\r
- * See bug #34023\r
- *\r
- * @param sampleName the sample file to test against\r
- */\r
- public void baseTestShiftWithFormulas(String sampleName) {\r
- Workbook wb = getTestDataProvider().openSampleWorkbook(sampleName);\r
-\r
- Sheet sheet = wb.getSheet("Sheet1");\r
- assertEquals(20, sheet.getLastRowNum());\r
-\r
- confirmRow(sheet, 0, 1, 171, 1, "ROW(D1)", "100+B1", "COUNT(D1:E1)");\r
- confirmRow(sheet, 1, 2, 172, 1, "ROW(D2)", "100+B2", "COUNT(D2:E2)");\r
- confirmRow(sheet, 2, 3, 173, 1, "ROW(D3)", "100+B3", "COUNT(D3:E3)");\r
-\r
- confirmCell(sheet, 6, 1, 271, "200+B1");\r
- confirmCell(sheet, 7, 1, 272, "200+B2");\r
- confirmCell(sheet, 8, 1, 273, "200+B3");\r
-\r
- confirmCell(sheet, 14, 0, 0.0, "A12"); // the cell referred to by this formula will be replaced\r
-\r
- // -----------\r
- // Row index 1 -> 11 (row "2" -> row "12")\r
- sheet.shiftRows(1, 1, 10);\r
-\r
- // Now check what sheet looks like after move\r
-\r
- // no changes on row "1"\r
- confirmRow(sheet, 0, 1, 171, 1, "ROW(D1)", "100+B1", "COUNT(D1:E1)");\r
-\r
- // row "2" is now empty\r
- confirmEmptyRow(sheet, 1);\r
-\r
- // Row "2" moved to row "12", and the formula has been updated.\r
- // note however that the cached formula result (2) has not been updated. (POI differs from Excel here)\r
- confirmRow(sheet, 11, 2, 172, 1, "ROW(D12)", "100+B12", "COUNT(D12:E12)");\r
-\r
- // no changes on row "3"\r
- confirmRow(sheet, 2, 3, 173, 1, "ROW(D3)", "100+B3", "COUNT(D3:E3)");\r
-\r
-\r
- confirmCell(sheet, 14, 0, 0.0, "#REF!");\r
-\r
-\r
- // Formulas on rows that weren't shifted:\r
- confirmCell(sheet, 6, 1, 271, "200+B1");\r
- confirmCell(sheet, 7, 1, 272, "200+B12"); // this one moved\r
- confirmCell(sheet, 8, 1, 273, "200+B3");\r
-\r
- // check formulas on other sheets\r
- Sheet sheet2 = wb.getSheet("Sheet2");\r
- confirmCell(sheet2, 0, 0, 371, "300+Sheet1!B1");\r
- confirmCell(sheet2, 1, 0, 372, "300+Sheet1!B12");\r
- confirmCell(sheet2, 2, 0, 373, "300+Sheet1!B3");\r
-\r
- confirmCell(sheet2, 11, 0, 300, "300+Sheet1!#REF!");\r
-\r
-\r
- // Note - named ranges formulas have not been updated\r
- }\r
-\r
- private static void confirmRow(Sheet sheet, int rowIx, double valA, double valB, double valC,\r
- String formulaA, String formulaB, String formulaC) {\r
- confirmCell(sheet, rowIx, 4, valA, formulaA);\r
- confirmCell(sheet, rowIx, 5, valB, formulaB);\r
- confirmCell(sheet, rowIx, 6, valC, formulaC);\r
- }\r
-\r
- private static void confirmCell(Sheet sheet, int rowIx, int colIx,\r
- double expectedValue, String expectedFormula) {\r
- Cell cell = sheet.getRow(rowIx).getCell(colIx);\r
- assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);\r
- assertEquals(expectedFormula, cell.getCellFormula());\r
- }\r
-}\r
+/* ====================================================================
+ 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.usermodel;
+
+import junit.framework.TestCase;
+import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.util.CellRangeAddress;
+
+/**
+ * Tests row shifting capabilities.
+ *
+ * @author Shawn Laubach (slaubach at apache dot com)
+ * @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
+ */
+public abstract class BaseTestSheetShiftRows extends TestCase {
+
+ /**
+ * @return an object that provides test data in HSSF / XSSF specific way
+ */
+ protected abstract ITestDataProvider getTestDataProvider();
+
+ /**
+ * Tests the shiftRows function. Does three different shifts.
+ * After each shift, writes the workbook to file and reads back to
+ * check. This ensures that if some changes code that breaks
+ * writing or what not, they realize it.
+ *
+ * @param sampleName the sample file to test against
+ */
+ public final void baseTestShiftRows(String sampleName){
+ // Read initial file in
+ Workbook wb = getTestDataProvider().openSampleWorkbook(sampleName);
+ Sheet s = wb.getSheetAt( 0 );
+
+ // Shift the second row down 1 and write to temp file
+ s.shiftRows( 1, 1, 1 );
+
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+
+ // Read from temp file and check the number of cells in each
+ // row (in original file each row was unique)
+ s = wb.getSheetAt( 0 );
+
+ assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 1);
+ confirmEmptyRow(s, 1);
+ assertEquals(s.getRow(2).getPhysicalNumberOfCells(), 2);
+ assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 4);
+ assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);
+
+ // Shift rows 1-3 down 3 in the current one. This tests when
+ // 1 row is blank. Write to a another temp file
+ s.shiftRows( 0, 2, 3 );
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+
+ // Read and ensure things are where they should be
+ s = wb.getSheetAt(0);
+ confirmEmptyRow(s, 0);
+ confirmEmptyRow(s, 1);
+ confirmEmptyRow(s, 2);
+ assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 1);
+ confirmEmptyRow(s, 4);
+ assertEquals(s.getRow(5).getPhysicalNumberOfCells(), 2);
+
+ // Read the first file again
+ wb = getTestDataProvider().openSampleWorkbook(sampleName);
+ s = wb.getSheetAt( 0 );
+
+ // Shift rows 3 and 4 up and write to temp file
+ s.shiftRows( 2, 3, -2 );
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ s = wb.getSheetAt( 0 );
+ assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 3);
+ assertEquals(s.getRow(1).getPhysicalNumberOfCells(), 4);
+ confirmEmptyRow(s, 2);
+ confirmEmptyRow(s, 3);
+ assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);
+ }
+ private static void confirmEmptyRow(Sheet s, int rowIx) {
+ Row row = s.getRow(rowIx);
+ assertTrue(row == null || row.getPhysicalNumberOfCells() == 0);
+ }
+
+ /**
+ * Tests when rows are null.
+ */
+ public final void baseTestShiftRow() {
+ Workbook b = getTestDataProvider().createWorkbook();
+ Sheet s = b.createSheet();
+ s.createRow(0).createCell(0).setCellValue("TEST1");
+ s.createRow(3).createCell(0).setCellValue("TEST2");
+ s.shiftRows(0,4,1);
+ }
+
+ /**
+ * Tests when shifting the first row.
+ */
+ public final void baseTestActiveCell() {
+ Workbook b = getTestDataProvider().createWorkbook();
+ Sheet s = b.createSheet();
+
+ s.createRow(0).createCell(0).setCellValue("TEST1");
+ s.createRow(3).createCell(0).setCellValue("TEST2");
+ s.shiftRows(0,4,1);
+ }
+
+ /**
+ * When shifting rows, the page breaks should go with it
+ *
+ */
+ public final void baseTestShiftRowBreaks() {
+ Workbook b = getTestDataProvider().createWorkbook();
+ Sheet s = b.createSheet();
+ Row row = s.createRow(4);
+ row.createCell(0).setCellValue("test");
+ s.setRowBreak(4);
+
+ s.shiftRows(4, 4, 2);
+ assertTrue("Row number 6 should have a pagebreak", s.isRowBroken(6));
+ }
+
+
+ public final void baseTestShiftWithComments(String sampleName) {
+ Workbook wb = getTestDataProvider().openSampleWorkbook(sampleName);
+
+ Sheet sheet = wb.getSheet("Sheet1");
+ assertEquals(3, sheet.getLastRowNum());
+
+ // Verify comments are in the position expected
+ assertNotNull(sheet.getCellComment(0,0));
+ assertNull(sheet.getCellComment(1,0));
+ assertNotNull(sheet.getCellComment(2,0));
+ assertNotNull(sheet.getCellComment(3,0));
+
+ String comment1 = sheet.getCellComment(0,0).getString().getString();
+ assertEquals(comment1,"comment top row1 (index0)\n");
+ String comment3 = sheet.getCellComment(2,0).getString().getString();
+ assertEquals(comment3,"comment top row3 (index2)\n");
+ String comment4 = sheet.getCellComment(3,0).getString().getString();
+ assertEquals(comment4,"comment top row4 (index3)\n");
+
+ // Shifting all but first line down to test comments shifting
+ sheet.shiftRows(1, sheet.getLastRowNum(), 1, true, true);
+
+ // Test that comments were shifted as expected
+ assertEquals(4, sheet.getLastRowNum());
+ assertNotNull(sheet.getCellComment(0,0));
+ assertNull(sheet.getCellComment(1,0));
+ assertNull(sheet.getCellComment(2,0));
+ assertNotNull(sheet.getCellComment(3,0));
+ assertNotNull(sheet.getCellComment(4,0));
+
+ String comment1_shifted = sheet.getCellComment(0,0).getString().getString();
+ assertEquals(comment1,comment1_shifted);
+ String comment3_shifted = sheet.getCellComment(3,0).getString().getString();
+ assertEquals(comment3,comment3_shifted);
+ String comment4_shifted = sheet.getCellComment(4,0).getString().getString();
+ assertEquals(comment4,comment4_shifted);
+
+ // Write out and read back in again
+ // Ensure that the changes were persisted
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ sheet = wb.getSheet("Sheet1");
+ assertEquals(4, sheet.getLastRowNum());
+
+ // Verify comments are in the position expected after the shift
+ assertNotNull(sheet.getCellComment(0,0));
+ assertNull(sheet.getCellComment(1,0));
+ assertNull(sheet.getCellComment(2,0));
+ assertNotNull(sheet.getCellComment(3,0));
+ assertNotNull(sheet.getCellComment(4,0));
+
+ comment1_shifted = sheet.getCellComment(0,0).getString().getString();
+ assertEquals(comment1,comment1_shifted);
+ comment3_shifted = sheet.getCellComment(3,0).getString().getString();
+ assertEquals(comment3,comment3_shifted);
+ comment4_shifted = sheet.getCellComment(4,0).getString().getString();
+ assertEquals(comment4,comment4_shifted);
+ }
+
+ public final void baseTestShiftWithNames() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet sheet1 = wb.createSheet("Sheet1");
+ wb.createSheet("Sheet2");
+ Row row = sheet1.createRow(0);
+ row.createCell(0).setCellValue(1.1);
+ row.createCell(1).setCellValue(2.2);
+
+ Name name1 = wb.createName();
+ name1.setNameName("name1");
+ name1.setRefersToFormula("Sheet1!$A$1+Sheet1!$B$1");
+
+ Name name2 = wb.createName();
+ name2.setNameName("name2");
+ name2.setRefersToFormula("Sheet1!$A$1");
+
+ //refers to A1 but on Sheet2. Should stay unaffected.
+ Name name3 = wb.createName();
+ name3.setNameName("name3");
+ name3.setRefersToFormula("Sheet2!$A$1");
+
+ //The scope of this one is Sheet2. Should stay unaffected.
+ Name name4 = wb.createName();
+ name4.setNameName("name4");
+ name4.setRefersToFormula("A1");
+ name4.setSheetIndex(1);
+
+ sheet1.shiftRows(0, 1, 2); //shift down the top row on Sheet1.
+ name1 = wb.getNameAt(0);
+ assertEquals("Sheet1!$A$3+Sheet1!$B$3", name1.getRefersToFormula());
+
+ name2 = wb.getNameAt(1);
+ assertEquals("Sheet1!$A$3", name2.getRefersToFormula());
+
+ //name3 and name4 refer to Sheet2 and should not be affected
+ name3 = wb.getNameAt(2);
+ assertEquals("Sheet2!$A$1", name3.getRefersToFormula());
+
+ name4 = wb.getNameAt(3);
+ assertEquals("A1", name4.getRefersToFormula());
+ }
+
+ public final void baseTestShiftWithMergedRegions() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet sheet = wb.createSheet();
+ Row row = sheet.createRow(0);
+ row.createCell(0).setCellValue(1.1);
+ row.createCell(1).setCellValue(2.2);
+ CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
+ assertEquals("A1:C1", region.formatAsString());
+
+ sheet.addMergedRegion(region);
+
+ sheet.shiftRows(0, 1, 2);
+ region = sheet.getMergedRegion(0);
+ assertEquals("A3:C3", region.formatAsString());
+ }
+
+ /**
+ * See bug #34023
+ *
+ * @param sampleName the sample file to test against
+ */
+ public void baseTestShiftWithFormulas(String sampleName) {
+ Workbook wb = getTestDataProvider().openSampleWorkbook(sampleName);
+
+ Sheet sheet = wb.getSheet("Sheet1");
+ assertEquals(20, sheet.getLastRowNum());
+
+ confirmRow(sheet, 0, 1, 171, 1, "ROW(D1)", "100+B1", "COUNT(D1:E1)");
+ confirmRow(sheet, 1, 2, 172, 1, "ROW(D2)", "100+B2", "COUNT(D2:E2)");
+ confirmRow(sheet, 2, 3, 173, 1, "ROW(D3)", "100+B3", "COUNT(D3:E3)");
+
+ confirmCell(sheet, 6, 1, 271, "200+B1");
+ confirmCell(sheet, 7, 1, 272, "200+B2");
+ confirmCell(sheet, 8, 1, 273, "200+B3");
+
+ confirmCell(sheet, 14, 0, 0.0, "A12"); // the cell referred to by this formula will be replaced
+
+ // -----------
+ // Row index 1 -> 11 (row "2" -> row "12")
+ sheet.shiftRows(1, 1, 10);
+
+ // Now check what sheet looks like after move
+
+ // no changes on row "1"
+ confirmRow(sheet, 0, 1, 171, 1, "ROW(D1)", "100+B1", "COUNT(D1:E1)");
+
+ // row "2" is now empty
+ confirmEmptyRow(sheet, 1);
+
+ // Row "2" moved to row "12", and the formula has been updated.
+ // note however that the cached formula result (2) has not been updated. (POI differs from Excel here)
+ confirmRow(sheet, 11, 2, 172, 1, "ROW(D12)", "100+B12", "COUNT(D12:E12)");
+
+ // no changes on row "3"
+ confirmRow(sheet, 2, 3, 173, 1, "ROW(D3)", "100+B3", "COUNT(D3:E3)");
+
+
+ confirmCell(sheet, 14, 0, 0.0, "#REF!");
+
+
+ // Formulas on rows that weren't shifted:
+ confirmCell(sheet, 6, 1, 271, "200+B1");
+ confirmCell(sheet, 7, 1, 272, "200+B12"); // this one moved
+ confirmCell(sheet, 8, 1, 273, "200+B3");
+
+ // check formulas on other sheets
+ Sheet sheet2 = wb.getSheet("Sheet2");
+ confirmCell(sheet2, 0, 0, 371, "300+Sheet1!B1");
+ confirmCell(sheet2, 1, 0, 372, "300+Sheet1!B12");
+ confirmCell(sheet2, 2, 0, 373, "300+Sheet1!B3");
+
+ confirmCell(sheet2, 11, 0, 300, "300+Sheet1!#REF!");
+
+
+ // Note - named ranges formulas have not been updated
+ }
+
+ private static void confirmRow(Sheet sheet, int rowIx, double valA, double valB, double valC,
+ String formulaA, String formulaB, String formulaC) {
+ confirmCell(sheet, rowIx, 4, valA, formulaA);
+ confirmCell(sheet, rowIx, 5, valB, formulaB);
+ confirmCell(sheet, rowIx, 6, valC, formulaC);
+ }
+
+ private static void confirmCell(Sheet sheet, int rowIx, int colIx,
+ double expectedValue, String expectedFormula) {
+ Cell cell = sheet.getRow(rowIx).getCell(colIx);
+ assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
+ assertEquals(expectedFormula, cell.getCellFormula());
+ }
+}
-package org.apache.poi.ss.usermodel;\r
-\r
-import junit.framework.TestCase;\r
-import junit.framework.AssertionFailedError;\r
-import org.apache.poi.ss.ITestDataProvider;\r
-import org.apache.poi.ss.util.CellRangeAddress;\r
-\r
-/**\r
- * @author Yegor Kozlov\r
- */\r
-public abstract class BaseTestWorkbook extends TestCase {\r
-\r
- protected abstract ITestDataProvider getTestDataProvider();\r
-\r
- public void testCreateSheet() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- assertEquals(0, wb.getNumberOfSheets());\r
-\r
- //getting a sheet by invalid index or non-existing name\r
- assertNull(wb.getSheet("Sheet1"));\r
- try {\r
- wb.getSheetAt(0);\r
- fail("should have thrown exceptiuon due to invalid sheet index");\r
- } catch (IllegalArgumentException e) {\r
- // expected during successful test\r
- ;\r
- }\r
-\r
- Sheet sheet0 = wb.createSheet();\r
- Sheet sheet1 = wb.createSheet();\r
- assertEquals("Sheet0", sheet0.getSheetName());\r
- assertEquals("Sheet1", sheet1.getSheetName());\r
- assertEquals(2, wb.getNumberOfSheets());\r
-\r
- //fetching sheets by name is case-insensitive\r
- Sheet originalSheet = wb.createSheet("Sheet3");\r
- Sheet fetchedSheet = wb.getSheet("sheet3");\r
- if (fetchedSheet == null) {\r
- throw new AssertionFailedError("Identified bug 44892");\r
- }\r
- assertEquals("Sheet3", fetchedSheet.getSheetName());\r
- assertEquals(3, wb.getNumberOfSheets());\r
- assertSame(originalSheet, fetchedSheet);\r
- try {\r
- wb.createSheet("sHeeT3");\r
- fail("should have thrown exceptiuon due to duplicate sheet name");\r
- } catch (IllegalArgumentException e) {\r
- // expected during successful test\r
- assertEquals("The workbook already contains a sheet of this name", e.getMessage());\r
- }\r
-\r
- //names cannot be blank or contain any of /\*?[]\r
- String[] invalidNames = {"", "Sheet/", "Sheet\\",\r
- "Sheet?", "Sheet*", "Sheet[", "Sheet]"};\r
- for (String sheetName : invalidNames) {\r
- try {\r
- wb.createSheet(sheetName);\r
- fail("should have thrown exceptiuon due to invalid sheet name: " + sheetName);\r
- } catch (IllegalArgumentException e) {\r
- // expected during successful test\r
- ;\r
- }\r
- }\r
- //still have 3 sheets\r
- assertEquals(3, wb.getNumberOfSheets());\r
-\r
- //change the name of the 3rd sheet\r
- wb.setSheetName(2, "I changed!");\r
-\r
- //try to assign an invalid name to the 2nd sheet\r
- try {\r
- wb.setSheetName(1, "[I'm invalid]");\r
- fail("should have thrown exceptiuon due to invalid sheet name");\r
- } catch (IllegalArgumentException e) {\r
- ; // expected during successful test\r
- }\r
-\r
- //check\r
- assertEquals(0, wb.getSheetIndex("sheet0"));\r
- assertEquals(1, wb.getSheetIndex("sheet1"));\r
- assertEquals(2, wb.getSheetIndex("I changed!"));\r
-\r
- assertSame(sheet0, wb.getSheet("sheet0"));\r
- assertSame(sheet1, wb.getSheet("sheet1"));\r
- assertSame(originalSheet, wb.getSheet("I changed!"));\r
- assertNull(wb.getSheet("unknown"));\r
-\r
- //serialize and read again\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
- assertEquals(3, wb.getNumberOfSheets());\r
- assertEquals(0, wb.getSheetIndex("sheet0"));\r
- assertEquals(1, wb.getSheetIndex("sheet1"));\r
- assertEquals(2, wb.getSheetIndex("I changed!"));\r
-\r
- }\r
-\r
- public void testRemoveSheetAt() {\r
- Workbook workbook = getTestDataProvider().createWorkbook();\r
- workbook.createSheet("sheet1");\r
- workbook.createSheet("sheet2");\r
- workbook.createSheet("sheet3");\r
- assertEquals(3, workbook.getNumberOfSheets());\r
- workbook.removeSheetAt(1);\r
- assertEquals(2, workbook.getNumberOfSheets());\r
- assertEquals("sheet3", workbook.getSheetName(1));\r
- workbook.removeSheetAt(0);\r
- assertEquals(1, workbook.getNumberOfSheets());\r
- assertEquals("sheet3", workbook.getSheetName(0));\r
- workbook.removeSheetAt(0);\r
- assertEquals(0, workbook.getNumberOfSheets());\r
-\r
- //re-create the sheets\r
- workbook.createSheet("sheet1");\r
- workbook.createSheet("sheet2");\r
- workbook.createSheet("sheet3");\r
- assertEquals(3, workbook.getNumberOfSheets());\r
- }\r
-\r
- public void testDefaultValues() {\r
- Workbook b = getTestDataProvider().createWorkbook();\r
- assertEquals(0, b.getActiveSheetIndex());\r
- assertEquals(0, b.getFirstVisibleTab());\r
- assertEquals(0, b.getNumberOfNames());\r
- assertEquals(0, b.getNumberOfSheets());\r
- }\r
-\r
-\r
- public void testSheetSelection() {\r
- Workbook b = getTestDataProvider().createWorkbook();\r
- b.createSheet("Sheet One");\r
- b.createSheet("Sheet Two");\r
- b.setActiveSheet(1);\r
- b.setSelectedTab(1);\r
- b.setFirstVisibleTab(1);\r
- assertEquals(1, b.getActiveSheetIndex());\r
- assertEquals(1, b.getFirstVisibleTab());\r
- }\r
-\r
- public void testPrintArea() {\r
- Workbook workbook = getTestDataProvider().createWorkbook();\r
- Sheet sheet1 = workbook.createSheet("Test Print Area");\r
- String sheetName1 = sheet1.getSheetName();\r
-\r
- // workbook.setPrintArea(0, reference);\r
- workbook.setPrintArea(0, 1, 5, 4, 9);\r
- String retrievedPrintArea = workbook.getPrintArea(0);\r
- assertEquals("'" + sheetName1 + "'!$B$5:$F$10", retrievedPrintArea);\r
-\r
- String reference = "$A$1:$B$1";\r
- workbook.setPrintArea(0, reference);\r
- retrievedPrintArea = workbook.getPrintArea(0);\r
- assertEquals("'" + sheetName1 + "'!" + reference, retrievedPrintArea);\r
-\r
- workbook.removePrintArea(0);\r
- assertNull(workbook.getPrintArea(0));\r
- }\r
-\r
- public void testGetSetActiveSheet(){\r
- Workbook workbook = getTestDataProvider().createWorkbook();\r
- assertEquals(0, workbook.getActiveSheetIndex());\r
-\r
- workbook.createSheet("sheet1");\r
- workbook.createSheet("sheet2");\r
- workbook.createSheet("sheet3");\r
- // set second sheet\r
- workbook.setActiveSheet(1);\r
- // test if second sheet is set up\r
- assertEquals(1, workbook.getActiveSheetIndex());\r
-\r
- workbook.setActiveSheet(0);\r
- // test if second sheet is set up\r
- assertEquals(0, workbook.getActiveSheetIndex());\r
- }\r
-\r
- public void testSetSheetOrder() {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
-\r
- for (int i=0; i < 10; i++) {\r
- Sheet sh = wb.createSheet("Sheet " + i);\r
- }\r
-\r
- // Check the initial order\r
- assertEquals(0, wb.getSheetIndex("Sheet 0"));\r
- assertEquals(1, wb.getSheetIndex("Sheet 1"));\r
- assertEquals(2, wb.getSheetIndex("Sheet 2"));\r
- assertEquals(3, wb.getSheetIndex("Sheet 3"));\r
- assertEquals(4, wb.getSheetIndex("Sheet 4"));\r
- assertEquals(5, wb.getSheetIndex("Sheet 5"));\r
- assertEquals(6, wb.getSheetIndex("Sheet 6"));\r
- assertEquals(7, wb.getSheetIndex("Sheet 7"));\r
- assertEquals(8, wb.getSheetIndex("Sheet 8"));\r
- assertEquals(9, wb.getSheetIndex("Sheet 9"));\r
-\r
- // Change\r
- wb.setSheetOrder("Sheet 6", 0);\r
- wb.setSheetOrder("Sheet 3", 7);\r
- wb.setSheetOrder("Sheet 1", 9);\r
-\r
- // Check they're currently right\r
- assertEquals(0, wb.getSheetIndex("Sheet 6"));\r
- assertEquals(1, wb.getSheetIndex("Sheet 0"));\r
- assertEquals(2, wb.getSheetIndex("Sheet 2"));\r
- assertEquals(3, wb.getSheetIndex("Sheet 4"));\r
- assertEquals(4, wb.getSheetIndex("Sheet 5"));\r
- assertEquals(5, wb.getSheetIndex("Sheet 7"));\r
- assertEquals(6, wb.getSheetIndex("Sheet 3"));\r
- assertEquals(7, wb.getSheetIndex("Sheet 8"));\r
- assertEquals(8, wb.getSheetIndex("Sheet 9"));\r
- assertEquals(9, wb.getSheetIndex("Sheet 1"));\r
-\r
- Workbook wbr = getTestDataProvider().writeOutAndReadBack(wb);\r
-\r
- assertEquals(0, wbr.getSheetIndex("Sheet 6"));\r
- assertEquals(1, wbr.getSheetIndex("Sheet 0"));\r
- assertEquals(2, wbr.getSheetIndex("Sheet 2"));\r
- assertEquals(3, wbr.getSheetIndex("Sheet 4"));\r
- assertEquals(4, wbr.getSheetIndex("Sheet 5"));\r
- assertEquals(5, wbr.getSheetIndex("Sheet 7"));\r
- assertEquals(6, wbr.getSheetIndex("Sheet 3"));\r
- assertEquals(7, wbr.getSheetIndex("Sheet 8"));\r
- assertEquals(8, wbr.getSheetIndex("Sheet 9"));\r
- assertEquals(9, wbr.getSheetIndex("Sheet 1"));\r
-\r
- // Now get the index by the sheet, not the name\r
- for(int i=0; i<10; i++) {\r
- Sheet s = wbr.getSheetAt(i);\r
- assertEquals(i, wbr.getSheetIndex(s));\r
- }\r
- }\r
-\r
- public void testCloneSheet() {\r
- Workbook book = getTestDataProvider().createWorkbook();\r
- Sheet sheet = book.createSheet("TEST");\r
- sheet.createRow(0).createCell(0).setCellValue("Test");\r
- sheet.createRow(1).createCell(0).setCellValue(36.6);\r
- sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));\r
- sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));\r
- assertTrue(sheet.isSelected());\r
-\r
- Sheet clonedSheet = book.cloneSheet(0);\r
- assertEquals("TEST (2)", clonedSheet.getSheetName());\r
- assertEquals(2, clonedSheet.getPhysicalNumberOfRows());\r
- assertEquals(2, clonedSheet.getNumMergedRegions());\r
- assertFalse(clonedSheet.isSelected());\r
-\r
- //cloned sheet is a deep copy, adding rows in the original does not affect the clone\r
- sheet.createRow(2).createCell(0).setCellValue(1);\r
- sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 2));\r
- assertEquals(2, clonedSheet.getPhysicalNumberOfRows());\r
- assertEquals(2, clonedSheet.getPhysicalNumberOfRows());\r
-\r
- clonedSheet.createRow(2).createCell(0).setCellValue(1);\r
- clonedSheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 2));\r
- assertEquals(3, clonedSheet.getPhysicalNumberOfRows());\r
- assertEquals(3, clonedSheet.getPhysicalNumberOfRows());\r
-\r
- }\r
-\r
- public void testParentReferences(){\r
- Workbook workbook = getTestDataProvider().createWorkbook();\r
- Sheet sheet = workbook.createSheet();\r
- assertSame(workbook, sheet.getWorkbook());\r
-\r
- Row row = sheet.createRow(0);\r
- assertSame(sheet, row.getSheet());\r
-\r
- Cell cell = row.createCell(1);\r
- assertSame(sheet, cell.getSheet());\r
- assertSame(row, cell.getRow());\r
-\r
- workbook = getTestDataProvider().writeOutAndReadBack(workbook);\r
- sheet = workbook.getSheetAt(0);\r
- assertSame(workbook, sheet.getWorkbook());\r
-\r
- row = sheet.getRow(0);\r
- assertSame(sheet, row.getSheet());\r
-\r
- cell = row.getCell(1);\r
- assertSame(sheet, cell.getSheet());\r
- assertSame(row, cell.getRow());\r
- }\r
-\r
- /** Tests that all of the unicode capable string fields can be set, written and then read back\r
- *\r
- *\r
- */\r
- public void testUnicodeInAll() throws Exception {\r
- Workbook wb = getTestDataProvider().createWorkbook();\r
- CreationHelper factory = wb.getCreationHelper();\r
- //Create a unicode dataformat (contains euro symbol)\r
- DataFormat df = wb.createDataFormat();\r
- final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)";\r
- short fmt = df.getFormat(formatStr);\r
-\r
- //Create a unicode sheet name (euro symbol)\r
- Sheet s = wb.createSheet("\u20ac");\r
-\r
- //Set a unicode header (you guessed it the euro symbol)\r
- Header h = s.getHeader();\r
- h.setCenter("\u20ac");\r
- h.setLeft("\u20ac");\r
- h.setRight("\u20ac");\r
-\r
- //Set a unicode footer\r
- Footer f = s.getFooter();\r
- f.setCenter("\u20ac");\r
- f.setLeft("\u20ac");\r
- f.setRight("\u20ac");\r
-\r
- Row r = s.createRow(0);\r
- Cell c = r.createCell(1);\r
- c.setCellValue(12.34);\r
- c.getCellStyle().setDataFormat(fmt);\r
-\r
- Cell c2 = r.createCell(2);\r
- c.setCellValue(factory.createRichTextString("\u20ac"));\r
-\r
- Cell c3 = r.createCell(3);\r
- String formulaString = "TEXT(12.34,\"\u20ac###,##\")";\r
- c3.setCellFormula(formulaString);\r
-\r
- wb = getTestDataProvider().writeOutAndReadBack(wb);\r
-\r
- //Test the sheetname\r
- s = wb.getSheet("\u20ac");\r
- assertNotNull(s);\r
-\r
- //Test the header\r
- h = s.getHeader();\r
- assertEquals(h.getCenter(), "\u20ac");\r
- assertEquals(h.getLeft(), "\u20ac");\r
- assertEquals(h.getRight(), "\u20ac");\r
-\r
- //Test the footer\r
- f = s.getFooter();\r
- assertEquals(f.getCenter(), "\u20ac");\r
- assertEquals(f.getLeft(), "\u20ac");\r
- assertEquals(f.getRight(), "\u20ac");\r
-\r
- //Test the dataformat\r
- r = s.getRow(0);\r
- c = r.getCell(1);\r
- df = wb.createDataFormat();\r
- assertEquals(formatStr, df.getFormat(c.getCellStyle().getDataFormat()));\r
-\r
- //Test the cell string value\r
- c2 = r.getCell(2);\r
- assertEquals(c.getRichStringCellValue().getString(), "\u20ac");\r
-\r
- //Test the cell formula\r
- c3 = r.getCell(3);\r
- assertEquals(c3.getCellFormula(), formulaString);\r
- }\r
- \r
-}\r
+/* ====================================================================
+ 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.usermodel;
+
+import junit.framework.TestCase;
+import junit.framework.AssertionFailedError;
+import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.util.CellRangeAddress;
+
+/**
+ * @author Yegor Kozlov
+ */
+public abstract class BaseTestWorkbook extends TestCase {
+
+ protected abstract ITestDataProvider getTestDataProvider();
+
+ public void testCreateSheet() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ assertEquals(0, wb.getNumberOfSheets());
+
+ //getting a sheet by invalid index or non-existing name
+ assertNull(wb.getSheet("Sheet1"));
+ try {
+ wb.getSheetAt(0);
+ fail("should have thrown exceptiuon due to invalid sheet index");
+ } catch (IllegalArgumentException e) {
+ // expected during successful test
+ }
+
+ Sheet sheet0 = wb.createSheet();
+ Sheet sheet1 = wb.createSheet();
+ assertEquals("Sheet0", sheet0.getSheetName());
+ assertEquals("Sheet1", sheet1.getSheetName());
+ assertEquals(2, wb.getNumberOfSheets());
+
+ //fetching sheets by name is case-insensitive
+ Sheet originalSheet = wb.createSheet("Sheet3");
+ Sheet fetchedSheet = wb.getSheet("sheet3");
+ if (fetchedSheet == null) {
+ throw new AssertionFailedError("Identified bug 44892");
+ }
+ assertEquals("Sheet3", fetchedSheet.getSheetName());
+ assertEquals(3, wb.getNumberOfSheets());
+ assertSame(originalSheet, fetchedSheet);
+ try {
+ wb.createSheet("sHeeT3");
+ fail("should have thrown exceptiuon due to duplicate sheet name");
+ } catch (IllegalArgumentException e) {
+ // expected during successful test
+ assertEquals("The workbook already contains a sheet of this name", e.getMessage());
+ }
+
+ //names cannot be blank or contain any of /\*?[]
+ String[] invalidNames = {"", "Sheet/", "Sheet\\",
+ "Sheet?", "Sheet*", "Sheet[", "Sheet]"};
+ for (String sheetName : invalidNames) {
+ try {
+ wb.createSheet(sheetName);
+ fail("should have thrown exception due to invalid sheet name: " + sheetName);
+ } catch (IllegalArgumentException e) {
+ // expected during successful test
+ }
+ }
+ //still have 3 sheets
+ assertEquals(3, wb.getNumberOfSheets());
+
+ //change the name of the 3rd sheet
+ wb.setSheetName(2, "I changed!");
+
+ //try to assign an invalid name to the 2nd sheet
+ try {
+ wb.setSheetName(1, "[I'm invalid]");
+ fail("should have thrown exceptiuon due to invalid sheet name");
+ } catch (IllegalArgumentException e) {
+ ; // expected during successful test
+ }
+
+ //check
+ assertEquals(0, wb.getSheetIndex("sheet0"));
+ assertEquals(1, wb.getSheetIndex("sheet1"));
+ assertEquals(2, wb.getSheetIndex("I changed!"));
+
+ assertSame(sheet0, wb.getSheet("sheet0"));
+ assertSame(sheet1, wb.getSheet("sheet1"));
+ assertSame(originalSheet, wb.getSheet("I changed!"));
+ assertNull(wb.getSheet("unknown"));
+
+ //serialize and read again
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ assertEquals(3, wb.getNumberOfSheets());
+ assertEquals(0, wb.getSheetIndex("sheet0"));
+ assertEquals(1, wb.getSheetIndex("sheet1"));
+ assertEquals(2, wb.getSheetIndex("I changed!"));
+ }
+
+ public void testRemoveSheetAt() {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ workbook.createSheet("sheet1");
+ workbook.createSheet("sheet2");
+ workbook.createSheet("sheet3");
+ assertEquals(3, workbook.getNumberOfSheets());
+ workbook.removeSheetAt(1);
+ assertEquals(2, workbook.getNumberOfSheets());
+ assertEquals("sheet3", workbook.getSheetName(1));
+ workbook.removeSheetAt(0);
+ assertEquals(1, workbook.getNumberOfSheets());
+ assertEquals("sheet3", workbook.getSheetName(0));
+ workbook.removeSheetAt(0);
+ assertEquals(0, workbook.getNumberOfSheets());
+
+ //re-create the sheets
+ workbook.createSheet("sheet1");
+ workbook.createSheet("sheet2");
+ workbook.createSheet("sheet3");
+ assertEquals(3, workbook.getNumberOfSheets());
+ }
+
+ public void testDefaultValues() {
+ Workbook b = getTestDataProvider().createWorkbook();
+ assertEquals(0, b.getActiveSheetIndex());
+ assertEquals(0, b.getFirstVisibleTab());
+ assertEquals(0, b.getNumberOfNames());
+ assertEquals(0, b.getNumberOfSheets());
+ }
+
+ public void testSheetSelection() {
+ Workbook b = getTestDataProvider().createWorkbook();
+ b.createSheet("Sheet One");
+ b.createSheet("Sheet Two");
+ b.setActiveSheet(1);
+ b.setSelectedTab(1);
+ b.setFirstVisibleTab(1);
+ assertEquals(1, b.getActiveSheetIndex());
+ assertEquals(1, b.getFirstVisibleTab());
+ }
+
+ public void testPrintArea() {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ Sheet sheet1 = workbook.createSheet("Test Print Area");
+ String sheetName1 = sheet1.getSheetName();
+
+ // workbook.setPrintArea(0, reference);
+ workbook.setPrintArea(0, 1, 5, 4, 9);
+ String retrievedPrintArea = workbook.getPrintArea(0);
+ assertEquals("'" + sheetName1 + "'!$B$5:$F$10", retrievedPrintArea);
+
+ String reference = "$A$1:$B$1";
+ workbook.setPrintArea(0, reference);
+ retrievedPrintArea = workbook.getPrintArea(0);
+ assertEquals("'" + sheetName1 + "'!" + reference, retrievedPrintArea);
+
+ workbook.removePrintArea(0);
+ assertNull(workbook.getPrintArea(0));
+ }
+
+ public void testGetSetActiveSheet(){
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ assertEquals(0, workbook.getActiveSheetIndex());
+
+ workbook.createSheet("sheet1");
+ workbook.createSheet("sheet2");
+ workbook.createSheet("sheet3");
+ // set second sheet
+ workbook.setActiveSheet(1);
+ // test if second sheet is set up
+ assertEquals(1, workbook.getActiveSheetIndex());
+
+ workbook.setActiveSheet(0);
+ // test if second sheet is set up
+ assertEquals(0, workbook.getActiveSheetIndex());
+ }
+
+ public void testSetSheetOrder() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+
+ for (int i=0; i < 10; i++) {
+ wb.createSheet("Sheet " + i);
+ }
+
+ // Check the initial order
+ assertEquals(0, wb.getSheetIndex("Sheet 0"));
+ assertEquals(1, wb.getSheetIndex("Sheet 1"));
+ assertEquals(2, wb.getSheetIndex("Sheet 2"));
+ assertEquals(3, wb.getSheetIndex("Sheet 3"));
+ assertEquals(4, wb.getSheetIndex("Sheet 4"));
+ assertEquals(5, wb.getSheetIndex("Sheet 5"));
+ assertEquals(6, wb.getSheetIndex("Sheet 6"));
+ assertEquals(7, wb.getSheetIndex("Sheet 7"));
+ assertEquals(8, wb.getSheetIndex("Sheet 8"));
+ assertEquals(9, wb.getSheetIndex("Sheet 9"));
+
+ // Change
+ wb.setSheetOrder("Sheet 6", 0);
+ wb.setSheetOrder("Sheet 3", 7);
+ wb.setSheetOrder("Sheet 1", 9);
+
+ // Check they're currently right
+ assertEquals(0, wb.getSheetIndex("Sheet 6"));
+ assertEquals(1, wb.getSheetIndex("Sheet 0"));
+ assertEquals(2, wb.getSheetIndex("Sheet 2"));
+ assertEquals(3, wb.getSheetIndex("Sheet 4"));
+ assertEquals(4, wb.getSheetIndex("Sheet 5"));
+ assertEquals(5, wb.getSheetIndex("Sheet 7"));
+ assertEquals(6, wb.getSheetIndex("Sheet 3"));
+ assertEquals(7, wb.getSheetIndex("Sheet 8"));
+ assertEquals(8, wb.getSheetIndex("Sheet 9"));
+ assertEquals(9, wb.getSheetIndex("Sheet 1"));
+
+ Workbook wbr = getTestDataProvider().writeOutAndReadBack(wb);
+
+ assertEquals(0, wbr.getSheetIndex("Sheet 6"));
+ assertEquals(1, wbr.getSheetIndex("Sheet 0"));
+ assertEquals(2, wbr.getSheetIndex("Sheet 2"));
+ assertEquals(3, wbr.getSheetIndex("Sheet 4"));
+ assertEquals(4, wbr.getSheetIndex("Sheet 5"));
+ assertEquals(5, wbr.getSheetIndex("Sheet 7"));
+ assertEquals(6, wbr.getSheetIndex("Sheet 3"));
+ assertEquals(7, wbr.getSheetIndex("Sheet 8"));
+ assertEquals(8, wbr.getSheetIndex("Sheet 9"));
+ assertEquals(9, wbr.getSheetIndex("Sheet 1"));
+
+ // Now get the index by the sheet, not the name
+ for(int i=0; i<10; i++) {
+ Sheet s = wbr.getSheetAt(i);
+ assertEquals(i, wbr.getSheetIndex(s));
+ }
+ }
+
+ public void testCloneSheet() {
+ Workbook book = getTestDataProvider().createWorkbook();
+ Sheet sheet = book.createSheet("TEST");
+ sheet.createRow(0).createCell(0).setCellValue("Test");
+ sheet.createRow(1).createCell(0).setCellValue(36.6);
+ sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
+ sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));
+ assertTrue(sheet.isSelected());
+
+ Sheet clonedSheet = book.cloneSheet(0);
+ assertEquals("TEST (2)", clonedSheet.getSheetName());
+ assertEquals(2, clonedSheet.getPhysicalNumberOfRows());
+ assertEquals(2, clonedSheet.getNumMergedRegions());
+ assertFalse(clonedSheet.isSelected());
+
+ //cloned sheet is a deep copy, adding rows in the original does not affect the clone
+ sheet.createRow(2).createCell(0).setCellValue(1);
+ sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 2));
+ assertEquals(2, clonedSheet.getPhysicalNumberOfRows());
+ assertEquals(2, clonedSheet.getPhysicalNumberOfRows());
+
+ clonedSheet.createRow(2).createCell(0).setCellValue(1);
+ clonedSheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 2));
+ assertEquals(3, clonedSheet.getPhysicalNumberOfRows());
+ assertEquals(3, clonedSheet.getPhysicalNumberOfRows());
+
+ }
+
+ public void testParentReferences(){
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ Sheet sheet = workbook.createSheet();
+ assertSame(workbook, sheet.getWorkbook());
+
+ Row row = sheet.createRow(0);
+ assertSame(sheet, row.getSheet());
+
+ Cell cell = row.createCell(1);
+ assertSame(sheet, cell.getSheet());
+ assertSame(row, cell.getRow());
+
+ workbook = getTestDataProvider().writeOutAndReadBack(workbook);
+ sheet = workbook.getSheetAt(0);
+ assertSame(workbook, sheet.getWorkbook());
+
+ row = sheet.getRow(0);
+ assertSame(sheet, row.getSheet());
+
+ cell = row.getCell(1);
+ assertSame(sheet, cell.getSheet());
+ assertSame(row, cell.getRow());
+ }
+
+ /**
+ * Tests that all of the unicode capable string fields can be set, written and then read back
+ */
+ public void testUnicodeInAll() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+ CreationHelper factory = wb.getCreationHelper();
+ //Create a unicode dataformat (contains euro symbol)
+ DataFormat df = wb.createDataFormat();
+ final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)";
+ short fmt = df.getFormat(formatStr);
+
+ //Create a unicode sheet name (euro symbol)
+ Sheet s = wb.createSheet("\u20ac");
+
+ //Set a unicode header (you guessed it the euro symbol)
+ Header h = s.getHeader();
+ h.setCenter("\u20ac");
+ h.setLeft("\u20ac");
+ h.setRight("\u20ac");
+
+ //Set a unicode footer
+ Footer f = s.getFooter();
+ f.setCenter("\u20ac");
+ f.setLeft("\u20ac");
+ f.setRight("\u20ac");
+
+ Row r = s.createRow(0);
+ Cell c = r.createCell(1);
+ c.setCellValue(12.34);
+ c.getCellStyle().setDataFormat(fmt);
+
+ Cell c2 = r.createCell(2); // TODO - c2 unused but changing next line ('c'->'c2') causes test to fail
+ c.setCellValue(factory.createRichTextString("\u20ac"));
+
+ Cell c3 = r.createCell(3);
+ String formulaString = "TEXT(12.34,\"\u20ac###,##\")";
+ c3.setCellFormula(formulaString);
+
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+
+ //Test the sheetname
+ s = wb.getSheet("\u20ac");
+ assertNotNull(s);
+
+ //Test the header
+ h = s.getHeader();
+ assertEquals(h.getCenter(), "\u20ac");
+ assertEquals(h.getLeft(), "\u20ac");
+ assertEquals(h.getRight(), "\u20ac");
+
+ //Test the footer
+ f = s.getFooter();
+ assertEquals(f.getCenter(), "\u20ac");
+ assertEquals(f.getLeft(), "\u20ac");
+ assertEquals(f.getRight(), "\u20ac");
+
+ //Test the dataformat
+ r = s.getRow(0);
+ c = r.getCell(1);
+ df = wb.createDataFormat();
+ assertEquals(formatStr, df.getFormat(c.getCellStyle().getDataFormat()));
+
+ //Test the cell string value
+ c2 = r.getCell(2);
+ assertEquals(c.getRichStringCellValue().getString(), "\u20ac");
+
+ //Test the cell formula
+ c3 = r.getCell(3);
+ assertEquals(c3.getCellFormula(), formulaString);
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.ss.util;\r
-\r
-import junit.framework.TestCase;\r
-\r
-\r
-/**\r
- * Tests that the common CellReference works as we need it to\r
- */\r
-public class TestRegion extends TestCase {\r
- \r
- public void testGetRegionRef() {\r
- int rowFrom = 3;\r
- short colFrom = 3;\r
- int rowTo = 9;\r
- short colTo = 9;\r
- Region region = new Region(rowFrom, colFrom, rowTo, colTo);\r
- assertEquals("D4:J10", region.getRegionRef());\r
- }\r
- \r
- public void testContains() {\r
- int rowFrom = 3;\r
- short colFrom = 3;\r
- int rowTo = 9;\r
- short colTo = 9;\r
- Region region = new Region(rowFrom, colFrom, rowTo, colTo);\r
- assertEquals("D4:J10", region.getRegionRef());\r
- assertTrue(region.contains(5, (short) 7));\r
- assertTrue(region.contains(9, (short) 9));\r
- assertFalse(region.contains(9, (short) 10));\r
- }\r
- \r
- public void testConstructors() {\r
- Region region_1 = new Region("A1:E7");\r
- assertEquals(0, region_1.getColumnFrom());\r
- assertEquals((short)4, region_1.getColumnTo());\r
- }\r
- \r
-}\r
+/* ====================================================================
+ 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.util;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests that the common CellReference works as we need it to
+ */
+@SuppressWarnings("deprecation") // the Region class is deprecated in the public API, but still needs to be tested
+public final class TestRegion extends TestCase {
+
+ public void testGetRegionRef() {
+ int rowFrom = 3;
+ short colFrom = 3;
+ int rowTo = 9;
+ short colTo = 9;
+ Region region = new Region(rowFrom, colFrom, rowTo, colTo);
+ assertEquals("D4:J10", region.getRegionRef());
+ }
+
+ public void testContains() {
+ int rowFrom = 3;
+ short colFrom = 3;
+ int rowTo = 9;
+ short colTo = 9;
+ Region region = new Region(rowFrom, colFrom, rowTo, colTo);
+ assertEquals("D4:J10", region.getRegionRef());
+ assertTrue(region.contains(5, (short) 7));
+ assertTrue(region.contains(9, (short) 9));
+ assertFalse(region.contains(9, (short) 10));
+ }
+
+ public void testConstructors() {
+ Region region_1 = new Region("A1:E7");
+ assertEquals(0, region_1.getColumnFrom());
+ assertEquals((short)4, region_1.getColumnTo());
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.util;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.io.ByteArrayOutputStream;\r
-\r
-import junit.framework.TestCase;\r
-\r
-/**\r
- * Class to test {@link LittleEndianInputStream} and {@link LittleEndianOutputStream}\r
- *\r
- * @author Josh Micich\r
- */\r
-public final class TestLittleEndianStreams extends TestCase {\r
- \r
- public void testRead() {\r
- ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
- LittleEndianOutput leo = new LittleEndianOutputStream(baos);\r
- leo.writeInt(12345678);\r
- leo.writeShort(12345);\r
- leo.writeByte(123);\r
- leo.writeShort(40000);\r
- leo.writeByte(200);\r
- leo.writeLong(1234567890123456789L);\r
- leo.writeDouble(123.456);\r
- \r
- LittleEndianInput lei = new LittleEndianInputStream(new ByteArrayInputStream(baos.toByteArray()));\r
- \r
- assertEquals(12345678, lei.readInt());\r
- assertEquals(12345, lei.readShort());\r
- assertEquals(123, lei.readByte());\r
- assertEquals(40000, lei.readUShort());\r
- assertEquals(200, lei.readUByte());\r
- assertEquals(1234567890123456789L, lei.readLong());\r
- assertEquals(123.456, lei.readDouble(), 0.0);\r
- }\r
-}\r
+/* ====================================================================
+ 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.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+import junit.framework.TestCase;
+
+/**
+ * Class to test {@link LittleEndianInputStream} and {@link LittleEndianOutputStream}
+ *
+ * @author Josh Micich
+ */
+public final class TestLittleEndianStreams extends TestCase {
+
+ public void testRead() {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ LittleEndianOutput leo = new LittleEndianOutputStream(baos);
+ leo.writeInt(12345678);
+ leo.writeShort(12345);
+ leo.writeByte(123);
+ leo.writeShort(40000);
+ leo.writeByte(200);
+ leo.writeLong(1234567890123456789L);
+ leo.writeDouble(123.456);
+
+ LittleEndianInput lei = new LittleEndianInputStream(new ByteArrayInputStream(baos.toByteArray()));
+
+ assertEquals(12345678, lei.readInt());
+ assertEquals(12345, lei.readShort());
+ assertEquals(123, lei.readByte());
+ assertEquals(40000, lei.readUShort());
+ assertEquals(200, lei.readUByte());
+ assertEquals(1234567890123456789L, lei.readLong());
+ assertEquals(123.456, lei.readDouble(), 0.0);
+ }
+}