ASF Bugzilla – Attachment 33099 Details for
Bug 58402
Rework AreaReference implementation details and tests
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
AreaReference.java patch
AreaReference.java.final_SpreadsheetVersion.patch (text/plain), 17.16 KB, created by
Javen O'Neal
on 2015-09-13 10:31:57 UTC
(
hide
)
Description:
AreaReference.java patch
Filename:
MIME Type:
Creator:
Javen O'Neal
Created:
2015-09-13 10:31:57 UTC
Size:
17.16 KB
patch
obsolete
>Index: src/java/org/apache/poi/ss/util/AreaReference.java >=================================================================== >--- src/java/org/apache/poi/ss/util/AreaReference.java (revision 1702702) >+++ src/java/org/apache/poi/ss/util/AreaReference.java (working copy) >@@ -31,11 +31,12 @@ > private static final char CELL_DELIMITER = ':'; > /** The character (') used to quote sheet names when they contain special characters */ > private static final char SPECIAL_NAME_DELIMITER = '\''; >+ private static final SpreadsheetVersion DEFAULT_SPREADSHEET_VERSION = SpreadsheetVersion.EXCEL97; > > private final CellReference _firstCell; > private final CellReference _lastCell; > private final boolean _isSingleCell; >- private SpreadsheetVersion _version; >+ private final SpreadsheetVersion _version; //never null > > /** > * @deprecated Prefer supplying a version. >@@ -42,7 +43,7 @@ > */ > @Deprecated > public AreaReference(String reference) { >- this(reference, SpreadsheetVersion.EXCEL97); >+ this(reference, DEFAULT_SPREADSHEET_VERSION); > } > > /** >@@ -51,7 +52,7 @@ > * The area reference must be contiguous (i.e. represent a single rectangle, not a union of rectangles) > */ > public AreaReference(String reference, SpreadsheetVersion version) { >- _version = version; >+ _version = (version != null) ? version : DEFAULT_SPREADSHEET_VERSION; > if(! isContiguous(reference)) { > throw new IllegalArgumentException( > "References passed to the AreaReference must be contiguous, " + >@@ -114,8 +115,18 @@ > > /** > * Creates an area ref from a pair of Cell References. >+ * @deprecated since September 2015. Use {@link #AreaReference(CellReference, CellReference, SpreadsheetVersion)} instead. > */ >+ @Deprecated > public AreaReference(CellReference topLeft, CellReference botRight) { >+ this(topLeft, botRight, DEFAULT_SPREADSHEET_VERSION); >+ } >+ >+ /** >+ * Creates an area ref from a pair of Cell References. >+ */ >+ public AreaReference(CellReference topLeft, CellReference botRight, SpreadsheetVersion version) { >+ _version = (version != null) ? version : DEFAULT_SPREADSHEET_VERSION; > boolean swapRows = topLeft.getRow() > botRight.getRow(); > boolean swapCols = topLeft.getCol() > botRight.getCol(); > if (swapRows || swapCols) { >@@ -179,10 +190,16 @@ > return false; > } > >+ /** >+ * @throws NullPointerException if version is null >+ */ > public static AreaReference getWholeRow(SpreadsheetVersion version, String start, String end) { > return new AreaReference("$A" + start + ":$" + version.getLastColumnName() + end, version); > } > >+ /** >+ * @throws NullPointerException if version is null >+ */ > public static AreaReference getWholeColumn(SpreadsheetVersion version, String start, String end) { > return new AreaReference(start + "$1:" + end + "$" + version.getMaxRows(), version); > } >@@ -193,7 +210,7 @@ > */ > public static boolean isWholeColumnReference(SpreadsheetVersion version, CellReference topLeft, CellReference botRight) { > if (null == version) { >- version = SpreadsheetVersion.EXCEL97; // how the code used to behave. >+ version = DEFAULT_SPREADSHEET_VERSION; // how the code used to behave. > } > > // These are represented as something like >@@ -263,9 +280,10 @@ > int maxRow = Math.max(_firstCell.getRow(), _lastCell.getRow()); > int minCol = Math.min(_firstCell.getCol(), _lastCell.getCol()); > int maxCol = Math.max(_firstCell.getCol(), _lastCell.getCol()); >+ final int size = (maxRow-minRow+1) * (maxCol-minCol+1); > String sheetName = _firstCell.getSheetName(); > >- List<CellReference> refs = new ArrayList<CellReference>(); >+ List<CellReference> refs = new ArrayList<CellReference>(size); > for(int row=minRow; row<=maxRow; row++) { > for(int col=minCol; col<=maxCol; col++) { > CellReference ref = new CellReference(sheetName, row, col, _firstCell.isRowAbsolute(), _firstCell.isColAbsolute()); >@@ -272,7 +290,8 @@ > refs.add(ref); > } > } >- return refs.toArray(new CellReference[refs.size()]); >+ assert size == refs.size(); >+ return refs.toArray(new CellReference[size]); > } > > /** >Index: src/testcases/org/apache/poi/ss/util/TestAreaReference.java >=================================================================== >--- src/testcases/org/apache/poi/ss/util/TestAreaReference.java (revision 1702702) >+++ src/testcases/org/apache/poi/ss/util/TestAreaReference.java (working copy) >@@ -17,6 +17,8 @@ > package org.apache.poi.ss.util; > > import org.apache.poi.ss.SpreadsheetVersion; >+import static org.apache.poi.ss.SpreadsheetVersion.EXCEL97; >+import static org.apache.poi.ss.SpreadsheetVersion.EXCEL2007; > > import junit.framework.TestCase; > >@@ -26,40 +28,180 @@ > * @author David North > */ > public class TestAreaReference extends TestCase { >+ >+ public void testConstructors() { >+ final CellReference topLeft = new CellReference("A1"); >+ final CellReference bottomRight = new CellReference("B23"); >+ assertEquals("A1:B23", new AreaReference(topLeft, bottomRight, EXCEL97).formatAsString()); >+ assertEquals("A1:B23", new AreaReference("A1:B23", EXCEL97).formatAsString()); >+ assertEquals("A1:B23", new AreaReference("Sheet1!A1:B23", EXCEL97).formatAsString()); //Sheet name is not stored in AreaReference object >+ assertEquals("A1:B23", new AreaReference("Sheet1!A1:Sheet1!B23", EXCEL97).formatAsString()); //Same sheet names >+ assertEquals("A1:B23", new AreaReference("Sheet1!A1:'Sheet1'!B23", EXCEL97).formatAsString()); //non-required single quotes don't change the name of the sheet >+ >+ // Check for NPE's >+ AreaReference ref; >+ try { >+ final String reference = null; >+ ref = new AreaReference(reference, EXCEL97); >+ fail("reference string may not be null"); >+ } catch (final NullPointerException e) { } //expected >+ try { >+ ref = new AreaReference(topLeft, null, EXCEL97); >+ fail("topLeft and bottomRight CellReferences may not be null"); >+ } catch (final NullPointerException e) { } //expected >+ try { >+ ref = new AreaReference(null, bottomRight, EXCEL97); >+ fail("topLeft and bottomRight CellReferences may not be null"); >+ } catch (final NullPointerException e) { } //expected >+ try { >+ ref = new AreaReference(null, null, EXCEL97); >+ fail("topLeft and bottomRight CellReferences may not be null"); >+ } catch (final NullPointerException e) { } //expected >+ >+ // Check for illegal arguments passed to constructor >+ try { >+ ref = new AreaReference("Sheet1!A1:Sheet2!B23", EXCEL97); >+ fail("reference cannot span multiple sheets"); >+ } catch (final RuntimeException e) { } //expected >+ try { >+ // This could be valid if the reference is stored somewhere in Sheet1 >+ // This is too complicated for POI, so POI should throw an error >+ ref = new AreaReference("A1:Sheet1!B23", EXCEL97); >+ fail("mixed implicit and explict sheet in area reference is not allowed"); >+ } catch (final RuntimeException e) { } //expected >+ try { >+ ref = new AreaReference("A1:B23,C8:D9", EXCEL97); >+ fail("AreaReferences instances cannot hold discontiguous regions."); >+ } catch (final IllegalArgumentException e) { } //expected >+ try { >+ ref = new AreaReference("Sheet1:'A1'", EXCEL97); >+ fail("single-quotes cannot exist around cell reference part"); >+ } catch (final RuntimeException e) { } //expected >+ try { >+ ref = new AreaReference("'Sheet1:A1'", EXCEL97); >+ fail("partially-quoted sheet name"); >+ } catch (final RuntimeException e) { } //expected >+ try { >+ ref = new AreaReference("'Sheet1:A1'!A1:B23", EXCEL97); >+ fail("illegal ':' character in sheet name"); >+ } catch (final RuntimeException e) { } //expected >+ } >+ >+ public void testIsContiguous() { >+ assertTrue("Contiguous area reference", AreaReference.isContiguous("A1:B23")); >+ assertTrue("3D reference with non-quoted sheet name", AreaReference.isContiguous("Sheet1!A1:B23")); >+ assertTrue("3D reference with quoted sheet name", AreaReference.isContiguous("'Sheet 1'!A1:B23")); >+ >+ // What's the correct behavior here? >+ assertFalse("Discontiguous reference that could be simplified as one continuous region", >+ AreaReference.isContiguous("A1:B23,C1:C23")); >+ >+ assertFalse("Discontiguous area reference", AreaReference.isContiguous("A1:B23,C8:D9")); >+ assertFalse("Discontiguous 3D reference with non-quoted sheet name", >+ AreaReference.isContiguous("'Sheet1'!C8:D9")); >+ assertFalse("Discontiguous 3D reference with quoted sheet name", >+ AreaReference.isContiguous("'Sheet 1'!A1:B23,C8:D9")); >+ } >+ >+ public void testIsWholeColumnReference() { >+ // whole column = first to last row in workbook >+ assertTrue(new AreaReference("A1:B65536", EXCEL97).isWholeColumnReference()); >+ assertFalse(new AreaReference("A1:B65536", EXCEL2007).isWholeColumnReference()); >+ assertTrue(new AreaReference("A1:B1048576", EXCEL2007).isWholeColumnReference()); >+ >+ // alternative ways to express whole column >+ assertTrue("use dollar signs", new AreaReference("C$1:C$65535", EXCEL97).isWholeColumnReference()); >+ assertTrue("use row zero", new AreaReference("D$1:F$0", EXCEL97).isWholeColumnReference()); >+ >+ // number-only cell references >+ assertFalse("whole row", new AreaReference("1:2", EXCEL97).isWholeColumnReference()); >+ assertFalse("whole row", new AreaReference("3:3", EXCEL97).isWholeColumnReference()); >+ assertFalse("whole row", new AreaReference("$3:$3", EXCEL97).isWholeColumnReference()); >+ >+ // letter-only cell references >+ assertTrue(new AreaReference("A:B", EXCEL97).isWholeColumnReference()); >+ assertTrue(new AreaReference("C:C", EXCEL97).isWholeColumnReference()); >+ assertTrue(new AreaReference("$C:$C", EXCEL97).isWholeColumnReference()); >+ >+ // entire worksheet >+ assertTrue(new AreaReference("A:IW", EXCEL97).isWholeColumnReference()); >+ assertTrue(new AreaReference("A:XFD", EXCEL2007).isWholeColumnReference()); >+ assertTrue(new AreaReference("1:65536", EXCEL97).isWholeColumnReference()); >+ assertTrue(new AreaReference("1:1048576", EXCEL2007).isWholeColumnReference()); >+ } >+ >+ public void testGetFirstCell() { >+ // TopLeft-BottomRight order >+ assertEquals(new CellReference("A1"), new AreaReference("A1:B23").getFirstCell()); >+ assertEquals(new CellReference("A$1"), new AreaReference("A$1:B23").getFirstCell()); >+ assertEquals(new CellReference("$A$1"), new AreaReference("$A$1:B23").getFirstCell()); >+ >+ //Constructor manipulates initial reference to determine top-left cell >+ assertEquals(new CellReference("A1"), new AreaReference("B23:A1").getFirstCell()); >+ assertEquals(new CellReference("A1"), new AreaReference("B1:A23").getFirstCell()); >+ assertEquals(new CellReference("A$1"), new AreaReference("$B$1:A23").getFirstCell()); >+ assertEquals(new CellReference("A1"), new AreaReference("A23:B1").getFirstCell()); >+ } >+ >+ public void testGetLastCell() { >+ // TopLeft-BottomRight order >+ assertEquals(new CellReference("B23"), new AreaReference("A1:B23").getLastCell()); >+ assertEquals(new CellReference("B$23"), new AreaReference("A1:B$23").getLastCell()); >+ assertEquals(new CellReference("$B$23"), new AreaReference("A1:$B$23").getLastCell()); >+ >+ //Constructor manipulates initial reference to determine top-left cell >+ assertEquals(new CellReference("B23"), new AreaReference("B23:A1").getLastCell()); >+ assertEquals(new CellReference("B23"), new AreaReference("B1:A23").getLastCell()); >+ assertEquals(new CellReference("B$23"), new AreaReference("B1:$A$23").getLastCell()); >+ assertEquals(new CellReference("B23"), new AreaReference("A23:B1").getLastCell()); >+ } >+ >+ public void testIsSingleCell() { >+ assertTrue(new AreaReference("A1", EXCEL97).isSingleCell()); >+ assertTrue(new AreaReference("A$1", EXCEL97).isSingleCell()); >+ assertTrue(new AreaReference("'Sheet 1'!A1", EXCEL97).isSingleCell()); >+ >+ assertFalse(new AreaReference("A1:A1", EXCEL97).isSingleCell()); >+ assertFalse(new AreaReference("A1:A$1", EXCEL97).isSingleCell()); >+ assertFalse(new AreaReference("A1:B23", EXCEL97).isSingleCell()); >+ assertFalse(new AreaReference("'Sheet 1'!A1:B23", EXCEL97).isSingleCell()); >+ >+ assertFalse(new AreaReference(new CellReference("A1"), new CellReference("A1"), EXCEL97).isSingleCell()); >+ } > >- public void testWholeColumn() { >- AreaReference oldStyle = AreaReference.getWholeColumn(SpreadsheetVersion.EXCEL97, "A", "B"); >+ public void testGetWholeColumn() { >+ AreaReference oldStyle = AreaReference.getWholeColumn(EXCEL97, "A", "B"); > assertEquals(0, oldStyle.getFirstCell().getCol()); > assertEquals(0, oldStyle.getFirstCell().getRow()); > assertEquals(1, oldStyle.getLastCell().getCol()); >- assertEquals(SpreadsheetVersion.EXCEL97.getLastRowIndex(), oldStyle.getLastCell().getRow()); >+ assertEquals(EXCEL97.getLastRowIndex(), oldStyle.getLastCell().getRow()); > assertTrue(oldStyle.isWholeColumnReference()); > >- AreaReference oldStyleNonWholeColumn = new AreaReference("A1:B23", SpreadsheetVersion.EXCEL97); >+ AreaReference oldStyleNonWholeColumn = new AreaReference("A1:B23", EXCEL97); > assertFalse(oldStyleNonWholeColumn.isWholeColumnReference()); > >- AreaReference newStyle = AreaReference.getWholeColumn(SpreadsheetVersion.EXCEL2007, "A", "B"); >+ AreaReference newStyle = AreaReference.getWholeColumn(EXCEL2007, "A", "B"); > assertEquals(0, newStyle.getFirstCell().getCol()); > assertEquals(0, newStyle.getFirstCell().getRow()); > assertEquals(1, newStyle.getLastCell().getCol()); >- assertEquals(SpreadsheetVersion.EXCEL2007.getLastRowIndex(), newStyle.getLastCell().getRow()); >+ assertEquals(EXCEL2007.getLastRowIndex(), newStyle.getLastCell().getRow()); > assertTrue(newStyle.isWholeColumnReference()); > >- AreaReference newStyleNonWholeColumn = new AreaReference("A1:B23", SpreadsheetVersion.EXCEL2007); >+ AreaReference newStyleNonWholeColumn = new AreaReference("A1:B23", EXCEL2007); > assertFalse(newStyleNonWholeColumn.isWholeColumnReference()); > } > >- public void testWholeRow() { >- AreaReference oldStyle = AreaReference.getWholeRow(SpreadsheetVersion.EXCEL97, "1", "2"); >+ public void testGetWholeRow() { >+ AreaReference oldStyle = AreaReference.getWholeRow(EXCEL97, "1", "2"); > assertEquals(0, oldStyle.getFirstCell().getCol()); > assertEquals(0, oldStyle.getFirstCell().getRow()); >- assertEquals(SpreadsheetVersion.EXCEL97.getLastColumnIndex(), oldStyle.getLastCell().getCol()); >+ assertEquals(EXCEL97.getLastColumnIndex(), oldStyle.getLastCell().getCol()); > assertEquals(1, oldStyle.getLastCell().getRow()); > >- AreaReference newStyle = AreaReference.getWholeRow(SpreadsheetVersion.EXCEL2007, "1", "2"); >+ AreaReference newStyle = AreaReference.getWholeRow(EXCEL2007, "1", "2"); > assertEquals(0, newStyle.getFirstCell().getCol()); > assertEquals(0, newStyle.getFirstCell().getRow()); >- assertEquals(SpreadsheetVersion.EXCEL2007.getLastColumnIndex(), newStyle.getLastCell().getCol()); >+ assertEquals(EXCEL2007.getLastColumnIndex(), newStyle.getLastCell().getCol()); > assertEquals(1, newStyle.getLastCell().getRow()); > } > >@@ -66,6 +208,17 @@ > @SuppressWarnings("deprecation") // deliberate test for behaviour if deprecated constructor used. > public void testFallbackToExcel97IfVersionNotSupplied() { > assertTrue(new AreaReference("A:B").isWholeColumnReference()); >- assertTrue(AreaReference.isWholeColumnReference(null, new CellReference("A$1"), new CellReference("A$" + SpreadsheetVersion.EXCEL97.getMaxRows()))); >+ assertTrue(AreaReference.isWholeColumnReference(null, new CellReference("A$1"), new CellReference("A$" + EXCEL97.getMaxRows()))); > } >+ >+ public void testGetAllReferencedCells() { >+ final CellReference[] expectedRefCells = new CellReference[]{ >+ new CellReference("A1"), new CellReference("B1"), >+ new CellReference("A2"), new CellReference("B2"), >+ new CellReference("A3"), new CellReference("B3")}; >+ // Function returns cell addresses (no absolute/relativeness) represented as row-relative column-relative CellReference objects >+ final CellReference[] actualRefCells = new AreaReference("$A$1:$B3").getAllReferencedCells(); >+ assertEquals(6, actualRefCells.length); >+ assertEquals(expectedRefCells, actualRefCells); >+ } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 58402
:
33099
|
33198