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.dataset;
22  
23  import org.dbunit.dataset.Columns.ColumnDiff;
24  import org.dbunit.dataset.datatype.DataType;
25  
26  import junit.framework.TestCase;
27  
28  /**
29   * @author gommma
30   * @version $Revision$
31   * @since 2.3.0
32   */
33  public class ColumnsTest extends TestCase
34  {
35      public void testGetColumn() throws Exception
36      {
37          Column[] columns = new Column[]{
38              new Column("c0", DataType.UNKNOWN),
39              new Column("c1", DataType.UNKNOWN),
40              new Column("c2", DataType.UNKNOWN),
41              new Column("c3", DataType.UNKNOWN),
42              new Column("c4", DataType.UNKNOWN),
43          };
44  
45          for (int i = 0; i < columns.length; i++)
46          {
47              assertEquals("find column same", columns[i],
48                      Columns.getColumn("c" + i, columns));
49          }
50      }
51  
52      public void testGetColumnCaseInsensitive() throws Exception
53      {
54          Column[] columns = new Column[]{
55              new Column("c0", DataType.UNKNOWN),
56              new Column("C1", DataType.UNKNOWN),
57              new Column("c2", DataType.UNKNOWN),
58              new Column("C3", DataType.UNKNOWN),
59              new Column("c4", DataType.UNKNOWN),
60          };
61  
62          for (int i = 0; i < columns.length; i++)
63          {
64              assertEquals("find column same", columns[i],
65                      Columns.getColumn("c" + i, columns));
66          }
67      }
68  
69      public void testGetColumnValidated() throws Exception
70      {
71          Column[] columns = new Column[]{
72                  new Column("c0", DataType.UNKNOWN),
73                  new Column("C1", DataType.UNKNOWN),
74                  new Column("c2", DataType.UNKNOWN),
75          };
76          for (int i = 0; i < columns.length; i++)
77          {
78              assertEquals("find column same", columns[i],
79                      Columns.getColumnValidated("c" + i, columns, "TableABC"));
80          }
81      }
82      
83      public void testGetColumnValidatedColumnNotFound() throws Exception
84      {
85          Column[] columns = new Column[]{
86                  new Column("c0", DataType.UNKNOWN),
87                  new Column("C1", DataType.UNKNOWN),
88                  new Column("c2", DataType.UNKNOWN),
89          };
90          try 
91          {
92              Columns.getColumnValidated("A1", columns, "TableABC");
93              fail("Should not be able to get a validated column that does not exist");
94          }
95          catch(NoSuchColumnException expected)
96          {
97              assertEquals("TableABC.A1", expected.getMessage());
98          }
99      }
100 
101     public void testGetColumnDiff_NoDifference() throws Exception
102     {
103         Column[] expectedColumns = new Column[]{
104                 new Column("c0", DataType.UNKNOWN),
105                 new Column("c1", DataType.UNKNOWN),
106         };
107         Column[] actualColumns = new Column[]{
108                 new Column("c0", DataType.UNKNOWN),
109                 new Column("c1", DataType.UNKNOWN),
110         };
111         ITableMetaData metaDataExpected = createMetaData(expectedColumns);
112         ITableMetaData metaDataActual = createMetaData(actualColumns);
113         
114         // Create the difference
115         ColumnDiff diff = new ColumnDiff(metaDataExpected, metaDataActual);
116         assertEquals(false, diff.hasDifference());
117         assertEquals("[]", diff.getExpectedAsString());
118         assertEquals("[]", diff.getActualAsString());
119         assertEquals("no difference found", diff.getMessage());
120     }
121 
122     public void testGetColumnDiffDifferentOrder_NoDifference() throws Exception
123     {
124     	// order [c0, c1]
125         Column[] expectedColumns = new Column[]{
126                 new Column("c0", DataType.UNKNOWN),
127                 new Column("c1", DataType.UNKNOWN),
128         };
129         // order [c1, c0]
130         Column[] actualColumnsDifferentOrder = new Column[]{
131                 new Column("c1", DataType.UNKNOWN),
132                 new Column("c0", DataType.UNKNOWN),
133         };
134         ITableMetaData metaDataExpected = createMetaData(expectedColumns);
135         ITableMetaData metaDataActual = createMetaData(actualColumnsDifferentOrder);
136         
137         // Create the difference
138         ColumnDiff diff = new ColumnDiff(metaDataExpected, metaDataActual);
139         assertEquals(false, diff.hasDifference());
140         assertEquals("[]", diff.getExpectedAsString());
141         assertEquals("[]", diff.getActualAsString());
142         assertEquals("no difference found", diff.getMessage());
143     }
144 
145     
146     public void testGetColumnDiff_Difference() throws Exception
147     {
148         Column[] expectedColumns = new Column[]{
149                 new Column("c0", DataType.UNKNOWN),
150                 new Column("c2", DataType.UNKNOWN),
151                 new Column("c1", DataType.UNKNOWN),
152         };
153         Column[] actualColumns = new Column[]{
154                 new Column("d0", DataType.UNKNOWN),
155                 new Column("c2", DataType.UNKNOWN),
156         };
157         ITableMetaData metaDataExpected = createMetaData(expectedColumns);
158         ITableMetaData metaDataActual = createMetaData(actualColumns);
159         
160         // Create the difference
161         ColumnDiff diff = new ColumnDiff(metaDataExpected, metaDataActual);
162         assertEquals(true, diff.hasDifference());
163         assertEquals(2, diff.getExpected().length);
164         assertEquals(1, diff.getActual().length);
165         assertEquals(expectedColumns[0], diff.getExpected()[0]);
166         assertEquals(expectedColumns[2], diff.getExpected()[1]);
167         assertEquals(actualColumns[0], diff.getActual()[0]);
168         assertEquals("[c0, c1]", diff.getExpectedAsString());
169         assertEquals("[d0]", diff.getActualAsString());
170         assertEquals("column count (table=MY_TABLE, expectedColCount=3, actualColCount=2)", diff.getMessage());
171     }
172 
173 
174     
175     private ITableMetaData createMetaData(Column[] columns)
176     {
177     	DefaultTableMetaData tableMetaData = new DefaultTableMetaData("MY_TABLE", columns);
178     	return tableMetaData;
179     }
180 
181 }