View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2004-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.util;
22  
23  import org.dbunit.database.DatabaseConfig;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * @author gommma
29   * @author Last changed by: $Author$
30   * @version $Revision$ $Date$
31   * @since 2.3.0
32   */
33  public class QualifiedTableNameTest extends TestCase 
34  {
35  
36  	public void testQualifiedTableNamePresent_PrecedesDefaultSchemaName()
37  	{
38  		QualifiedTableName qualifiedTableName = new QualifiedTableName("MYSCHEMA.MYTABLE", "DEFAULT_SCHEMA");
39  		assertEquals("MYSCHEMA", qualifiedTableName.getSchema());
40  		assertEquals("MYTABLE", qualifiedTableName.getTable());
41  		assertEquals("MYSCHEMA.MYTABLE", qualifiedTableName.getQualifiedName());
42  	}
43  
44  	public void testQualifiedTableNameNotPresentUsingDefaultSchema()
45  	{
46  		QualifiedTableName qualifiedTableName = new QualifiedTableName("MYTABLE", "DEFAULT_SCHEMA");
47  		assertEquals("DEFAULT_SCHEMA", qualifiedTableName.getSchema());
48  		assertEquals("MYTABLE", qualifiedTableName.getTable());
49  		assertEquals("DEFAULT_SCHEMA.MYTABLE", qualifiedTableName.getQualifiedName());
50  	}
51  
52  	public void testQualifiedTableNameNotPresentAndNoDefaultSchema()
53  	{
54  		QualifiedTableName qualifiedTableName = new QualifiedTableName("MYTABLE", null);
55  		assertEquals(null, qualifiedTableName.getSchema());
56  		assertEquals("MYTABLE", qualifiedTableName.getTable());
57  		assertEquals("MYTABLE", qualifiedTableName.getQualifiedName());
58  	}
59  
60  	public void testQualifiedTableNameNotPresentAndEmptyDefaultSchema()
61  	{
62  		QualifiedTableName qualifiedTableName = new QualifiedTableName("MYTABLE", "");
63  		assertEquals("", qualifiedTableName.getSchema());
64  		assertEquals("MYTABLE", qualifiedTableName.getTable());
65  		assertEquals("MYTABLE", qualifiedTableName.getQualifiedName());
66  	}
67  
68  	public void testGetQualifiedTableName()
69  	{
70  		String qualifiedName = new QualifiedTableName("MY_SCHEMA.MY_TABLE", null, "'?'").getQualifiedName();
71  		assertEquals("'MY_SCHEMA'.'MY_TABLE'", qualifiedName);
72  	}
73  	
74  	public void testGetQualifiedTableName_DefaultSchema()
75  	{
76  		String qualifiedName = new QualifiedTableName("MY_TABLE", "DEFAULT_SCHEMA", "'?'").getQualifiedName();
77  		assertEquals("'DEFAULT_SCHEMA'.'MY_TABLE'", qualifiedName);
78  	}
79  	
80  	public void testGetQualifiedTableName_DefaultSchema_FeatureEnabled()
81  	{
82  		DatabaseConfig config = new DatabaseConfig();
83  		config.setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
84  		String qualifiedName = new QualifiedTableName("MY_TABLE", "DEFAULT_SCHEMA", null).getQualifiedNameIfEnabled(config);
85  		assertEquals("DEFAULT_SCHEMA.MY_TABLE", qualifiedName);
86  	}
87  
88  	public void testGetQualifiedTableName_DefaultSchema_FeatureDisabled()
89  	{
90  		DatabaseConfig config = new DatabaseConfig();
91  		config.setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, false);
92  		String qualifiedName = new QualifiedTableName("MY_TABLE", "DEFAULT_SCHEMA", null).getQualifiedNameIfEnabled(config);
93  		assertEquals("MY_TABLE", qualifiedName);
94  	}
95  
96  	public void testGetQualifiedTableName_DefaultSchema_FeatureEnabled_Escaping()
97  	{
98  		DatabaseConfig config = new DatabaseConfig();
99  		config.setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
100 		String qualifiedName = new QualifiedTableName("MY_TABLE", "DEFAULT_SCHEMA", "'?'").getQualifiedNameIfEnabled(config);
101 		assertEquals("'DEFAULT_SCHEMA'.'MY_TABLE'", qualifiedName);
102 	}
103 
104 	public void testGetQualifiedTableName_DefaultSchema_FeatureDisabled_Escaping()
105 	{
106 		DatabaseConfig config = new DatabaseConfig();
107 		config.setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, false);
108 		String qualifiedName = new QualifiedTableName("MY_TABLE", "DEFAULT_SCHEMA", "'?'").getQualifiedNameIfEnabled(config);
109 		assertEquals("'MY_TABLE'", qualifiedName);
110 	}
111 	
112 	public void testGetQualifiedTableName_DefaultSchema_FeatureEnabled_EscapingWithoutQuestionmark()
113     {
114         DatabaseConfig config = new DatabaseConfig();
115         config.setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
116         String qualifiedName = new QualifiedTableName("MY_TABLE", "DEFAULT_SCHEMA", "'").getQualifiedNameIfEnabled(config);
117         assertEquals("'DEFAULT_SCHEMA'.'MY_TABLE'", qualifiedName);
118     }
119 
120 	public void testConstructorWithNullTable()
121 	{
122 		try {
123 			new QualifiedTableName(null, "SCHEMA");
124 			fail("Should not be able to create object with null table");
125 		}
126 		catch(NullPointerException expected){
127 			assertEquals("The parameter 'tableName' must not be null", expected.getMessage());
128 		}
129 	}
130 
131 }