1 /*
2 QueryJ
3
4 Copyright (C) 2002 Jose San Leandro Armend?riz
5 jsanleandro@yahoo.es
6 chousz@yahoo.com
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Thanks to ACM S.L. for distributing this library under the GPL license.
23 Contact info: jsanleandro@yahoo.es
24 Postal Address: c/Playa de Lagoa, 1
25 Urb. Valdecaba?as
26 Boadilla del monte
27 28660 Madrid
28 Spain
29
30 ******************************************************************************
31 *
32 * Filename: $RCSfile: SelectQueryTest.java,v $
33 *
34 * Author: Jose San Leandro Armend?riz
35 *
36 * Description: Indicates JUnit how to tests SelectQuery classes.
37 *
38 * Last modified by: $Author: chous $ at $Date: 2003/08/29 08:15:02 $
39 *
40 * File version: $Revision: 1.1 $
41 *
42 * Project version: $Name: $
43 *
44 * $Id: SelectQueryTest.java,v 1.1 2003/08/29 08:15:02 chous Exp $
45 *
46 */
47 package unittests.org.acmsl.queryj;
48
49 /*
50 * Importing some ACM-SL classes.
51 */
52 import org.acmsl.queryj.Field;
53 import org.acmsl.queryj.IntField;
54 import org.acmsl.queryj.QueryFactory;
55 import org.acmsl.queryj.SelectQuery;
56 import org.acmsl.queryj.Table;
57 import org.acmsl.queryj.TableAlias;
58
59 /*
60 * Importing JUnit classes.
61 */
62 import junit.framework.TestCase;
63
64 /***
65 * Indicates JUnit how to tests SelectQuery classes.
66 * @author <a href="mailto:jsanleandro@yahoo.es">Jose San Leandro</a>
67 * @version $Revision: 1.1 $
68 */
69 public class SelectQueryTest
70 extends TestCase
71 {
72 /***
73 * The expected query.
74 */
75 protected static final String EXPECTED_QUERY =
76 "SELECT USERID, NAME FROM USERS "
77 + "WHERE (USERID > 10) AND ((NAME = ?) OR (NAME is null))";
78
79 /***
80 * The USERS table.
81 */
82 protected static final UsersTable USERS = new UsersTable() {};
83
84 /***
85 * The tested instance.
86 */
87 private SelectQuery m__Query;
88
89 /***
90 * Constructs a test case with the given name.
91 * @param name the test case name.
92 */
93 public SelectQueryTest(String name)
94 {
95 super(name);
96 }
97
98 /***
99 * Specifies a new tested instance.
100 * @param query the query to test.
101 */
102 protected void setTestedInstance(SelectQuery query)
103 {
104 m__Query = query;
105 }
106
107 /***
108 * Retrieves the tested instance.
109 * @return such query.
110 */
111 protected SelectQuery getTestedInstance()
112 {
113 return m__Query;
114 }
115
116 /***
117 * Sets up the test fixture. (Called before every test case method.)
118 */
119 protected void setUp()
120 {
121 QueryFactory t_QueryFactory = QueryFactory.getInstance();
122
123 assertNotNull(t_QueryFactory);
124
125 SelectQuery t_Query = t_QueryFactory.createSelectQuery();
126
127 t_Query.select(USERS.USERID);
128 t_Query.select(USERS.NAME);
129
130 t_Query.from(USERS);
131
132 t_Query.where(
133 USERS.USERID.greaterThan(10)
134 .and(
135 USERS.NAME.equals()
136 .or(USERS.NAME.isNull())));
137
138 setTestedInstance(t_Query);
139 }
140
141 /***
142 * Tears down the test fixture. (Called after every test case method.)
143 */
144 protected void tearDown()
145 {
146 setTestedInstance(null);
147 }
148
149 /***
150 * Tests the toString() method
151 *
152 * @see org.acmsl.queryj.SelectQuery#toString()
153 */
154 public void testToString()
155 {
156 SelectQuery t_Query = getTestedInstance();
157
158 assertNotNull(t_Query);
159
160 String t_strQuery = t_Query.toString();
161
162 assertNotNull(t_strQuery);
163
164 System.out.println(t_strQuery);
165
166 assertEquals(EXPECTED_QUERY, t_strQuery);
167 }
168
169 /***
170 * Executes the tests from command line.
171 * @param args the command-line arguments. Not needed so far.
172 */
173 public static void main(String args[])
174 {
175 junit.textui.TestRunner.run(SelectQueryTest.class);
176 }
177
178 /***
179 * Test-only table.
180 * @author <a href="mailto:jsanleandro@yahoo.es">Jose San Leandro</a>
181 * @version $Revision: 1.1 $
182 */
183 public static class UsersTable
184 extends Table
185 {
186 /***
187 * The USERS table USERID field.
188 */
189 public static final IntField USERID =
190 new IntField("USERID", USERS) {};
191
192 /***
193 * The USERS table NAME field.
194 */
195 public static final Field NAME =
196 new Field("NAME", USERS) {};
197
198 /***
199 * All fields.
200 */
201 public static final Field[] ALL =
202 new Field[] {USERID, NAME};
203
204 /***
205 * The table name.
206 */
207 public static final String TABLE_NAME = "USERS";
208
209 /***
210 * The table alias.
211 */
212 public static final TableAlias ALIAS =
213 new TableAlias("usrs", USERS) {};
214
215 /***
216 * Creates a USERS table.
217 */
218 protected UsersTable()
219 {
220 super(TABLE_NAME);
221 }
222
223 /***
224 * Retrieves <code>all</code> fields. It's equivalent to a
225 * star in a query.
226 * @return such fields.
227 */
228 public Field[] getAll()
229 {
230 return ALL;
231 }
232 }
233 }
234
This page was automatically generated by Maven