001: package org.apache.torque.util;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.torque.BaseTestCase;
023: import org.apache.torque.TorqueException;
024:
025: /**
026: * Tests for SqlExpression
027: *
028: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
029: * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
030: * @version $Id: SqlExpressionTest.java 239636 2005-08-24 12:38:09Z henning $
031: */
032: public class SqlBuilderTest extends BaseTestCase {
033: /**
034: * Creates a new instance.
035: *
036: * @param name the name of the test case to run
037: */
038: public SqlBuilderTest(String name) {
039: super (name);
040: }
041:
042: public void testExtractTableName() throws TorqueException {
043: // standard cases with / without schema
044: String columnName = "table.column";
045: String tableName = SQLBuilder.getTableName(columnName, null);
046: assertEquals("table", tableName);
047:
048: columnName = "schema.table.column";
049: tableName = SQLBuilder.getTableName(columnName, null);
050: assertEquals("schema.table", tableName);
051:
052: // functions
053: columnName = "function(table.column)";
054: tableName = SQLBuilder.getTableName(columnName, null);
055: assertEquals("table", tableName);
056:
057: columnName = "function(1,table.column,2)";
058: tableName = SQLBuilder.getTableName(columnName, null);
059: assertEquals("table", tableName);
060:
061: // comparisons
062: columnName = "table.column < 10";
063: tableName = SQLBuilder.getTableName(columnName, null);
064: assertEquals("table", tableName);
065:
066: columnName = "table.column<10";
067: tableName = SQLBuilder.getTableName(columnName, null);
068: assertEquals("table", tableName);
069:
070: columnName = "10 > table.column";
071: tableName = SQLBuilder.getTableName(columnName, null);
072: assertEquals("table", tableName);
073:
074: columnName = "10>table.column";
075: tableName = SQLBuilder.getTableName(columnName, null);
076: assertEquals("table", tableName);
077:
078: columnName = "10>table.column";
079: tableName = SQLBuilder.getTableName(columnName, null);
080: assertEquals("table", tableName);
081:
082: // in clause
083: columnName = "table.column in (1,2,3)";
084: tableName = SQLBuilder.getTableName(columnName, null);
085: assertEquals("table", tableName);
086:
087: // wildcard
088: columnName = "*";
089: tableName = SQLBuilder.getTableName(columnName, null);
090: assertEquals(null, tableName);
091:
092: // function with wildcard
093: columnName = "count(*)";
094: tableName = SQLBuilder.getTableName(columnName, null);
095: assertEquals(null, tableName);
096:
097: // empty String and null
098: columnName = "";
099: try {
100: tableName = SQLBuilder.getTableName(columnName, null);
101: fail("getTableName() should fail for empty column name");
102: } catch (TorqueException e) {
103: }
104:
105: columnName = null;
106: try {
107: tableName = SQLBuilder.getTableName(columnName, null);
108: fail("getTableName() should fail for null as column name");
109: } catch (TorqueException e) {
110: }
111:
112: // failure: no dot or wildcard
113: columnName = "column";
114: try {
115: tableName = SQLBuilder.getTableName(columnName, null);
116: fail("getTableName() should fail for column name "
117: + "without a dot or wildcard");
118: } catch (TorqueException e) {
119: }
120:
121: }
122: }
|