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  import org.dbunit.dataset.datatype.DataType;
26  
27  import java.sql.Time;
28  import java.sql.Timestamp;
29  
30  /**
31   * @author Manuel Laflamme
32   * @version $Revision$
33   * @since Feb 19, 2002
34   */
35  public class DataSetUtilsTest extends TestCase
36  {
37      public DataSetUtilsTest(String s)
38      {
39          super(s);
40      }
41  
42  
43      public void testGetQualifiedName() throws Exception
44      {
45          assertEquals("prefix + name", "prefix.name",
46                  DataSetUtils.getQualifiedName("prefix", "name"));
47  
48          assertEquals("null prefix", "name",
49                  DataSetUtils.getQualifiedName(null, "name"));
50  
51          assertEquals("empty prefix", "name",
52                  DataSetUtils.getQualifiedName("", "name"));
53  
54          assertEquals("existing prefix", "prefix.name",
55                  DataSetUtils.getQualifiedName("wrongPrefix", "prefix.name"));
56  
57          assertEquals("escaped prefix + name", "prefix.name",
58                  DataSetUtils.getQualifiedName("prefix", "name"));
59  
60          assertEquals("escaped prefix + name", "[prefix].[name]",
61                  DataSetUtils.getQualifiedName("prefix", "name", "[?]"));
62  
63          assertEquals("escaped prefix + name", "\"prefix\".\"name\"",
64                  DataSetUtils.getQualifiedName("prefix", "name", "\""));
65      }
66  
67      public void testGetEscapedName() throws Exception
68      {
69          assertEquals("'name'", DataSetUtils.getEscapedName("name", "'?'"));
70  
71          assertEquals("[name]", DataSetUtils.getEscapedName("name", "[?]"));
72  
73  //        assertEquals(null, DataSetUtils.getEscapedName(null, "[?]"));
74  
75          assertEquals("name", DataSetUtils.getEscapedName("name", null));
76  
77          assertEquals("name", DataSetUtils.getEscapedName("name", "invalid pattern!"));
78  
79          assertEquals("\"name\"", DataSetUtils.getEscapedName("name", "\""));
80      }
81  
82      public void testGetColumn() throws Exception
83      {
84          Column[] columns = new Column[]{
85              new Column("c0", DataType.UNKNOWN),
86              new Column("c1", DataType.UNKNOWN),
87              new Column("c2", DataType.UNKNOWN),
88              new Column("c3", DataType.UNKNOWN),
89              new Column("c4", DataType.UNKNOWN),
90          };
91  
92          for (int i = 0; i < columns.length; i++)
93          {
94              assertEquals("find column same", columns[i],
95                      DataSetUtils.getColumn("c" + i, columns));
96          }
97      }
98  
99      public void testGetColumnCaseInsensitive() throws Exception
100     {
101         Column[] columns = new Column[]{
102             new Column("c0", DataType.UNKNOWN),
103             new Column("C1", DataType.UNKNOWN),
104             new Column("c2", DataType.UNKNOWN),
105             new Column("C3", DataType.UNKNOWN),
106             new Column("c4", DataType.UNKNOWN),
107         };
108 
109         for (int i = 0; i < columns.length; i++)
110         {
111             assertEquals("find column same", columns[i],
112                     DataSetUtils.getColumn("c" + i, columns));
113         }
114     }
115 
116     public void testGetTables() throws Exception
117     {
118         String[] expected = {"t0", "t1", "t2", "t3"};
119         ITable[] testTables = new ITable[]{
120             new DefaultTable("t0"),
121             new DefaultTable("t1"),
122             new DefaultTable("t2"),
123             new DefaultTable("t3"),
124         };
125 
126         ITable[] tables = DataSetUtils.getTables(new DefaultDataSet(testTables));
127         assertEquals("table count", expected.length, tables.length);
128         for (int i = 0; i < tables.length; i++)
129         {
130             String name = tables[i].getTableMetaData().getTableName();
131             assertEquals("table name", expected[i], name);
132         }
133     }
134 
135     public void testGetTablesByNames() throws Exception
136     {
137         String[] expected = {"t0", "t2"};
138         ITable[] testTables = new ITable[]{
139             new DefaultTable("t0"),
140             new DefaultTable("t1"),
141             new DefaultTable("t2"),
142             new DefaultTable("t3"),
143         };
144 
145         ITable[] tables = DataSetUtils.getTables(expected,
146                 new DefaultDataSet(testTables));
147         assertEquals("table count", expected.length, tables.length);
148         for (int i = 0; i < tables.length; i++)
149         {
150             String name = tables[i].getTableMetaData().getTableName();
151             assertEquals("table name", expected[i], name);
152         }
153     }
154 
155     public void testGetReserseNames() throws Exception
156     {
157         String[] expected = {"t3", "t2", "t1", "t0"};
158         ITable[] testTables = new ITable[]{
159             new DefaultTable("t0"),
160             new DefaultTable("t1"),
161             new DefaultTable("t2"),
162             new DefaultTable("t3"),
163         };
164 
165         String[] names = DataSetUtils.getReverseTableNames(new DefaultDataSet(testTables));
166         assertEquals("table count", expected.length, names.length);
167         for (int i = 0; i < names.length; i++)
168         {
169             assertEquals("table name", expected[i], names[i]);
170         }
171     }
172 
173     public void testGetSqlValueString() throws Exception
174     {
175         ValueStringData[] values = new ValueStringData[]{
176             new ValueStringData(null, DataType.REAL, "NULL"),
177             new ValueStringData("1234", DataType.NUMERIC, "1234"),
178             new ValueStringData("1234", DataType.VARCHAR, "'1234'"),
179             new ValueStringData(new Float(1234.45), DataType.REAL, "1234.45"),
180             new ValueStringData(new java.sql.Date(0L), DataType.DATE,
181                     "{d '" + new java.sql.Date(0L).toString() + "'}"),
182             new ValueStringData(new Time(0L), DataType.TIME,
183                     "{t '" + new Time(0L).toString() + "'}"),
184             new ValueStringData(new Timestamp(0L), DataType.TIMESTAMP,
185                     "{ts '" + new Timestamp(0L).toString() + "'}"),
186             new ValueStringData("12'34", DataType.VARCHAR, "'12''34'"),
187             new ValueStringData("'1234", DataType.VARCHAR, "'''1234'"),
188             new ValueStringData("1234'", DataType.VARCHAR, "'1234'''"),
189             new ValueStringData("'12'34'", DataType.VARCHAR, "'''12''34'''"),
190         };
191 
192         for (int i = 0; i < values.length; i++)
193         {
194             ValueStringData data = values[i];
195             String valueString = DataSetUtils.getSqlValueString(
196                     data.getValue(), data.getDataType());
197             assertEquals("data " + i, data.getExpected(), valueString);
198         }
199     }
200 
201     private class ValueStringData
202     {
203         private final Object _value;
204         private final DataType _dataType;
205         private final String _expected;
206 
207         public ValueStringData(Object value, DataType dataType, String expected)
208         {
209             _value = value;
210             _dataType = dataType;
211             _expected = expected;
212         }
213 
214         public Object getValue()
215         {
216             return _value;
217         }
218 
219         public DataType getDataType()
220         {
221             return _dataType;
222         }
223 
224         public String getExpected()
225         {
226             return _expected;
227         }
228     }
229 
230 }
231 
232 
233 
234 
235 
236 
237 
238