db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1224848 [3/4] - in /db/torque/torque4/trunk/torque-runtime/src: main/java/org/apache/torque/criteria/ main/java/org/apache/torque/map/ main/java/org/apache/torque/sql/ main/java/org/apache/torque/util/ test/java/org/apache/torque/criteria/...
Date Tue, 27 Dec 2011 01:17:12 GMT
Added: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java?rev=1224848&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java Tue Dec 27 01:17:11 2011
@@ -0,0 +1,688 @@
+package org.apache.torque.criteria;
+
+/*
+ * 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.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.apache.torque.BaseTestCase;
+import org.apache.torque.Column;
+import org.apache.torque.ColumnImpl;
+import org.apache.torque.Torque;
+import org.apache.torque.TorqueException;
+import org.apache.torque.map.ColumnMap;
+import org.apache.torque.map.DatabaseMap;
+import org.apache.torque.map.TableMap;
+import org.apache.torque.sql.OrderBy;
+import org.apache.torque.sql.Query;
+import org.apache.torque.sql.SqlBuilder;
+
+/**
+ * Test class for Criteria.
+ *
+ * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a>
+ * @author <a href="mailto:sam@neurogrid.com">Sam Joseph</a>
+ * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
+ * @version $Id: CriteriaTest.java 1206426 2011-11-26 10:59:15Z tfischer $
+ */
+public class CriteriaTest extends BaseTestCase
+{
+
+    /** The criteria to use in the test. */
+    private Criteria c;
+
+    /**
+     * Initializes the criteria.
+     */
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        c = new Criteria();
+        DatabaseMap databaseMap = Torque.getDatabaseMap("postgresql");
+        if (!databaseMap.containsTable("TABLE"))
+        {
+            TableMap tableMap = databaseMap.addTable("TABLE");
+            {
+                ColumnMap columnMap1 = new ColumnMap("COLUMN1", tableMap);
+                columnMap1.setType(new String(""));
+                columnMap1.setJavaType("String");
+                tableMap.addColumn(columnMap1);
+            }
+            {
+                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);
+            }
+            {
+                ColumnMap columnMap4 = new ColumnMap("COLUMN4", tableMap);
+                columnMap4.setType(new Integer(0));
+                columnMap4.setJavaType("Integer");
+                tableMap.addColumn(columnMap4);
+            }
+        }
+    }
+
+    /**
+     * Test basic adding of strings.
+     */
+    public void testAddString()
+    {
+        final Column column = new ColumnImpl("myTable", "myColumn");
+        final String value = "myValue";
+
+        // Add the string
+        c.where(column, value);
+
+        // Verify that the Criterion is not composite
+        assertFalse(c.getTopLevelCriterion().isComposite());
+
+        // Verify that what we get out is what we put in
+        assertEquals(column, c.getTopLevelCriterion().getColumn());
+        assertEquals(value, c.getTopLevelCriterion().getValue());
+        assertEquals(Criteria.EQUAL, c.getTopLevelCriterion().getComparison());
+    }
+
+    /**
+     * Test that nesting Criterions works for equals comparison.
+     */
+    public void testNestedCriterionComparisonEqual() throws TorqueException
+    {
+        final Column column2 = new ColumnImpl("myTable2", "myColumn2");
+        final String value2 = "myValue2";
+
+        final Column column3 = new ColumnImpl("myTable3", "myColumn3");
+        final String value3 = "myValue3";
+
+        final Column column4 = new ColumnImpl("myTable4", "myColumn4");
+        final String value4 = "myValue4";
+
+        final Column column5 = new ColumnImpl("myTable5", "myColumn5");
+        final String value5 = "myValue5";
+
+        Criterion crit2 =
+            new Criterion(column2, value2, Criteria.EQUAL);
+        Criterion crit3 =
+            new Criterion(column3, value3, Criteria.EQUAL);
+        Criterion crit4 =
+            new Criterion(column4, value4, Criteria.EQUAL);
+        Criterion crit5 =
+            new Criterion(column5, value5, Criteria.EQUAL);
+
+        crit2.and(crit3).or(crit4.and(crit5));
+        c.where(crit2);
+        c.addSelectColumn(new ColumnImpl(null, "myTable2", null, "*"));
+
+        String expect =
+            "SELECT * FROM myTable2, myTable3, myTable4, myTable5 WHERE "
+            + "((myTable2.myColumn2=? "
+                + "AND myTable3.myColumn3=?) "
+            + "OR (myTable4.myColumn4=? "
+                + "AND myTable5.myColumn5=?))";
+        Query result = SqlBuilder.buildQuery(c);
+        assertEquals(expect, result.toString());
+        List<Object> preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(4, preparedStatementReplacements.size());
+        assertEquals("myValue2", preparedStatementReplacements.get(0));
+        assertEquals("myValue3", preparedStatementReplacements.get(1));
+        assertEquals("myValue4", preparedStatementReplacements.get(2));
+        assertEquals("myValue5", preparedStatementReplacements.get(3));
+    }
+
+    /**
+     * Test that nesting Criterions works for other comparisons than equal.
+     */
+    public void testNestedCriterionComparisonLessGreaterThan()
+            throws TorqueException
+    {
+        final Column column2 = new ColumnImpl("myTable2", "myColumn2");
+        final String value2 = "myValue2";
+
+        final Column column3 = new ColumnImpl("myTable3", "myColumn3");
+        final String value3 = "myValue3";
+
+        final Column column4 = new ColumnImpl("myTable4", "myColumn4");
+        final String value4 = "myValue4";
+
+        final Column column5 = new ColumnImpl("myTable5", "myColumn5");
+        final String value5 = "myValue5";
+
+        c = new Criteria();
+        Criterion crit2 = new Criterion(
+                column2,
+                value2,
+                Criteria.LESS_THAN);
+        Criterion crit3 = new Criterion(
+                column3,
+                value3,
+                Criteria.LESS_EQUAL);
+        Criterion crit4 = new Criterion(
+                column4,
+                value4,
+                Criteria.GREATER_THAN);
+        Criterion crit5 = new Criterion(
+                column5,
+                value5,
+                Criteria.GREATER_EQUAL);
+
+        crit2.and(crit3).or(crit4).and(crit5);
+        c.where(crit2);
+        c.addSelectColumn(new ColumnImpl(null, "myTable2", null, "*"));
+        String expect =
+            "SELECT * FROM myTable2, myTable3, myTable4, myTable5 WHERE "
+                + "(((myTable2.myColumn2<? "
+                    + "AND myTable3.myColumn3<=?) "
+                + "OR myTable4.myColumn4>?) "
+                    + "AND myTable5.myColumn5>=?)";
+        Query result = SqlBuilder.buildQuery(c);
+        assertEquals(expect, result.toString());
+        List<Object> preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(4, preparedStatementReplacements.size());
+        assertEquals("myValue2", preparedStatementReplacements.get(0));
+        assertEquals("myValue3", preparedStatementReplacements.get(1));
+        assertEquals("myValue4", preparedStatementReplacements.get(2));
+        assertEquals("myValue5", preparedStatementReplacements.get(3));
+    }
+
+    /**
+     * Tests &lt;= and =&gt;.
+     */
+    public void testBetweenCriterion() throws TorqueException
+    {
+        Criterion cn1 = new Criterion(
+                new ColumnImpl("INVOICE", "COST"),
+                -1,
+                Criteria.GREATER_EQUAL);
+        Criterion cn2 = new Criterion(
+                new ColumnImpl("INVOICE", "COST"),
+                1,
+                Criteria.LESS_EQUAL);
+        c.where(cn1).and(cn2);
+        String expect =
+            "SELECT  FROM INVOICE WHERE "
+            + "(INVOICE.COST>=? AND INVOICE.COST<=?)";
+        Query result = SqlBuilder.buildQuery(c);
+
+        assertEquals(expect, result.toString());
+        assertEquals(2, result.getPreparedStatementReplacements().size());
+        assertEquals(-1, result.getPreparedStatementReplacements().get(0));
+        assertEquals(1, result.getPreparedStatementReplacements().get(1));
+    }
+
+    /**
+     * Test Criterion.setIgnoreCase().
+     */
+    public void testCriterionIgnoreCase() throws TorqueException
+    {
+        Criterion criterion1 = new Criterion(
+                new ColumnImpl("TABLE", "COLUMN1"), "FoObAr1", Criteria.LIKE);
+        criterion1.setIgnoreCase(true);
+        Criterion criterion2 = new Criterion(
+                new ColumnImpl("TABLE", "COLUMN2"), "FoObAr2", Criteria.EQUAL);
+        criterion2.setIgnoreCase(true);
+        Criterion criterion3 = new Criterion(
+                new ColumnImpl("TABLE", "COLUMN3"), "FoObAr3", Criteria.EQUAL);
+        Criterion criterion4 = new Criterion(
+                new ColumnImpl("TABLE", "COLUMN4"), new Integer(1), Criteria.EQUAL);
+        criterion4.setIgnoreCase(true);
+        c.where(criterion1.and(criterion2).and(criterion3).and(criterion4));
+        c.addSelectColumn(new ColumnImpl(null, "TABLE", null, "*"));
+        c.setDbName("postgresql");
+        Query result = SqlBuilder.buildQuery(c);
+
+        String expect = "SELECT * FROM TABLE WHERE "
+            + "(UPPER(TABLE.COLUMN1)=UPPER(?)"
+            + " AND UPPER(TABLE.COLUMN2)=UPPER(?)"
+            + " AND TABLE.COLUMN3=? AND TABLE.COLUMN4=?)";
+        assertEquals(expect, result.toString());
+        List<Object> replacements = result.getPreparedStatementReplacements();
+        assertEquals(4, replacements.size());
+        assertEquals("FoObAr1", replacements.get(0));
+        assertEquals("FoObAr2", replacements.get(1));
+        assertEquals("FoObAr3", replacements.get(2));
+        assertEquals(new Integer(1), replacements.get(3));
+    }
+
+    /**
+     * Test that true is evaluated correctly in Mysql.
+     */
+    public void testBooleanMysql() throws TorqueException
+    {
+        c.where(new ColumnImpl("TABLE", "COLUMN"), true);
+        c.setDbName("mysql");
+
+        Query result = SqlBuilder.buildQuery(c);
+
+        assertEquals(
+                "SELECT  FROM TABLE WHERE TABLE.COLUMN=?",
+                result.toString());
+        List<Object> preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(Boolean.TRUE, preparedStatementReplacements.get(0));
+    }
+
+    /**
+     * Test that true is evaluated correctly in Postgresql.
+     */
+    public void testBooleanPostgresql() throws TorqueException
+    {
+        // test the postgresql variation
+        c.where(new ColumnImpl("TABLE", "COLUMN"), true);
+        c.setDbName("postgresql");
+
+        Query result = SqlBuilder.buildQuery(c);
+
+        assertEquals(
+                "SELECT  FROM TABLE WHERE TABLE.COLUMN=?",
+                result.toString());
+        List<Object> preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(Boolean.TRUE, preparedStatementReplacements.get(0));
+    }
+
+    /**
+     * Testcase for whereDate()
+     */
+    public void testWhereDate() throws TorqueException
+    {
+         c.whereDate(new ColumnImpl("TABLE", "DATE_COLUMN"), 2003, 0, 22);
+
+        Query result = SqlBuilder.buildQuery(c);
+
+        assertEquals(
+                "SELECT  FROM TABLE WHERE TABLE.DATE_COLUMN=?",
+                result.toString());
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(
+                new GregorianCalendar(2003, 0, 22).getTime(),
+                preparedStatementReplacements.get(0));
+    }
+
+    /**
+     * Testcase for andDate().
+     */
+    public void testAndDate() throws TorqueException
+    {
+        c.whereDate(new ColumnImpl("TABLE", "DATE_COLUMN"),
+                2003,
+                0,
+                22,
+                Criteria.GREATER_THAN);
+        c.andDate(new ColumnImpl("TABLE", "DATE_COLUMN"),
+                2004,
+                2,
+                24,
+                Criteria.LESS_THAN);
+
+        Query result = SqlBuilder.buildQuery(c);
+        assertEquals(
+                "SELECT  FROM TABLE WHERE "
+                    + "(TABLE.DATE_COLUMN>? AND TABLE.DATE_COLUMN<?)",
+                result.toString());
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(2, preparedStatementReplacements.size());
+        assertEquals(
+                new GregorianCalendar(2003, 0, 22).getTime(),
+                preparedStatementReplacements.get(0));
+        assertEquals(
+                new GregorianCalendar(2004, 2, 24).getTime(),
+                preparedStatementReplacements.get(1));
+    }
+
+    /**
+     * testcase for where(Date)
+     */
+    public void testDateWhere() throws TorqueException
+    {
+        Calendar cal = new GregorianCalendar(2003, 0, 22);
+        Date date = cal.getTime();
+        c.where(new ColumnImpl("TABLE", "DATE_COLUMN"), date);
+
+        Query result = SqlBuilder.buildQuery(c);
+        assertEquals(
+                "SELECT  FROM TABLE WHERE "
+                    + "TABLE.DATE_COLUMN=?",
+                result.toString());
+
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(
+                new GregorianCalendar(2003, 0, 22).getTime(),
+                preparedStatementReplacements.get(0));
+    }
+
+    public void testCurrentDate() throws TorqueException
+    {
+        c.where(new ColumnImpl("TABLE", "DATE_COLUMN"), Criteria.CURRENT_DATE);
+        c.addSelectColumn(new ColumnImpl(null, "TABLE", null, "COUNT(*)"));
+
+        Query result = SqlBuilder.buildQuery(c);
+        assertEquals(
+                "SELECT COUNT(*) FROM TABLE WHERE "
+                    + "TABLE.DATE_COLUMN=CURRENT_DATE",
+                result.toString());
+
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(0, preparedStatementReplacements.size());
+    }
+
+    public void testCurrentTime() throws TorqueException
+    {
+        c.where(new ColumnImpl("TABLE", "TIME_COLUMN"), Criteria.CURRENT_TIME);
+        c.addSelectColumn(new ColumnImpl(null, "TABLE", null, "COUNT(*)"));
+
+        Query result = SqlBuilder.buildQuery(c);
+        assertEquals(
+                "SELECT COUNT(*) FROM TABLE WHERE "
+                    + "TABLE.TIME_COLUMN=CURRENT_TIME",
+                result.toString());
+
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(0, preparedStatementReplacements.size());
+    }
+
+    public void testCriteriaOffsetLimit() throws TorqueException
+    {
+        c.whereDate(new ColumnImpl("TABLE", "DATE_COLUMN"), 2003, 0, 22);
+        c.setOffset(3).setLimit(5);
+        c.addSelectColumn(new ColumnImpl(null, "TABLE", null, "COUNT(*)"));
+
+        Query result = SqlBuilder.buildQuery(c);
+        String expect
+            = "SELECT COUNT(*) FROM TABLE WHERE TABLE.DATE_COLUMN=?"
+                + " LIMIT 5 OFFSET 3";
+        assertEquals(expect, result.toString());
+    }
+
+    public void testCriteriaWithOffsetNoLimitPostgresql()
+            throws TorqueException
+    {
+        c.whereDate(new ColumnImpl("TABLE", "DATE_COLUMN"), 2003, 0, 22);
+        c.setOffset(3);
+        c.addSelectColumn(new ColumnImpl(null, "TABLE", null, "COUNT(*)"));
+
+        Query result = SqlBuilder.buildQuery(c);
+        String expect
+            = "SELECT COUNT(*) FROM TABLE WHERE TABLE.DATE_COLUMN=?"
+                + " OFFSET 3";
+        assertEquals(expect, result.toString());
+    }
+
+    /**
+     * TORQUE-87
+     */
+    public void testCriteriaWithOffsetNoLimitMysql() throws TorqueException
+    {
+        c.whereDate(new ColumnImpl("TABLE", "DATE_COLUMN"), 2003, 0, 22);
+        c.setOffset(3);
+        c.addSelectColumn(new ColumnImpl(null, "TABLE", null, "COUNT(*)"));
+        c.setDbName("mysql");
+
+        Query result = SqlBuilder.buildQuery(c);
+        String expect
+            = "SELECT COUNT(*) FROM TABLE WHERE TABLE.DATE_COLUMN=?"
+                + " LIMIT 18446744073709551615 OFFSET 3";
+        assertEquals(expect, result.toString());
+    }
+
+    public void testCriteriaToStringLimit() throws TorqueException
+    {
+        c.whereDate(new ColumnImpl("TABLE", "DATE_COLUMN"), 2003, 0, 22);
+        c.setLimit(5);
+        c.addSelectColumn(new ColumnImpl(null, "TABLE", null, "COUNT(*)"));
+
+        Query result = SqlBuilder.buildQuery(c);
+        String expect
+            = "SELECT COUNT(*) FROM TABLE WHERE TABLE.DATE_COLUMN=?"
+                + " LIMIT 5";
+        assertEquals(expect, result.toString());
+    }
+
+    /**
+     * This test case verifies if the Criteria.LIKE comparison type will
+     * get replaced through Criteria.EQUAL if there are no SQL wildcards
+     * in the given value.
+     */
+    public void testLikeWithoutWildcards() throws TorqueException
+    {
+        c.where(new ColumnImpl("TABLE", "COLUMN"),
+                "no wildcards",
+                Criteria.LIKE);
+
+        Query result = SqlBuilder.buildQuery(c);
+        assertEquals(
+                "SELECT  FROM TABLE WHERE "
+                    + "TABLE.COLUMN=?",
+                result.toString());
+
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(
+                "no wildcards",
+                preparedStatementReplacements.get(0));
+    }
+
+    /**
+     * This test case verifies if the Criteria.NOT_LIKE comparison type will
+     * get replaced through Criteria.NOT_EQUAL if there are no SQL wildcards
+     * in the given value.
+     */
+    public void testNotLikeWithoutWildcards()
+    {
+        c.where(new ColumnImpl("TABLE", "COLUMN"),
+                "no wildcards",
+                Criteria.NOT_LIKE);
+
+        String firstExpect = "SELECT  FROM TABLE WHERE TABLE.COLUMN!=?";
+        String secondExpect = "SELECT  FROM TABLE WHERE TABLE.COLUMN<>?";
+
+        Query result = null;
+        try
+        {
+            result = SqlBuilder.buildQuery(c);
+        }
+        catch (TorqueException e)
+        {
+            e.printStackTrace();
+            fail("TorqueException thrown in BasePeer.createQueryString()");
+        }
+
+        assertTrue(result.toString().equals(firstExpect)
+                || result.toString().equals(secondExpect));
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals("no wildcards", preparedStatementReplacements.get(0));
+    }
+
+    /**
+     * Test that serialization works.
+     */
+    public void testSerialization()
+    {
+        c.setOffset(10);
+        c.setLimit(11);
+        c.setIgnoreCase(true);
+        c.setSingleRecord(true);
+        c.setDbName("myDB");
+        c.setAll();
+        c.setDistinct();
+        c.addSelectColumn(new ColumnImpl("Author", "NAME"));
+        c.addSelectColumn(new ColumnImpl("Author", "AUTHOR_ID"));
+        c.addDescendingOrderByColumn(new ColumnImpl("Author", "NAME"));
+        c.addAscendingOrderByColumn(new ColumnImpl("Author", "AUTHOR_ID"));
+        c.addAlias("Writer", "Author");
+        c.addAsColumn("AUTHOR_NAME", new ColumnImpl("Author", "NAME"));
+        c.addJoin(new ColumnImpl("Author", "AUTHOR_ID"),
+                new ColumnImpl("Book", "AUTHOR_ID"),
+                Criteria.INNER_JOIN);
+        c.where(new ColumnImpl("Author", "NAME"), "author%", Criteria.LIKE);
+
+        // Some direct Criterion checks
+        Criterion cn = c.getTopLevelCriterion();
+        cn.setIgnoreCase(true);
+        assertEquals("author%", cn.getValue());
+        assertEquals(Criteria.LIKE, cn.getComparison());
+        Criterion cnDirectClone = (Criterion) SerializationUtils.clone(cn);
+        assertEquals(cn, cnDirectClone);
+
+        // Clone the object
+        Criteria cClone = (Criteria) SerializationUtils.clone(c);
+
+        // Check the clone
+        assertEquals(c.getTopLevelCriterion(), cClone.getTopLevelCriterion());
+        assertEquals(10, cClone.getOffset());
+        assertEquals(c.getOffset(), cClone.getOffset());
+        assertEquals(11, cClone.getLimit());
+        assertEquals(c.getLimit(), cClone.getLimit());
+        assertEquals(true, cClone.isIgnoreCase());
+        assertEquals(c.isIgnoreCase(), cClone.isIgnoreCase());
+        assertEquals(true, cClone.isSingleRecord());
+        assertEquals(c.isSingleRecord(), cClone.isSingleRecord());
+        assertEquals("myDB", cClone.getDbName());
+        assertEquals(c.getDbName(), cClone.getDbName());
+        List<String> selectModifiersClone = cClone.getSelectModifiers();
+        assertTrue(selectModifiersClone.contains(
+                Criteria.ALL.toString()));
+        assertTrue(selectModifiersClone.contains(
+                Criteria.DISTINCT.toString()));
+        assertEquals(c.getSelectModifiers(), cClone.getSelectModifiers());
+        List<Column> selectColumnsClone = cClone.getSelectColumns();
+        assertTrue(selectColumnsClone.contains(
+                new ColumnImpl("Author", "NAME")));
+        assertTrue(selectColumnsClone.contains(
+                new ColumnImpl("Author", "AUTHOR_ID")));
+        assertEquals(c.getSelectColumns(), cClone.getSelectColumns());
+        List<OrderBy> orderByColumnsClone = cClone.getOrderByColumns();
+        assertTrue(orderByColumnsClone.contains(new OrderBy(
+                new ColumnImpl("Author.NAME"),
+                SqlEnum.DESC,
+                false)));
+        assertTrue(orderByColumnsClone.contains(new OrderBy(
+                new ColumnImpl("Author.AUTHOR_ID"),
+                SqlEnum.ASC,
+                false)));
+        assertEquals(c.getOrderByColumns(), cClone.getOrderByColumns());
+        Map<String, String> aliasesClone = cClone.getAliases();
+        assertTrue(aliasesClone.containsKey("Writer"));
+        assertEquals("Author", aliasesClone.get("Writer"));
+        assertEquals(c.getAliases(), cClone.getAliases());
+        Map<String, Column> asColumnsClone = cClone.getAsColumns();
+        assertTrue(asColumnsClone.containsKey("AUTHOR_NAME"));
+        assertEquals(new ColumnImpl("Author", "NAME"),
+                asColumnsClone.get("AUTHOR_NAME"));
+        assertEquals(c.getAsColumns(), cClone.getAsColumns());
+
+        // Check Joins
+        List<Join> joinsClone = cClone.getJoins();
+        Join joinClone = joinsClone.get(0);
+        assertEquals(new ColumnImpl("Author", "AUTHOR_ID"),
+                joinClone.getLeftColumn());
+        assertEquals(new ColumnImpl("Book", "AUTHOR_ID"),
+                joinClone.getRightColumn());
+        assertEquals(Criteria.INNER_JOIN, joinClone.getJoinType());
+        assertEquals(c.getJoins(), cClone.getJoins());
+
+        // Some Criterion checks
+        Criterion cnClone = cClone.getTopLevelCriterion();
+        assertEquals("author%", cnClone.getValue());
+        assertEquals(Criteria.LIKE, cnClone.getComparison());
+        assertEquals(cn.isIgnoreCase(), cnClone.isIgnoreCase());
+
+        // Confirm that equals() checks all of the above.
+        assertEquals(c, cClone);
+
+        // Check hashCode() too.
+        assertEquals(c.hashCode(), cClone.hashCode());
+    }
+
+    /**
+     * Test that {@link Criteria#equals(Object)} works correctly for a simple
+     * Criteria object.
+     * @throws TorqueException
+     */
+    public void testEquals() throws TorqueException
+    {
+        c.addSelectColumn(new ColumnImpl("Author", "NAME"));
+        c.addSelectColumn(new ColumnImpl("Author", "AUTHOR_ID"));
+        c.where(new ColumnImpl("Author", "NAME"), "foobar");
+        Criteria cClone = (Criteria) SerializationUtils.clone(c);
+        assertTrue(c.equals(cClone));
+    }
+
+    /**
+     * Checks whether orderBy works.
+     */
+    public void testOrderBy() throws TorqueException
+    {
+        // we need a rudimentary databaseMap for this test case to work
+        DatabaseMap dbMap = Torque.getDatabaseMap(Torque.getDefaultDB());
+
+        TableMap tableMap = dbMap.addTable("AUTHOR");
+
+        ColumnMap columnMap = new ColumnMap("NAME", tableMap);
+        columnMap.setType("");
+        tableMap.addColumn(columnMap);
+
+        columnMap = new ColumnMap("AUTHOR_ID", tableMap);
+        columnMap.setType(new Integer(0));
+        tableMap.addColumn(columnMap);
+
+        // check that alias'ed tables are referenced by their alias
+        // name when added to the select clause.
+        c.addSelectColumn(new ColumnImpl("AUTHOR", "NAME"));
+        c.addAlias("a", "AUTHOR");
+        c.addJoin(new ColumnImpl("AUTHOR", "AUTHOR_ID"),
+                new ColumnImpl("a", "AUTHOR_ID"));
+        c.addAscendingOrderByColumn(
+                new ColumnImpl("a", "NAME"));
+
+        Query result = SqlBuilder.buildQuery(c);
+        assertEquals("SELECT AUTHOR.NAME, a.NAME "
+                    + "FROM AUTHOR, AUTHOR a "
+                    + "WHERE AUTHOR.AUTHOR_ID=a.AUTHOR_ID "
+                    + "ORDER BY a.NAME ASC",
+                result.toString());
+        List<Object> preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(0, preparedStatementReplacements.size());
+    }
+}

Added: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriterionTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriterionTest.java?rev=1224848&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriterionTest.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriterionTest.java Tue Dec 27 01:17:11 2011
@@ -0,0 +1,27 @@
+package org.apache.torque.criteria;
+
+import org.apache.torque.BaseTestCase;
+import org.apache.torque.ColumnImpl;
+
+/**
+ * Test for the Criterion class.
+ *
+ * @version $Id: $
+ */
+public class CriterionTest extends BaseTestCase
+{
+
+    public void testHashCodeAndEquals()
+    {
+        Criterion criterion = new Criterion(
+                new ColumnImpl("myTable", "myColumn"),
+                "myValue",
+                Criteria.GREATER_EQUAL);
+        Criterion compareToCriterion = new Criterion(
+                new ColumnImpl("myTable", "myColumn"),
+                "myValue",
+                Criteria.GREATER_EQUAL);
+        assertEquals(criterion.hashCode(), compareToCriterion.hashCode());
+        assertEquals(criterion, compareToCriterion);
+    }
+}

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=1224848&r1=1224847&r2=1224848&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 Tue Dec 27 01:17:11 2011
@@ -26,10 +26,12 @@ import java.util.List;
 
 import org.apache.torque.BaseTestCase;
 import org.apache.torque.Column;
+import org.apache.torque.ColumnImpl;
 import org.apache.torque.Database;
 import org.apache.torque.TorqueException;
+import org.apache.torque.criteria.Criteria;
+import org.apache.torque.criteria.Criterion;
 import org.apache.torque.om.NumberKey;
-import org.apache.torque.util.Criteria;
 
 /**
  * Tests for SqlExpression
@@ -47,8 +49,8 @@ public class SqlBuilderTest extends Base
     public void testignoreCaseUnknownColumnType() throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("unknownTable.column1");
-        criteria.add("column1", "1");
+        criteria.addSelectColumn(new ColumnImpl("unknownTable.column1"));
+        criteria.where(new ColumnImpl("column1"), "1");
         criteria.setIgnoreCase(true);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -61,7 +63,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn(stringColumnMap);
-        criteria.add(stringColumnMap, "1");
+        criteria.where(stringColumnMap, "1");
         criteria.setIgnoreCase(true);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -74,7 +76,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn(integerColumnMap);
-        criteria.add(integerColumnMap, "1");
+        criteria.where(integerColumnMap, "1");
         criteria.setIgnoreCase(true);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -86,7 +88,7 @@ public class SqlBuilderTest extends Base
     public void testOrderByDesc() throws TorqueException
     {
         Criteria criteria = new Criteria();
-        criteria.addDescendingOrderByColumn("table.column1");
+        criteria.addDescendingOrderByColumn(new ColumnImpl("table.column1"));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT table.column1 FROM table ORDER BY table.column1 DESC",
@@ -96,7 +98,7 @@ public class SqlBuilderTest extends Base
     public void testOrderByAsc() throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addAscendingOrderByColumn("table.column1");
+        criteria.addAscendingOrderByColumn(new ColumnImpl("table.column1"));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT table.column1 FROM table ORDER BY table.column1 ASC",
@@ -120,9 +122,9 @@ public class SqlBuilderTest extends Base
     public void testOrderByMultiple() throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addAscendingOrderByColumn("table.column1");
-        criteria.addDescendingOrderByColumn("table2.column2");
-        criteria.addAscendingOrderByColumn("table3.column1");
+        criteria.addAscendingOrderByColumn(new ColumnImpl("table.column1"));
+        criteria.addDescendingOrderByColumn(new ColumnImpl("table2.column2"));
+        criteria.addAscendingOrderByColumn(new ColumnImpl("table3.column1"));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT table.column1, table2.column2, table3.column1"
@@ -136,7 +138,7 @@ public class SqlBuilderTest extends Base
     public void testOrderByWithDefaultSchema() throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addAscendingOrderByColumn("table.column1");
+        criteria.addAscendingOrderByColumn(new ColumnImpl("table.column1"));
         Database database = databaseMap.getDatabase();
         database.setSchema("schema1");
         criteria.setDbName(database.getName());
@@ -255,7 +257,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAlias("alias", "table");
-        criteria.addSelectColumn("alias.column1");
+        criteria.addSelectColumn(new ColumnImpl("alias.column1"));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT alias.column1 FROM table alias",
@@ -266,7 +268,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAlias("alias", "table");
-        criteria.addSelectColumn("alias.column1");
+        criteria.addSelectColumn(new ColumnImpl("alias.column1"));
         Database database = databaseMap.getDatabase();
         database.setSchema("schema1");
         criteria.setDbName(database.getName());
@@ -280,8 +282,8 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAlias("alias", "table");
-        criteria.addSelectColumn("alias.column1");
-        criteria.add("alias.column1", "1");
+        criteria.addSelectColumn(new ColumnImpl("alias.column1"));
+        criteria.where(new ColumnImpl("alias.column1"), "1");
         criteria.setIgnoreCase(true);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -294,8 +296,8 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAlias("alias", tableMap.getName());
-        criteria.addSelectColumn("alias.COLUMN1");
-        criteria.add("alias.COLUMN1", "1");
+        criteria.addSelectColumn(new ColumnImpl("alias.COLUMN1"));
+        criteria.where(new ColumnImpl("alias.COLUMN1"), "1");
         criteria.setIgnoreCase(true);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -308,8 +310,8 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAlias("alias", tableMap.getName());
-        criteria.addSelectColumn("alias.COLUMN4");
-        criteria.add("alias.COLUMN4", "1");
+        criteria.addSelectColumn(new ColumnImpl("alias.COLUMN4"));
+        criteria.where(new ColumnImpl("alias.COLUMN4"), "1");
         criteria.setIgnoreCase(true);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -323,8 +325,8 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAlias("alias", tableMap.getName());
-        criteria.addSelectColumn("alias.COLUMN1");
-        criteria.add("alias.COLUMN1", "1");
+        criteria.addSelectColumn(new ColumnImpl("alias.COLUMN1"));
+        criteria.where(new ColumnImpl("alias.COLUMN1"), "1");
         criteria.setIgnoreCase(true);
         Database database = databaseMap.getDatabase();
         database.setSchema("schema1");
@@ -341,8 +343,8 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAlias("alias", tableMap.getName());
-        criteria.addSelectColumn("alias.COLUMN4");
-        criteria.add("alias.COLUMN4", "1");
+        criteria.addSelectColumn(new ColumnImpl("alias.COLUMN4"));
+        criteria.where(new ColumnImpl("alias.COLUMN4"), "1");
         criteria.setIgnoreCase(true);
         Database database = databaseMap.getDatabase();
         database.setSchema("schema1");
@@ -367,8 +369,8 @@ public class SqlBuilderTest extends Base
     public void testAsColumnWithIgnoreCaseUnknownColumn() throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addAsColumn("columnAlias", "table.column");
-        criteria.add("columnAlias", "1");
+        criteria.addAsColumn("columnAlias", new ColumnImpl("table.column"));
+        criteria.where(new ColumnImpl("columnAlias"), "1");
         criteria.setIgnoreCase(true);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -381,7 +383,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAsColumn("columnAlias", stringColumnMap);
-        criteria.add("columnAlias", "1");
+        criteria.where(new ColumnImpl("columnAlias"), "1");
         criteria.setIgnoreCase(true);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -394,7 +396,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAsColumn("columnAlias", integerColumnMap);
-        criteria.add("columnAlias", "1");
+        criteria.where(new ColumnImpl("columnAlias"), "1");
         criteria.setIgnoreCase(true);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -408,8 +410,8 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAsColumn("columnAlias", stringColumnMap);
-        criteria.addSelectColumn("columnAlias");
-        criteria.add("columnAlias", "1");
+        criteria.addSelectColumn(new ColumnImpl("columnAlias"));
+        criteria.where(new ColumnImpl("columnAlias"), "1");
         criteria.setIgnoreCase(true);
         Database database = databaseMap.getDatabase();
         database.setSchema("schema1");
@@ -426,8 +428,8 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAsColumn("columnAlias", integerColumnMap);
-        criteria.addSelectColumn("columnAlias");
-        criteria.add("columnAlias", "1");
+        criteria.addSelectColumn(new ColumnImpl("columnAlias"));
+        criteria.where(new ColumnImpl("columnAlias"), "1");
         criteria.setIgnoreCase(true);
         Database database = databaseMap.getDatabase();
         database.setSchema("schema1");
@@ -443,8 +445,10 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("table1.column");
-        criteria.addJoin("table1.column1", "table2.column2");
+        criteria.addSelectColumn(new ColumnImpl("table1.column"));
+        criteria.addJoin(
+                new ColumnImpl("table1.column1"),
+                new ColumnImpl("table2.column2"));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT table1.column FROM table1, table2"
@@ -457,10 +461,10 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("table1.column1");
+        criteria.addSelectColumn(new ColumnImpl("table1.column1"));
         criteria.addJoin(
-                "table1.column1",
-                "table2.column2",
+                new ColumnImpl("table1.column1"),
+                new ColumnImpl("table2.column2"),
                 Criteria.INNER_JOIN);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -473,9 +477,15 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("table1.column1");
-        criteria.addJoin("table2.column2", "table3.column3", Criteria.INNER_JOIN);
-        criteria.addJoin("table1.column1", "table2.column2", Criteria.INNER_JOIN);
+        criteria.addSelectColumn(new ColumnImpl("table1.column1"));
+        criteria.addJoin(
+                new ColumnImpl("table2.column2"),
+                new ColumnImpl("table3.column3"),
+                Criteria.INNER_JOIN);
+        criteria.addJoin(
+                new ColumnImpl("table1.column1"),
+                new ColumnImpl("table2.column2"),
+                Criteria.INNER_JOIN);
         Query query = SqlBuilder.buildQuery(criteria);
         // second join condition must be changed in order to satisfy
         // first join condition
@@ -492,10 +502,19 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("table1.column1");
-        criteria.addJoin("table2.column2", "table3.column3", Criteria.INNER_JOIN);
-        criteria.addJoin("table1.column1", "table4.column4", Criteria.INNER_JOIN);
-        criteria.addJoin("table1.column1", "table2.column2", Criteria.INNER_JOIN);
+        criteria.addSelectColumn(new ColumnImpl("table1.column1"));
+        criteria.addJoin(
+                new ColumnImpl("table2.column2"),
+                new ColumnImpl("table3.column3"),
+                Criteria.INNER_JOIN);
+        criteria.addJoin(
+                new ColumnImpl("table1.column1"),
+                new ColumnImpl("table4.column4"),
+                Criteria.INNER_JOIN);
+        criteria.addJoin(
+                new ColumnImpl("table1.column1"),
+                new ColumnImpl("table2.column2"),
+                Criteria.INNER_JOIN);
         try
         {
             SqlBuilder.buildQuery(criteria);
@@ -514,8 +533,11 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("table1.column1");
-        criteria.addJoin("table1.column", "table2.column", Criteria.LEFT_JOIN);
+        criteria.addSelectColumn(new ColumnImpl("table1.column1"));
+        criteria.addJoin(
+                new ColumnImpl("table1.column"),
+                new ColumnImpl("table2.column"),
+                Criteria.LEFT_JOIN);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT table1.column1"
@@ -528,9 +550,16 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("table1.column1");
-        criteria.addJoin("table2.column2", "table3.column3", Criteria.INNER_JOIN);
-        criteria.addJoin("table1.column1", "table2.column2", Criteria.LEFT_JOIN);
+        criteria.addSelectColumn(
+                new ColumnImpl("table1.column1"));
+        criteria.addJoin(
+                new ColumnImpl("table2.column2"),
+                new ColumnImpl("table3.column3"),
+                Criteria.INNER_JOIN);
+        criteria.addJoin(
+                new ColumnImpl("table1.column1"),
+                new ColumnImpl("table2.column2"),
+                Criteria.LEFT_JOIN);
         Query query = SqlBuilder.buildQuery(criteria);
         // left join must be converted to right join to satisfy
         // first join condition
@@ -547,8 +576,11 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("table1.column1");
-        criteria.addJoin("table1.column", "table2.column", Criteria.RIGHT_JOIN);
+        criteria.addSelectColumn(new ColumnImpl("table1.column1"));
+        criteria.addJoin(
+                new ColumnImpl("table1.column"),
+                new ColumnImpl("table2.column"),
+                Criteria.RIGHT_JOIN);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT table1.column1"
@@ -561,9 +593,16 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("table1.column1");
-        criteria.addJoin("table2.column2", "table3.column3", Criteria.INNER_JOIN);
-        criteria.addJoin("table1.column1", "table2.column2", Criteria.RIGHT_JOIN);
+        criteria.addSelectColumn(
+                new ColumnImpl("table1.column1"));
+        criteria.addJoin(
+                new ColumnImpl("table2.column2"),
+                new ColumnImpl("table3.column3"),
+                Criteria.INNER_JOIN);
+        criteria.addJoin(
+                new ColumnImpl("table1.column1"),
+                new ColumnImpl("table2.column2"),
+                Criteria.RIGHT_JOIN);
         Query query = SqlBuilder.buildQuery(criteria);
         // right join must be converted to left join to satisfy
         // first join condition
@@ -580,9 +619,11 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("alias.column1");
+        criteria.addSelectColumn(new ColumnImpl("alias.column1"));
         criteria.addAlias("alias", "table1");
-        criteria.addJoin("alias.column", "table2.column");
+        criteria.addJoin(
+                new ColumnImpl("alias.column"),
+                new ColumnImpl("table2.column"));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT alias.column1 FROM table1 alias, table2"
@@ -594,9 +635,9 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addAsColumn("x", "alias.column");
+        criteria.addAsColumn("x", new ColumnImpl("alias.column"));
         criteria.addAlias("alias", "table1");
-        criteria.addJoin("x", "table2.column");
+        criteria.addJoin(new ColumnImpl("x"), new ColumnImpl("table2.column"));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT alias.column AS x FROM table2, table1 alias"
@@ -609,8 +650,10 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("*");
-        criteria.addJoin("table1.column", "table2.column");
+        criteria.addSelectColumn(new ColumnImpl("*"));
+        criteria.addJoin(
+                new ColumnImpl("table1.column"),
+                new ColumnImpl("table2.column"));
         Database database = databaseMap.getDatabase();
         database.setSchema("schema1");
         criteria.setDbName(database.getName());
@@ -626,9 +669,11 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("alias.column1");
+        criteria.addSelectColumn(new ColumnImpl("alias.column1"));
         criteria.addAlias("alias", "table1");
-        criteria.addJoin("alias.column", "table2.column");
+        criteria.addJoin(
+                new ColumnImpl("alias.column"),
+                new ColumnImpl("table2.column"));
         Database database = databaseMap.getDatabase();
         database.setSchema("schema1");
         criteria.setDbName(database.getName());
@@ -643,9 +688,12 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("schema1.alias.column1");
+        criteria.addSelectColumn(
+                new ColumnImpl("schema1.alias.column1"));
         criteria.addAlias("alias", "table1");
-        criteria.addJoin("schema1.alias.column", "schema2.table2.column");
+        criteria.addJoin(
+                new ColumnImpl("schema1.alias.column"),
+                new ColumnImpl("schema2.table2.column"));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
                 "SELECT alias.column1 FROM schema1.table1 alias, schema2.table2"
@@ -657,7 +705,7 @@ public class SqlBuilderTest extends Base
             throws Exception
     {
         Criteria criteria = new Criteria();
-        criteria.addSelectColumn("table1.column1");
+        criteria.addSelectColumn(new ColumnImpl("table1.column1"));
         criteria.setDistinct();
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals(
@@ -757,7 +805,7 @@ public class SqlBuilderTest extends Base
         criteria.addAsColumn("count", "count(*)");
         criteria.addSelectColumn(stringColumnMap);
         criteria.addHaving(
-                criteria.getNewCriterion("count", 10, Criteria.GREATER_EQUAL));
+                new Criterion("count", 10, Criteria.GREATER_EQUAL));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT TABLE.COLUMN1, count(*) AS count FROM TABLE "
                 + "GROUP BY TABLE.COLUMN1 HAVING count>=10",
@@ -778,7 +826,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column");
-        criteria.add("A", (Object) "A = functionOf(B)", Criteria.CUSTOM);
+        criteria.where("A", "A = functionOf(B)", Criteria.CUSTOM);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table WHERE A = functionOf(B)",
                 query.toString());
@@ -789,7 +837,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column");
-        criteria.add("column", Criteria.CURRENT_DATE);
+        criteria.where("column", Criteria.CURRENT_DATE);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table "
                 + "WHERE column=CURRENT_DATE",
@@ -801,7 +849,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column");
-        criteria.add("column", Criteria.CURRENT_TIME);
+        criteria.where("column", Criteria.CURRENT_TIME);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table "
                 + "WHERE column=CURRENT_TIME",
@@ -813,7 +861,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column");
-        criteria.add("column", new NumberKey(11));
+        criteria.where("column", new NumberKey(11));
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table "
                 + "WHERE column=?",
@@ -828,7 +876,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column");
-        criteria.add("column", null);
+        criteria.where("column", null);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table "
                 + "WHERE column IS NULL",
@@ -840,7 +888,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column");
-        criteria.add("column", (Object) null, Criteria.NOT_EQUAL);
+        criteria.where("column", (Object) null, Criteria.NOT_EQUAL);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table "
                 + "WHERE column IS NOT NULL",
@@ -852,7 +900,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column");
-        criteria.add("column", (Object) null, Criteria.ALT_NOT_EQUAL);
+        criteria.where("column", (Object) null, Criteria.ALT_NOT_EQUAL);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table "
                 + "WHERE column IS NOT NULL",
@@ -864,7 +912,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column");
-        criteria.add("column", (Object) "value ignored", Criteria.ISNULL);
+        criteria.where("column", "value ignored", Criteria.ISNULL);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table "
                 + "WHERE column IS NULL",
@@ -876,7 +924,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column");
-        criteria.add("column", (Object) "value ignored", Criteria.ISNOTNULL);
+        criteria.where("column", "value ignored", Criteria.ISNOTNULL);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table "
                 + "WHERE column IS NOT NULL",
@@ -890,9 +938,9 @@ public class SqlBuilderTest extends Base
         criteria.addSelectColumn("table.column1");
 
         Criteria subquery = new Criteria();
-        subquery.add("table.column2", "value2");
+        subquery.where("table.column2", "value2");
         subquery.addSelectColumn("table.column3");
-        criteria.add("table.column3", subquery);
+        criteria.where("table.column3", subquery);
 
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column1 FROM table "
@@ -908,7 +956,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add("table.column2", (Object) "*v%al_e2?", Criteria.LIKE);
+        criteria.where("table.column2", "*v%al_e2?", Criteria.LIKE);
 
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column1 FROM table "
@@ -927,9 +975,9 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add(
+        criteria.where(
                 "table.column2",
-                (Object) "\\*v\\%al\\_e\\\\*2\\?\\",
+                "\\*v\\%al\\_e\\\\*2\\?\\",
                 Criteria.LIKE);
 
         Query query = SqlBuilder.buildQuery(criteria);
@@ -950,9 +998,9 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add(
+        criteria.where(
                 "table.column2",
-                (Object) "\\*v\\%al\\_e\\\\*2\\?\\",
+                "\\*v\\%al\\_e\\\\*2\\?\\",
                 Criteria.LIKE);
         criteria.setDbName(databaseOracle.getName());
 
@@ -970,7 +1018,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add("table.column2", (Object) "*v%al_e2?", Criteria.LIKE);
+        criteria.where("table.column2", "*v%al_e2?", Criteria.LIKE);
         criteria.setIgnoreCase(true);
 
         Query query = SqlBuilder.buildQuery(criteria);
@@ -987,7 +1035,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add("table.column2", (Object) "value\\\\2", Criteria.LIKE);
+        criteria.where("table.column2", "value\\\\2", Criteria.LIKE);
         criteria.setIgnoreCase(true);
 
         Query query = SqlBuilder.buildQuery(criteria);
@@ -1004,10 +1052,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add(
-                "table.column2",
-                1,
-                Criteria.LIKE);
+        criteria.where("table.column2", 1, Criteria.LIKE);
 
         try
         {
@@ -1025,7 +1070,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add("table.column2", (Object) "*val_e2?", Criteria.NOT_LIKE);
+        criteria.where("table.column2", "*val_e2?", Criteria.NOT_LIKE);
 
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column1 FROM table "
@@ -1041,7 +1086,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add("table.column2", (Object) "*v%al_e2?", Criteria.NOT_LIKE);
+        criteria.where("table.column2", "*v%al_e2?", Criteria.NOT_LIKE);
         criteria.setIgnoreCase(true);
 
         Query query = SqlBuilder.buildQuery(criteria);
@@ -1058,7 +1103,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add("table.column2", (Object) "value\\\\2", Criteria.NOT_LIKE);
+        criteria.where("table.column2", "value\\\\2", Criteria.NOT_LIKE);
         criteria.setIgnoreCase(true);
 
         Query query = SqlBuilder.buildQuery(criteria);
@@ -1075,7 +1120,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add("table.column2", (Object) "*val_e2?", Criteria.ILIKE);
+        criteria.where("table.column2", "*val_e2?", Criteria.ILIKE);
 
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column1 FROM table "
@@ -1091,7 +1136,7 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add("table.column2", (Object) "*val_e2?", Criteria.NOT_ILIKE);
+        criteria.where("table.column2", "*val_e2?", Criteria.NOT_ILIKE);
 
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column1 FROM table "
@@ -1107,40 +1152,39 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add(
-                "table.column2",
-                new String[] {"a", "b", null},
-                Criteria.IN);
+        String[] inValue = new String[] {"a", "b", null};
+        criteria.whereIn("table.column2", inValue.clone());
 
         Query query = SqlBuilder.buildQuery(criteria);
+
         assertEquals("SELECT table.column1 FROM table "
                 + "WHERE table.column2 IN (?,?,?)",
             query.toString());
-        assertEquals(3, query.getPreparedStatementReplacements().size());
-        assertEquals("a", query.getPreparedStatementReplacements().get(0));
-        assertEquals("b", query.getPreparedStatementReplacements().get(1));
-        assertEquals(null, query.getPreparedStatementReplacements().get(2));
+        List<Object> replacements = query.getPreparedStatementReplacements();
+        assertEquals(inValue.length, replacements.size());
+        assertEquals(inValue[0], replacements.get(0));
+        assertEquals(inValue[1], replacements.get(1));
+        assertEquals(inValue[2], replacements.get(2));
     }
 
     public void testCriterionInArrayIgnoreCase() throws Exception
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add(
-                "table.column2",
-                new String[] {"a", "b", null},
-                Criteria.IN);
+        String[] inValue = new String[] {"a", "b", null};
+        criteria.whereIn("table.column2", inValue.clone());
         criteria.setIgnoreCase(true);
 
         Query query = SqlBuilder.buildQuery(criteria);
+
         assertEquals("SELECT table.column1 FROM table "
                 + "WHERE UPPER(table.column2) IN (UPPER(?),UPPER(?),?)",
             query.toString());
         List<Object> replacements = query.getPreparedStatementReplacements();
-        assertEquals(3, replacements.size());
-        assertEquals("a", replacements.get(0));
-        assertEquals("b", replacements.get(1));
-        assertEquals(null, replacements.get(2));
+        assertEquals(inValue.length, replacements.size());
+        assertEquals(inValue[0], replacements.get(0));
+        assertEquals(inValue[1], replacements.get(1));
+        assertEquals(inValue[2], replacements.get(2));
     }
 
     public void testCriterionInList() throws Exception
@@ -1151,16 +1195,15 @@ public class SqlBuilderTest extends Base
         inList.add(1);
         inList.add(2);
         inList.add(null);
-        criteria.add(
-                "table.column2",
-                inList,
-                Criteria.IN);
+        criteria.whereIn("table.column2", inList);
 
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column1 FROM table "
                 + "WHERE table.column2 IN (?,?,?)",
             query.toString());
-        assertEquals(3, query.getPreparedStatementReplacements().size());
+        assertEquals(
+                inList.size(),
+                query.getPreparedStatementReplacements().size());
         assertEquals(1, query.getPreparedStatementReplacements().get(0));
         assertEquals(2, query.getPreparedStatementReplacements().get(1));
         assertEquals(null, query.getPreparedStatementReplacements().get(2));
@@ -1174,10 +1217,7 @@ public class SqlBuilderTest extends Base
         inList.add("a");
         inList.add("b");
         inList.add(null);
-        criteria.add(
-                "table.column2",
-                inList,
-                Criteria.IN);
+        criteria.whereIn("table.column2", inList);
         criteria.setIgnoreCase(true);
 
         Query query = SqlBuilder.buildQuery(criteria);
@@ -1185,7 +1225,7 @@ public class SqlBuilderTest extends Base
                 + "WHERE UPPER(table.column2) IN (UPPER(?),UPPER(?),?)",
             query.toString());
         List<Object> replacements = query.getPreparedStatementReplacements();
-        assertEquals(3, replacements.size());
+        assertEquals(inList.size(), replacements.size());
         assertEquals("a", replacements.get(0));
         assertEquals("b", replacements.get(1));
         assertEquals(null, replacements.get(2));
@@ -1201,10 +1241,7 @@ public class SqlBuilderTest extends Base
         }
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add(
-                "table.column2",
-                values,
-                Criteria.IN);
+        criteria.whereIn("table.column2", values);
         long start = System.currentTimeMillis();
         Query query = SqlBuilder.buildQuery(criteria);
         long end =  System.currentTimeMillis();
@@ -1218,9 +1255,9 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("table.column1");
-        criteria.add(
+        criteria.where(
                 "table.column2",
-                (Object) "illegal in value",
+                "illegal in value",
                 Criteria.IN);
 
         try



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message