KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
24 import org.dbunit.dataset.Column;
25 import org.dbunit.dataset.datatype.DataType;
26
27 /**
28  * @author Manuel Laflamme
29  * @since Apr 29, 2003
30  * @version $Revision: 1.3 $
31  */

32 public abstract class AbstractProducerTest extends TestCase
33 {
34     private static final String JavaDoc[] TABLE_NAMES = {
35         "DUPLICATE_TABLE",
36         "SECOND_TABLE",
37         "TEST_TABLE",
38         "DUPLICATE_TABLE",
39         "NOT_NULL_TABLE",
40         "EMPTY_TABLE",
41     };
42
43     public AbstractProducerTest(String JavaDoc s)
44     {
45         super(s);
46     }
47
48     protected String JavaDoc[] getExpectedNames() throws Exception JavaDoc
49     {
50         return (String JavaDoc[])TABLE_NAMES.clone();
51     }
52
53     protected int[] getExpectedRowCount() throws Exception JavaDoc
54     {
55         return new int[] {1, 2, 3, 2, 1, 0};
56     }
57
58     protected String JavaDoc getNotNullTableName() throws Exception JavaDoc
59     {
60         return "NOT_NULL_TABLE";
61     }
62
63     protected Column[] createExpectedColumns(Column.Nullable nullable) throws Exception JavaDoc
64     {
65         Column[] columns = new Column[4];
66         for (int i = 0; i < columns.length; i++)
67         {
68             columns[i] = new Column("COLUMN" + i, DataType.UNKNOWN, nullable);
69         }
70         return columns;
71     }
72
73     protected Object JavaDoc[] createExpectedRow(int row) throws Exception JavaDoc
74     {
75         Object JavaDoc[] values = new Object JavaDoc[4];
76         for (int i = 0; i < values.length; i++)
77         {
78             values[i] = "row " + row + " col " + i;
79         }
80         return values;
81     }
82
83     protected abstract IDataSetProducer createProducer() throws Exception JavaDoc;
84
85     public void testProduce() throws Exception JavaDoc
86     {
87         // Setup consumer
88
MockDataSetConsumer consumer = new MockDataSetConsumer();
89         consumer.addExpectedStartDataSet();
90         String JavaDoc[] expectedNames = getExpectedNames();
91         int[] rowCounts = getExpectedRowCount();
92         for (int i = 0; i < expectedNames.length; i++)
93         {
94             String JavaDoc expectedName = expectedNames[i];
95             Column.Nullable nullable = expectedName.equals(getNotNullTableName()) ?
96                     Column.NO_NULLS : Column.NULLABLE;
97             Column[] expectedColumns = createExpectedColumns(nullable);
98
99             consumer.addExpectedStartTable(expectedName, expectedColumns);
100             for (int j = 0; j < rowCounts[i]; j++)
101             {
102                 consumer.addExpectedRow(expectedName, createExpectedRow(j));
103             }
104             consumer.addExpectedEndTable(expectedName);
105         }
106         consumer.addExpectedEndDataSet();
107
108         // Setup producer
109
IDataSetProducer producer = createProducer();
110         producer.setConsumer(consumer);
111
112         // Produce and verify consumer
113
producer.produce();
114         consumer.verify();
115     }
116
117     public void testProduceWithoutConsumer() throws Exception JavaDoc
118     {
119         IDataSetProducer producer = createProducer();
120         producer.produce();
121     }
122
123 }
124
Popular Tags