Skip to content

Commit

Permalink
Closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Chédru committed Mar 11, 2016
1 parent e0788e0 commit 55501ae
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ So, fastexcel has been created to offer an alternative with the following key po
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>fastexcel</artifactId>
<version>0.8.2</version>
<version>0.8.3</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14-beta1</version>
<version>3.14</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/dhatim/fastexcel/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public class Range {
*/
Range(Worksheet worksheet, int top, int left, int bottom, int right) {
this.worksheet = Objects.requireNonNull(worksheet);

// Check limits
if (top < 0 || top >= Worksheet.MAX_ROWS || bottom < 0 || bottom >= Worksheet.MAX_ROWS) {
throw new IllegalArgumentException();
}
if (left < 0 || left >= Worksheet.MAX_COLS || right < 0 || right >= Worksheet.MAX_COLS) {
throw new IllegalArgumentException();
}
this.top = top <= bottom ? top : bottom;
this.left = left <= right ? left : right;
this.bottom = bottom >= top ? bottom : top;
Expand Down Expand Up @@ -197,7 +205,7 @@ public boolean contains(int r, int c) {
*
* @param fill Fill pattern.
*/
public void shadeAlternateRows(Fill fill) {
void shadeAlternateRows(Fill fill) {
worksheet.shadeAlternateRows(this, fill);
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/dhatim/fastexcel/Worksheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
*/
public class Worksheet {

/**
* Maximum number of rows in Excel.
*/
public static final int MAX_ROWS = 1_048_576;

/**
* Maximum number of columns in Excel.
*/
public static final int MAX_COLS = 16_384;

private final Workbook workbook;
private final String name;
/**
Expand Down Expand Up @@ -88,6 +98,11 @@ public Workbook getWorkbook() {
* @return An existing or newly created cell.
*/
Cell cell(int r, int c) {
// Check limits
if (r < 0 || r >= MAX_ROWS || c < 0 || c >= MAX_COLS) {
throw new IllegalArgumentException();
}

// Add null for missing rows.
while (r >= rows.size()) {
rows.add(null);
Expand Down
32 changes: 31 additions & 1 deletion src/test/java/org/dhatim/fastexcel/Correctness.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
Expand Down Expand Up @@ -47,6 +46,7 @@ private byte[] writeWorkbook(Consumer<Workbook> consumer) throws IOException {
public void colToName() throws Exception {
assertEquals("AA", Range.colToString(26));
assertEquals("AAA", Range.colToString(702));
assertEquals("XFD", Range.colToString(Worksheet.MAX_COLS - 1));
}

@Test(expected = IllegalArgumentException.class)
Expand All @@ -65,6 +65,36 @@ public void singleEmptyWorksheet() throws Exception {
byte[] data = writeWorkbook(wb -> wb.newWorksheet("Worksheet 1"));
}

@Test
public void checkMaxRows() throws Exception {
byte[] data = writeWorkbook(wb -> wb.newWorksheet("Worksheet 1").value(Worksheet.MAX_ROWS - 1, 0, "test"));
}

@Test
public void checkMaxCols() throws Exception {
byte[] data = writeWorkbook(wb -> wb.newWorksheet("Worksheet 1").value(0, Worksheet.MAX_COLS - 1, "test"));
}

@Test(expected = IllegalArgumentException.class)
public void exceedMaxRows() throws Exception {
byte[] data = writeWorkbook(wb -> wb.newWorksheet("Worksheet 1").value(Worksheet.MAX_ROWS, 0, "test"));
}

@Test(expected = IllegalArgumentException.class)
public void negativeRow() throws Exception {
byte[] data = writeWorkbook(wb -> wb.newWorksheet("Worksheet 1").value(-1, 0, "test"));
}

@Test(expected = IllegalArgumentException.class)
public void exceedMaxCols() throws Exception {
byte[] data = writeWorkbook(wb -> wb.newWorksheet("Worksheet 1").value(0, Worksheet.MAX_COLS, "test"));
}

@Test(expected = IllegalArgumentException.class)
public void negativeCol() throws Exception {
byte[] data = writeWorkbook(wb -> wb.newWorksheet("Worksheet 1").value(0, -1, "test"));
}

@Test
public void singleWorksheet() throws Exception {
String sheetName = "Worksheet 1";
Expand Down

0 comments on commit 55501ae

Please sign in to comment.