001: // Copyright © 2006-2007 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.plugins.exceltest;
003:
004: import com.canoo.webtest.self.TestBlock;
005: import com.canoo.webtest.self.ThrowAssert;
006: import com.canoo.webtest.steps.Step;
007: import org.apache.poi.hssf.usermodel.HSSFCell;
008: import org.apache.poi.hssf.usermodel.HSSFCellStyle;
009: import org.apache.poi.hssf.usermodel.HSSFFont;
010:
011: /**
012: * Test class for {@link ExcelVerifyCellStyle}.<p>
013: *
014: * @author Rob Nielsen
015: */
016: public class ExcelVerifyCellStyleTest extends BaseExcelStepTestCase {
017: protected Step createStep() {
018: return new ExcelVerifyCellStyle();
019: }
020:
021: public void testVerifyWithEmpty() throws Exception {
022: final ExcelVerifyCellStyle step = (ExcelVerifyCellStyle) getStep();
023: step.setCell("B3");
024: executeStep(step);
025: }
026:
027: public void testVerifyStyleOnEmptyCell() throws Exception {
028: final ExcelVerifyCellStyle step = (ExcelVerifyCellStyle) getStep();
029: step.setCell("A1");
030: step.setFormat("XXX");
031: assertErrorOnExecute(step, "can't find cell",
032: "Can't find cell for A1");
033: }
034:
035: public void testInvalidSideOnBorder() throws Exception {
036: final ExcelVerifyCellStyle step = (ExcelVerifyCellStyle) getStep();
037: step.setCell("B3");
038: step.setBorder("blah:blah");
039: assertErrorOnExecute(step, "border unknown",
040: "Border side 'blah' unknown. Specify one of top, down, left right.");
041: }
042:
043: public void testFailedAssertionInBorderColor() throws Exception {
044: final ExcelVerifyCellStyle step = (ExcelVerifyCellStyle) getStep();
045: step.setBorderColor("left:black;top:red");
046: checkFailedAssertion("D4", "topBorderColor", "red", "black");
047: step.setBorderColor("black;right:blue");
048: checkFailedAssertion("D4", "rightBorderColor", "blue", "black");
049: step.setBorderColor("bottom:black;bottom:#00ff00");
050: checkFailedAssertion("D4", "bottomBorderColor", "bright green",
051: "black");
052: step.setBorderColor("left:xxx");
053: checkFailedAssertion("D4", "leftBorderColor", "xxx", "black");
054: }
055:
056: public void testFailedAssertionInFillColor() throws Exception {
057: ((ExcelVerifyCellStyle) getStep()).setFillColor("blue");
058: checkFailedAssertion("F20", "fillColor", "blue", "dark red");
059: }
060:
061: public void testFailedAssertionInFillBackgroundColor()
062: throws Exception {
063: ((ExcelVerifyCellStyle) getStep())
064: .setFillBackgroundColor("#ff0000");
065: checkFailedAssertion("G22", "fillBackgroundColor", "red",
066: "orange");
067: }
068:
069: public void testFailedAssertionInFillPattern() throws Exception {
070: ((ExcelVerifyCellStyle) getStep())
071: .setFillPattern("horizontal strip");
072: checkFailedAssertion("J16", "fillPattern", "horizontal strip",
073: "horizontal stripe");
074: }
075:
076: public void testFailedAssertionInFontStyle() throws Exception {
077: ((ExcelVerifyCellStyle) getStep())
078: .setFontStyle("bold underline strikethrough");
079: checkFailedAssertion("L14", "fontStyle",
080: "bold strikethrough underline",
081: "bold italic strikethrough underline");
082: }
083:
084: public void testFailedAssertionInFontName() throws Exception {
085: ((ExcelVerifyCellStyle) getStep()).setFontName("arial");
086: checkFailedAssertion("L20", "fontName", "arial", "Arial");
087: }
088:
089: public void testFailedAssertionInFontSize() throws Exception {
090: ((ExcelVerifyCellStyle) getStep()).setFontSize("16.0");
091: checkFailedAssertion("L31", "fontSize", "16.0", "16");
092: }
093:
094: public void testFailedAssertionInAlignment() throws Exception {
095: ((ExcelVerifyCellStyle) getStep()).setAlign("left");
096: checkFailedAssertion("M6", "align", "left", "center");
097: }
098:
099: public void testFailedAssertionInVerticalAlignment()
100: throws Exception {
101: ((ExcelVerifyCellStyle) getStep()).setValign("left");
102: checkFailedAssertion("M12", "valign", "left", "top");
103: }
104:
105: public void testFailedAssertionInType() throws Exception {
106: ((ExcelVerifyCellStyle) getStep()).setType("blah");
107: checkFailedAssertion("N23", "type", "blah", "boolean");
108: }
109:
110: public void testFailedAssertionInFormat() throws Exception {
111: ((ExcelVerifyCellStyle) getStep()).setFormat("xxx");
112: checkFailedAssertion("N31", "format", "xxx", "m/d/yy");
113: }
114:
115: public void testFailedAssertionInLocked() throws Exception {
116: ((ExcelVerifyCellStyle) getStep()).setLocked("xxx");
117: checkFailedAssertion("O4", "locked", "xxx", "true");
118: }
119:
120: public void testFailedAssertionInWrapped() throws Exception {
121: ((ExcelVerifyCellStyle) getStep()).setWrap("xxx");
122: checkFailedAssertion("O8", "wrap", "xxx", "false");
123: }
124:
125: private void checkFailedAssertion(final String cell,
126: final String property, final String expected,
127: final String actual) {
128: final ExcelVerifyCellStyle step = (ExcelVerifyCellStyle) getStep();
129: step.setSheetIndex("1");
130: step.setCell(cell);
131: assertFailOnExecute(step, "border color",
132: "Wrong cell style found for property '" + property
133: + "' in cell " + cell + ". Expected value \""
134: + expected + "\" but got \"" + actual + "\"");
135: }
136:
137: public void testErrorCellType() throws Exception {
138: final ExcelVerifyCellStyle step = (ExcelVerifyCellStyle) getStep();
139: step.setCell("B2");
140: step.getExcelCell().setCellErrorValue((byte) 42);
141: step.getExcelCell().setCellType(HSSFCell.CELL_TYPE_ERROR);
142: step.setType("error");
143: executeStep(step);
144: }
145:
146: public void testUnknownProperties() throws Exception {
147: final ExcelVerifyCellStyle step = (ExcelVerifyCellStyle) getStep();
148: step.setCell("B2");
149: final HSSFCellStyle cellStyle = step.getExcelCell()
150: .getCellStyle();
151: cellStyle.setBorderTop(Short.MAX_VALUE);
152: cellStyle.setAlignment(Short.MAX_VALUE);
153: cellStyle.setVerticalAlignment(Short.MAX_VALUE);
154: cellStyle.setFillPattern(Short.MAX_VALUE);
155: final HSSFFont fontAt = step.getExcelWorkbook().getFontAt(
156: cellStyle.getFontIndex());
157: fontAt.setUnderline(Byte.MAX_VALUE);
158: step.setBorder("top:unknown");
159: step.setAlign("unknown");
160: step.setValign("unknown");
161: step.setFillPattern("unknown");
162: step.setFontStyle("bold underline-unknown");
163: executeStep(step);
164: }
165:
166: public void testUnknownCellType() {
167: assertEquals("unknown", ExcelCellUtils.getCellType(6));
168: }
169:
170: public void testInvalidSides() throws Exception {
171: final ExcelVerifyCellStyle step = (ExcelVerifyCellStyle) getStep();
172: step.setCell("A1");
173: ThrowAssert.assertThrows(IllegalArgumentException.class,
174: "Invalid side: 4", new TestBlock() {
175: public void call() throws Exception {
176: step.getBorder(null, 4);
177: }
178: });
179: ThrowAssert.assertThrows(IllegalArgumentException.class,
180: "Invalid side: 4", new TestBlock() {
181: public void call() throws Exception {
182: step.getBorderColor(null, 4);
183: }
184: });
185: ThrowAssert.assertThrows(IllegalArgumentException.class,
186: "Invalid side: 4", new TestBlock() {
187: public void call() throws Exception {
188: step.getAdjacentCell(4);
189: }
190: });
191: }
192: }
|