aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache
diff options
context:
space:
mode:
authorJosh Micich <josh@apache.org>2009-08-20 23:25:10 +0000
committerJosh Micich <josh@apache.org>2009-08-20 23:25:10 +0000
commit5f991cb6168932c7bba5c81bead14d16d69774db (patch)
tree454179dfe68a8635a6e9bd3db66549ba2928bb2d /src/testcases/org/apache
parentaf0f440a47a94f509717f74d76af741f8518190f (diff)
downloadpoi-5f991cb6168932c7bba5c81bead14d16d69774db.tar.gz
poi-5f991cb6168932c7bba5c81bead14d16d69774db.zip
minor improvements to sheet name validation and identification of cell references vs defined names
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@806395 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache')
-rw-r--r--src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java148
-rw-r--r--src/testcases/org/apache/poi/hssf/util/TestCellReference.java40
2 files changed, 115 insertions, 73 deletions
diff --git a/src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java b/src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java
index 766e396473..72d26d04d0 100644
--- a/src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java
+++ b/src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java
@@ -18,6 +18,10 @@
package org.apache.poi.hssf.record;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.poi.util.HexRead;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
@@ -32,67 +36,95 @@ import junit.framework.TestCase;
public final class TestBoundSheetRecord extends TestCase {
- public void testRecordLength() {
- BoundSheetRecord record = new BoundSheetRecord("Sheet1");
- assertEquals(18, record.getRecordSize());
- }
-
- public void testWideRecordLength() {
- BoundSheetRecord record = new BoundSheetRecord("Sheet\u20ac");
- assertEquals(24, record.getRecordSize());
- }
-
- public void testName() {
- BoundSheetRecord record = new BoundSheetRecord("1234567890223456789032345678904");
-
- try {
- record.setSheetname("s//*s");
- throw new AssertionFailedError("Should have thrown IllegalArgumentException, but didnt");
- } catch (IllegalArgumentException e) {
- // expected
- }
- }
-
- public void testDeserializeUnicode() {
-
- byte[] data = {
-// (byte)0x85, 0x00, // sid
-// 0x1a, 0x00, // length
- 0x3C, 0x09, 0x00, 0x00, // bof
- 0x00, 0x00, // flags
- 0x09, // len( str )
- 0x01, // unicode
- // <str>
- 0x21, 0x04, 0x42, 0x04, 0x40, 0x04,
- 0x30, 0x04, 0x3D, 0x04, 0x38, 0x04,
- 0x47, 0x04, 0x3A, 0x04, 0x30, 0x04
- // </str>
- };
-
- RecordInputStream in = TestcaseRecordInputStream.create(BoundSheetRecord.sid, data);
+ public void testRecordLength() {
+ BoundSheetRecord record = new BoundSheetRecord("Sheet1");
+ assertEquals(18, record.getRecordSize());
+ }
+
+ public void testWideRecordLength() {
+ BoundSheetRecord record = new BoundSheetRecord("Sheet\u20ac");
+ assertEquals(24, record.getRecordSize());
+ }
+
+ public void testName() {
+ BoundSheetRecord record = new BoundSheetRecord("1234567890223456789032345678904");
+
+ try {
+ record.setSheetname("s//*s");
+ throw new AssertionFailedError("Should have thrown IllegalArgumentException, but didnt");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ public void testDeserializeUnicode() {
+
+ byte[] data = HexRead.readFromString(""
+ + "85 00 1A 00" // sid, length
+ + "3C 09 00 00" // bof
+ + "00 00"// flags
+ + "09 01" // str-len. unicode flag
+ // string data
+ + "21 04 42 04 40 04"
+ + "30 04 3D 04 38 04"
+ + "47 04 3A 04 30 04"
+ );
+
+ RecordInputStream in = TestcaseRecordInputStream.create(data);
BoundSheetRecord bsr = new BoundSheetRecord(in);
// sheet name is unicode Russian for 'minor page'
assertEquals("\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430", bsr.getSheetname());
-
+
+ byte[] data2 = bsr.serialize();
+ assertTrue(Arrays.equals(data, data2));
+ }
+
+ public void testOrdering() {
+ BoundSheetRecord bs1 = new BoundSheetRecord("SheetB");
+ BoundSheetRecord bs2 = new BoundSheetRecord("SheetC");
+ BoundSheetRecord bs3 = new BoundSheetRecord("SheetA");
+ bs1.setPositionOfBof(11);
+ bs2.setPositionOfBof(33);
+ bs3.setPositionOfBof(22);
+
+ List<BoundSheetRecord> l = new ArrayList<BoundSheetRecord>();
+ l.add(bs1);
+ l.add(bs2);
+ l.add(bs3);
+
+ BoundSheetRecord[] r = BoundSheetRecord.orderByBofPosition(l);
+ assertEquals(3, r.length);
+ assertEquals(bs1, r[0]);
+ assertEquals(bs3, r[1]);
+ assertEquals(bs2, r[2]);
}
- public void testOrdering() {
- BoundSheetRecord bs1 = new BoundSheetRecord("SheetB");
- BoundSheetRecord bs2 = new BoundSheetRecord("SheetC");
- BoundSheetRecord bs3 = new BoundSheetRecord("SheetA");
- bs1.setPositionOfBof(11);
- bs2.setPositionOfBof(33);
- bs3.setPositionOfBof(22);
-
- ArrayList l = new ArrayList();
- l.add(bs1);
- l.add(bs2);
- l.add(bs3);
-
- BoundSheetRecord[] r = BoundSheetRecord.orderByBofPosition(l);
- assertEquals(3, r.length);
- assertEquals(bs1, r[0]);
- assertEquals(bs3, r[1]);
- assertEquals(bs2, r[2]);
- }
+ public void testValidNames() {
+ confirmValid("Sheet1", true);
+ confirmValid("O'Brien's sales", true);
+ confirmValid(" data # ", true);
+ confirmValid("data $1.00", true);
+
+ confirmValid("data?", false);
+ confirmValid("abc/def", false);
+ confirmValid("data[0]", false);
+ confirmValid("data*", false);
+ confirmValid("abc\\def", false);
+ confirmValid("'data", false);
+ confirmValid("data'", false);
+ }
+
+ private static void confirmValid(String sheetName, boolean expectedResult) {
+
+ try {
+ new BoundSheetRecord(sheetName);
+ if (!expectedResult) {
+ throw new AssertionFailedError("Expected sheet name '" + sheetName + "' to be invalid");
+ }
+ } catch (IllegalArgumentException e) {
+ if (expectedResult) {
+ throw new AssertionFailedError("Expected sheet name '" + sheetName + "' to be valid");
+ }
+ }
+ }
}
diff --git a/src/testcases/org/apache/poi/hssf/util/TestCellReference.java b/src/testcases/org/apache/poi/hssf/util/TestCellReference.java
index acd3f00e79..523a86bf6b 100644
--- a/src/testcases/org/apache/poi/hssf/util/TestCellReference.java
+++ b/src/testcases/org/apache/poi/hssf/util/TestCellReference.java
@@ -14,7 +14,7 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
+
package org.apache.poi.hssf.util;
@@ -26,12 +26,12 @@ import org.apache.poi.ss.SpreadsheetVersion;
public final class TestCellReference extends TestCase {
-
+
public void testAbsRef1(){
CellReference cf = new CellReference("$B$5");
confirmCell(cf, null, 4, 1, true, true, "$B$5");
}
-
+
public void testAbsRef2(){
CellReference cf = new CellReference(4,1,true,true);
confirmCell(cf, null, 4, 1, true, true, "$B$5");
@@ -41,17 +41,17 @@ public final class TestCellReference extends TestCase {
CellReference cf = new CellReference("B$5");
confirmCell(cf, null, 4, 1, true, false, "B$5");
}
-
+
public void testAbsRef4(){
CellReference cf = new CellReference(4,1,true,false);
confirmCell(cf, null, 4, 1, true, false, "B$5");
}
-
+
public void testAbsRef5(){
CellReference cf = new CellReference("$B5");
confirmCell(cf, null, 4, 1, false, true, "$B5");
}
-
+
public void testAbsRef6(){
CellReference cf = new CellReference(4,1,false,true);
confirmCell(cf, null, 4, 1, false, true, "$B5");
@@ -61,27 +61,27 @@ public final class TestCellReference extends TestCase {
CellReference cf = new CellReference("B5");
confirmCell(cf, null, 4, 1, false, false, "B5");
}
-
+
public void testAbsRef8(){
CellReference cf = new CellReference(4,1,false,false);
confirmCell(cf, null, 4, 1, false, false, "B5");
}
-
+
public void testSpecialSheetNames() {
CellReference cf;
cf = new CellReference("'profit + loss'!A1");
confirmCell(cf, "profit + loss", 0, 0, false, false, "'profit + loss'!A1");
-
+
cf = new CellReference("'O''Brien''s Sales'!A1");
confirmCell(cf, "O'Brien's Sales", 0, 0, false, false, "'O''Brien''s Sales'!A1");
-
+
cf = new CellReference("'Amazing!'!A1");
confirmCell(cf, "Amazing!", 0, 0, false, false, "'Amazing!'!A1");
}
- /* package */ static void confirmCell(CellReference cf, String expSheetName, int expRow,
+ /* package */ static void confirmCell(CellReference cf, String expSheetName, int expRow,
int expCol, boolean expIsRowAbs, boolean expIsColAbs, String expText) {
-
+
assertEquals(expSheetName, cf.getSheetName());
assertEquals("row index is wrong", expRow, cf.getRow());
assertEquals("col index is wrong", expCol, cf.getCol());
@@ -103,9 +103,19 @@ public final class TestCellReference extends TestCase {
confirmNameType("A.1", NameType.NAMED_RANGE);
confirmNameType("A1.", NameType.NAMED_RANGE);
}
-
- private void confirmNameType(String ref, int expectedResult) {
- int actualResult = CellReference.classifyCellReference(ref, SpreadsheetVersion.EXCEL97);
+
+ public void testClassificationOfRowReferences(){
+ confirmNameType("10", NameType.ROW);
+ confirmNameType("$10", NameType.ROW);
+ confirmNameType("65536", NameType.ROW);
+
+ confirmNameType("65537", NameType.BAD_CELL_OR_NAMED_RANGE);
+ confirmNameType("$100000", NameType.BAD_CELL_OR_NAMED_RANGE);
+ confirmNameType("$1$1", NameType.BAD_CELL_OR_NAMED_RANGE);
+ }
+
+ private void confirmNameType(String ref, NameType expectedResult) {
+ NameType actualResult = CellReference.classifyCellReference(ref, SpreadsheetVersion.EXCEL97);
assertEquals(expectedResult, actualResult);
}
}