001: package org.apache.torque.util;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.lang.reflect.Array;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.torque.adapter.DB;
027: import org.apache.torque.adapter.DBFactory;
028:
029: /**
030: * Tests for SqlExpression
031: *
032: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
033: * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
034: * @version $Id: SqlExpressionTest.java 618901 2008-02-06 05:17:24Z seade $
035: */
036: public class SqlExpressionTest extends TestCase {
037: private DB db = null;
038:
039: /**
040: * Constructor for SqlExpressionTest.
041: * @param arg0
042: */
043: public SqlExpressionTest(String arg0) {
044: super (arg0);
045: }
046:
047: /**
048: * set up environment
049: */
050: public void setUp() {
051: try {
052: db = DBFactory.create("mysql");
053: } catch (Exception ex) {
054: ex.printStackTrace();
055: }
056: }
057:
058: /**
059: * Test for String buildInnerJoin(String, String)
060: */
061: public void testBuildInnerJoinStringString() {
062: String result = SqlExpression.buildInnerJoin("TA.COLA",
063: "TB.COLB");
064: assertEquals(result, "TA.COLA=TB.COLB");
065: }
066:
067: /**
068: * Test for String buildInnerJoin(String, String, boolean, DB)
069: */
070: public void testBuildInnerJoinStringStringbooleanDB() {
071: String result = SqlExpression.buildInnerJoin("TA.COLA",
072: "TB.COLB", true, db);
073: assertEquals(result, "TA.COLA=TB.COLB");
074: }
075:
076: /**
077: * Test for String buildIn(String, Object, SqlEnum, boolean, DB)
078: */
079: public void testBuildInStringObjectSqlEnumbooleanDB() {
080: String[] values = new String[] { "42", "43", "44" };
081: String result = SqlExpression.buildIn("COL", values,
082: SqlEnum.IN, true, db);
083: // It seems the order of the values is different for jdk1.3 vs 1.4, etc.
084: // In any case, the order is not significant.
085: if (result.equals("COL IN ('43','42','44')")) {
086: // jdk 1.6
087: assertEquals(result, "COL IN ('43','42','44')");
088: } else if (result.equals("COL IN ('42','43','44')")) {
089: // jdk 1.4
090: assertEquals(result, "COL IN ('42','43','44')");
091: } else {
092: // jdk 1.3
093: assertEquals(result, "COL IN ('43','44','42')");
094: }
095: }
096:
097: public void testLargeBuildInStringObjectSqlEnumbooleanDB() {
098: int size = 10000;
099: String[] values = new String[size];
100: for (int i = 0; i < size; i++) {
101: Array.set(values, i, String.valueOf(i));
102: }
103: long start = System.currentTimeMillis();
104: String result = SqlExpression.buildIn("COL", values,
105: SqlEnum.IN, true, db);
106: long end = System.currentTimeMillis();
107: System.out.println("large buildIn took " + (end - start)
108: + " milliseconds");
109: }
110:
111: /**
112: * Test whether LIKE clauses are built correctly.
113: */
114: public void testBuildLike() {
115: String result = SqlExpression.buildLike("COL", "fre%",
116: SqlEnum.LIKE, false, db);
117: assertEquals("COL LIKE fre%", result);
118:
119: result = SqlExpression.buildLike("COL", "50\\\\%",
120: SqlEnum.LIKE, false, db);
121: assertEquals("COL = 50%", result);
122: }
123: }
|