1   package org.apache.torque.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.torque.BaseTestCase;
23  import org.apache.torque.TorqueException;
24  
25  /***
26   * Tests for SqlExpression
27   *
28   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
29   * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
30   * @version $Id: SqlExpressionTest.java 239636 2005-08-24 12:38:09Z henning $
31   */
32  public class SqlBuilderTest extends BaseTestCase
33  {
34      /***
35       * Creates a new instance.
36       *
37       * @param name the name of the test case to run
38       */
39      public SqlBuilderTest(String name)
40      {
41          super(name);
42      }
43  
44      public void testExtractTableName() throws TorqueException
45      {
46          // standard cases with / without schema
47          String columnName = "table.column";
48          String tableName = SQLBuilder.getTableName(columnName, null);
49          assertEquals("table", tableName);
50  
51          columnName = "schema.table.column";
52          tableName = SQLBuilder.getTableName(columnName, null);
53          assertEquals("schema.table", tableName);
54  
55          // functions
56          columnName = "function(table.column)";
57          tableName = SQLBuilder.getTableName(columnName, null);
58          assertEquals("table", tableName);
59  
60          columnName = "function(1,table.column,2)";
61          tableName = SQLBuilder.getTableName(columnName, null);
62          assertEquals("table", tableName);
63  
64          // comparisons
65          columnName = "table.column < 10";
66          tableName = SQLBuilder.getTableName(columnName, null);
67          assertEquals("table", tableName);
68  
69          columnName = "table.column<10";
70          tableName = SQLBuilder.getTableName(columnName, null);
71          assertEquals("table", tableName);
72  
73          columnName = "10 > table.column";
74          tableName = SQLBuilder.getTableName(columnName, null);
75          assertEquals("table", tableName);
76  
77          columnName = "10>table.column";
78          tableName = SQLBuilder.getTableName(columnName, null);
79          assertEquals("table", tableName);
80  
81          columnName = "10>table.column";
82          tableName = SQLBuilder.getTableName(columnName, null);
83          assertEquals("table", tableName);
84  
85          // in clause
86          columnName = "table.column in (1,2,3)";
87          tableName = SQLBuilder.getTableName(columnName, null);
88          assertEquals("table", tableName);
89  
90          // wildcard
91          columnName = "*";
92          tableName = SQLBuilder.getTableName(columnName, null);
93          assertEquals(null, tableName);
94  
95          // function with wildcard
96          columnName = "count(*)";
97          tableName = SQLBuilder.getTableName(columnName, null);
98          assertEquals(null, tableName);
99  
100         // empty String and null
101         columnName = "";
102         try
103         {
104             tableName = SQLBuilder.getTableName(columnName, null);
105             fail("getTableName() should fail for empty column name");
106         }
107         catch (TorqueException e)
108         {
109         }
110 
111         columnName = null;
112         try
113         {
114             tableName = SQLBuilder.getTableName(columnName, null);
115             fail("getTableName() should fail for null as column name");
116         }
117         catch (TorqueException e)
118         {
119         }
120 
121         // failure: no dot or wildcard
122         columnName = "column";
123         try
124         {
125             tableName = SQLBuilder.getTableName(columnName, null);
126             fail("getTableName() should fail for column name "
127                     + "without a dot or wildcard");
128         }
129         catch (TorqueException e)
130         {
131         }
132 
133     }
134 }