View Javadoc
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  
23  import java.io.IOException;
24  import java.net.URL;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.dbunit.DatabaseUnitRuntimeException;
29  import org.dbunit.dataset.DataSetException;
30  import org.dbunit.dataset.DefaultDataSet;
31  import org.dbunit.dataset.IDataSet;
32  import org.dbunit.dataset.ReplacementDataSet;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * Base class with common implementation for dbUnit data file loaders.
38   * 
39   * @author Jeff Jensen jeffjensen AT users.sourceforge.net
40   * @author Last changed by: $Author$
41   * @version $Revision$ $Date$
42   * @since 2.4.8
43   */
44  public abstract class AbstractDataFileLoader implements DataFileLoader {
45      private final Logger LOG =
46              LoggerFactory.getLogger(AbstractDataFileLoader.class);
47  
48      private Map replacementObjects;
49      private Map replacementSubstrings;
50  
51      /** Create new instance. */
52      public AbstractDataFileLoader() {
53          this(new HashMap(), new HashMap());
54      }
55  
56      /**
57       * Create new instance with replacement objects.
58       * 
59       * @param replacementObjects
60       *            The replacement objects for use with
61       *            {@link org.dbunit.dataset.ReplacementDataSet}.
62       */
63      public AbstractDataFileLoader(Map ro) {
64          this(ro, new HashMap());
65      }
66  
67      /**
68       * Create new instance with replacement objects and replacement substrings.
69       * 
70       * @param ro
71       *            The replacement objects for use with
72       *            {@link org.dbunit.dataset.ReplacementDataSet}.
73       * @param rs
74       *            The replacement substrings for use with
75       *            {@link org.dbunit.dataset.ReplacementDataSet}.
76       */
77      public AbstractDataFileLoader(Map ro, Map rs) {
78          if (ro == null) {
79              throw new IllegalArgumentException(
80                      "Replacement object map is null.");
81          }
82  
83          if (rs == null) {
84              throw new IllegalArgumentException(
85                      "Replacement substrings map is null.");
86          }
87  
88          this.replacementObjects = ro;
89          this.replacementSubstrings = rs;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      public IDataSet load(String filename) throws DatabaseUnitRuntimeException {
96          IDataSet ds = new DefaultDataSet();
97  
98          LOG.debug("load: processing file={}", filename);
99  
100         if (filename == null || "".equals(filename)) {
101             final String msg =
102                     "load: filename is null or empty string,"
103                             + " using DefaultDataSet()";
104             LOG.debug(msg);
105         } else {
106             URL url = this.getClass().getResource(filename);
107 
108             if (url == null) {
109                 final String msg = "Could not find file named=" + filename;
110                 throw new DatabaseUnitRuntimeException(msg);
111             }
112 
113             try {
114                 ds = loadDataSet(url);
115                 ds = processReplacementTokens(ds);
116             } catch (DataSetException e) {
117                 final String msg =
118                         "DataSetException occurred loading data set file name='"
119                                 + filename + "', msg='"
120                                 + e.getLocalizedMessage() + "'";
121                 throw new DatabaseUnitRuntimeException(msg, e);
122             } catch (IOException e) {
123                 final String msg =
124                         "IOException occurred loading data set file name='"
125                                 + filename + '\'' + ", msg='"
126                                 + e.getLocalizedMessage() + "'";
127                 throw new DatabaseUnitRuntimeException(msg, e);
128             }
129         }
130 
131         return ds;
132     }
133 
134     /** 
135      * Add the replacements in the maps (objects and substrings) to the
136      * specified dataset.
137      * 
138      * @param ds
139      *            The dataset to wrap with a <code>ReplacementDataSet</code> and
140      *            process replacement tokens on.
141      * @return The specified dataset decorated with
142      *         <code>ReplacementDataSet</code> and processed with the tokens in
143      *         the replacement maps.
144      * @since 2.4.8
145      */
146     protected ReplacementDataSet processReplacementTokens(IDataSet ds) {
147         ReplacementDataSet rds =
148                 new ReplacementDataSet(ds, replacementObjects,
149                         replacementSubstrings);
150 
151         return rds;
152     }
153 
154     /**
155      * {@inheritDoc}
156      */
157     public void addReplacementObjects(Map ro) {
158         this.replacementObjects.putAll(ro);
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     public void addReplacementSubstrings(Map rs) {
165         this.replacementSubstrings.putAll(rs);
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     public void removeAllReplacementObjects() {
172         this.replacementObjects.clear();
173     }
174 
175     /**
176      * {@inheritDoc}
177      */
178     public void removeAllReplacementSubstrings() {
179         this.replacementSubstrings.clear();
180     }
181 }