Abs extends UDF { public Integer evaluate(Integer number) { if (number == null) { return null; } return Math.abs(number); } Abs(Map<String, String> config); Integer evaluate(Integer number); Long evaluate(Long number); Float evaluate(Float number); Double evaluate(Double number); BigDecimal evaluate(BigDecimal number);  }
@Test public void testEvaluateInteger() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(-1)}); Integer value = (Integer)methodExpression.evaluate(new TupleEvent()); assertTrue(value.equals(1)); }
@Test public void testEvaluateInteger2() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(Integer.MIN_VALUE)}); Integer value = (Integer)methodExpression.evaluate(new TupleEvent()); assertTrue(value.equals(Integer.MIN_VALUE)); }
@Test public void testEvaluateLong() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(-1l)}); Long value = (Long)methodExpression.evaluate(new TupleEvent()); assertTrue(value.equals(1l)); }
@Test public void testEvaluateFloat() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(-1.0f)}); Float value = (Float)methodExpression.evaluate(new TupleEvent()); assertTrue(value.equals(1.0f)); }
@Test public void testEvaluateDouble() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(-1.0d)}); Double value = (Double)methodExpression.evaluate(new TupleEvent()); assertTrue(value.equals(1.0d)); }
@Test public void testEvaluateBigDecimal() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression( new BigDecimal(-1))}); BigDecimal value = (BigDecimal)methodExpression.evaluate(new TupleEvent()); assertTrue(value.intValue() == 1); }
@Test public void testEvaluateString() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression("-1")}); try { methodExpression.evaluate(new TupleEvent()); fail("Expected an StreamingRuntimeException to be thrown"); } catch (StreamingRuntimeException e) { assertEquals(e.getErrorCode(), ErrorCode.FUNCTION_UNSUPPORTED_PARAMETERS); } }
@Test public void testEvaluateBoolean() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(true)}); try { methodExpression.evaluate(new TupleEvent()); fail("Expected an StreamingRuntimeException to be thrown"); } catch (StreamingRuntimeException e) { assertEquals(e.getErrorCode(), ErrorCode.FUNCTION_UNSUPPORTED_PARAMETERS); } }
@Test public void testEvaluateTime() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(new Time(0))}); try { methodExpression.evaluate(new TupleEvent()); fail("Expected an StreamingRuntimeException to be thrown"); } catch (StreamingRuntimeException e) { assertEquals(e.getErrorCode(), ErrorCode.FUNCTION_UNSUPPORTED_PARAMETERS); } }
@Test public void testEvaluateDate() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(new Date(0))}); try { methodExpression.evaluate(new TupleEvent()); fail("Expected an StreamingRuntimeException to be thrown"); } catch (StreamingRuntimeException e) { assertEquals(e.getErrorCode(), ErrorCode.FUNCTION_UNSUPPORTED_PARAMETERS); } }
@Test public void testEvaluateTimeStamp() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(new Timestamp(0))}); try { methodExpression.evaluate(new TupleEvent()); fail("Expected an StreamingRuntimeException to be thrown"); } catch (StreamingRuntimeException e) { assertEquals(e.getErrorCode(), ErrorCode.FUNCTION_UNSUPPORTED_PARAMETERS); } }
@Test public void testEvaluateNull() { MethodExpression methodExpression = new MethodExpression(new Abs(null), "evaluate", new IExpression[] {new ConstExpression(null)}); assertNull(methodExpression.evaluate(new TupleEvent())); }
@Test public void testEvaluate() { System.out.println("Integer max value : " + Integer.MAX_VALUE + ", Integer min value " + Integer.MIN_VALUE); Abs abs = new Abs(null); assertTrue(abs.evaluate(1).equals(1)); assertTrue(abs.evaluate(-1).equals(1)); assertTrue(abs.evaluate(0).equals(0)); assertTrue(abs.evaluate(-0).equals(0)); assertTrue(abs.evaluate(Integer.MAX_VALUE).equals(Integer.MAX_VALUE)); assertTrue(abs.evaluate(Integer.MIN_VALUE).equals(Integer.MIN_VALUE)); System.out.println("Long max value : " + Long.MAX_VALUE + ", Long min value " + Long.MIN_VALUE); assertTrue(abs.evaluate(1L).equals(1L)); assertTrue(abs.evaluate(-1L).equals(1L)); assertTrue(abs.evaluate(0L).equals(0L)); assertTrue(abs.evaluate(-0L).equals(0L)); assertTrue(abs.evaluate(Long.MAX_VALUE).equals(Long.MAX_VALUE)); assertTrue(abs.evaluate(Long.MIN_VALUE).equals(Long.MIN_VALUE)); assertTrue(abs.evaluate(1.0f).equals(1.0f)); assertTrue(abs.evaluate(-1.0f).equals(1.0f)); assertTrue(abs.evaluate(0.0f).equals(0.0f)); assertTrue(abs.evaluate(-0.0f).equals(0.0f)); assertTrue(abs.evaluate(1.0d).equals(1.0d)); assertTrue(abs.evaluate(-1.0d).equals(1.0d)); assertTrue(abs.evaluate(0.0d).equals(0.0d)); assertTrue(abs.evaluate(-0.0d).equals(0.0d)); assertTrue(abs.evaluate(new BigDecimal("1.0")).intValue() == 1); assertTrue(abs.evaluate(new BigDecimal("-1.0")).intValue() == 1); assertTrue(abs.evaluate(new BigDecimal("0.0")).intValue() == 0); assertTrue(abs.evaluate(new BigDecimal("-0.0")).intValue() == 0); }