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  
22  package org.dbunit.dataset;
23  
24  import junit.framework.TestCase;
25  
26  import org.dbunit.DatabaseEnvironment;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * @author Manuel Laflamme
32   * @version $Revision$
33   * @since Feb 17, 2002
34   */
35  public abstract class AbstractTableTest extends TestCase
36  {
37      protected static final int ROW_COUNT = 6;
38      protected static final int COLUMN_COUNT = 4;
39  
40      protected final Logger logger = LoggerFactory.getLogger(getClass());
41      
42      public AbstractTableTest(String s)
43      {
44          super(s);
45      }
46  
47      /**
48       * Creates a table having 6 row and 4 column where columns are named
49       * "COLUMN1, COLUMN2, COLUMN3, COLUMN4" and values are string follwing this
50       * template "row ? col ?"
51       */
52      protected abstract ITable createTable() throws Exception;
53  
54      /**
55       * Returns the string converted as an identifier according to the metadata rules of the database environment.
56       * Most databases convert all metadata identifiers to uppercase.
57       * PostgreSQL converts identifiers to lowercase.
58       * MySQL preserves case.
59       * @param str The identifier.
60       * @return The identifier converted according to database rules.
61       */
62      protected String convertString(String str) throws Exception
63      {
64          return str;
65      }
66  
67      ////////////////////////////////////////////////////////////////////////////
68      // Test methods
69  
70      public void testGetRowCount() throws Exception
71      {
72          assertEquals("row count", ROW_COUNT, createTable().getRowCount());
73      }
74  
75      public void testTableMetaData() throws Exception
76      {
77          Column[] columns = createTable().getTableMetaData().getColumns();
78          assertEquals("column count", COLUMN_COUNT, columns.length);
79          for (int i = 0; i < columns.length; i++)
80          {
81              String expected = convertString("COLUMN" + i);
82              String actual = columns[i].getColumnName();
83              assertEquals("column name", expected, actual);
84          }
85      }
86  
87      public void testGetValue() throws Exception
88      {
89          ITable table = createTable();
90          for (int i = 0; i < ROW_COUNT; i++)
91          {
92              for (int j = 0; j < COLUMN_COUNT; j++)
93              {
94                  String columnName = "COLUMN" + j;
95                  String expected = "row " + i + " col " + j;
96                  Object value = table.getValue(i, columnName);
97                  assertEquals("value", expected, value);
98              }
99          }
100     }
101 
102     public void testGetValueCaseInsensitive() throws Exception
103     {
104         ITable table = createTable();
105         for (int i = 0; i < ROW_COUNT; i++)
106         {
107             for (int j = 0; j < COLUMN_COUNT; j++)
108             {
109                 String columnName = "CoLUmN" + j;
110                 String expected = "row " + i + " col " + j;
111                 Object value = table.getValue(i, columnName);
112                 assertEquals("value", expected, value);
113             }
114         }
115     }
116 
117     public abstract void testGetMissingValue() throws Exception;
118 
119     public void testGetValueRowBounds() throws Exception
120     {
121         int[] rows = new int[]{-2, -1, -ROW_COUNT, ROW_COUNT, ROW_COUNT + 1};
122         ITable table = createTable();
123         String columnName = table.getTableMetaData().getColumns()[0].getColumnName();
124 
125         for (int i = 0; i < rows.length; i++)
126         {
127             try
128             {
129                 table.getValue(rows[i], columnName);
130                 fail("Should throw a RowOutOfBoundsException!");
131             }
132             catch (RowOutOfBoundsException e)
133             {
134             }
135         }
136     }
137 
138     public void testGetValueAndNoSuchColumn() throws Exception
139     {
140         ITable table = createTable();
141         String columnName = "Unknown";
142 
143         try
144         {
145             table.getValue(0, columnName);
146             fail("Should throw a NoSuchColumnException!");
147         }
148         catch (NoSuchColumnException e)
149         {
150         }
151     }
152     
153     /**
154      * This method is used so sub-classes can disable the tests according to 
155      * some characteristics of the environment
156      * @param testName name of the test to be checked
157      * @return flag indicating if the test should be executed or not
158      */
159     protected boolean runTest(String testName) {
160       return true;
161     }
162 
163     protected void runTest() throws Throwable {
164       if ( runTest(getName()) ) {
165         super.runTest();
166       } else { 
167         if ( logger.isDebugEnabled() ) {
168           logger.debug( "Skipping test " + getClass().getName() + "." + getName() );
169         }
170       }
171     }    
172     
173 }
174 
175 
176 
177 
178 
179