001: /* ExcelDataSetLoader.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License version 2.1 as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, visit
017: * http://www.opensource.org/licenses/lgpl-license.php
018: */
019: package org.ddsteps.dataset.excel;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import jxl.Workbook;
025:
026: import org.apache.commons.lang.ClassUtils;
027: import org.apache.commons.lang.Validate;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.ddsteps.dataset.DataSet;
031: import org.ddsteps.dataset.DataSetLoader;
032: import org.springframework.core.io.ClassPathResource;
033:
034: /**
035: * DataSet loader for Excel files.
036: *
037: * @author Adam
038: * @version $Id: ExcelDataSetLoader.java,v 1.1 2005/08/25 08:32:37 adamskogman
039: * Exp $
040: */
041: public class ExcelDataSetLoader implements DataSetLoader {
042:
043: /**
044: * Logger for this class
045: */
046: private static final Log logger = LogFactory
047: .getLog(ExcelDataSetLoader.class);
048:
049: /**
050: * Default constructor.
051: */
052: public ExcelDataSetLoader() {
053: }
054:
055: /**
056: * Load an excel file with the same name as this clazz parameter.
057: *
058: * @param clazz
059: * The class. The classloader of that class will be used to load
060: * the file.
061: * @return Null if not found
062: * @throws IllegalArgumentException
063: * If class is null
064: */
065: public DataSet loadDataSet(Class clazz) {
066:
067: Validate.notNull(clazz, "Argument clazz must not be null");
068:
069: String shortName = ClassUtils.getShortClassName(clazz);
070: String fileName = shortName + ".xls";
071:
072: InputStream stream;
073: try {
074: ClassPathResource resource = new ClassPathResource(
075: fileName, clazz);
076: stream = resource.getInputStream();
077: } catch (IOException e) {
078: logger.debug("IOException getting Excel file.", e);
079: // Not finding the file is ok.
080: return null;
081: }
082:
083: try {
084: Workbook workbook = Workbook.getWorkbook(stream);
085:
086: if (logger.isInfoEnabled()) {
087: logger.info("loadDataSet(clazz = " + clazz
088: + ") - Excel file loaded.");
089: }
090:
091: return new ExcelDataSetAdapter(workbook);
092:
093: } catch (Exception e) {
094: logger.warn("loadDataSet(clazz = " + clazz
095: + ") - Error loading Excel file.");
096: throw new ExcelDataException(
097: "Exception opening excel file for class "
098: + clazz.getName(), e);
099: }
100:
101: }
102:
103: }
|