com.pureinfo.dolphin.query.logic.SQLConditionTest.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.dolphin.query.logic.SQLConditionTest.java

Source

/**
 * PureInfo Dolphin
 * @(#)SQLConditionTest.java   1.0 Oct 30, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.dolphin.query.logic;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.pureinfo.dolphin.query.SQLOperator;
import com.pureinfo.dolphin.query.logic.SQLComparison;
import com.pureinfo.dolphin.query.logic.SQLCondition;
import com.pureinfo.force.PureTestCase;
import com.pureinfo.force.object.DataTypes;
import com.pureinfo.force.object.TypedValue;

/**
 * <P>
 * Created on Oct 30, 2005 7:35:16 AM <BR>
 * Last modified on Oct 30, 2005
 * </P>
 * SQLConditionTest: test for SQL condition.
 * 
 * @author Why
 * @version 1.0, Oct 30, 2005
 * @since Dolphin 1.0
 */
public class SQLConditionTest extends PureTestCase {

    public void testToSQL() {
        log("to test toSQL()... ");
        SQLCondition condition = new SQLCondition(true, 3);

        //string
        condition.append("{this.flag}=1");

        List params = new ArrayList(1);
        params.add(new TypedValue(true));
        condition.append("{this.active}=?", params);

        SQLLogicString logic = new SQLLogicString("a>b+? and b<?");
        logic.addParameter(1);
        logic.addParameter(DataTypes.INT, new Integer(100));
        condition.append(logic);

        //comparison
        SQLComparison comparison = new SQLComparison("{b.weight}", SQLOperator.EQUAL, DataTypes.FLOAT, "1.2");
        comparison.addAlias("b");
        condition.append(comparison);

        //condition
        SQLCondition conditionOr = new SQLCondition(false, 2);
        conditionOr.append("{c.age}>12");
        conditionOr.append("{d.name} like '%snoopy%'");
        conditionOr.addAliases(new String[] { "c", "d" });
        condition.append(conditionOr);

        //toSQL
        params = new ArrayList();
        String strSQL = condition.toSQL(params);
        System.out.println("SQL=" + strSQL);
        for (int i = 0; i < params.size(); i++) {
            System.out.println("[param-" + i + "]" + params.get(i).toString());
        }
        assertEquals(5, params.size());

        String sAliases = StringUtils.join(condition.getAliases(), ",");
        System.out.println("aliases=" + sAliases);
        assertEquals("b,c,d", sAliases);

        condition.append(new SQLComparison("{a.desc}", SQLOperator.IS_EMPTY, null));
        params.clear();
        strSQL = condition.toSQL(params);
        System.out.println("SQL=" + strSQL);
        assertEquals(
                "({this.flag}=1) AND ({this.active}=?) AND (a>b+? and b<?) AND ({b.weight}>=? AND {b.weight}<=?) AND (({c.age}>12) OR ({d.name} like '%snoopy%')) AND (({a.desc} IS NULL) OR ({a.desc}=''))",
                strSQL);
    }

    public void testShrink() {
        log("to test shrink()... ");
        SQLCondition condition = new SQLCondition();
        ISQLLogic logic = new SQLLogicString("filed>=2");
        condition.append(logic);
        assertEquals(logic, condition.shrink());

        condition.append("field2=1");
        assertEquals(condition, condition.shrink());

        condition.clear();
        assertEquals(SQLLogicString.TRUE, condition.shrink());
        condition.setNot(true);
        assertEquals(SQLLogicString.FALSE, condition.shrink());

        condition.setAnd(false);
        condition.setNot(false);
        assertEquals(SQLLogicString.FALSE, condition.shrink());
        condition.setNot(true);
        assertEquals(SQLLogicString.TRUE, condition.shrink());
    }

}