From aa253ea74abc5465839d36cd61491d2923697deb Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 16 Jul 2010 16:02:09 +0000 Subject: Patch from Andrew Shirley from bug #49185 - Support for HSSFNames where the comment is stored in a NameCommentRecord git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@964845 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/model/TestLinkTable.java | 33 ++++++++++++++++- .../poi/hssf/record/TestNameCommentRecord.java | 42 ++++++++++++++++++++++ .../org/apache/poi/hssf/usermodel/TestBugs.java | 27 ++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/testcases/org/apache/poi/hssf/record/TestNameCommentRecord.java (limited to 'src/testcases/org/apache/poi') diff --git a/src/testcases/org/apache/poi/hssf/model/TestLinkTable.java b/src/testcases/org/apache/poi/hssf/model/TestLinkTable.java index f88cd4860f..72097b2a61 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestLinkTable.java +++ b/src/testcases/org/apache/poi/hssf/model/TestLinkTable.java @@ -18,12 +18,17 @@ package org.apache.poi.hssf.model; import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.hssf.record.NameCommentRecord; +import org.apache.poi.hssf.record.NameRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.SSTRecord; import org.apache.poi.hssf.record.SupBookRecord; @@ -138,7 +143,7 @@ public final class TestLinkTable extends TestCase { LinkTable lt; try { - lt = new LinkTable(recList, 0, wrl); + lt = new LinkTable(recList, 0, wrl, Collections.emptyMap()); } catch (RuntimeException e) { if (e.getMessage().equals("Expected an EXTERNSHEET record but got (org.apache.poi.hssf.record.SSTRecord)")) { throw new AssertionFailedError("Identified bug 47001b"); @@ -148,4 +153,30 @@ public final class TestLinkTable extends TestCase { } assertNotNull(lt); } + + /** + * + */ + public void testNameCommentRecordBetweenNameRecords() { + + final Record[] recs = { + new NameRecord(), + new NameCommentRecord("name1", "comment1"), + new NameRecord(), + new NameCommentRecord("name2", "comment2"), + + }; + final List recList = Arrays.asList(recs); + final WorkbookRecordList wrl = new WorkbookRecordList(); + final Map commentRecords = new LinkedHashMap(); + + final LinkTable lt = new LinkTable(recList, 0, wrl, commentRecords); + assertNotNull(lt); + + assertEquals(2, commentRecords.size()); + assertTrue(recs[1] == commentRecords.get("name1")); //== is intentionally not .equals()! + assertTrue(recs[3] == commentRecords.get("name2")); //== is intentionally not .equals()! + + assertEquals(2, lt.getNumNames()); + } } diff --git a/src/testcases/org/apache/poi/hssf/record/TestNameCommentRecord.java b/src/testcases/org/apache/poi/hssf/record/TestNameCommentRecord.java new file mode 100644 index 0000000000..f7a29eb466 --- /dev/null +++ b/src/testcases/org/apache/poi/hssf/record/TestNameCommentRecord.java @@ -0,0 +1,42 @@ +/* ==================================================================== + 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.TestCase; + +import org.apache.poi.util.HexRead; + +/** + * Tests the NameCommentRecord serializes/deserializes correctly + * + * @author Andrew Shirley (aks at corefiling.co.uk) + */ +public final class TestNameCommentRecord extends TestCase { + public void testReserialize() { + final byte[] data = HexRead + .readFromString("" + + "94 08 00 00 00 00 00 00 00 00 00 00 04 00 07 00 00 6E 61 6D 65 00 63 6F 6D 6D 65 6E 74]"); + final RecordInputStream in = TestcaseRecordInputStream.create(NameCommentRecord.sid, data); + final NameCommentRecord ncr = new NameCommentRecord(in); + assertEquals(0x0894, ncr.getRecordType()); + assertEquals("name", ncr.getNameText()); + assertEquals("comment", ncr.getCommentText()); + final byte[] data2 = ncr.serialize(); + TestcaseRecordInputStream.confirmRecordEncoding(NameCommentRecord.sid, data, data2); + } +} diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index 5901f53124..706ccbfa04 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -1734,4 +1734,31 @@ if(1==2) { assertEquals(234.0, row.getCell(1).getNumericCellValue()); } } + + /** + * Test for a file with NameRecord with NameCommentRecord comments + */ + public void test49185() throws Exception { + HSSFWorkbook wb = openSample("49185.xls"); + Name name = wb.getName("foobarName"); + assertEquals("This is a comment", name.getComment()); + + // Rename the name, comment comes with it + name.setNameName("ChangedName"); + assertEquals("This is a comment", name.getComment()); + + // Save and re-check + wb = writeOutAndReadBack(wb); + name = wb.getName("ChangedName"); + assertEquals("This is a comment", name.getComment()); + + // Now try to change it + name.setComment("Changed Comment"); + assertEquals("Changed Comment", name.getComment()); + + // Save and re-check + wb = writeOutAndReadBack(wb); + name = wb.getName("ChangedName"); + assertEquals("Changed Comment", name.getComment()); + } } -- cgit v1.2.3