@Test public void testComments() { final Map<String, String> attributes = new HashMap<>(); attributes.put("abc", "xyz"); final String expression = "# hello, world\n" + "${# ref attr\n" + "\t" + "abc" + "\t" + "#end ref attr\n" + "}"; Query query = Query.compile(expression); QueryResult<?> result = query.evaluate(attributes); assertEquals(ResultType.STRING, result.getResultType()); assertEquals("xyz", result.getValue()); query = Query.compile("${abc:append('# hello') #good-bye \n}"); result = query.evaluate(attributes); assertEquals(ResultType.STRING, result.getResultType()); assertEquals("xyz# hello", result.getValue()); }
private void verifyEquals( final String expression, final Map<String, String> attributes, final Object expectedResult) { Query.validateExpression(expression, false); assertEquals( String.valueOf(expectedResult), Query.evaluateExpressions(expression, attributes, null)); Query query = Query.compile(expression); QueryResult<?> result = query.evaluate(attributes); if (expectedResult instanceof Number) { assertEquals(ResultType.NUMBER, result.getResultType()); } else if (expectedResult instanceof Boolean) { assertEquals(ResultType.BOOLEAN, result.getResultType()); } else { assertEquals(ResultType.STRING, result.getResultType()); } assertEquals(expectedResult, result.getValue()); }
@Test @Ignore("Depends on TimeZone") public void testDateToNumber() { final Query query = Query.compile("${dateTime:toDate('yyyy/MM/dd HH:mm:ss.SSS'):toNumber()}"); final Map<String, String> attributes = new HashMap<>(); attributes.put("dateTime", "2013/11/18 10:22:27.678"); final QueryResult<?> result = query.evaluate(attributes); assertEquals(ResultType.NUMBER, result.getResultType()); assertEquals(1384788147678L, result.getValue()); }
@Test @Ignore("Requires specific locale") public void implicitDateConversion() { final Date date = new Date(); final Query query = Query.compile("${dateTime:format('yyyy/MM/dd HH:mm:ss.SSS')}"); final Map<String, String> attributes = new HashMap<>(); attributes.put("dateTime", date.toString()); // the date.toString() above will end up truncating the milliseconds. So remove millis from the // Date before // formatting it final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS", Locale.US); final long millis = date.getTime() % 1000L; final Date roundedToNearestSecond = new Date(date.getTime() - millis); final String formatted = sdf.format(roundedToNearestSecond); final QueryResult<?> result = query.evaluate(attributes); assertEquals(ResultType.STRING, result.getResultType()); assertEquals(formatted, result.getValue()); }