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 * Make the dbUnit dataset ({@link org.dbunit.dataset.IDataSet}) from the
136 * input stream of a dbUnit data file. The type of dbUnit dataset created is
137 * delegated to the implementing subclass.
138 *
139 * @param url
140 * The dbUnit data file url.
141 * @return dbUnit dataset of the corresponding input file type.
142 * @throws DataSetException
143 * On data errors.
144 * @throws IOException
145 * On file errors.
146 * @since 2.4.8
147 */
148 protected abstract IDataSet loadDataSet(URL url) throws DataSetException,
149 IOException;
150
151 /**
152 * Add the replacements in the maps (objects and substrings) to the
153 * specified dataset.
154 *
155 * @param ds
156 * The dataset to wrap with a <code>ReplacementDataSet</code> and
157 * process replacement tokens on.
158 * @return The specified dataset decorated with
159 * <code>ReplacementDataSet</code> and processed with the tokens in
160 * the replacement maps.
161 * @since 2.4.8
162 */
163 protected ReplacementDataSet processReplacementTokens(IDataSet ds) {
164 ReplacementDataSet rds =
165 new ReplacementDataSet(ds, replacementObjects,
166 replacementSubstrings);
167
168 return rds;
169 }
170
171 /**
172 * {@inheritDoc}
173 */
174 public void addReplacementObjects(Map ro) {
175 this.replacementObjects.putAll(ro);
176 }
177
178 /**
179 * {@inheritDoc}
180 */
181 public void addReplacementSubstrings(Map rs) {
182 this.replacementSubstrings.putAll(rs);
183 }
184
185 /**
186 * {@inheritDoc}
187 */
188 public void removeAllReplacementObjects() {
189 this.replacementObjects.clear();
190 }
191
192 /**
193 * {@inheritDoc}
194 */
195 public void removeAllReplacementSubstrings() {
196 this.replacementSubstrings.clear();
197 }
198 }