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;
22  
23  import junit.framework.TestCase;
24  
25  import java.util.Arrays;
26  import java.util.List;
27  
28  /**
29   * @author Manuel Laflamme
30   * @since Apr 6, 2003
31   * @version $Revision$
32   */
33  public abstract class AbstractTest extends TestCase
34  {
35      private static final String[] TABLE_NAMES = {
36          "TEST_TABLE",
37          "SECOND_TABLE",
38          "EMPTY_TABLE",
39          "PK_TABLE",
40          "ONLY_PK_TABLE",
41          "EMPTY_MULTITYPE_TABLE",
42      };
43      private static final String[] DUPLICATE_TABLE_NAMES = {
44          "DUPLICATE_TABLE",
45          "EMPTY_TABLE",
46          "DUPLICATE_TABLE",
47      };
48      private static final String EXTRA_TABLE_NAME = "EXTRA_TABLE";
49  
50      public AbstractTest(String s)
51      {
52          super(s);
53      }
54  
55      /**
56       * Returns the string converted as an identifier according to the metadata rules of the database environment.
57       * Most databases convert all metadata identifiers to uppercase.
58       * PostgreSQL converts identifiers to lowercase.
59       * MySQL preserves case.
60       * @param str The identifier.
61       * @return The identifier converted according to database rules.
62       */
63      protected String convertString(String str) throws Exception
64      {
65          return str;
66      }
67  
68      protected String[] getExpectedNames() throws Exception
69      {
70          return (String[])AbstractTest.TABLE_NAMES.clone();
71      }
72  
73      protected String[] getExpectedLowerNames() throws Exception
74      {
75          String[] names = (String[])AbstractTest.TABLE_NAMES.clone();
76          for (int i = 0; i < names.length; i++)
77          {
78              names[i] = names[i].toLowerCase();
79          }
80  
81          return names;
82      }
83  
84      protected String[] getExpectedDuplicateNames()
85      {
86          return (String[])AbstractTest.DUPLICATE_TABLE_NAMES.clone();
87      }
88  
89      protected String getDuplicateTableName()
90      {
91          return "DUPLICATE_TABLE";
92      }
93  
94      public String getExtraTableName()
95      {
96          return AbstractTest.EXTRA_TABLE_NAME;
97      }
98  
99      public void assertEqualsIgnoreCase(String message, String expected, String actual)
100     {
101         if (!expected.equalsIgnoreCase(actual))
102         {
103             assertEquals(message, expected, actual);
104         }
105     }
106 
107     public void assertContains(String message, Object[] expected, Object[] actual)
108     {
109         List expectedList = Arrays.asList(expected);
110         List actualList = Arrays.asList(actual);
111 
112         if (!actualList.containsAll(expectedList))
113         {
114             fail(message + " expected contains:<" + expectedList + "> but was:<"
115                     + actualList + ">");
116         }
117     }
118 
119     public void assertContainsIgnoreCase(String message, String[] expected, String[] actual)
120     {
121         String[] expectedLowerCase = new String[expected.length];
122         for (int i = 0; i < expected.length; i++)
123         {
124             expectedLowerCase[i] = expected[i].toLowerCase();
125         }
126 
127         String[] actualLowerCase = new String[actual.length];
128         for (int i = 0; i < actual.length; i++)
129         {
130             actualLowerCase[i] = actual[i].toLowerCase();
131         }
132 
133         assertContains(message, expectedLowerCase, actualLowerCase);
134     }
135 
136 }