package org.apache.poi.hssf.util;
-import junit.framework.TestCase;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.CellReference.NameType;
* Tests for the HSSF and SS versions of CellReference.
* See also {@link org.apache.poi.ss.util.TestCellReference}
*/
-public final class TestCellReference extends TestCase {
+public final class TestCellReference {
+ @Test
public void testColNumConversion() {
assertEquals(0, CellReference.convertColStringToIndex("A"));
assertEquals(1, CellReference.convertColStringToIndex("B"));
} catch (IllegalArgumentException e) {}
}
+ @Test
public void testAbsRef1(){
CellReference cf = new CellReference("$B$5");
confirmCell(cf, null, 4, 1, true, true, "$B$5");
}
+ @Test
public void testAbsRef2(){
CellReference cf = new CellReference(4,1,true,true);
confirmCell(cf, null, 4, 1, true, true, "$B$5");
}
+ @Test
public void testAbsRef3(){
CellReference cf = new CellReference("B$5");
confirmCell(cf, null, 4, 1, true, false, "B$5");
}
+ @Test
public void testAbsRef4(){
CellReference cf = new CellReference(4,1,true,false);
confirmCell(cf, null, 4, 1, true, false, "B$5");
}
+ @Test
public void testAbsRef5(){
CellReference cf = new CellReference("$B5");
confirmCell(cf, null, 4, 1, false, true, "$B5");
}
+ @Test
public void testAbsRef6(){
CellReference cf = new CellReference(4,1,false,true);
confirmCell(cf, null, 4, 1, false, true, "$B5");
}
+ @Test
public void testAbsRef7(){
CellReference cf = new CellReference("B5");
confirmCell(cf, null, 4, 1, false, false, "B5");
}
+ @Test
public void testAbsRef8(){
CellReference cf = new CellReference(4,1,false,false);
confirmCell(cf, null, 4, 1, false, false, "B5");
}
+ @Test
public void testSpecialSheetNames() {
CellReference cf;
cf = new CellReference("'profit + loss'!A1");
assertEquals("text is wrong", expText, cf.formatAsString());
}
+ @Test
public void testClassifyCellReference() {
confirmNameType("a1", NameType.CELL);
confirmNameType("pfy1", NameType.NAMED_RANGE);
confirmNameType("A1.", NameType.NAMED_RANGE);
}
+ @Test
public void testClassificationOfRowReferences(){
confirmNameType("10", NameType.ROW);
confirmNameType("$10", NameType.ROW);
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.util.CellReference;
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
/**
* Tests that the common CellReference works as we need it to.
* Note - some additional testing is also done in the HSSF class,
* {@link org.apache.poi.hssf.util.TestCellReference}
*/
-public final class TestCellReference extends TestCase {
+public final class TestCellReference {
+ @Test
public void testConstructors() {
CellReference cellReference;
final String sheet = "Sheet1";
assertEquals("Sheet1!A$1", cellReference.formatAsString());
}
+ @Test
public void testFormatAsString() {
CellReference cellReference;
assertEquals("'Sheet 1'!A$1", cellReference.formatAsString());
}
+ @Test
public void testGetCellRefParts() {
CellReference cellReference;
String[] parts;
assertEquals("AABC", parts[2]);
}
+ @Test
public void testGetColNumFromRef() {
String cellRef = "A1";
CellReference cellReference = new CellReference(cellRef);
assertEquals(54, cellReference.getCol());
}
+ @Test
public void testGetRowNumFromRef() {
String cellRef = "A1";
CellReference cellReference = new CellReference(cellRef);
assertEquals(120, cellReference.getRow());
}
+ @Test
public void testConvertNumToColString() {
short col = 702;
String collRef = new CellReference(0, col).formatAsString();
assertEquals("CBA1", collRef4);
}
+ @Test
public void testBadRowNumber() {
SpreadsheetVersion v97 = SpreadsheetVersion.EXCEL97;
SpreadsheetVersion v2007 = SpreadsheetVersion.EXCEL2007;
confirmCrInRange(false, "XFD", "1048577", v2007);
confirmCrInRange(false, "XFE", "1048576", v2007);
- if (CellReference.cellReferenceIsWithinRange("B", "0", v97)) {
- throw new AssertionFailedError("Identified bug 47312a");
- }
+ assertFalse("Identified bug 47312a", CellReference.cellReferenceIsWithinRange("B", "0", v97));
confirmCrInRange(false, "A", "0", v97);
confirmCrInRange(false, "A", "0", v2007);
}
+ @Test
public void testInvalidReference() {
try {
new CellReference("Sheet1!#REF!");
fail("Shouldn't be able to create a #REF! refence");
- } catch(IllegalArgumentException e) {}
+ } catch(IllegalArgumentException expected) {}
try {
new CellReference("'MySheetName'!#REF!");
fail("Shouldn't be able to create a #REF! refence");
- } catch(IllegalArgumentException e) {}
+ } catch(IllegalArgumentException expected) {}
try {
new CellReference("#REF!");
fail("Shouldn't be able to create a #REF! refence");
- } catch(IllegalArgumentException e) {}
+ } catch(IllegalArgumentException expected) {}
}
private static void confirmCrInRange(boolean expResult, String colStr, String rowStr,
if (expResult == CellReference.cellReferenceIsWithinRange(colStr, rowStr, sv)) {
return;
}
- throw new AssertionFailedError("expected (c='" + colStr + "', r='" + rowStr + "' to be "
+ fail("expected (c='" + colStr + "', r='" + rowStr + "' to be "
+ (expResult ? "within" : "out of") + " bounds for version " + sv.name());
}
+ @Test
public void testConvertColStringToIndex() {
assertEquals(0, CellReference.convertColStringToIndex("A"));
assertEquals(1, CellReference.convertColStringToIndex("B"));
try {
CellReference.convertColStringToIndex("A$");
fail("Should throw exception here");
- } catch (IllegalArgumentException e) {
- assertTrue(e.getMessage().contains("A$"));
+ } catch (IllegalArgumentException expected) {
+ assertTrue(expected.getMessage().contains("A$"));
}
}
+ @Test
public void testConvertNumColColString() {
assertEquals("A", CellReference.convertNumToColString(0));
assertEquals("AV", CellReference.convertNumToColString(47));