public void testGetWhen() { CaseExpression expr = example(3); assertNotNull(expr.getWhen()); assertEquals(3, expr.getWhen().size()); try { expr.getWhen().add(new Object()); fail("Should not be modifiable"); //$NON-NLS-1$ } catch (UnsupportedOperationException e) { } }
public void testEquals() { CaseExpression sc1 = example(3); assertTrue(sc1.equals(sc1)); assertTrue(sc1.equals(sc1.clone())); assertTrue(sc1.clone().equals(sc1)); assertTrue(sc1.equals(example(3))); CaseExpression sc2 = example(4); assertFalse(sc1.equals(sc2)); assertFalse(sc2.equals(sc1)); CaseExpression sc3 = example(3); sc3.setElseExpression(new ElementSymbol("y")); //$NON-NLS-1$ assertFalse(sc1.equals(sc3)); assertFalse(sc3.equals(sc1)); CaseExpression sc4 = example(3); sc4.setExpression(new ElementSymbol("y")); //$NON-NLS-1$ assertFalse(sc1.equals(sc4)); assertFalse(sc4.equals(sc1)); } }
public void testGetType() { CaseExpression expr = example(4); assertNull(expr.getType()); expr.setType(Integer.class); assertEquals(Integer.class, expr.getType()); }
public void testGetWhenExpression() { helpTestWhenExpressions(example(3), 3); }
public void testGetThenExpression() { helpTestThenExpressions(example(3), 3); }
public void testGetWhenCount() { assertEquals(1, example(1).getWhenCount()); assertEquals(2, example(2).getWhenCount()); assertEquals(3, example(3).getWhenCount()); assertEquals(4, example(4).getWhenCount()); }
public void testSetWhen() { CaseExpression caseExpr = example(4); fail("Setting WHEN and THEN to null should have failed."); //$NON-NLS-1$ } catch (IllegalArgumentException e) { helpTestWhenExpressions(caseExpr, 4); helpTestThenExpressions(caseExpr, 4); caseExpr.setWhen(getWhenExpressions(2), null); fail("Setting THEN to null should have failed."); //$NON-NLS-1$ } catch (IllegalArgumentException e) { helpTestWhenExpressions(caseExpr, 4); helpTestThenExpressions(caseExpr, 4); caseExpr.setWhen(null, getThenExpressions(2)); fail("Setting WHEN to null should have failed."); //$NON-NLS-1$ } catch (IllegalArgumentException e) { helpTestWhenExpressions(caseExpr, 4); helpTestThenExpressions(caseExpr, 4); caseExpr.setWhen(getWhenExpressions(0), getThenExpressions(0)); fail("Setting WHEN and THEN to empty lists should have failed."); //$NON-NLS-1$ } catch (IllegalArgumentException e) { helpTestWhenExpressions(caseExpr, 4); helpTestThenExpressions(caseExpr, 4);
public void testGetExpression() { assertNotNull(example(1).getExpression()); assertEquals(new ElementSymbol("x"), example(1).getExpression()); //$NON-NLS-1$ }
public void testSetExpression() { CaseExpression caseExpr = example(1); ElementSymbol y = new ElementSymbol("y"); //$NON-NLS-1$ caseExpr.setExpression(y); assertEquals(y, caseExpr.getExpression()); try { caseExpr.setExpression(null); fail("Setting the expression to null should fail."); //$NON-NLS-1$ } catch (IllegalArgumentException e) { // There should be no side-effects of an illegal argument assertEquals(y, caseExpr.getExpression()); } }
assertTrue(expr != clone); helpTestStrictEquivalence(x, clone.getExpression()); helpTestStrictEquivalence(expr.getExpression(), clone.getExpression()); assertEquals(2, clone.getWhenCount()); helpTestStrictEquivalence(e1, clone.getWhenExpression(0)); helpTestStrictEquivalence(expr.getWhenExpression(0), clone.getWhenExpression(0)); helpTestStrictEquivalence(e2, clone.getWhenExpression(1)); helpTestStrictEquivalence(expr.getWhenExpression(1), clone.getWhenExpression(1)); helpTestStrictEquivalence(const1, clone.getThenExpression(0)); helpTestStrictEquivalence(expr.getThenExpression(0), clone.getThenExpression(0)); helpTestStrictEquivalence(const2, clone.getThenExpression(1)); helpTestStrictEquivalence(expr.getThenExpression(1), clone.getThenExpression(1)); helpTestStrictEquivalence(expr.getElseExpression(), clone.getElseExpression()); assertEquals(expr.getType(), clone.getType());
TestCaseExpression.helpTestThenExpressions(caseExpr, 4); TestCaseExpression.helpTestThenExpressions(caseExpr, 4); caseExpr.setWhen(null, TestCaseExpression.getThenExpressions(2)); fail("Setting WHEN to null should have failed."); //$NON-NLS-1$ } catch (IllegalArgumentException e) { TestCaseExpression.helpTestThenExpressions(caseExpr, 4); caseExpr.setWhen(getWhenCriteria(0), TestCaseExpression.getThenExpressions(0)); fail("Setting WHEN and THEN to empty lists should have failed."); //$NON-NLS-1$ } catch (IllegalArgumentException e) { TestCaseExpression.helpTestThenExpressions(caseExpr, 4); caseExpr.setWhen(TestCaseExpression.getWhenExpressions(3), TestCaseExpression.getThenExpressions(3)); fail("Setting WHEN non Criteria types should have failed."); //$NON-NLS-1$ } catch (IllegalArgumentException e) { TestCaseExpression.helpTestThenExpressions(caseExpr, 4);
public static CaseExpression example(int whens) { ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$ CaseExpression caseExpr = new CaseExpression(x, getWhenExpressions(whens), getThenExpressions(whens)); caseExpr.setElseExpression(new Constant(new Integer(9999))); return caseExpr; }
@Test public void testCaseExpression1() { helpTest(TestCaseExpression.example(2), "CASE x WHEN 'a' THEN 0 WHEN 'b' THEN 1 ELSE 9999 END"); //$NON-NLS-1$ }
public static SearchedCaseExpression helpExample() { SearchedCaseExpression caseExpr = new SearchedCaseExpression(getWhenCriteria(3), TestCaseExpression.getThenExpressions(3)); caseExpr.setElseExpression(new Constant(new Integer(9999))); return caseExpr; }
public static void helpTestWhenExpressions(CaseExpression caseExpr, int expectedWhens) { assertEquals(expectedWhens, caseExpr.getWhenCount()); for (int i = 0; i < expectedWhens; i++) { assertEquals(new Constant(String.valueOf((char)('a' + i))), caseExpr.getWhenExpression(i)); } }
public static List getWhenExpressions(int expressions) { return getWhenExpressions(expressions, -1, false); }
/** * Test that the Object references are not equal, but that the result of * the equals() method is true. * @param obj1 * @param obj2 */ public static void helpTestStrictEquivalence(Object obj1, Object obj2) { assertTrue(obj1 != obj2); assertTrue(obj1 != null); assertTrue(obj2 != null); assertTrue(obj1.equals(obj2)); assertTrue(obj2.equals(obj1)); }
TestCaseExpression.helpTestStrictEquivalence(c1, clone.getWhenCriteria(0)); TestCaseExpression.helpTestStrictEquivalence(expr.getWhenCriteria(0), clone.getWhenCriteria(0)); TestCaseExpression.helpTestStrictEquivalence(c2, clone.getWhenCriteria(1)); TestCaseExpression.helpTestStrictEquivalence(expr.getWhenCriteria(1), clone.getWhenCriteria(1)); TestCaseExpression.helpTestStrictEquivalence(const1, clone.getThenExpression(0)); TestCaseExpression.helpTestStrictEquivalence(expr.getThenExpression(0), clone.getThenExpression(0)); TestCaseExpression.helpTestStrictEquivalence(const2, clone.getThenExpression(1)); TestCaseExpression.helpTestStrictEquivalence(expr.getThenExpression(1), clone.getThenExpression(1)); TestCaseExpression.helpTestStrictEquivalence(expr.getElseExpression(), clone.getElseExpression()); assertEquals(expr.getType(), clone.getType());
public static CaseExpression example(int whens, int nullIndex, boolean includeNull) { ArgCheck.isTrue(nullIndex < whens, "Null Index must be less than the number of When expressions"); //$NON-NLS-1$ ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$ CaseExpression caseExpr = new CaseExpression(x, getWhenExpressions(whens, nullIndex, includeNull), getThenExpressions(whens)); caseExpr.setElseExpression(new Constant(new Integer(9999))); return caseExpr; }
@Test public void testCaseExpression3() { CaseExpression example = TestCaseExpression.example(3, 0, true); helpTest(example, "CASE x WHEN null THEN 0 WHEN 'b' THEN 1 WHEN 'c' THEN 2 ELSE 9999 END"); //$NON-NLS-1$ }