KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > DbQueryTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.db;
4
5 import junit.framework.TestCase;
6
7 import java.util.List JavaDoc;
8
9 import jodd.util.collection.IntArrayList;
10
11 public class DbQueryTest extends TestCase {
12
13     static class DbQueryEx extends DbQueryParser {
14         String JavaDoc prepare(String JavaDoc sql) {
15             super.parseSql(sql);
16             return this.sql;
17         }
18     }
19
20
21     private void doTestSingleNamedParam(DbQueryEx dbp, String JavaDoc paramName, int position) {
22         IntArrayList list = dbp.getNamedParameterPositions(paramName);
23         assertEquals(1, list.size());
24         assertEquals(position, list.get(0));
25         assertTrue(dbp.prepared);
26     }
27
28     private void doTestDoubleNamedParam(DbQueryEx dbp, String JavaDoc paramName, int position1, int position2) {
29         IntArrayList list = dbp.getNamedParameterPositions(paramName);
30         assertEquals(2, list.size());
31         assertEquals(position1, list.get(0));
32         assertEquals(position2, list.get(1));
33         assertTrue(dbp.prepared);
34     }
35
36     public void testPrepareSql() {
37         DbQueryEx dbp = new DbQueryEx();
38         assertEquals("aaa", dbp.prepare("aaa"));
39         assertFalse(dbp.prepared);
40         assertEquals("", dbp.prepare(""));
41         assertFalse(dbp.prepared);
42         assertEquals("aaa ? aa", dbp.prepare("aaa ? aa"));
43         assertTrue(dbp.prepared);
44         assertEquals("? aaa ? aa ?", dbp.prepare("? aaa ? aa ?"));
45         assertTrue(dbp.prepared);
46         assertEquals("aaa ? aa ? x ? x", dbp.prepare("aaa ? aa ? x ? x"));
47         assertTrue(dbp.prepared);
48
49         assertEquals("aaa ? aa", dbp.prepare("aaa :x aa"));
50         doTestSingleNamedParam(dbp, "x", 1);
51
52         assertEquals("? aaa ?", dbp.prepare(":x aaa :y"));
53         doTestSingleNamedParam(dbp, "x", 1);
54         doTestSingleNamedParam(dbp, "y", 2);
55
56         assertEquals("? aaa ?", dbp.prepare(":xxx aaa :x"));
57         doTestSingleNamedParam(dbp, "xxx", 1);
58         doTestSingleNamedParam(dbp, "x", 2);
59
60         assertEquals("aaa ? aaa ? aaa ? aa ? aaa ?", dbp.prepare("aaa :x aaa :y aaa ? aa :x aaa ?"));
61         doTestDoubleNamedParam(dbp, "x", 1, 4);
62         doTestSingleNamedParam(dbp, "y", 2);
63
64         assertEquals("aaa ? aaa ? aaa ? aa ? aaa ?", dbp.prepare("aaa :x aaa ?1 aaa ? aa :x aaa ?1"));
65         doTestDoubleNamedParam(dbp, "1", 2, 5);
66         doTestDoubleNamedParam(dbp, "x", 1, 4);
67
68         assertTrue(dbp.prepared);
69
70     }
71 }
72
Popular Tags