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