You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2011/11/26 11:59:52 UTC
svn commit: r1206426 [4/7] - in /db/torque/torque4/trunk:
torque-runtime/src/main/java/org/apache/torque/
torque-runtime/src/main/java/org/apache/torque/avalon/
torque-runtime/src/main/java/org/apache/torque/dsfactory/
torque-runtime/src/main/java/org/...
Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java Sat Nov 26 10:59:15 2011
@@ -33,6 +33,8 @@ import java.util.Vector;
import org.apache.commons.collections.OrderedMapIterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.torque.Column;
+import org.apache.torque.ColumnImpl;
import org.apache.torque.TorqueException;
import org.apache.torque.om.mapper.ObjectListMapper;
import org.apache.torque.om.mapper.RecordMapper;
@@ -92,8 +94,8 @@ public class SummaryHelper
/** The class log. */
private static Log logger = LogFactory.getLog(SummaryHelper.class);
- /** A list of the group by columns names (e.g. TABLE.COLUMN) */
- private List<String> groupByColumns;
+ /** A list of the group by columns. */
+ private List<Column> groupByColumns;
/** A ListOrderMapCI<String, Aggregate.Function> with the aggregate functions
* to use in generating results. */
private ListOrderedMapCI aggregates;
@@ -268,19 +270,9 @@ public class SummaryHelper
List<ListOrderedMapCI> resultsList = new Vector<ListOrderedMapCI>(rows.size());
List<String> columnNames = new ArrayList<String>();
- for (String columnName : c.getSelectColumns())
+ for (Column column : c.getSelectColumns())
{
- int dotPos = columnName.lastIndexOf(".");
- String unqualifiedColumnName;
- if (dotPos == -1)
- {
- unqualifiedColumnName = columnName;
- }
- else
- {
- unqualifiedColumnName = columnName.substring(dotPos + 1);
- }
- columnNames.add(unqualifiedColumnName);
+ columnNames.add(column.getColumnName());
}
columnNames.addAll(c.getAsColumns().keySet());
for (List<Object> row : rows)
@@ -329,11 +321,9 @@ public class SummaryHelper
}
c.setIgnoreCase(false);
- List<String> cols = null;
-
- cols = getGroupByColumns();
+ List<Column> cols = getGroupByColumns();
boolean haveFromTable = !cols.isEmpty(); // Group By cols define src table.
- for (String col : cols)
+ for (Column col : cols)
{
c.addGroupByColumn(col);
c.addSelectColumn(col);
@@ -356,18 +346,24 @@ public class SummaryHelper
{
String key = (String) iMap.next();
SQLFunction f = (SQLFunction) iMap.getValue();
- c.addAsColumn(key, f.toSQL());
+ Column col = f.getColumn();
+ c.addAsColumn(key, new ColumnImpl(
+ null,
+ col.getTableName(),
+ col.getColumnName(),
+ f.toSQL()));
if (!haveFromTable) // Last chance. Get it from the func.
{
- String col = f.getArgument(0).toString();
- if (col.contains("."))
{
// Kludgy Where table.col = table.col clause to force
// from table identification.
- c.add(col, (Object) (col + "=" + col), SqlEnum.CUSTOM);
+ c.add(col,
+ (col.getColumnName()
+ + "=" + col.getColumnName()),
+ SqlEnum.CUSTOM);
haveFromTable = true;
- String table = col.substring(0, col.indexOf('.'));
+ String table = col.getTableName();
logger.debug("From table, '" + table
+ "', defined from aggregate column");
}
@@ -396,7 +392,7 @@ public class SummaryHelper
*
* @param column
*/
- public void addGroupBy(String column)
+ public void addGroupBy(Column column)
{
getGroupByColumns().add(column);
}
@@ -424,11 +420,11 @@ public class SummaryHelper
setExcludeExprColumns(false);
}
- public List<String> getGroupByColumns()
+ public List<Column> getGroupByColumns()
{
if (groupByColumns == null)
{
- groupByColumns = new Vector<String>();
+ groupByColumns = new Vector<Column>();
}
return groupByColumns;
}
@@ -524,5 +520,4 @@ public class SummaryHelper
{
this.excludeExprColumns = excludeExprColumns;
}
-
}
Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/UniqueColumnList.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/UniqueColumnList.java?rev=1206426&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/UniqueColumnList.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/UniqueColumnList.java Sat Nov 26 10:59:15 2011
@@ -0,0 +1,100 @@
+package org.apache.torque.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+
+import org.apache.torque.Column;
+
+/**
+ * List with unique entries. UniqueList does not allow null nor will
+ * Columns with the same SQL expression be added twice.
+ *
+ * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
+ * @version $Id: UniqueList.java 1003834 2010-10-02 16:51:32Z tfischer $
+ */
+public class UniqueColumnList extends ArrayList<Column>
+{
+ /**
+ * Serial version
+ */
+ private static final long serialVersionUID = 4467847559423445120L;
+
+ /**
+ * Constructs an empty UniqueList.
+ */
+ public UniqueColumnList()
+ {
+ }
+
+ /**
+ * Copy-constructor. Creates a shallow copy of an UniqueList.
+ * @param list the uniqueList to copy
+ */
+ public UniqueColumnList(UniqueColumnList list)
+ {
+ this.addAll(list);
+ }
+
+ /**
+ * Adds a Column to the list, if no column with the same SQL Expression
+ * is not already contained.
+ *
+ * @param column the Column to add, not null.
+ *
+ * @return true if the Object is added.
+ *
+ * @throws NullPointerException if column is null.
+ */
+ public boolean add(Column column)
+ {
+ if (column == null)
+ {
+ throw new NullPointerException("column must not be null");
+ }
+ if (!containsSqlExpression(column))
+ {
+ return super.add(column);
+ }
+ return false;
+ }
+
+ /**
+ * Checks if this list already contains a column with the same
+ * SQL expression.
+ *
+ * @param column the column to check, not null.
+ *
+ * @return true if a column with the same Sql Expression is contained,
+ * false otherwise.
+ */
+ public boolean containsSqlExpression(Column column)
+ {
+ for (Column candidate : this)
+ {
+ if (candidate.getSqlExpression().equals(
+ column.getSqlExpression()))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+}
Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/AbstractFunction.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/AbstractFunction.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/AbstractFunction.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/AbstractFunction.java Sat Nov 26 10:59:15 2011
@@ -22,6 +22,7 @@ package org.apache.torque.util.functions
import java.util.List;
import java.util.Vector;
+import org.apache.torque.Column;
import org.apache.torque.Torque;
/**
@@ -56,16 +57,6 @@ public abstract class AbstractFunction i
public abstract String toSQL();
/**
- * Return a string representation of the function parameter
- * at the specified index. Should be null if parameter does not exist.
- *
- * @param index The 0 based index of the parameter to get.
- * @return A String representation of the parameter. Null if one does not
- * exist.
- */
- public abstract String getArgument(int i);
-
- /**
* Return all the parameters as an object array. This allow for
* processing of the parameters in their original format rather
* than just in String format. E.g. a parameter might be specified
@@ -85,6 +76,27 @@ public abstract class AbstractFunction i
}
/**
+ * Returns the column to which this function is applied.
+ *
+ * @return the column, not null.
+ *
+ * @throws IllegalStateException if the column cannot be determined.
+ */
+ public Column getColumn()
+ {
+ for (Object argument : getArgumentList())
+ {
+ if (argument instanceof Column)
+ {
+ return (Column) argument;
+ }
+ }
+ throw new IllegalStateException(
+ "Column could not be determined from arguments "
+ + getArgumentList());
+ }
+
+ /**
* Get the name of the Torque Database associated with this function.
*
* @return The DB name. Should not be null (use default DB in this case).
@@ -113,7 +125,7 @@ public abstract class AbstractFunction i
* @return The parameter object. Null if one does not
* exist.
*/
- protected Object getArgumentObject(int index)
+ public Object getArgument(int index)
{
List<Object> argList = getArgumentList();
if (index >= argList.size())
Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/Aggregate.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/Aggregate.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/Aggregate.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/Aggregate.java Sat Nov 26 10:59:15 2011
@@ -19,6 +19,7 @@ package org.apache.torque.util.functions
* under the License.
*/
+import org.apache.torque.Column;
/**
* <p>A container for classes that will generate SQL for the SQL99 Standard
@@ -98,7 +99,12 @@ public class Aggregate
}
this.distinct = ((Boolean) args[1]).booleanValue();
}
- addArgument(args[0].toString());
+ if (!(args[0] instanceof Column))
+ {
+ throw new IllegalArgumentException(
+ "First argument object is not type Column!");
+ }
+ addArgument(args[0]);
this.argumentsSet = true;
}
@@ -150,18 +156,11 @@ public class Aggregate
{
sb.append("DISTINCT ");
}
- sb.append(getArgument(0).toString()).append(")");
+ sb.append(((Column) getArgument(0)).getSqlExpression())
+ .append(")");
return sb.toString();
}
- /* (non-Javadoc)
- * @see org.apache.torque.util.functions.AbstractFunction#getArgument(int)
- */
- public String getArgument(int index)
- {
- return (String) getArgumentObject(index);
- }
-
}
/**
Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/FunctionFactory.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/FunctionFactory.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/FunctionFactory.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/FunctionFactory.java Sat Nov 26 10:59:15 2011
@@ -21,6 +21,7 @@ package org.apache.torque.util.functions
import java.lang.reflect.Constructor;
+import org.apache.torque.Column;
import org.apache.torque.Torque;
import org.apache.torque.adapter.DB;
@@ -35,7 +36,7 @@ import org.apache.torque.adapter.DB;
* specific functions, e.g. avg( Object ) and the like.</p>
*
* @see SQLFunction
- * @see SummaryHelper
+ * @see org.apache.torque.util.SummaryHelper
*
* @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
* @version $Id
@@ -53,185 +54,165 @@ public final class FunctionFactory
* Create an AVG SQL function implementation.
*
* @param dbName The name of the Torque db
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
* @param distinct If true, the function expr will have be prefaced
* with "DISTINCT", e.g. function(DISTINCT column).
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction avg (String dbName, Object expr1,
+ public static SQLFunction avg (String dbName, Column column,
boolean distinct)
throws Exception
{
return functionInstance(dbName, FunctionEnum.AVG,
- new Object[]{expr1, new Boolean(distinct)});
+ new Object[]{column, new Boolean(distinct)});
}
/**
* Convenience method for creating a non-distinct function that uses
* the default DB.
*
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction avg (Object expr1)
+ public static SQLFunction avg (Column column)
throws Exception
{
- return avg (null, expr1, false);
+ return avg (null, column, false);
}
/**
* Create a COUNT SQL function implementation.
*
* @param dbName The name of the Torque db
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
* @param distinct If true, the function expr will have be prefaced
* with "DISTINCT", e.g. function(DISTINCT column).
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction count (String dbName, Object expr1,
+ public static SQLFunction count (String dbName, Column column,
boolean distinct)
throws Exception
{
return functionInstance(dbName, FunctionEnum.COUNT,
- new Object[]{expr1, new Boolean(distinct)});
+ new Object[]{column, new Boolean(distinct)});
}
/**
* Convenience method for creating a non-distinct function that uses
* the default DB.
*
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction count (Object expr1)
+ public static SQLFunction count (Column column)
throws Exception
{
- return count(null, expr1, false);
+ return count(null, column, false);
}
/**
* Create a MAX SQL function implementation.
*
* @param dbName The name of the Torque db
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
* @param distinct If true, the function expr will have be prefaced
* with "DISTINCT", e.g. function(DISTINCT column).
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction max(String dbName, Object expr1,
+ public static SQLFunction max(String dbName, Column column,
boolean distinct)
throws Exception
{
return functionInstance(dbName, FunctionEnum.MAX,
- new Object[]{expr1, new Boolean(distinct)});
+ new Object[]{column, new Boolean(distinct)});
}
/**
* Convenience method for creating a non-distinct function that uses
* the default DB.
*
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction max(Object expr1)
+ public static SQLFunction max(Column column)
throws Exception
{
- return max(null, expr1, false);
+ return max(null, column, false);
}
/**
* Create a MIN SQL function implementation.
*
* @param dbName The name of the Torque db
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
* @param distinct If true, the function expr will have be prefaced
* with "DISTINCT", e.g. function(DISTINCT column).
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction min(String dbName, Object expr1,
+ public static SQLFunction min(String dbName, Column column,
boolean distinct)
throws Exception
{
return functionInstance(dbName, FunctionEnum.MIN,
- new Object[]{expr1, new Boolean(distinct)});
+ new Object[]{column, new Boolean(distinct)});
}
/**
* Convenience method for creating a non-distinct function that uses
* the default DB.
*
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction min (Object expr1)
+ public static SQLFunction min (Column column)
throws Exception
{
- return min (null, expr1, false);
+ return min (null, column, false);
}
/**
* Create a SUM SQL function implementation.
*
* @param dbName The name of the Torque db
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
* @param distinct If true, the function expr will have be prefaced
* with "DISTINCT", e.g. function(DISTINCT column).
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction sum (String dbName, Object expr1,
+ public static SQLFunction sum (String dbName, Column column,
boolean distinct)
throws Exception
{
return functionInstance(dbName, FunctionEnum.SUM,
- new Object[]{expr1, new Boolean(distinct)});
+ new Object[]{column, new Boolean(distinct)});
}
/**
* Convenience method for creating a non-distinct function that uses
* the default DB.
*
- * @param expr1 The expression to apply the function to. Generally a
- * column name in table.column format. It is assumed
- * that the "toString()" method of this object will be
- * acceptable for an argument.
+ * @param column The column or pseudocolumn to apply the function to.
+ *
* @return The function object to use.
* @throws Exception
*/
- public static SQLFunction sum (Object expr1)
+ public static SQLFunction sum (Column column)
throws Exception
{
- return sum (null, expr1, false);
+ return sum (null, column, false);
}
/**
Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/SQLFunction.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/SQLFunction.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/SQLFunction.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/SQLFunction.java Sat Nov 26 10:59:15 2011
@@ -19,6 +19,8 @@ package org.apache.torque.util.functions
* under the License.
*/
+import org.apache.torque.Column;
+
/**
* Define the basic methods that classes that support SQL Functions
* need to implement for Classes that use them. This is intended to
@@ -43,14 +45,22 @@ public interface SQLFunction
String toSQL();
/**
- * Return a string representation of the function parameters
- * at index i. Should be null if parameter does not exist.
+ * Returns the function parameters at index i.
+ * Should be null if parameter does not exist.
*
* @param i The 0 based parameter to get.
- * @return A String representation of the parameter. Null if one does not
- * exist.
+ * @return The parameter. Null if one does not exist.
+ */
+ Object getArgument(int i);
+
+ /**
+ * Returns the column to which this function is applied.
+ *
+ * @return the column, not null.
+ *
+ * @throws IllegalStateException if the column cannot be determined.
*/
- String getArgument(int i);
+ Column getColumn();
/**
* Return all the parameters as an object array. This allow for
Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java Sat Nov 26 10:59:15 2011
@@ -21,6 +21,10 @@ package org.apache.torque;
import junit.framework.TestCase;
+import org.apache.torque.map.ColumnMap;
+import org.apache.torque.map.DatabaseMap;
+import org.apache.torque.map.TableMap;
+
/**
* Base functionality to be extended by all Torque test cases. Test
* case implementations are used to automate unit testing via JUnit.
@@ -32,37 +36,61 @@ import junit.framework.TestCase;
public abstract class BaseTestCase extends TestCase
{
/** The path to the configuration file. */
- protected static final String CONFIG_FILE
+ public static final String CONFIG_FILE
= "src/test/resources/Torque.properties";
- /** Whether torque has been initialized. */
- private static boolean hasInitialized = false;
+ /** A pre-filled table map. */
+ protected TableMap tableMap;
- /**
- * Creates a new instance.
- *
- * @param name the name of the test case to run
- */
- public BaseTestCase(String name)
- {
- super(name);
- }
+ /** A pre-filled database map. */
+ protected DatabaseMap databaseMap;
+
+ /** A pre-filled database. */
+ protected Database database;
+
+ /** A pre-filled String column map. */
+ protected ColumnMap stringColumnMap;
+
+ /** A pre-filled Integer column map. */
+ protected ColumnMap integerColumnMap;
/**
- * Initialize Torque on the first setUp(). Subclasses which
+ * Re-Initialize Torque and fill supplied data. Subclasses which
* override setUp() must call super.setUp() as their first action.
*
- * @throws TorqueException if initialisation fails.
+ * @throws Exception if initialisation fails.
*/
- public void setUp() throws TorqueException
+ public void setUp() throws Exception
{
- synchronized (BaseTestCase.class)
+ Torque.setInstance(null);
+ Torque.init(CONFIG_FILE);
+
+ databaseMap = Torque.getDatabaseMap("postgresql");
+ database = databaseMap.getDatabase();
+ tableMap = databaseMap.addTable("TABLE");
+ {
+ stringColumnMap = new ColumnMap("COLUMN1", tableMap);
+ stringColumnMap.setType(new String(""));
+ stringColumnMap.setJavaType("String");
+ tableMap.addColumn(stringColumnMap);
+ }
+ {
+ ColumnMap columnMap2 = new ColumnMap("COLUMN2", tableMap);
+ columnMap2.setType(new String(""));
+ columnMap2.setJavaType("String");
+ tableMap.addColumn(columnMap2);
+ }
+ {
+ ColumnMap columnMap3 = new ColumnMap("COLUMN3", tableMap);
+ columnMap3.setType(new String(""));
+ columnMap3.setJavaType("String");
+ tableMap.addColumn(columnMap3);
+ }
{
- if (!hasInitialized)
- {
- Torque.init(CONFIG_FILE);
- hasInitialized = true;
- }
+ integerColumnMap = new ColumnMap("COLUMN4", tableMap);
+ integerColumnMap.setType(new Integer(0));
+ integerColumnMap.setJavaType("Integer");
+ tableMap.addColumn(integerColumnMap);
}
}
}
Added: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/ColumnImplTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/ColumnImplTest.java?rev=1206426&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/ColumnImplTest.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/ColumnImplTest.java Sat Nov 26 10:59:15 2011
@@ -0,0 +1,506 @@
+package org.apache.torque;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Test for the class ColumnImpl.
+ *
+ * @version $Id: $
+ */
+public class ColumnImplTest extends BaseTestCase
+{
+ public void testTableColumnConstructor()
+ {
+ ColumnImpl column = new ColumnImpl(
+ "tableName",
+ "columnName");
+ assertEquals(null, column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "tableName.columnName",
+ column.getSqlExpression());
+ }
+
+ public void testTableColumnConstructorWithSchemaInTable()
+ {
+ ColumnImpl column = new ColumnImpl(
+ "schemaName.tableName",
+ "columnName");
+ assertEquals("schemaName", column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "schemaName.tableName.columnName",
+ column.getSqlExpression());
+ }
+
+ public void testTableColumnConstructorNullTableName()
+ {
+ try
+ {
+ new ColumnImpl(null, "columnName");
+ fail("Exception expected");
+ }
+ catch (NullPointerException e)
+ {
+ // expected
+ }
+ }
+
+ public void testTableColumnConstructorBlankTableName()
+ {
+ try
+ {
+ new ColumnImpl(" ", "columnName");
+ fail("Exception expected");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testTableColumnConstructorNullColumnName()
+ {
+ try
+ {
+ new ColumnImpl("tableName", null);
+ fail("Exception expected");
+ }
+ catch (NullPointerException e)
+ {
+ // expected
+ }
+ }
+
+ public void testTableColumnConstructorBlankColumnName()
+ {
+ try
+ {
+ new ColumnImpl("tableName", " ");
+ fail("Exception expected");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSchemaTableColumnConstructor()
+ {
+ ColumnImpl column = new ColumnImpl(
+ "schemaName",
+ "tableName",
+ "columnName");
+ assertEquals("schemaName", column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "tableName.columnName",
+ column.getSqlExpression());
+ }
+
+ public void testSchemaTableColumnConstructorWithSchemaInConstructorAndTable()
+ {
+ ColumnImpl column = new ColumnImpl(
+ "schemaName",
+ "otherSchema.tableName",
+ "columnName");
+ assertEquals("schemaName", column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "tableName.columnName",
+ column.getSqlExpression());
+ }
+
+ public void testSchemaTableColumnConstructorWithSchemaInTable()
+ {
+ ColumnImpl column = new ColumnImpl(
+ null,
+ "schemaName.tableName",
+ "columnName");
+ assertEquals("schemaName", column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "tableName.columnName",
+ column.getSqlExpression());
+ }
+
+ public void testSchemaTableColumnConstructorNullSchemaName()
+ {
+ ColumnImpl column = new ColumnImpl(
+ null,
+ "tableName",
+ "columnName");
+ assertEquals(null, column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals("tableName.columnName", column.getSqlExpression());
+ }
+
+ public void testSchemaTableColumnConstructorNullTableName()
+ {
+ ColumnImpl column = new ColumnImpl("schemaName", null, "columnName");
+ assertEquals("schemaName", column.getSchemaName());
+ assertEquals(null, column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals("columnName", column.getSqlExpression());
+ }
+
+ public void testSchemaTableColumnConstructorBlankSchemaName()
+ {
+ try
+ {
+ new ColumnImpl(" ", "tableName", "columnName");
+ fail("Exception expected");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSchemaTableColumnConstructorBlankTableName()
+ {
+ try
+ {
+ new ColumnImpl("schemaName", " ", "columnName");
+ fail("Exception expected");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSchemaTableColumnConstructorNullColumnName()
+ {
+ try
+ {
+ new ColumnImpl("schemaName", "tableName", null);
+ fail("Exception expected");
+ }
+ catch (NullPointerException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSchemaTableColumnConstructorBlankColumnName()
+ {
+ try
+ {
+ new ColumnImpl("schemaName", "tableName", " ");
+ fail("Exception expected");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSqlExpressionConstructor()
+ {
+ ColumnImpl column = new ColumnImpl("schemaName.tableName.columnName");
+ assertEquals("schemaName", column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals("tableName.columnName", column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorNull()
+ {
+ try
+ {
+ new ColumnImpl(null);
+ fail("Exception expected");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSqlExpressionConstructorBlank()
+ {
+ try
+ {
+ new ColumnImpl("");
+ fail("Exception expected");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSqlExpressionConstructorNoDot()
+ {
+ ColumnImpl column = new ColumnImpl("columnName");
+ assertEquals(null, column.getSchemaName());
+ assertEquals(null, column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals("columnName", column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorAsterisk()
+ {
+ ColumnImpl column = new ColumnImpl("tableName.*");
+ assertEquals(null, column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals(null, column.getColumnName());
+ assertEquals("tableName.*", column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorAsteriskAndSchema()
+ {
+ ColumnImpl column = new ColumnImpl("schemaName.tableName.*");
+ assertEquals("schemaName", column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals(null, column.getColumnName());
+ assertEquals("tableName.*", column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorBlankColumnName()
+ {
+ try
+ {
+ new ColumnImpl("tableName.");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSqlExpressionConstructorBlankTableName()
+ {
+ try
+ {
+ new ColumnImpl(".columnName");
+ fail("Exception expected");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSqlExpressionConstructorCount()
+ {
+ ColumnImpl column
+ = new ColumnImpl("count(schemaName.tableName.columnName)");
+ assertEquals("schemaName", column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "count(tableName.columnName)",
+ column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorSqlFunction()
+ {
+ ColumnImpl column = new ColumnImpl(
+ "function(1, schemaName.tableName.columnName ,2)");
+ assertEquals("schemaName", column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "function(1, tableName.columnName ,2)",
+ column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorComparisonAfter()
+ {
+ ColumnImpl column = new ColumnImpl("tableName.columnName < 10");
+ assertEquals(null, column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "tableName.columnName < 10",
+ column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorComparisonBefore()
+ {
+ ColumnImpl column = new ColumnImpl("10 < tableName.columnName");
+ assertEquals(null, column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "10 < tableName.columnName",
+ column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorInClause()
+ {
+ ColumnImpl column = new ColumnImpl("tableName.columnName in (1,2,3)");
+ assertEquals(null, column.getSchemaName());
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "tableName.columnName in (1,2,3)",
+ column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorSqlFunctionAsteriskColumnName()
+ {
+ ColumnImpl column = new ColumnImpl("count(tableName.*)");
+ assertEquals("tableName", column.getTableName());
+ assertEquals(null, column.getColumnName());
+ assertEquals("count(tableName.*)", column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorSqlFunctionAsteriskOnly()
+ {
+ ColumnImpl column = new ColumnImpl("count(*)");
+ assertEquals(null, column.getTableName());
+ assertEquals(null, column.getColumnName());
+ assertEquals("count(*)", column.getSqlExpression());
+ }
+
+ public void testSqlExpressionConstructorSqlFunctionEmptyTableName()
+ {
+ try
+ {
+ new ColumnImpl("count(.columnName)");
+ fail("Exception expected");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testSqlExpressionConstructorAlias()
+ {
+ ColumnImpl column = new ColumnImpl("tableName.columnName AS x");
+ assertEquals("tableName", column.getTableName());
+ assertEquals("columnName", column.getColumnName());
+ assertEquals(
+ "tableName.columnName AS x",
+ column.getSqlExpression());
+ }
+
+ public void testGetSqlExpressionNoSchema()
+ {
+ ColumnImpl column = new ColumnImpl(null, "tableName", "columnName");
+ assertEquals("tableName.columnName", column.getSqlExpression());
+ }
+
+ public void testGetSqlExpressionWithSchema()
+ {
+ ColumnImpl column
+ = new ColumnImpl("schemaName", "tableName", "columnName");
+ assertEquals(
+ "tableName.columnName",
+ column.getSqlExpression());
+ }
+
+ public void testEquals()
+ {
+ ColumnImpl column1 = new ColumnImpl(
+ "schemaName",
+ "tableName",
+ "columnName",
+ "tableName.columnName AS x");
+ ColumnImpl column2 = new ColumnImpl(
+ "schemaName",
+ "tableName",
+ "columnName",
+ "tableName.columnName AS x");
+ assertTrue(column1.equals(column2));
+ }
+
+ public void testEqualsSchemaTableColumnNull()
+ {
+ ColumnImpl column1 = new ColumnImpl(
+ null,
+ null,
+ null,
+ "*");
+ ColumnImpl column2 = new ColumnImpl(
+ null,
+ null,
+ null,
+ "*");
+ assertTrue(column1.equals(column2));
+ }
+
+ public void testEqualsWrongColumn()
+ {
+ ColumnImpl column1 = new ColumnImpl(
+ "schemaName",
+ "tableName",
+ "columnName1");
+ ColumnImpl column2 = new ColumnImpl(
+ "schemaName",
+ "tableName",
+ "columnName2");
+ assertFalse(column1.equals(column2));
+ }
+
+ public void testEqualsWrongTable()
+ {
+ ColumnImpl column1 = new ColumnImpl(
+ "schemaName",
+ "tableName1",
+ "columnName");
+ ColumnImpl column2 = new ColumnImpl(
+ "schemaName",
+ "tableName2",
+ "columnName");
+ assertFalse(column1.equals(column2));
+ }
+
+ public void testEqualsWrongSchema()
+ {
+ ColumnImpl column1 = new ColumnImpl(
+ "schemaName1",
+ "tableName",
+ "columnName");
+ ColumnImpl column2 = new ColumnImpl(
+ "schemaName2",
+ "tableName",
+ "columnName");
+ assertFalse(column1.equals(column2));
+ }
+
+ public void testEqualsWrongSqlExpression()
+ {
+ ColumnImpl column1 = new ColumnImpl("tableName.columnName AS x");
+ ColumnImpl column2 = new ColumnImpl("tableName.columnName AS y");
+ assertFalse(column1.equals(column2));
+ }
+
+ public void testToString()
+ {
+ ColumnImpl column = new ColumnImpl(
+ "schemaName1.tableName1.columnName1 AS x");
+ assertEquals("ColumnImpl [columnName=columnName1, tableName="
+ + "tableName1, schemaName=schemaName1, "
+ + "sqlExpression=tableName1.columnName1 AS x]",
+ column.toString());
+ }
+}
Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java Sat Nov 26 10:59:15 2011
@@ -1,20 +1,5 @@
package org.apache.torque;
-import java.util.Map;
-
-import junit.framework.TestCase;
-
-import org.apache.commons.configuration.Configuration;
-import org.apache.commons.configuration.ConfigurationException;
-import org.apache.commons.configuration.PropertiesConfiguration;
-import org.apache.torque.adapter.DB;
-import org.apache.torque.dsfactory.DataSourceFactory;
-import org.apache.torque.map.ColumnMap;
-import org.apache.torque.map.DatabaseMap;
-import org.apache.torque.map.MapBuilder;
-import org.apache.torque.map.TableMap;
-import org.apache.torque.util.BasePeer;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -34,6 +19,16 @@ import org.apache.torque.util.BasePeer;
* under the License.
*/
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.torque.adapter.DB;
+import org.apache.torque.dsfactory.DataSourceFactory;
+
/**
* Tests the TorqueInstance Class.
*
@@ -61,19 +56,16 @@ public class TorqueInstanceTest extends
}
/**
- * Tests whether registration of Map Builders works before and after
- * initialisation of Torque.
- * @throws Exception if an error occurs during the Test.
+ * Performs initialisations before the test.
+ *
+ * @throws Exception if the initialisation fails.
*/
- public void testClassLoading() throws Exception
+ protected void setUp() throws Exception
{
- Torque.registerMapBuilder(MapBuilderA.CLASS_NAME);
- Torque.init(getConfiguration());
- BasePeer.getMapBuilder(MapBuilderB.CLASS_NAME);
-
- DatabaseMap databaseMap = Torque.getDatabaseMap(Torque.getDefaultDB());
- assertNotNull(databaseMap.getTable(MapBuilderA.TABLE_NAME));
- assertNotNull(databaseMap.getTable(MapBuilderB.TABLE_NAME));
+ if (!Torque.isInit())
+ {
+ Torque.init(getConfiguration());
+ }
}
/**
@@ -161,115 +153,11 @@ public class TorqueInstanceTest extends
* @return
* @throws ConfigurationException
*/
- private Configuration getConfiguration() throws ConfigurationException
+ private static Configuration getConfiguration()
+ throws ConfigurationException
{
Configuration conf
= new PropertiesConfiguration(BaseTestCase.CONFIG_FILE);
return conf;
}
-
- /**
- * The base class for the Map Builders used in this testbed.
- */
- public abstract static class MapBuilderBase implements MapBuilder
- {
-
- /** The name of the associated table. */
- private String tableName;
-
- /** The database map. */
- private DatabaseMap dbMap = null;
-
- /**
- * Constructs a MapBuilder.
- * @param tableName the name of the table to register.
- */
- public MapBuilderBase(String tableName)
- {
- this.tableName = tableName;
- }
-
- /**
- * Tells us if this DatabaseMapBuilder is built so that we
- * don't have to re-build it every time.
- *
- * @return true if this DatabaseMapBuilder is built
- */
- public boolean isBuilt()
- {
- return (dbMap != null);
- }
-
- /**
- * Gets the databasemap this map builder built.
- *
- * @return the databasemap
- */
- public DatabaseMap getDatabaseMap()
- {
- return this.dbMap;
- }
-
- /**
- * Builds the DatabaseMap.
- *
- * @throws TorqueException in an error occurs during building.
- */
- public void doBuild() throws TorqueException
- {
- dbMap = Torque.getDatabaseMap(MYSQL_NAME);
-
- dbMap.addTable(tableName);
- TableMap tMap = dbMap.getTable(tableName);
-
- tMap.setPrimaryKeyMethod(TableMap.NATIVE);
-
- tMap.setPrimaryKeyMethodInfo(tableName);
-
- ColumnMap idColumn = new ColumnMap(tableName + "ID", tMap);
- idColumn.setType(new Integer(0));
- idColumn.setPrimaryKey(true);
- tMap.addColumn(idColumn);
- ColumnMap nameColumn = new ColumnMap(tableName + "ID", tMap);
- nameColumn.setType("");
- nameColumn.setSize(50);
- tMap.addColumn(nameColumn);
- }
- }
-
- /**
- * Map builder implementation for testing.
- */
- public static class MapBuilderA extends MapBuilderBase implements MapBuilder
- {
- /** The name of this class. */
- public static final String CLASS_NAME =
- MapBuilderA.class.getName();
-
- /** The name of the associated table. */
- public static final String TABLE_NAME = "a";
-
- public MapBuilderA()
- {
- super(TABLE_NAME);
- }
- }
-
- /**
- * Second map builder implementation for testing.
- */
- public static class MapBuilderB extends MapBuilderBase implements MapBuilder
- {
- /** The name of this class. */
- public static final String CLASS_NAME =
- MapBuilderB.class.getName();
-
- /** The name of the associated table. */
- public static final String TABLE_NAME = "b";
-
- public MapBuilderB()
- {
- super(TABLE_NAME);
- }
- }
}
Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueRuntimeExceptionTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueRuntimeExceptionTest.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueRuntimeExceptionTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueRuntimeExceptionTest.java Sat Nov 26 10:59:15 2011
@@ -28,16 +28,6 @@ import java.io.StringWriter;
public class TorqueRuntimeExceptionTest extends BaseTestCase
{
/**
- * Creates a new instance.
- *
- * @param name the name of the test case.
- */
- public TorqueRuntimeExceptionTest(String name)
- {
- super(name);
- }
-
- /**
* Tests whether printstackTrace works.
*/
public void testPrintStackTrace()
@@ -57,6 +47,6 @@ public class TorqueRuntimeExceptionTest
"org.apache.torque.TorqueRuntimeExceptionTest.testPrintStackTrace")
> 0);
}
-
+
}
}
Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/avalon/AvalonTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/avalon/AvalonTest.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/avalon/AvalonTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/avalon/AvalonTest.java Sat Nov 26 10:59:15 2011
@@ -18,8 +18,8 @@ package org.apache.torque.avalon;
* under the License.
*/
-import org.apache.avalon.framework.component.ComponentException;
import org.apache.fulcrum.testcontainer.BaseUnitTest;
+import org.apache.torque.BaseTestCase;
/**
* Basic testing of the Torque Avalon Component
@@ -38,30 +38,39 @@ public class AvalonTest extends BaseUnit
*/
public AvalonTest(String name)
{
- super( name );
+ super(name);
}
public void setUp() throws Exception
{
- setConfigurationFileName("src/test/resources/TestComponentConfig.xml");
- setRoleFileName("src/test/resources/TestRoleConfig.xml");
+ }
+
+ /**
+ * Verifies that the container initialization and lookup works properly.
+ */
+ public void testAvalonTorqueNotInitialized() throws Exception
+ {
+ setConfigurationFileName("src/test/resources/TestComponentConfig.xml");
+ setRoleFileName("src/test/resources/TestRoleConfig.xml");
super.setUp();
- try
- {
- torque = (Torque) this.resolve( Torque.class.getName() );
- }
- catch (ComponentException e)
- {
- e.printStackTrace();
- fail(e.getMessage());
- }
+ torque = (Torque) this.resolve(Torque.class.getName());
+
+ assertTrue(torque.isInit());
+ assertTrue("Instances should be identical", torque == org.apache.torque.Torque.getInstance());
}
/**
* Verifies that the container initialization and lookup works properly.
*/
- public void testAvalon()
+ public void testAvalonTorqueInitialized() throws Exception
{
+ org.apache.torque.Torque.setInstance(null);
+ org.apache.torque.Torque.init(BaseTestCase.CONFIG_FILE);
+ setConfigurationFileName("src/test/resources/TestComponentConfig.xml");
+ setRoleFileName("src/test/resources/TestRoleConfig.xml");
+ super.setUp();
+ torque = (Torque) this.resolve(Torque.class.getName());
+
assertTrue(torque.isInit());
assertTrue("Instances should be identical", torque == org.apache.torque.Torque.getInstance());
}
Added: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/map/ColumnMapTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/map/ColumnMapTest.java?rev=1206426&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/map/ColumnMapTest.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/map/ColumnMapTest.java Sat Nov 26 10:59:15 2011
@@ -0,0 +1,54 @@
+package org.apache.torque.map;
+
+import org.apache.torque.BaseTestCase;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Test for the class ColumnMap.
+ *
+ * @version $Id: $
+ */
+public class ColumnMapTest extends BaseTestCase
+{
+ public void testGetSqlExpressionNoSchemaDefaultSchemaNull()
+ {
+ ColumnMap column = new ColumnMap("columnName", tableMap);
+ assertEquals("TABLE.columnName", column.getSqlExpression());
+ }
+
+ public void testGetFullTableNameWithoutDefaultSchema()
+ {
+ ColumnMap column = new ColumnMap("columnName", tableMap);
+ assertEquals(
+ "TABLE",
+ column.getFullTableName());
+ }
+
+ public void testGetFullTableNameWithDefaultSchema()
+ {
+ tableMap = databaseMap.addTable("schemaName.TABLE");
+ ColumnMap column = new ColumnMap("columnName", tableMap);
+ assertEquals(
+ "schemaName.TABLE",
+ column.getFullTableName());
+ }
+}
Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java Sat Nov 26 10:59:15 2011
@@ -31,16 +31,6 @@ import org.apache.torque.util.UniqueList
*/
public class QueryTest extends BaseTestCase
{
-
- /**
- * Constructor for QueryTest.
- * @param arg0
- */
- public QueryTest(String arg0)
- {
- super(arg0);
- }
-
/**
* Test for String toString()
*/
Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java Sat Nov 26 10:59:15 2011
@@ -23,9 +23,11 @@ import java.lang.reflect.Array;
import java.util.List;
import org.apache.torque.BaseTestCase;
+import org.apache.torque.Database;
import org.apache.torque.TorqueException;
import org.apache.torque.adapter.DB;
import org.apache.torque.adapter.DBFactory;
+import org.apache.torque.util.Criteria;
import org.apache.torque.util.SqlEnum;
/**
@@ -40,18 +42,9 @@ public class SqlBuilderTest extends Base
private DB db = null;
/**
- * Constructor for SqlExpressionTest.
- * @param arg0
- */
- public SqlBuilderTest(String arg0)
- {
- super(arg0);
- }
-
- /**
* set up environment
*/
- public void setUp() throws TorqueException
+ public void setUp() throws Exception
{
super.setUp();
try
@@ -162,95 +155,408 @@ public class SqlBuilderTest extends Base
assertEquals(1, replacements.size());
assertEquals("50%", replacements.get(0));
}
-
- public void testExtractTableName() throws TorqueException
+
+ /**
+ * Test that unknown columns are treated case-insensitive if ignoreCase
+ * is set.
+ */
+ public void testignoreCaseUnknownColumnType() throws Exception
{
- // standard cases with / without schema
- String columnName = "table.column";
- String tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("table", tableName);
-
- columnName = "schema.table.column";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("schema.table", tableName);
-
- // functions
- columnName = "function(table.column)";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("table", tableName);
-
- columnName = "function(1,table.column,2)";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("table", tableName);
-
- // comparisons
- columnName = "table.column < 10";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("table", tableName);
-
- columnName = "table.column<10";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("table", tableName);
-
- columnName = "10 > table.column";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("table", tableName);
-
- columnName = "10>table.column";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("table", tableName);
-
- columnName = "10>table.column";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("table", tableName);
-
- // in clause
- columnName = "table.column in (1,2,3)";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals("table", tableName);
-
- // wildcard
- columnName = "*";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals(null, tableName);
-
- // function with wildcard
- columnName = "count(*)";
- tableName = SqlBuilder.getTableName(columnName, null);
- assertEquals(null, tableName);
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn("unknownTable.column1");
+ criteria.add("column1", "1");
+ criteria.setIgnoreCase(true);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT unknownTable.column1 FROM unknownTable "
+ + "WHERE UPPER(column1)=UPPER(?)",
+ query.toString());
+ }
- // empty String and null
- columnName = "";
- try
- {
- tableName = SqlBuilder.getTableName(columnName, null);
- fail("getTableName() should fail for empty column name");
- }
- catch (TorqueException e)
- {
- }
+ public void testIgnoreCaseStringColumnType() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn(stringColumnMap);
+ criteria.add(stringColumnMap, "1");
+ criteria.setIgnoreCase(true);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT TABLE.COLUMN1 FROM TABLE "
+ + "WHERE UPPER(TABLE.COLUMN1)=UPPER(?)",
+ query.toString());
+ }
- columnName = null;
- try
- {
- tableName = SqlBuilder.getTableName(columnName, null);
- fail("getTableName() should fail for null as column name");
- }
- catch (TorqueException e)
- {
- }
+ public void testIgnoreCaseIntegerColumnType() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn(integerColumnMap);
+ criteria.add(integerColumnMap, "1");
+ criteria.setIgnoreCase(true);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT TABLE.COLUMN4 FROM TABLE "
+ + "WHERE TABLE.COLUMN4=?",
+ query.toString());
+ }
- // failure: no dot or wildcard
- columnName = "column";
- try
- {
- tableName = SqlBuilder.getTableName(columnName, null);
- fail("getTableName() should fail for column name "
- + "without a dot or wildcard");
- }
- catch (TorqueException e)
- {
- }
+ public void testOrderByDesc() throws TorqueException
+ {
+ Criteria criteria = new Criteria();
+ criteria.addDescendingOrderByColumn("table.column1");
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT table.column1 FROM table ORDER BY table.column1 DESC",
+ query.toString());
+ }
+
+ public void testOrderByAsc() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAscendingOrderByColumn("table.column1");
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT table.column1 FROM table ORDER BY table.column1 ASC",
+ query.toString());
+ }
+
+ public void testMultipleOrderBy() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAscendingOrderByColumn("table.column1");
+ criteria.addDescendingOrderByColumn("table2.column2");
+ criteria.addAscendingOrderByColumn("table3.column1");
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT table.column1, table2.column2, table3.column1"
+ + " FROM table, table2, table3"
+ + " ORDER BY table.column1 ASC,"
+ + " table2.column2 DESC,"
+ + " table3.column1 ASC",
+ query.toString());
+ }
+
+ public void testOrderByWithDefaultSchema() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAscendingOrderByColumn("table.column1");
+ Database database = databaseMap.getDatabase();
+ database.setSchema("schema1");
+ criteria.setDbName(database.getName());
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT table.column1 FROM schema1.table "
+ + "ORDER BY table.column1 ASC",
+ query.toString());
+ }
+
+ public void testOrderByIgnoreCase()
+ {
+ // TODO implement
+ }
+
+ public void testAlias() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAlias("alias", "table");
+ criteria.addSelectColumn("alias.column1");
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.column1 FROM table alias",
+ query.toString());
+ }
+
+ public void testAliasWithDefaultSchema() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAlias("alias", "table");
+ criteria.addSelectColumn("alias.column1");
+ Database database = databaseMap.getDatabase();
+ database.setSchema("schema1");
+ criteria.setDbName(database.getName());
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.column1 FROM schema1.table alias",
+ query.toString());
+ }
+
+ public void testAliasWithIgnoreCaseUnknownColumnType() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAlias("alias", "table");
+ criteria.addSelectColumn("alias.column1");
+ criteria.add("alias.column1", "1");
+ criteria.setIgnoreCase(true);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.column1 FROM table alias "
+ + "WHERE UPPER(alias.column1)=UPPER(?)",
+ query.toString());
+ }
+
+ public void testAliasWithIgnoreCaseStringColumnType() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAlias("alias", tableMap.getName());
+ criteria.addSelectColumn("alias.COLUMN1");
+ criteria.add("alias.COLUMN1", "1");
+ criteria.setIgnoreCase(true);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.COLUMN1 FROM TABLE alias "
+ + "WHERE UPPER(alias.COLUMN1)=UPPER(?)",
+ query.toString());
+ }
+
+ public void testAliasWithIgnoreCaseIntegerColumnType() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAlias("alias", tableMap.getName());
+ criteria.addSelectColumn("alias.COLUMN4");
+ criteria.add("alias.COLUMN4", "1");
+ criteria.setIgnoreCase(true);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.COLUMN4 FROM TABLE alias "
+ + "WHERE alias.COLUMN4=?",
+ query.toString());
+ }
+
+ public void testAliasWithIgnoreCaseStringColumnTypeAndDefaultSchema()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAlias("alias", tableMap.getName());
+ criteria.addSelectColumn("alias.COLUMN1");
+ criteria.add("alias.COLUMN1", "1");
+ criteria.setIgnoreCase(true);
+ Database database = databaseMap.getDatabase();
+ database.setSchema("schema1");
+ criteria.setDbName(database.getName());
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.COLUMN1 FROM schema1.TABLE alias "
+ + "WHERE UPPER(alias.COLUMN1)=UPPER(?)",
+ query.toString());
+ }
+
+ public void testAliasWithIgnoreCaseIntegerColumnTypeAndDefaultSchema()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAlias("alias", tableMap.getName());
+ criteria.addSelectColumn("alias.COLUMN4");
+ criteria.add("alias.COLUMN4", "1");
+ criteria.setIgnoreCase(true);
+ Database database = databaseMap.getDatabase();
+ database.setSchema("schema1");
+ criteria.setDbName(database.getName());
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.COLUMN4 FROM schema1.TABLE alias "
+ + "WHERE alias.COLUMN4=?",
+ query.toString());
+ }
+
+ public void testAsColumn() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAsColumn("columnAlias", stringColumnMap);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT TABLE.COLUMN1 AS columnAlias FROM TABLE",
+ query.toString());
+ }
+
+ public void testAsColumnWithIgnoreCaseUnknownColumn() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAsColumn("columnAlias", "table.column");
+ criteria.add("columnAlias", "1");
+ criteria.setIgnoreCase(true);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT table.column AS columnAlias FROM table"
+ + " WHERE UPPER(columnAlias)=UPPER(?)",
+ query.toString());
+ }
+
+ public void testAsColumnWithIgnoreCaseStringColumn() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAsColumn("columnAlias", stringColumnMap);
+ criteria.add("columnAlias", "1");
+ criteria.setIgnoreCase(true);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT TABLE.COLUMN1 AS columnAlias FROM TABLE"
+ + " WHERE UPPER(columnAlias)=UPPER(?)",
+ query.toString());
+ }
+
+ public void testAsColumnWithIgnoreCaseIntegerColumn() throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAsColumn("columnAlias", integerColumnMap);
+ criteria.add("columnAlias", "1");
+ criteria.setIgnoreCase(true);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT TABLE.COLUMN4 AS columnAlias FROM TABLE"
+ + " WHERE columnAlias=?",
+ query.toString());
+ }
+
+ public void testAsColumnWithIgnoreCaseStringColumnAndDefaultSchema()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAsColumn("columnAlias", stringColumnMap);
+ criteria.addSelectColumn("columnAlias");
+ criteria.add("columnAlias", "1");
+ criteria.setIgnoreCase(true);
+ Database database = databaseMap.getDatabase();
+ database.setSchema("schema1");
+ criteria.setDbName(database.getName());
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT TABLE.COLUMN1 AS columnAlias FROM schema1.TABLE"
+ + " WHERE UPPER(columnAlias)=UPPER(?)",
+ query.toString());
+ }
+ public void testAsColumnWithIgnoreCaseIntegerColumnAndDefaultSchema()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAsColumn("columnAlias", integerColumnMap);
+ criteria.addSelectColumn("columnAlias");
+ criteria.add("columnAlias", "1");
+ criteria.setIgnoreCase(true);
+ Database database = databaseMap.getDatabase();
+ database.setSchema("schema1");
+ criteria.setDbName(database.getName());
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT TABLE.COLUMN4 AS columnAlias FROM schema1.TABLE"
+ + " WHERE columnAlias=?",
+ query.toString());
+ }
+
+ public void testInnerJoin()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn("table1.column1");
+ criteria.addJoin("table1.column", "table2.column");
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT table1.column1 FROM table1, table2"
+ + " WHERE table1.column=table2.column",
+ query.toString());
+ }
+
+ public void testLeftJoin()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn("table1.column1");
+ criteria.addJoin("table1.column", "table2.column", Criteria.LEFT_JOIN);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT table1.column1"
+ + " FROM table1 LEFT JOIN table2"
+ + " ON table1.column=table2.column",
+ query.toString());
+ }
+
+ public void testRightJoin()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn("table1.column1");
+ criteria.addJoin("table1.column", "table2.column", Criteria.RIGHT_JOIN);
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT table1.column1"
+ + " FROM table1 RIGHT JOIN table2"
+ + " ON table1.column=table2.column",
+ query.toString());
+ }
+
+ public void testInnerJoinWithAlias()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn("alias.column1");
+ criteria.addAlias("alias", "table1");
+ criteria.addJoin("alias.column", "table2.column");
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.column1 FROM table1 alias, table2"
+ + " WHERE alias.column=table2.column",
+ query.toString());
+ }
+
+ public void testInnerJoinWithAliasAndAsColumn()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAsColumn("x", "alias.column");
+ criteria.addAlias("alias", "table1");
+ criteria.addJoin("x", "table2.column");
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.column AS x FROM table2, table1 alias"
+ + " WHERE x=table2.column",
+ query.toString());
+ }
+
+
+ public void testInnerJoinWithDefaultSchema()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn("*");
+ criteria.addJoin("table1.column", "table2.column");
+ Database database = databaseMap.getDatabase();
+ database.setSchema("schema1");
+ criteria.setDbName(database.getName());
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT *"
+ + " FROM schema1.table1, schema1.table2"
+ + " WHERE table1.column=table2.column",
+ query.toString());
+ }
+
+ public void testInnerJoinWithAliasAndDefaultSchema()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn("alias.column1");
+ criteria.addAlias("alias", "table1");
+ criteria.addJoin("alias.column", "table2.column");
+ Database database = databaseMap.getDatabase();
+ database.setSchema("schema1");
+ criteria.setDbName(database.getName());
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.column1 FROM schema1.table1 alias, schema1.table2"
+ + " WHERE alias.column=table2.column",
+ query.toString());
+ }
+
+ public void testInnerJoinWithAliasAndSchema()
+ throws Exception
+ {
+ Criteria criteria = new Criteria();
+ criteria.addSelectColumn("schema1.alias.column1");
+ criteria.addAlias("alias", "table1");
+ criteria.addJoin("schema1.alias.column", "schema2.table2.column");
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals(
+ "SELECT alias.column1 FROM schema1.table1 alias, schema2.table2"
+ + " WHERE alias.column=table2.column",
+ query.toString());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org