1   package org.apache.torque.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Array;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.torque.adapter.DB;
27  import org.apache.torque.adapter.DBFactory;
28  
29  /***
30   * Tests for SqlExpression
31   *
32   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
33   * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
34   * @version $Id: SqlExpressionTest.java 473821 2006-11-11 22:37:25Z tv $
35   */
36  public class SqlExpressionTest extends TestCase
37  {
38      private DB db = null;
39  
40  
41          /***
42           * Constructor for SqlExpressionTest.
43           * @param arg0
44           */
45          public SqlExpressionTest(String arg0)
46          {
47                  super(arg0);
48          }
49  
50      /***
51       * set up environment
52       */
53      public void setUp()
54      {
55          try
56          {
57              db = DBFactory.create("mysql");
58          }
59          catch (Exception ex)
60          {
61              ex.printStackTrace();
62          }
63      }
64  
65          /***
66           * Test for String buildInnerJoin(String, String)
67           */
68          public void testBuildInnerJoinStringString()
69          {
70          String result = SqlExpression.buildInnerJoin("TA.COLA", "TB.COLB");
71          assertEquals(result, "TA.COLA=TB.COLB");
72          }
73  
74          /***
75           * Test for String buildInnerJoin(String, String, boolean, DB)
76           */
77          public void testBuildInnerJoinStringStringbooleanDB()
78          {
79          String result = SqlExpression.buildInnerJoin("TA.COLA", "TB.COLB",
80                  true, db);
81          assertEquals(result, "TA.COLA=TB.COLB");
82          }
83  
84          /***
85           * Test for String buildIn(String, Object, SqlEnum, boolean, DB)
86           */
87          public void testBuildInStringObjectSqlEnumbooleanDB()
88          {
89          String[] values = new String[] { "42", "43", "44" };
90          String result = SqlExpression.buildIn("COL", values, SqlEnum.IN,
91                  true, db);
92          // It seems the order of the values is different for jdk1.3 vs 1.4
93          // In any case, the order is not significant.
94          if (result.equals("COL IN ('42','43','44')"))
95          {
96              // jdk 1.4
97              assertEquals(result, "COL IN ('42','43','44')");
98          }
99          else
100         {
101             // jdk 1.3
102             assertEquals(result, "COL IN ('43','44','42')");
103         }
104     }
105 
106     public void testLargeBuildInStringObjectSqlEnumbooleanDB()
107     {
108         int size = 10000;
109         String[] values = new String[size];
110         for (int i = 0; i < size; i++)
111         {
112             Array.set(values, i, String.valueOf(i));
113         }
114         long start = System.currentTimeMillis();
115         String result = SqlExpression.buildIn("COL", values, SqlEnum.IN,
116                 true, db);
117         long end =  System.currentTimeMillis();
118         System.out.println("large buildIn took " + (end - start) + " milliseconds");
119     }
120 
121     /***
122      * Test whether LIKE clauses are built correctly.
123      */
124     public void testBuildLike()
125     {
126         String result = SqlExpression.buildLike(
127                 "COL", "fre%", SqlEnum.LIKE, false, db);
128         assertEquals("COL LIKE fre%", result);
129 
130         result = SqlExpression.buildLike(
131                 "COL", "50////%", SqlEnum.LIKE, false, db);
132         assertEquals("COL = 50%", result);
133     }
134 }