KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > stream > MockDataSetConsumer


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, 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.dataset.stream;
22
23 import com.mockobjects.ExpectationList;
24 import com.mockobjects.Verifiable;
25 import org.dbunit.dataset.Column;
26 import org.dbunit.dataset.DataSetException;
27 import org.dbunit.dataset.DefaultTableMetaData;
28 import org.dbunit.dataset.ITableMetaData;
29
30 import java.util.Arrays JavaDoc;
31
32 /**
33  * @author Manuel Laflamme
34  * @since Apr 29, 2003
35  * @version $Revision: 1.3 $
36  */

37 public class MockDataSetConsumer implements Verifiable, IDataSetConsumer
38 {
39     private static final ProducerEvent START_DATASET_EVENT =
40             new ProducerEvent("startDataSet()");
41     private static final ProducerEvent END_DATASET_EVENT =
42             new ProducerEvent("endDataSet()");
43
44     private final ExpectationList _expectedList = new ExpectationList("");
45     private String JavaDoc _actualTableName;
46
47     public void addExpectedStartDataSet() throws Exception JavaDoc
48     {
49         _expectedList.addExpected(START_DATASET_EVENT);
50     }
51
52     public void addExpectedEndDataSet() throws Exception JavaDoc
53     {
54         _expectedList.addExpected(END_DATASET_EVENT);
55     }
56
57     public void addExpectedStartTable(ITableMetaData metaData) throws Exception JavaDoc
58     {
59         _expectedList.addExpected(new StartTableEvent(metaData, false));
60     }
61
62     public void addExpectedStartTable(String JavaDoc tableName, Column[] columns) throws Exception JavaDoc
63     {
64         addExpectedStartTable(new DefaultTableMetaData(tableName, columns));
65     }
66
67     public void addExpectedStartTableIgnoreColumns(String JavaDoc tableName) throws Exception JavaDoc
68     {
69         _expectedList.addExpected(new StartTableEvent(tableName, true));
70     }
71
72     public void addExpectedEmptyTable(String JavaDoc tableName, Column[] columns) throws Exception JavaDoc
73     {
74         addExpectedStartTable(tableName, columns);
75         addExpectedEndTable(tableName);
76     }
77
78     public void addExpectedEmptyTableIgnoreColumns(String JavaDoc tableName) throws Exception JavaDoc
79     {
80         addExpectedStartTableIgnoreColumns(tableName);
81         addExpectedEndTable(tableName);
82     }
83
84     public void addExpectedEndTable(String JavaDoc tableName) throws Exception JavaDoc
85     {
86         _expectedList.addExpected(new EndTableEvent(tableName));
87     }
88
89     public void addExpectedRow(String JavaDoc tableName, Object JavaDoc[] values) throws Exception JavaDoc
90     {
91         _expectedList.addExpected(new RowEvent(tableName, values));
92     }
93
94     ////////////////////////////////////////////////////////////////////////////
95
// Verifiable interface
96

97     public void verify()
98     {
99         _expectedList.verify();
100     }
101
102     ////////////////////////////////////////////////////////////////////////////
103
// IDataSetConsumer interface
104

105     public void startDataSet() throws DataSetException
106     {
107         _expectedList.addActual(START_DATASET_EVENT);
108     }
109
110     public void endDataSet() throws DataSetException
111     {
112         _expectedList.addActual(END_DATASET_EVENT);
113     }
114
115     public void startTable(ITableMetaData metaData) throws DataSetException
116     {
117         _expectedList.addActual(new StartTableEvent(metaData, false));
118         _actualTableName = metaData.getTableName();
119     }
120
121     public void endTable() throws DataSetException
122     {
123         _expectedList.addActual(new EndTableEvent(_actualTableName));
124         _actualTableName = null;
125     }
126
127     public void row(Object JavaDoc[] values) throws DataSetException
128     {
129         _expectedList.addActual(
130                 new RowEvent(_actualTableName, values));
131     }
132
133     ////////////////////////////////////////////////////////////////////////////
134
//
135

136     private static class ProducerEvent
137     {
138         protected final String JavaDoc _name;
139
140         public ProducerEvent(String JavaDoc name)
141         {
142             _name = name;
143         }
144
145         public boolean equals(Object JavaDoc o)
146         {
147             if (this == o) return true;
148             if (!(o instanceof ProducerEvent)) return false;
149
150             final ProducerEvent item = (ProducerEvent)o;
151
152             if (!_name.equals(item._name)) return false;
153
154             return true;
155         }
156
157         public int hashCode()
158         {
159             return _name.hashCode();
160         }
161
162         public String JavaDoc toString()
163         {
164             return _name;
165         }
166     }
167
168     private static class StartTableEvent extends ProducerEvent
169     {
170         private final String JavaDoc _tableName;
171         private final Column[] _columns;
172         private final boolean _ignoreColumns;
173
174         public StartTableEvent(ITableMetaData metaData, boolean ignoreColumns) throws DataSetException
175         {
176             super("startTable()");
177             _tableName = metaData.getTableName();
178             _columns = metaData.getColumns();
179             _ignoreColumns = ignoreColumns;
180         }
181
182         public StartTableEvent(String JavaDoc tableName, boolean ignoreColumns) throws DataSetException
183         {
184             super("startTable()");
185             _tableName = tableName;
186             _columns = new Column[0];
187             _ignoreColumns = ignoreColumns;
188         }
189
190         public boolean equals(Object JavaDoc o)
191         {
192             if (this == o) return true;
193             if (!(o instanceof StartTableEvent)) return false;
194             if (!super.equals(o)) return false;
195
196             final StartTableEvent startTableItem = (StartTableEvent)o;
197
198             if (!_tableName.equals(startTableItem._tableName)) return false;
199             if (!_ignoreColumns)
200             {
201                 if (!Arrays.equals(_columns, startTableItem._columns)) return false;
202             }
203
204             return true;
205         }
206
207         public int hashCode()
208         {
209             int result = super.hashCode();
210             result = 29 * result + _tableName.hashCode();
211             return result;
212         }
213
214         public String JavaDoc toString()
215         {
216             String JavaDoc string = _name + ": table=" + _tableName;
217             if (!_ignoreColumns)
218             {
219                 string += ", columns=" + Arrays.asList(_columns);
220             }
221             return string;
222         }
223     }
224
225     private static class EndTableEvent extends ProducerEvent
226     {
227         private final String JavaDoc _tableName;
228
229         public EndTableEvent(String JavaDoc tableName)
230         {
231             super("endTable()");
232             _tableName = tableName;
233         }
234
235         public boolean equals(Object JavaDoc o)
236         {
237             if (this == o) return true;
238             if (!(o instanceof EndTableEvent)) return false;
239             if (!super.equals(o)) return false;
240
241             final EndTableEvent endTableItem = (EndTableEvent)o;
242
243             if (!_tableName.equals(endTableItem._tableName)) return false;
244
245             return true;
246         }
247
248         public int hashCode()
249         {
250             int result = super.hashCode();
251             result = 29 * result + _tableName.hashCode();
252             return result;
253         }
254
255         public String JavaDoc toString()
256         {
257             return _name + ": table=" + _tableName;
258         }
259     }
260
261     private static class RowEvent extends ProducerEvent
262     {
263         private final String JavaDoc _tableName;
264         private final Object JavaDoc[] _values;
265
266         public RowEvent(String JavaDoc tableName, Object JavaDoc[] values)
267         {
268             super("row()");
269             _tableName = tableName;
270             _values = values;
271         }
272
273         public boolean equals(Object JavaDoc o)
274         {
275             if (this == o) return true;
276             if (!(o instanceof RowEvent)) return false;
277             if (!super.equals(o)) return false;
278
279             final RowEvent rowItem = (RowEvent)o;
280
281             if (!_tableName.equals(rowItem._tableName)) return false;
282 // Probably incorrect - comparing Object[] arrays with Arrays.equals
283
if (!Arrays.equals(_values, rowItem._values)) return false;
284
285             return true;
286         }
287
288         public int hashCode()
289         {
290             int result = super.hashCode();
291             result = 29 * result + _tableName.hashCode();
292             return result;
293         }
294
295         public String JavaDoc toString()
296         {
297             return _name + ": table=" + _tableName + ", values=" + Arrays.asList(_values);
298         }
299
300     }
301 }
302
Popular Tags