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.assertion;
22  
23  import junit.framework.TestCase;
24  
25  import org.dbunit.dataset.Column;
26  import org.dbunit.dataset.DataSetException;
27  import org.dbunit.dataset.DefaultTable;
28  import org.dbunit.dataset.ITable;
29  import org.dbunit.dataset.datatype.DataType;
30  import org.dbunit.dataset.filter.DefaultColumnFilter;
31  
32  /**
33   * @author gommma (gommma AT users.sourceforge.net)
34   * @since 2.4.0
35   */
36  public class DefaultFailureHandlerTest extends TestCase
37  {
38      private static final String MY_TABLE = "MY_TABLE";
39  
40      private static final String COL_NAME_1 = "COL_1";
41      private static final String COL_NAME_2 = "COL_2";
42  
43      private static final String COL_VALUE_1 = "value1";
44      private static final String COL_VALUE_2 = "value2";
45  
46      public void testGetColumn() throws Exception
47      {
48          Column[] cols = new Column[]{
49                  new Column(COL_NAME_1, DataType.UNKNOWN),
50                  new Column(COL_NAME_2, DataType.UNKNOWN)
51          };
52          DefaultTable table = new DefaultTable(MY_TABLE, cols);
53          table.addRow(new Object[] {COL_VALUE_1, COL_VALUE_2});
54  
55          // Filter COL_NAME_1
56          ITable tableFiltered = DefaultColumnFilter.excludedColumnsTable(table, new String[]{COL_NAME_1});
57  
58          DefaultFailureHandler failureHandler = new DefaultFailureHandler(cols);
59          String info = failureHandler.getAdditionalInfo(tableFiltered, tableFiltered, 0, COL_NAME_1);
60  
61          String expectedInfo =
62                  "Additional row info: ('"
63                          + COL_NAME_1
64                          + "': expected=<"
65                          + COL_VALUE_1
66                          + ">, actual=<"
67                          + COL_VALUE_1
68                          + ">) ('"
69                          + COL_NAME_2 + "': expected=<" + COL_VALUE_2
70                          + ">, actual=<" + COL_VALUE_2 + ">)";
71          assertEquals(expectedInfo, info);
72      }
73  
74      public void testMakeAdditionalColumnInfoErrorMessage()
75      {
76          DefaultFailureHandler defaultFailureHandler = new DefaultFailureHandler();
77  
78          String columnName = "testColumnName";
79          DataSetException e = new DataSetException("test exception message");
80          String actual =
81                  defaultFailureHandler.makeAdditionalColumnInfoErrorMessage(
82                          columnName,
83                          e);
84          assertNotNull("Error message is null.", actual);
85  
86          // manually review log for acceptable message content
87      }
88  
89      public void testGetColumnValue_Found() throws DataSetException
90      {
91          Column[] cols =
92                  new Column[] {new Column(COL_NAME_1, DataType.UNKNOWN),
93                  new Column(COL_NAME_2, DataType.UNKNOWN)};
94          DefaultTable table = new DefaultTable(MY_TABLE, cols);
95          table.addRow(new Object[] {COL_VALUE_1, COL_VALUE_2});
96  
97          DefaultColumnFilter.excludedColumnsTable(table,
98                  new String[] {COL_NAME_1});
99          DefaultFailureHandler defaultFailureHandler =
100                 new DefaultFailureHandler();
101 
102         Object expected = COL_VALUE_1;
103 
104         int rowIndex = 0;
105         String columnName = COL_NAME_1;
106         Object actual = defaultFailureHandler.getColumnValue(table, rowIndex, columnName);
107 
108         assertEquals("Wrong column value found.", expected, actual);
109     }
110 
111     public void testGetColumnValue_NotFound() throws DataSetException
112     {
113         Column[] cols =
114                 new Column[] {new Column(COL_NAME_1, DataType.UNKNOWN),
115                 new Column(COL_NAME_2, DataType.UNKNOWN)};
116         DefaultTable table = new DefaultTable(MY_TABLE, cols);
117         table.addRow(new Object[] {COL_VALUE_1, COL_VALUE_2});
118 
119         DefaultColumnFilter.excludedColumnsTable(table,
120                 new String[] {COL_NAME_1});
121         DefaultFailureHandler defaultFailureHandler =
122                 new DefaultFailureHandler();
123 
124         Object expected = COL_VALUE_1;
125 
126         int rowIndex = 0;
127         String columnName = "NonExistingColumnName";
128         Object actual =
129                 defaultFailureHandler.getColumnValue(table, rowIndex,
130                         columnName);
131 
132         assertNotSame("Wrong column value found.", expected, actual);
133     }
134 }