001: // Copyright © 2006-2007 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.plugins.exceltest;
003:
004: import java.io.InputStream;
005: import java.util.WeakHashMap;
006: import java.util.Map;
007:
008: import org.apache.commons.io.IOUtils;
009: import org.apache.log4j.Logger;
010: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
011: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
012:
013: import com.canoo.webtest.engine.MimeMap;
014: import com.canoo.webtest.engine.StepExecutionException;
015: import com.canoo.webtest.steps.Step;
016: import com.gargoylesoftware.htmlunit.Page;
017:
018: /**
019: * Base class for Excel steps.
020: *
021: * @author Rob Nielsen
022: * @author Paul King
023: */
024: public abstract class AbstractExcelStep extends Step {
025: private static final Logger LOG = Logger
026: .getLogger(AbstractExcelStep.class);
027:
028: private static final Map sMapWorkbooks = new WeakHashMap(); // needed until we plug a "ExcelPage" in htmlunit
029: protected static final String KEY_CURRENT_SHEET = AbstractExcelStep.class
030: .getName()
031: + "#CurrentSheet";
032:
033: protected AbstractExcelStep() {
034: }
035:
036: /**
037: * @throws StepExecutionException
038: * if pdf analyzer cannot be initialized correctly
039: */
040: protected HSSFWorkbook getExcelWorkbook() {
041: final Page currentPage = getContext().getCurrentResponse();
042: HSSFWorkbook workbook = (HSSFWorkbook) sMapWorkbooks
043: .get(currentPage);
044: if (workbook == null) {
045: workbook = createWorkbook(currentPage);
046: sMapWorkbooks.put(currentPage, workbook); // weak map, analyser garbage collected together with the page
047: }
048: return workbook;
049: }
050:
051: /**
052: * Creates an HSSFWorkbook for the page. This method should not be used directly,
053: * use {@link #getExcelWorkbook()} instead.
054: * @param currentPage the page containing the Excel document
055: * @return the analyzer
056: */
057: private HSSFWorkbook createWorkbook(final Page currentPage) {
058: InputStream is = null;
059: try {
060: getContext().put(KEY_CURRENT_SHEET, null);
061: is = currentPage.getWebResponse().getContentAsStream();
062:
063: final POIFSFileSystem excelFile = new RetryWithCapsPOIFSFileSystem(
064: is);
065: return new HSSFWorkbook(excelFile);
066: } catch (final Exception e) {
067: final String message = "Could not open Excel file.";
068: LOG.debug(message, e);
069: throw new StepExecutionException(message, this , e);
070: } finally {
071: IOUtils.closeQuietly(is);
072: }
073: }
074:
075: protected void verifyParameters() {
076: super .verifyParameters();
077: nullResponseCheck();
078: final Page currentResponse = getContext().getCurrentResponse();
079:
080: String contentType = currentResponse.getWebResponse()
081: .getContentType();
082: if (!MimeMap.EXCEL_MIME_TYPE.equals(contentType)) {
083: throw new StepExecutionException(
084: "File does not have correct content type (not a '.xls' file?): "
085: + currentResponse.getWebResponse()
086: .getContentType(), this );
087: }
088: }
089:
090: protected boolean verifyStrings(final String expectedValue,
091: final String actualValue) {
092: boolean regex = false;
093: String newExpectedValue = expectedValue;
094: if (expectedValue != null
095: && expectedValue.length() >= 2
096: && expectedValue.charAt(0) == '/'
097: && expectedValue.charAt(expectedValue.length() - 1) == '/') {
098: regex = true;
099: newExpectedValue = expectedValue.substring(1, expectedValue
100: .length() - 1);
101: }
102: return getVerifier(regex).verifyStrings(newExpectedValue,
103: actualValue);
104: }
105: }
|