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