1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/tests/test/org/dbforms/config/TestQuery.java,v 1.6 2005/07/09 16:34:00 hkollmann Exp $
3    * $Revision: 1.6 $
4    * $Date: 2005/07/09 16:34:00 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.config;
25  
26  import org.dbforms.config.Constants;
27  import org.dbforms.config.Field;
28  import org.dbforms.config.FieldValue;
29  
30  
31  
32  /***
33   *  Description of the Class
34   *
35   * @author     epugh
36   * @created    May 3, 2002
37   */
38  public class TestQuery extends org.dbforms.util.AbstractTestCase {
39     Field      fAuthorId             = null;
40     Field      fBookId               = null;
41     FieldValue fvLogicalOR           = null;
42     FieldValue fvLogicalORLikeFilter = null;
43     FieldValue fvNotLogicalOR        = null;
44     Query      table                 = null;
45  
46     /***
47      * DOCUMENT ME!
48      *
49      * @throws Exception DOCUMENT ME!
50      */
51     public void setUp() throws Exception {
52        super.setUp();
53        fAuthorId = new Field();
54        fAuthorId.setName("AUTHOR_ID");
55        fAuthorId.setFieldType("char");
56  
57        fBookId = new Field();
58        fBookId.setName("BOOK_ID");
59  
60        fvLogicalOR = new FieldValue(fAuthorId, "10");
61        fvLogicalOR.setOperator(Constants.FILTER_EQUAL);
62        fvLogicalOR.setLogicalOR(true);
63  
64        fvNotLogicalOR = new FieldValue(fBookId, "10");
65        fvNotLogicalOR.setOperator(Constants.FILTER_EQUAL);
66        fvNotLogicalOR.setLogicalOR(false);
67  
68        fvLogicalORLikeFilter = new FieldValue(fAuthorId, "10");
69        fvLogicalORLikeFilter.setOperator(Constants.FILTER_LIKE);
70        fvLogicalORLikeFilter.setLogicalOR(true);
71  
72        table = new Query();
73        table.setWhere("td.id = td1.id and td.id1 = td1.id1");
74        table.setFollowAfterWhere("AND");
75        table.setFrom("td");
76     }
77  
78  
79     /***
80      * DOCUMENT ME!
81      *
82      * @throws Exception DOCUMENT ME!
83      */
84     public void testGetSelectQuery() throws Exception {
85        FieldValue[] fvs = {
86                              fvLogicalOR,
87                              fvNotLogicalOR
88                           };
89        String       s = table.getSelectQuery(null, null, null, "f1 >= ?",
90                                              Constants.COMPARE_NONE);
91        assertTrue("Test fvs Where clause equals:" + s,
92                   s.indexOf("SELECT * FROM td WHERE ( td.id = td1.id and td.id1 = td1.id1 )  AND  ( f1 >= ? )") >= 0);
93  
94        s = table.getSelectQuery(null, fvs, null, "f1 >= ?",
95                                 Constants.COMPARE_NONE);
96        assertTrue("Test fvs Where clause equals:" + s,
97                   s.indexOf("SELECT * FROM td WHERE ( td.id = td1.id and td.id1 = td1.id1 )  AND  ( f1 >= ? )  AND ( (  ( AUTHOR_ID =  ?  ) AND ( BOOK_ID =  ?  )  ) )") >= 0);
98     }
99  
100 
101    /***
102     * DOCUMENT ME!
103     *
104     * @throws Exception DOCUMENT ME!
105     */
106    public void testGetWhereClause() throws Exception {
107       FieldValue[] fvs = {
108                             fvLogicalOR,
109                             fvNotLogicalOR
110                          };
111       String       s = table.getWhereClause(fvs);
112       assertTrue("Test fvs Where clause equals: (AUTHOR_ID =  ?)  AND (BOOK_ID =  ?):"
113                  + s,
114                  s.indexOf("( AUTHOR_ID =  ?  ) AND ( BOOK_ID =  ?  )") >= 0);
115 
116       FieldValue[] fvs2 = {
117                              fvLogicalOR,
118                              fvLogicalOR
119                           };
120       assertTrue("Test fvs2 Where clause equals:" + table.getWhereClause(fvs2),
121                  table.getWhereClause(fvs2).indexOf("AUTHOR_ID =  ?  OR AUTHOR_ID =  ?") >= 0);
122 
123       FieldValue[] fvs3 = {
124                              fvLogicalORLikeFilter,
125                              fvLogicalOR
126                           };
127       s = table.getWhereClause(fvs3);
128       assertTrue("Test fvs3 Where clause equals:" + s,
129                  s.indexOf("AUTHOR_ID LIKE  ?  OR AUTHOR_ID =  ?") >= 0);
130    }
131 }