View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2008, 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.database;
22  
23  import org.dbunit.dataset.datatype.DataType;
24  import org.dbunit.dataset.datatype.DataTypeException;
25  import org.dbunit.dataset.datatype.IDataTypeFactory;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * @author gommma (gommma AT users.sourceforge.net)
31   * @author Last changed by: $Author$
32   * @version $Revision$ $Date$
33   * @since 2.4.0
34   */
35  public class DatabaseConfigTest extends TestCase
36  {
37      public void testSetProperty_InvalidType_Array() throws Exception
38      {
39          DatabaseConfig config = new DatabaseConfig();
40          String simpleString = "TABLE";
41          try {
42              config.setProperty(DatabaseConfig.PROPERTY_TABLE_TYPE, simpleString);
43              fail("The property 'table type' should be a string array");
44          }
45          catch(IllegalArgumentException expected){
46              String expectedMsg = "Cannot cast object of type 'class java.lang.String' to allowed type 'class [Ljava.lang.String;'.";
47              assertEquals(expectedMsg, expected.getMessage());
48          }
49      }
50      
51      public void testSetProperty_CorrectType_Array() throws Exception
52      {
53          DatabaseConfig config = new DatabaseConfig();
54          String[] stringArray = new String[] {"TABLE"};
55          config.setProperty(DatabaseConfig.PROPERTY_TABLE_TYPE, stringArray);
56          assertEquals(stringArray, config.getProperty(DatabaseConfig.PROPERTY_TABLE_TYPE));
57      }
58  
59      public void testSetProperty_Interface() throws Exception
60      {
61          DatabaseConfig config = new DatabaseConfig();
62          IDataTypeFactory myFactory = new IDataTypeFactory() {
63          
64              public DataType createDataType(int sqlType, String sqlTypeName,
65                      String tableName, String columnName) throws DataTypeException {
66                  return null;
67              }
68          
69              public DataType createDataType(int sqlType, String sqlTypeName)
70                      throws DataTypeException {
71                  return null;
72              }
73          };
74          config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, myFactory);
75          assertEquals(myFactory, config.getProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY));
76      }
77      
78      public void testSetPropertyToNullWhereNotAllowed() throws Exception
79      {
80          DatabaseConfig config = new DatabaseConfig();
81          try {
82              config.setProperty(DatabaseConfig.PROPERTY_BATCH_SIZE, null);
83              assertEquals(null, config.getProperty(DatabaseConfig.PROPERTY_BATCH_SIZE));
84              fail("Should not be able to set a not-nullable property to null");
85          }
86          catch(IllegalArgumentException expected){
87              String expectedMsg = "The property 'http://www.dbunit.org/properties/batchSize' is not nullable.";
88              assertEquals(expectedMsg, expected.getMessage());
89          }
90      }
91  
92      public void testSetPropertyToNullWhereAllowed() throws Exception
93      {
94          DatabaseConfig config = new DatabaseConfig();
95          config.setProperty(DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER, null);
96          assertEquals(null, config.getProperty(DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER));
97      }
98  
99      public void testSetFeatureViaSetPropertyMethod() throws Exception
100     {
101         DatabaseConfig config = new DatabaseConfig();
102         config.setProperty(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, "true");
103         assertEquals(Boolean.TRUE, config.getProperty(DatabaseConfig.FEATURE_BATCHED_STATEMENTS));
104         assertEquals(true, config.getFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS));
105     }
106 
107     public void testSetFeatureViaSetFeatureMethod() throws Exception
108     {
109         DatabaseConfig config = new DatabaseConfig();
110         config.setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, true);
111         assertEquals(Boolean.TRUE, config.getProperty(DatabaseConfig.FEATURE_BATCHED_STATEMENTS));
112         assertEquals(true, config.getFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS));
113     }
114 
115 }