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