1 package com.tonbeller.wcf.param; 2 3 import java.math.BigDecimal ; 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 (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 ex.setSqlValues(new Object []{}); 46 assertEquals("1=0", sg.generate(ex)); 47 48 ex.setSqlValues(new Object []{new Integer (1)}); 50 assertEquals("C = 1", sg.generate(ex)); 51 ex.setSqlValues(new Object []{null}); 52 assertEquals("C IS NULL", sg.generate(ex)); 53 54 ex.setSqlValues(new String []{"a","b"}); 56 assertEquals("C IN ('a', 'b')", sg.generate(ex)); 57 ex.setSqlValues(new String []{"a", null}); 58 assertEquals("(C = 'a' OR C IS NULL)", sg.generate(ex)); 59 ex.setSqlValues(new String []{null, "a"}); 60 assertEquals("(C = 'a' OR C IS NULL)", sg.generate(ex)); 61 62 ex.setSqlValues(new String []{"a","b","c"}); 64 assertEquals("C IN ('a', 'b', 'c')", sg.generate(ex)); 65 ex.setSqlValues(new String []{"a", null, "b"}); 66 assertEquals("(C IN ('a', 'b') OR C IS NULL)", sg.generate(ex)); 67 68 ex.setSqlValues(new Object []{new Double (12),new Integer (0), new Double (Double.NaN), new BigDecimal (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 (12)); 81 ex.setLast(new Integer (22)); 82 assertEquals("C BETWEEN 12 AND 22", sg.generate(ex)); 83 } 84 85 private void eqlExprWithOperandsTest(SqlExprWithOperands expr, String 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 { 99 sg = new SqlGenerator("C"); 100 } 101 } | Popular Tags |