KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > param > SqlExprTest


1 package com.tonbeller.wcf.param;
2
3 import java.math.BigDecimal JavaDoc;
4
5 import junit.framework.TestCase;
6
7 public class SqlExprTest extends TestCase {
8   SqlGenerator sg;
9   
10   public void testSqlAndExpr() {
11     SqlAndExpr and = new SqlAndExpr();
12     eqlExprWithOperandsTest(and, "(C1 = 'c1' AND C2 = 'c2')");
13   }
14
15   public void testSqlOrExpr() {
16     SqlOrExpr and = new SqlOrExpr();
17     eqlExprWithOperandsTest(and, "(C1 = 'c1' OR C2 = 'c2')");
18   }
19   
20   public void testAndOrExpr() {
21     SqlEqualExpr ex = new SqlEqualExpr();
22     ex.setSqlValue(new Integer JavaDoc(1));
23     SqlAndExpr and = new SqlAndExpr();
24     and.getOperands().add(ex);
25     SqlOrExpr or = new SqlOrExpr();
26     or.getOperands().add(ex);
27     or.getOperands().add(ex);
28     and.getOperands().add(or);
29     assertEquals("(C = 1 AND (C = 1 OR C = 1))", sg.generate(and));
30   }
31   
32   public void testSqlEqualExpr() {
33     SqlEqualExpr ex = new SqlEqualExpr();
34     ex.setSqlValue("ab");
35     assertEquals("C = 'ab'", sg.generate(ex));
36
37     ex.setSqlValue(null);
38     assertEquals("C IS NULL", sg.generate(ex));
39   }
40
41   public void testSqlInExpr() {
42     SqlInExpr ex = new SqlInExpr();
43     
44     // empty value
45
ex.setSqlValues(new Object JavaDoc[]{});
46     assertEquals("1=0", sg.generate(ex));
47     
48     // 1 value
49
ex.setSqlValues(new Object JavaDoc[]{new Integer JavaDoc(1)});
50     assertEquals("C = 1", sg.generate(ex));
51     ex.setSqlValues(new Object JavaDoc[]{null});
52     assertEquals("C IS NULL", sg.generate(ex));
53     
54     // 2 values
55
ex.setSqlValues(new String JavaDoc[]{"a","b"});
56     assertEquals("C IN ('a', 'b')", sg.generate(ex));
57     ex.setSqlValues(new String JavaDoc[]{"a", null});
58     assertEquals("(C = 'a' OR C IS NULL)", sg.generate(ex));
59     ex.setSqlValues(new String JavaDoc[]{null, "a"});
60     assertEquals("(C = 'a' OR C IS NULL)", sg.generate(ex));
61
62     // 3 or more values
63
ex.setSqlValues(new String JavaDoc[]{"a","b","c"});
64     assertEquals("C IN ('a', 'b', 'c')", sg.generate(ex));
65     ex.setSqlValues(new String JavaDoc[]{"a", null, "b"});
66     assertEquals("(C IN ('a', 'b') OR C IS NULL)", sg.generate(ex));
67     
68     // 3 or more number
69
ex.setSqlValues(new Object JavaDoc[]{new Double JavaDoc(12),new Integer JavaDoc(0), new Double JavaDoc(Double.NaN), new BigDecimal JavaDoc(1234.0)});
70     assertEquals("C IN (12.0, 0, NaN, 1234)", sg.generate(ex));
71   }
72   
73   public void testDummyExpr() {
74     SqlDummyExpr ex = new SqlDummyExpr();
75     assertEquals("1 = 1", sg.generate(ex));
76   }
77   
78   public void testBetweenExpr() {
79     SqlBetweenExpr ex = new SqlBetweenExpr();
80     ex.setFirst(new Integer JavaDoc(12));
81     ex.setLast(new Integer JavaDoc(22));
82     assertEquals("C BETWEEN 12 AND 22", sg.generate(ex));
83   }
84
85   private void eqlExprWithOperandsTest(SqlExprWithOperands expr, String JavaDoc expected) {
86     SqlEqualExpr opd1 = new SqlEqualExpr();
87     opd1.setColumnId("C1");
88     opd1.setSqlValue("c1");
89     expr.addOperand(opd1);
90     SqlEqualExpr opd2 = new SqlEqualExpr();
91     opd2.setColumnId("C2");
92     opd2.setSqlValue("c2");
93     expr.addOperand(opd2);
94     SqlGenerator sg = new SqlGenerator();
95     assertEquals(expected, sg.generate(expr));
96   }
97   
98   protected void setUp() throws Exception JavaDoc {
99     sg = new SqlGenerator("C");
100   }
101 }
Popular Tags