KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > sqls > junit > CreateTest


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.sqls.junit;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import junit.framework.TestCase;
25 import org.apache.ws.jaxme.sqls.BooleanConstraint;
26 import org.apache.ws.jaxme.sqls.Column;
27 import org.apache.ws.jaxme.sqls.ColumnReference;
28 import org.apache.ws.jaxme.sqls.CombinedConstraint;
29 import org.apache.ws.jaxme.sqls.DeleteStatement;
30 import org.apache.ws.jaxme.sqls.Expression;
31 import org.apache.ws.jaxme.sqls.ForeignKey;
32 import org.apache.ws.jaxme.sqls.Index;
33 import org.apache.ws.jaxme.sqls.InsertStatement;
34 import org.apache.ws.jaxme.sqls.JoinReference;
35 import org.apache.ws.jaxme.sqls.SQLFactory;
36 import org.apache.ws.jaxme.sqls.SQLGenerator;
37 import org.apache.ws.jaxme.sqls.Schema;
38 import org.apache.ws.jaxme.sqls.SelectStatement;
39 import org.apache.ws.jaxme.sqls.SelectTableReference;
40 import org.apache.ws.jaxme.sqls.StringColumn;
41 import org.apache.ws.jaxme.sqls.Table;
42 import org.apache.ws.jaxme.sqls.TableReference;
43 import org.apache.ws.jaxme.sqls.UpdateStatement;
44 import org.apache.ws.jaxme.sqls.impl.SQLFactoryImpl;
45 import org.apache.ws.jaxme.sqls.impl.VirtualColumn;
46
47
48 public class CreateTest extends TestCase {
49     private SQLFactory sqlFactory;
50     private SQLGenerator sqlGenerator;
51     private Schema schema;
52     
53     protected SQLFactory newSQLFactory() {
54         return new SQLFactoryImpl();
55     }
56     
57     protected SQLGenerator newSQLGenerator() {
58         return sqlFactory.newSQLGenerator();
59     }
60     
61     protected SQLFactory getSQLFactory() {
62         return sqlFactory;
63     }
64     
65     protected SQLGenerator getSQLGenerator() {
66         return sqlGenerator;
67     }
68     
69     public void setUp() {
70         sqlFactory = newSQLFactory();
71         sqlGenerator = newSQLGenerator();
72         schema = sqlFactory.newSchema("MySchema");
73     }
74     
75     /** <p>Creates a new instance of CreateTest.java.</p>
76      */

77     public CreateTest(String JavaDoc pName) { super(pName); }
78     
79     /** <p>Creates a basic table</p>
80      */

81     protected Table getBasicTable() {
82         Table table = schema.newTable("MyTable");
83         Column myIndex = table.newColumn("MyIndex", Column.Type.INTEGER);
84         assertTrue(!myIndex.isStringColumn());
85         assertTrue(!myIndex.isBinaryColumn());
86         Column myName = table.newColumn("MyName", Column.Type.VARCHAR);
87         assertTrue(myName.isStringColumn()); // myName may be casted to a StringColumn
88
assertTrue(!myName.isBinaryColumn());
89         ((StringColumn) myName).setLength(60);
90         Column myDate = table.newColumn("MyDate", Column.Type.DATE);
91         assertTrue(!myDate.isStringColumn());
92         assertTrue(!myDate.isBinaryColumn());
93         myDate.setNullable(true);
94         return table;
95     }
96     
97     /** <p>Creates a table with primary key</p>
98      */

99     protected Table getPrimaryKeyTable() {
100         Table table = getBasicTable();
101         Index index = table.newPrimaryKey();
102         index.addColumn("MyIndex");
103         return table;
104     }
105     
106     protected Table getForeignKeyTable(Table pTable) {
107         Table otherTable = pTable.getSchema().newTable("OtherTable");
108         Column otherIndex = otherTable.newColumn("MyIndex", Column.Type.INTEGER);
109         Column referenceColumn = otherTable.newColumn("RefIndex", Column.Type.INTEGER);
110         Column companyColumn = otherTable.newColumn("Company", Column.Type.VARCHAR);
111         ((StringColumn) companyColumn).setLength(60);
112         otherTable.newPrimaryKey().addColumn(otherIndex);
113         
114         ForeignKey reference = otherTable.newForeignKey(pTable);
115         reference.addColumnLink(referenceColumn, pTable.getColumn("MyIndex"));
116         return otherTable;
117     }
118     
119     /** <p>Basic test for creating a <code>CREATE TABLE</code>
120      * statement.</p>
121      */

122     public void testBasicCreate() {
123         Table table = getBasicTable();
124         SQLGenerator generator = getSQLGenerator();
125         generator.setLineTerminator("\n");
126         Collection JavaDoc statements = generator.getCreate(table.getSchema(), true);
127         Iterator JavaDoc iter = statements.iterator();
128         assertTrue(iter.hasNext());
129         assertEquals("CREATE SCHEMA MySchema", iter.next());
130         assertTrue(iter.hasNext());
131         assertEquals("CREATE TABLE MySchema.MyTable (\n" +
132                 " MyIndex INT NOT NULL,\n" +
133                 " MyName VARCHAR(60) NOT NULL,\n" +
134                 " MyDate DATE\n" +
135                 ")\n", iter.next());
136         assertTrue(!iter.hasNext());
137     }
138     
139     /** <p>Basic test for creating an <code>INSERT</code> statement.</p>
140      */

141     public void testBasicInsert() {
142         Table table = getBasicTable();
143         InsertStatement insertStatement = table.getInsertStatement();
144         SQLGenerator generator = getSQLGenerator();
145         generator.setLineTerminator("\n");
146         String JavaDoc s = generator.getQuery(insertStatement);
147         assertEquals("INSERT INTO MySchema.MyTable (MyIndex, MyName, MyDate) VALUES (?, ?, ?)", s);
148     }
149
150     protected String JavaDoc getBulkInsertResult() {
151         return "INSERT INTO MySchema.MyTable (MyTable.MyIndex, MyTable.MyName, MyTable.MyDate) (SELECT MyTable0.MyIndex, MyTable0.MyName, MyTable0.MyDate FROM MySchema.MyTable AS MyTable0)";
152     }
153
154     public void testBulkInsert() {
155         Table table = getBasicTable();
156         InsertStatement insertStatement = getSQLFactory().newInsertStatement();
157         insertStatement.setTable(table);
158         SelectStatement st = table.getSelectStatement();
159         SQLGenerator generator = getSQLGenerator();
160         generator.setLineTerminator("\n");
161         insertStatement.setSubSelect(st);
162         String JavaDoc got = generator.getQuery(insertStatement);
163         String JavaDoc expect = getBulkInsertResult();
164         assertEquals(expect, got);
165     }
166
167     /** <p>Basic test for creating a <code>SELECT</code> statement.</p>
168      */

169     public void testBasicSelect() {
170         Table table = getBasicTable();
171         SelectStatement selectStatement = table.getSelectStatement();
172         SQLGenerator generator = getSQLGenerator();
173         generator.setLineTerminator("\n");
174         String JavaDoc s = generator.getQuery(selectStatement);
175         assertEquals("SELECT MyIndex, MyName, MyDate FROM MySchema.MyTable", s);
176     }
177     
178     /** <p>Basic test for creating an <code>UPDATE</code> statement.</p>
179      */

180     public void testBasicUpdate() {
181         Table table = getPrimaryKeyTable();
182         UpdateStatement updateStatement = table.getUpdateStatement();
183         SQLGenerator generator = getSQLGenerator();
184         generator.setLineTerminator("\n");
185         String JavaDoc s = generator.getQuery(updateStatement);
186         assertEquals("UPDATE MySchema.MyTable SET MyName=?, MyDate=? WHERE MyIndex=?", s);
187     }
188     
189     /** <p>Basic test for creating an <code>DELETE</code> statement.</p>
190      */

191     public void testBasicDelete() {
192         Table table = getPrimaryKeyTable();
193         DeleteStatement deleteStatement = table.getDeleteStatement();
194         SQLGenerator generator = getSQLGenerator();
195         generator.setLineTerminator("\n");
196         String JavaDoc s = generator.getQuery(deleteStatement);
197         assertEquals("DELETE FROM MySchema.MyTable WHERE MyIndex=?", s);
198     }
199     
200     protected String JavaDoc getCreateForeignKeyResult() {
201         return "CREATE TABLE MySchema.OtherTable ( MyIndex INT NOT NULL, RefIndex INT NOT NULL, Company VARCHAR(60) NOT NULL)";
202     }
203     
204     /** <p>Test for a FOREIGN KEY definition.</p>
205      */

206     public void testCreateForeignKey() {
207         Table table = getPrimaryKeyTable();
208         Table otherTable = getForeignKeyTable(table);
209         SQLGenerator generator = getSQLGenerator();
210         Collection JavaDoc c = generator.getCreate(otherTable);
211         assertEquals(1, c.size());
212         String JavaDoc expect = getCreateForeignKeyResult();
213         String JavaDoc got = (String JavaDoc) c.iterator().next();
214         assertEquals(expect, got);
215     }
216     
217     protected String JavaDoc getTestJoinResult() {
218         return "SELECT OtherTable.MyIndex, RefIndex, Company FROM MySchema.OtherTable JOIN MySchema.MyTable ON RefIndex=MyTable.MyIndex WHERE OtherTable.MyIndex=?";
219     }
220
221     protected SelectStatement getJoinStatement() {
222         Table table = getPrimaryKeyTable();
223         Table otherTable = getForeignKeyTable(table);
224         SelectStatement statement = otherTable.getSelectStatement();
225         SelectTableReference tableReference = statement.getSelectTableReference();
226         JoinReference joinReference = tableReference.join(table);
227         
228         TableReference refLocal = tableReference;
229         TableReference refRef = tableReference.getRightJoinedTableReference();
230         
231         joinReference.getOn().addJoin((ForeignKey) otherTable.getForeignKeys().next(),
232                 refLocal, refRef);
233         CombinedConstraint cc = statement.getWhere();
234         BooleanConstraint bc = cc.createEQ();
235         bc.addPart(tableReference.newColumnReference("MyIndex"));
236         bc.addPlaceholder();
237         return statement;
238     }
239
240     /** <p>Test for a JOIN statement.</p>
241      */

242     public void testJoin() {
243         SelectStatement statement = getJoinStatement();
244         SQLGenerator generator = getSQLGenerator();
245         generator.setLineTerminator("\n");
246         String JavaDoc got = generator.getQuery(statement);
247         String JavaDoc expect = getTestJoinResult();
248         assertEquals(expect, got);
249     }
250     
251     protected String JavaDoc getTestLeftOuterJoinResult() {
252         return "SELECT OtherTable.MyIndex, RefIndex, Company FROM MySchema.OtherTable LEFT OUTER JOIN MySchema.MyTable ON RefIndex=MyTable.MyIndex WHERE OtherTable.MyIndex=?";
253     }
254     
255     /** <p>Test for a LEFT OUTER JOIN statement.</p>
256      */

257     public void testLeftOuterJoin() {
258         Table table = getPrimaryKeyTable();
259         Table otherTable = getForeignKeyTable(table);
260         SelectStatement statement = otherTable.getSelectStatement();
261         SelectTableReference tableReference = statement.getSelectTableReference();
262         JoinReference joinReference = tableReference.leftOuterJoin(table);
263         
264         TableReference refLocal = tableReference;
265         TableReference refRef = tableReference.getRightJoinedTableReference();
266         
267         joinReference.getOn().addJoin((ForeignKey) otherTable.getForeignKeys().next(),
268                 refLocal, refRef);
269         CombinedConstraint cc = statement.getWhere();
270         BooleanConstraint bc = cc.createEQ();
271         bc.addPart(tableReference.newColumnReference("MyIndex"));
272         bc.addPlaceholder();
273         
274         SQLGenerator generator = getSQLGenerator();
275         generator.setLineTerminator("\n");
276         String JavaDoc got = generator.getQuery(statement);
277         String JavaDoc expect = getTestLeftOuterJoinResult();
278         assertEquals(expect, got);
279     }
280     
281     /** <p>Test for an EXISTS clause.</p>
282      */

283     public void testExists() {
284         Table table = getPrimaryKeyTable();
285         Table otherTable = getForeignKeyTable(table);
286         SelectStatement statement = table.getSelectStatement();
287         SelectTableReference tableReference = statement.getSelectTableReference();
288         SelectStatement existsStatement = otherTable.getSelectStatement();
289         SelectTableReference existsTableReference = existsStatement.getSelectTableReference();
290         BooleanConstraint bc = existsStatement.getWhere().createEQ();
291         bc.addPart(existsTableReference.newColumnReference("RefIndex"));
292         bc.addPart(tableReference.newColumnReference("MyIndex"));
293         statement.getWhere().createEXISTS(existsStatement);
294         
295         SQLGenerator generator = getSQLGenerator();
296         generator.setLineTerminator("\n");
297         String JavaDoc got = generator.getQuery(statement);
298         String JavaDoc expect = "SELECT MyTable.MyIndex, MyName, MyDate FROM MySchema.MyTable" +
299         " WHERE EXISTS(SELECT OtherTable.MyIndex, RefIndex, Company" +
300         " FROM MySchema.OtherTable WHERE RefIndex=MyTable.MyIndex)";
301         assertEquals(expect, got);
302     }
303
304     /** <p>Test for a BETWEEN clause.</p>
305      */

306     public void testBetween() {
307         Table table = getBasicTable();
308         SelectStatement statement = table.getSelectStatement();
309         BooleanConstraint between = statement.getWhere().createBETWEEN();
310         between.addPart(statement.getTableReference().newColumnReference("MyIndex"));
311         between.addPart(3);
312         between.addPart(5);
313
314         SQLGenerator generator = getSQLGenerator();
315         generator.setLineTerminator("\n");
316         String JavaDoc got = generator.getQuery(statement);
317         String JavaDoc expect = "SELECT MyIndex, MyName, MyDate FROM MySchema.MyTable WHERE MyIndex BETWEEN 3 AND 5";
318         assertEquals(expect, got);
319     }
320
321     /** <p>Creates a table with a composed primary key.</p>
322      */

323     protected Table getComposedKeyTable() {
324         Table table = getPrimaryKeyTable();
325         Column verNumColumn = table.newColumn("VerNum", Column.Type.INTEGER);
326         assertTrue(!verNumColumn.isStringColumn());
327         assertTrue(!verNumColumn.isBinaryColumn());
328         Index index = table.getPrimaryKey();
329         index.addColumn("VerNum");
330         return table;
331     }
332     
333     /** <p>Test for composed primary keys.</p>
334      */

335     public void testComposedPrimaryKey() {
336         Table table = getComposedKeyTable();
337         
338         SelectStatement statement = table.getSelectStatement();
339         statement.getWhere().addColumnSetQuery(table.getPrimaryKey(), statement.getTableReference());
340         SQLGenerator generator = getSQLGenerator();
341         generator.setLineTerminator("\n");
342         String JavaDoc s = generator.getQuery(statement);
343         assertEquals("SELECT MyIndex, MyName, MyDate, VerNum FROM MySchema.MyTable WHERE (MyIndex=? AND VerNum=?)", s);
344     }
345     
346     /** <p>Test for index names.</p>
347      */

348     public void testIndexNames() {
349         SQLGenerator gen = getSQLGenerator();
350         Table table = getBasicTable();
351         for (int i = 0; i < 10; i++) {
352             Index index = table.newIndex();
353             index.addColumn("MyName");
354             String JavaDoc s = (String JavaDoc) gen.getCreate(index).iterator().next();
355             assertEquals("CREATE INDEX MyTable_I" + i + " ON MySchema.MyTable (MyName)", s);
356         }
357         
358         Collection JavaDoc coll = gen.getCreate(schema, true);
359         String JavaDoc[] cmds = (String JavaDoc[]) coll.toArray(new String JavaDoc[coll.size()]);
360         assertEquals(12, cmds.length);
361         assertEquals("CREATE SCHEMA MySchema", cmds[0]);
362         assertEquals("CREATE TABLE MySchema.MyTable ( MyIndex INT NOT NULL, MyName VARCHAR(60) NOT NULL, MyDate DATE)", cmds[1]);
363         for (int i = 0; i < 10; i++) {
364             assertEquals("CREATE INDEX MyTable_I" + i + " ON MySchema.MyTable (MyName)", cmds[i+2]);
365         }
366     }
367     
368     /** <p>Test for subselects.</p>
369      */

370     public void testSubSelect() {
371         SQLGenerator gen = getSQLGenerator();
372         Table table = getComposedKeyTable();
373         
374         Table otherTable = table.getSchema().newTable("OtherTable");
375         Column otherIndex = otherTable.newColumn("MyIndex", Column.Type.INTEGER);
376         otherTable.newPrimaryKey().addColumn(otherIndex);
377         ForeignKey foreignKey = otherTable.newForeignKey(table);
378         SelectStatement selectStatement = sqlFactory.newSelectStatement();
379         selectStatement.setTable(otherTable);
380         DeleteStatement deleteStatement = sqlFactory.newDeleteStatement();
381         deleteStatement.setTable(table);
382         List JavaDoc columns = new ArrayList JavaDoc();
383         for (Iterator JavaDoc iter = table.getColumns(); iter.hasNext(); ) {
384             Column column = (Column) iter.next();
385             Column refColumn = otherTable.newColumn("Ref" + column.getName(), column.getType());
386             foreignKey.addColumnLink(refColumn, column);
387             if (column.isPrimaryKeyPart()) {
388                 selectStatement.addResultColumn(selectStatement.getTableReference().newColumnReference(refColumn));
389                 columns.add(deleteStatement.getTableReference().newColumnReference(column));
390             }
391         }
392         BooleanConstraint eq = selectStatement.getWhere().createEQ();
393         eq.addPart(selectStatement.getTableReference().newColumnReference("RefMyName"));
394         eq.addPlaceholder();
395         
396         BooleanConstraint bc = deleteStatement.getWhere().createIN();
397         bc.addPart((ColumnReference[]) columns.toArray(new ColumnReference[columns.size()]));
398         bc.addPart(selectStatement);
399         String JavaDoc expect = "DELETE FROM MySchema.MyTable WHERE (MyTable.MyIndex, VerNum) IN ((SELECT RefMyIndex, RefVerNum FROM MySchema.OtherTable WHERE RefMyName=?))";
400         String JavaDoc got = gen.getQuery(deleteStatement);
401         assertEquals(expect, got);
402     }
403     
404     public void testVirtualColumn() {
405         Table table = getBasicTable();
406         SelectStatement selectStatement = table.getSelectStatement();
407         VirtualColumn col = new VirtualColumn("virtCol", Column.Type.VARCHAR);
408         selectStatement.addResultColumn(col);
409         col.setValue("null");
410         SQLGenerator gen = getSQLGenerator();
411         String JavaDoc query = gen.getQuery(selectStatement);
412         assertEquals("SELECT MyIndex, MyName, MyDate, null AS virtCol FROM MySchema.MyTable", query);
413     }
414
415     public void testNOT() {
416         Table table = getBasicTable();
417         SelectStatement selectStatement = table.getSelectStatement();
418         SelectTableReference ref = selectStatement.getSelectTableReference();
419         CombinedConstraint and = selectStatement.getWhere();
420         BooleanConstraint bc = and.createLIKE();
421         bc.addPart(ref.newColumnReference("MyName"));
422         bc.addPart("%a%");
423         CombinedConstraint or = and.createOrConstraint();
424         or.setNOT(true);
425         bc = or.createEQ();
426         bc.addPart(ref.newColumnReference("MyIndex"));
427         bc.addPart(1);
428         SQLGenerator gen = getSQLGenerator();
429         String JavaDoc query = gen.getQuery(selectStatement);
430         assertEquals("SELECT MyIndex, MyName, MyDate FROM MySchema.MyTable WHERE (MyName LIKE '%a%' AND NOT (MyIndex=1))", query);
431
432         bc = or.createEQ();
433         bc.addPart(ref.newColumnReference("MyIndex"));
434         bc.addPart(2);
435         query = gen.getQuery(selectStatement);
436         assertEquals("SELECT MyIndex, MyName, MyDate FROM MySchema.MyTable WHERE (MyName LIKE '%a%' AND (NOT (MyIndex=1 OR MyIndex=2)))", query);
437     }
438
439     /** Test for expressions.
440      */

441     public void testExpressions() {
442         Table t = getBasicTable();
443         SelectStatement st = t.getSelectStatement();
444         SelectTableReference ref = st.getSelectTableReference();
445         BooleanConstraint bc = st.getWhere().createGT();
446         Expression e1 = bc.createSUM();
447         e1.addPart(ref.newColumnReference("MyIndex"));
448         e1.addPart(3);
449         Expression e2 = bc.createSUM();
450         e2.addPart(5);
451         SQLGenerator gen = getSQLGenerator();
452         String JavaDoc got = gen.getQuery(st);
453         String JavaDoc expect = "SELECT MyIndex, MyName, MyDate FROM MySchema.MyTable WHERE (MyIndex+3)>5";
454         assertEquals(expect, got);
455     }
456 }
457
Popular Tags