AbstractDataFileLoader.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2008, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */
  21. package org.dbunit.util.fileloader;

  22. import java.io.IOException;
  23. import java.net.URL;
  24. import java.util.HashMap;
  25. import java.util.Map;

  26. import org.dbunit.DatabaseUnitRuntimeException;
  27. import org.dbunit.dataset.DataSetException;
  28. import org.dbunit.dataset.DefaultDataSet;
  29. import org.dbunit.dataset.IDataSet;
  30. import org.dbunit.dataset.ReplacementDataSet;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;

  33. /**
  34.  * Base class with common implementation for dbUnit data file loaders.
  35.  *
  36.  * @author Jeff Jensen jeffjensen AT users.sourceforge.net
  37.  * @author Last changed by: $Author$
  38.  * @version $Revision$ $Date$
  39.  * @since 2.4.8
  40.  */
  41. public abstract class AbstractDataFileLoader implements DataFileLoader {
  42.     private final Logger LOG =
  43.             LoggerFactory.getLogger(AbstractDataFileLoader.class);

  44.     private Map replacementObjects;
  45.     private Map replacementSubstrings;

  46.     /** Create new instance. */
  47.     public AbstractDataFileLoader() {
  48.         this(new HashMap(), new HashMap());
  49.     }

  50.     /**
  51.      * Create new instance with replacement objects.
  52.      *
  53.      * @param replacementObjects
  54.      *            The replacement objects for use with
  55.      *            {@link org.dbunit.dataset.ReplacementDataSet}.
  56.      */
  57.     public AbstractDataFileLoader(Map ro) {
  58.         this(ro, new HashMap());
  59.     }

  60.     /**
  61.      * Create new instance with replacement objects and replacement substrings.
  62.      *
  63.      * @param ro
  64.      *            The replacement objects for use with
  65.      *            {@link org.dbunit.dataset.ReplacementDataSet}.
  66.      * @param rs
  67.      *            The replacement substrings for use with
  68.      *            {@link org.dbunit.dataset.ReplacementDataSet}.
  69.      */
  70.     public AbstractDataFileLoader(Map ro, Map rs) {
  71.         if (ro == null) {
  72.             throw new IllegalArgumentException(
  73.                     "Replacement object map is null.");
  74.         }

  75.         if (rs == null) {
  76.             throw new IllegalArgumentException(
  77.                     "Replacement substrings map is null.");
  78.         }

  79.         this.replacementObjects = ro;
  80.         this.replacementSubstrings = rs;
  81.     }

  82.     /**
  83.      * {@inheritDoc}
  84.      */
  85.     public IDataSet load(String filename) throws DatabaseUnitRuntimeException {
  86.         IDataSet ds = new DefaultDataSet();

  87.         LOG.debug("load: processing file={}", filename);

  88.         if (filename == null || "".equals(filename)) {
  89.             final String msg =
  90.                     "load: filename is null or empty string,"
  91.                             + " using DefaultDataSet()";
  92.             LOG.debug(msg);
  93.         } else {
  94.             URL url = this.getClass().getResource(filename);

  95.             if (url == null) {
  96.                 final String msg = "Could not find file named=" + filename;
  97.                 throw new DatabaseUnitRuntimeException(msg);
  98.             }

  99.             try {
  100.                 ds = loadDataSet(url);
  101.                 ds = processReplacementTokens(ds);
  102.             } catch (DataSetException e) {
  103.                 final String msg =
  104.                         "DataSetException occurred loading data set file name='"
  105.                                 + filename + "', msg='"
  106.                                 + e.getLocalizedMessage() + "'";
  107.                 throw new DatabaseUnitRuntimeException(msg, e);
  108.             } catch (IOException e) {
  109.                 final String msg =
  110.                         "IOException occurred loading data set file name='"
  111.                                 + filename + '\'' + ", msg='"
  112.                                 + e.getLocalizedMessage() + "'";
  113.                 throw new DatabaseUnitRuntimeException(msg, e);
  114.             }
  115.         }

  116.         return ds;
  117.     }

  118.     /**
  119.      * Add the replacements in the maps (objects and substrings) to the
  120.      * specified dataset.
  121.      *
  122.      * @param ds
  123.      *            The dataset to wrap with a <code>ReplacementDataSet</code> and
  124.      *            process replacement tokens on.
  125.      * @return The specified dataset decorated with
  126.      *         <code>ReplacementDataSet</code> and processed with the tokens in
  127.      *         the replacement maps.
  128.      * @since 2.4.8
  129.      */
  130.     protected ReplacementDataSet processReplacementTokens(IDataSet ds) {
  131.         ReplacementDataSet rds =
  132.                 new ReplacementDataSet(ds, replacementObjects,
  133.                         replacementSubstrings);

  134.         return rds;
  135.     }

  136.     /**
  137.      * {@inheritDoc}
  138.      */
  139.     public void addReplacementObjects(Map ro) {
  140.         this.replacementObjects.putAll(ro);
  141.     }

  142.     /**
  143.      * {@inheritDoc}
  144.      */
  145.     public void addReplacementSubstrings(Map rs) {
  146.         this.replacementSubstrings.putAll(rs);
  147.     }

  148.     /**
  149.      * {@inheritDoc}
  150.      */
  151.     public void removeAllReplacementObjects() {
  152.         this.replacementObjects.clear();
  153.     }

  154.     /**
  155.      * {@inheritDoc}
  156.      */
  157.     public void removeAllReplacementSubstrings() {
  158.         this.replacementSubstrings.clear();
  159.     }
  160. }