javax.el.ELProcessor Java Examples

The following examples show how to use javax.el.ELProcessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 6 votes vote down vote up
@Test
public void testEnvironmentResolver(){
    EnvironmentImpl environment = new EnvironmentImpl();
    environment.setAttribute( "testAtr1", true );
    environment.setAttribute( "testAtr2", "33" );
    environment.setAttribute( "testAtr3", null );

    ELProcessor processor = ElUtil.initNewELProcessor( environment, null );

    boolean result = (boolean)processor.eval( "empty testAtr3 && testAtr1" );

    // this again fails: "ELResolver cannot handle a null base Object with identifier 'unknownAtr'"
    //boolean result = (boolean)processor.eval( "empty testAtr3 && testAtr1 && empty unknownAtr" );

    Assert.assertTrue( result );
}
 
Example #2
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 6 votes vote down vote up
@Test
public void testEnvironmentResolverDate(){
    Date start = new Date();

    EnvironmentImpl environment = new EnvironmentImpl();
    environment.setAttribute( "currentMillis", System.currentTimeMillis() );

    try{
        Thread.sleep( 30 );
    }
    catch( InterruptedException e ){
    }

    ELProcessor processor = ElUtil.initNewELProcessor( environment, null );

    boolean result = (boolean)processor.eval( "NOW.time > currentMillis" );
    Assert.assertTrue( result );

    Date now = (Date)processor.eval( "NOW" );
    Assert.assertTrue( now.after( start ) );
}
 
Example #3
Source File: MessagesTest.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
@Test
public void testElExpressionGettingMessagesByCaegory() {
	messages.add(new SimpleMessage("client.id", "will generated", Severity.INFO));
	messages.add(new SimpleMessage("client.name", "not null"));
	messages.add(new SimpleMessage("client.name", "not empty"));

	ELProcessor el = new ELProcessor();
	el.defineBean("messages", messages);

	String result = el.eval("messages.errors.from('client.name')").toString();
	assertThat(result, is("not null, not empty"));

	result = el.eval("messages.errors.from('client.name').join(' - ')").toString();
	assertThat(result, is("not null - not empty"));

	result = el.eval("messages.errors.from('client.id')").toString();
	assertThat(result, isEmptyString());

	result = el.eval("messages.info.from('client.id')").toString();
	assertThat(result, is("will generated"));
}
 
Example #4
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testForEach01() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);
    processor.getValue(
            "beans.stream().forEach(b->b.setValLong(b.valLong + 1))",
            Object.class);

    Assert.assertEquals(2, bean01.getValLong());
    Assert.assertEquals(3, bean02.getValLong());
    Assert.assertEquals(4, bean03.getValLong());

    // Restore the beans to their default state
    processor.getValue(
            "beans.stream().forEach(b->b.setValLong(b.valLong - 1))",
            Object.class);

    Assert.assertEquals(1, bean01.getValLong());
    Assert.assertEquals(2, bean02.getValLong());
    Assert.assertEquals(3, bean03.getValLong());
}
 
Example #5
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testMap02() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);
    Object result = processor.getValue(
            "beans.stream().filter(b->b.valLong > 1).map(b->[b.name, b.valLong]).toList()",
            List.class);

    Assert.assertTrue(result instanceof List);

    @SuppressWarnings("unchecked")
    List<List<Object>> list = (List<List<Object>>) result;

    Assert.assertEquals(2, list.size());
    Assert.assertEquals("bean02", list.get(0).get(0));
    Assert.assertEquals(Long.valueOf(2), list.get(0).get(1));
    Assert.assertEquals("bean03", list.get(1).get(0));
    Assert.assertEquals(Long.valueOf(3), list.get(1).get(1));
}
 
Example #6
Source File: TestAstMapData.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testSimple01() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.getValue(
            "{'a':'1','b':'2','c':'3'}", Map.class);
    Assert.assertEquals(simpleMap, result);
}
 
Example #7
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test(expected=ELException.class)
public void testFindFirst02() {
    ELProcessor processor = new ELProcessor();

    Optional result = (Optional) processor.getValue(
            "[].stream().findFirst()",
            Object.class);

    result.get();
}
 
Example #8
Source File: TestAstSemicolon.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testGetType() {
    ELProcessor processor = new ELProcessor();
    ELContext context = processor.getELManager().getELContext();
    ExpressionFactory factory = ELManager.getExpressionFactory();

    ValueExpression ve = factory.createValueExpression(
            context, "${1+1;2+2}", Integer.class);

    Assert.assertEquals(Number.class, ve.getType(context));
    Assert.assertEquals(Integer.valueOf(4), ve.getValue(context));
}
 
Example #9
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Test
public void testEnvironmentResolverInstanceIdNull(){
    EnvironmentImpl environment = new EnvironmentImpl();

    ELProcessor processor = ElUtil.initNewELProcessor( environment, null );

    Long instanceId = (Long)processor.eval( "WORKFLOW_INSTANCE_ID" );
    Assert.assertEquals( null, instanceId );
}
 
Example #10
Source File: TestAstLambdaExpression.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testSpec05() {
    ELProcessor processor = new ELProcessor();
    Object result =
            processor.getValue("fact = n -> n==0? 1: n*fact(n-1); fact(5)",
                    Integer.class);
    Assert.assertEquals(Integer.valueOf(120), result);
}
 
Example #11
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test(expected=ELException.class)
public void testAverage06() {
    ELProcessor processor = new ELProcessor();

    processor.getValue(
            "[].stream().average().orElseGet(10)",
            Object.class);
}
 
Example #12
Source File: TestAstFunction.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testImport02() {
    ELProcessor processor = new ELProcessor();
    processor.getELManager().getELContext().getImportHandler()
            .importStatic("java.lang.Integer.valueOf");
    Object result = processor.getValue("valueOf(1000)", Integer.class);
    Assert.assertEquals(Integer.valueOf(1000), result);
}
 
Example #13
Source File: TestAstLambdaExpression.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testSpec06() {
    ELProcessor processor = new ELProcessor();
    Object result =
            processor.getValue("(x->y->x-y)(2)(1)",
                    Integer.class);
    Assert.assertEquals(Integer.valueOf(1), result);
}
 
Example #14
Source File: TestAstAssign.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testGetValue01() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("bean01", new TesterBeanB());

    Object result = processor.getValue(
            "bean01.text = 'hello'", String.class);

    Assert.assertEquals("hello", result);
}
 
Example #15
Source File: TestAstConcatenation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Test string concatenation with whitespace.
 */
@Test
public void testConcatenation03() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.getValue("' a' += ' b '", String.class);
    Assert.assertEquals(" a b ", result);
}
 
Example #16
Source File: TestAstConcatenation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Test string concatenation with mixed types.
 */
@Test
public void testConcatenation04() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.getValue("'a' += 3", String.class);
    Assert.assertEquals("a3", result);
}
 
Example #17
Source File: TestAstConcatenation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Test operator precedence (+ before +=).
 */
@Test
public void testPrecedence01() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.getValue("1 + 2 += 3", String.class);
    Assert.assertEquals("33", result);
}
 
Example #18
Source File: TestAstLambdaExpression.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testLambdaAsFunction06() {
    ELProcessor processor = new ELProcessor();
    Object result =
            processor.getValue("v = (()->y->()->()->x->x-y); v()(1)()()(2)",
                    Integer.class);
    Assert.assertEquals(Integer.valueOf(1), result);
}
 
Example #19
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testNoneMatch03() {
    ELProcessor processor = new ELProcessor();

    Optional result = (Optional) processor.getValue(
            "[1,2,3,4,5].stream().noneMatch(x->x>10)",
            Object.class);

    Assert.assertEquals(Boolean.TRUE, result.get());
}
 
Example #20
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Test
public void testValue(){
    ELProcessor processor = ElUtil.initNewELProcessor( new EnvironmentImpl(), null );

    processor.setValue( "throwException", false );
    boolean success = (boolean)processor.eval( "not throwException" );

    Assert.assertTrue( success );
}
 
Example #21
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test(expected=ELException.class)
public void testAllMatch04() {
    ELProcessor processor = new ELProcessor();

    Optional result = (Optional) processor.getValue(
            "[].stream().allMatch(x->x==7)",
            Object.class);

    result.get();
}
 
Example #22
Source File: ElUtil.java    From tessera with Apache License 2.0 5 votes vote down vote up
public static String process(String data, Map<String, ?> parameters) {
    ELProcessor eLProcessor = new ELProcessor();

    parameters.entrySet().forEach(e -> eLProcessor.defineBean(e.getKey(), e.getValue()));

    ELContext eLContext = eLProcessor.getELManager().getELContext();
    ValueExpression valueExpression = ExpressionFactory.newInstance()
            .createValueExpression(eLContext, data, String.class);
    return (String) valueExpression.getValue(eLContext);
}
 
Example #23
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testAllMatch02() {
    ELProcessor processor = new ELProcessor();

    Optional result = (Optional) processor.getValue(
            "[1,2,3,4,5].stream().allMatch(x->x>0)",
            Object.class);

    Assert.assertEquals(Boolean.TRUE, result.get());
}
 
Example #24
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testAllMatch01() {
    ELProcessor processor = new ELProcessor();

    Optional result = (Optional) processor.getValue(
            "[1,2,3,4,5].stream().allMatch(x->x>3)",
            Object.class);

    Assert.assertEquals(Boolean.FALSE, result.get());
}
 
Example #25
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test(expected=ELException.class)
public void testAnyMatch03() {
    ELProcessor processor = new ELProcessor();

    Optional result = (Optional) processor.getValue(
            "[].stream().anyMatch(x->x==7)",
            Object.class);

    result.get();
}
 
Example #26
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testToList01() {
    ELProcessor processor = new ELProcessor();
    Object result = processor.getValue("['a','b','c'].stream().toList()",
            List.class);
    List<String> expected = new ArrayList<>(3);
    expected.add("a");
    expected.add("b");
    expected.add("c");

    Assert.assertEquals(expected, result);
}
 
Example #27
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testAnyMatch01() {
    ELProcessor processor = new ELProcessor();

    Optional result = (Optional) processor.getValue(
            "[1,2,3,4,5].stream().anyMatch(x->x==7)",
            Object.class);

    Assert.assertEquals(Boolean.FALSE, result.get());
}
 
Example #28
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testCount02() {
    ELProcessor processor = new ELProcessor();

    Object result = processor.getValue(
            "[].stream().count()",
            Object.class);

    Assert.assertTrue("Result: " + result.toString(),
            ELSupport.equals(null, Long.valueOf(0), result));
}
 
Example #29
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testSum02() {
    ELProcessor processor = new ELProcessor();

    Object result = processor.getValue(
            "[].stream().sum()",
            Object.class);

    Assert.assertTrue("Result: " + result.toString(),
            ELSupport.equals(null, Long.valueOf(0), result));
}
 
Example #30
Source File: TestAstConcatenation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testGetType() {
    ELProcessor processor = new ELProcessor();
    ELContext context = processor.getELManager().getELContext();
    ExpressionFactory factory = ELManager.getExpressionFactory();

    ValueExpression ve = factory.createValueExpression(
            context, "${'a' += 3}", String.class);

    Assert.assertEquals(String.class, ve.getType(context));
    Assert.assertEquals("a3", ve.getValue(context));
}