Java Code Examples for javax.el.ELProcessor#defineBean()

The following examples show how to use javax.el.ELProcessor#defineBean() . 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: 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 2
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 3
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testPeek01() {
    ELProcessor processor = new ELProcessor();
    List<TesterBeanA> debug = new ArrayList<>();
    processor.defineBean("beans", beans);
    processor.defineBean("debug", debug);

    Object result = processor.getValue(
            "beans.stream().peek(b->debug.add(b)).toList()",
            Object.class);

    List<TesterBeanA> expected = new ArrayList<>(3);
    expected.add(bean01);
    expected.add(bean02);
    expected.add(bean03);

    Assert.assertEquals(expected, result);
    Assert.assertEquals(expected, debug);
}
 
Example 4
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testToArray01() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);

    Object result = processor.getValue(
            "beans.stream().toArray()",
            Object.class);

    Object[] expected = new Object[3];
    expected[0] = bean01;
    expected[1] = bean02;
    expected[2] = bean03;

    Assert.assertArrayEquals(expected, (Object[]) result);
}
 
Example 5
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Test
public void testBean(){
    ELProcessor processor = ElUtil.initNewELProcessor( new EnvironmentImpl(), null );

    processor.defineBean( "client", new Client( 1, "Heli Kopter" ) );
    String name = (String)processor.eval( "client.name" );

    Assert.assertEquals( name, "Heli Kopter" );
}
 
Example 6
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testToList02() {
    ELProcessor processor = new ELProcessor();
    String[] src = new String[] { "a", "b", "c" };
    processor.defineBean("src", src);
    Object result = processor.getValue("src.stream().toList()",
            List.class);
    List<String> expected = new ArrayList<>(3);
    expected.add("a");
    expected.add("b");
    expected.add("c");

    Assert.assertEquals(expected, result);
}
 
Example 7
Source File: TestAstAssign.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testGetValue02() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("bean01", new TesterBeanB());

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

    Assert.assertEquals("hello", result);
}
 
Example 8
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 9
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testFindFirst01() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);

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

    Assert.assertEquals(bean01, result.get());
}
 
Example 10
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testMinLambda01() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);

    Object result = processor.getValue(
            "beans.stream().min((x,y)->x.name.compareTo(y.name))",
            Object.class);

    Assert.assertEquals(bean01, ((Optional) result).get());
}
 
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 testMin04() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);

    processor.getValue(
            "beans.stream().min()",
            Object.class);
}
 
Example 12
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testMaxLambda02() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);
    processor.setVariable("comparison", "v->(x,y)->v(x).compareTo(v(y))");

    Object result = processor.getValue(
            "beans.stream().max(comparison(x->x.name))",
            Object.class);

    Assert.assertEquals(bean03, ((Optional) result).get());
}
 
Example 13
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testMaxLambda01() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);

    Object result = processor.getValue(
            "beans.stream().max((x,y)->x.name.compareTo(y.name))",
            Object.class);

    Assert.assertEquals(bean03, ((Optional) result).get());
}
 
Example 14
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test(expected=ELException.class)
public void testMax04() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);

    processor.getValue(
            "beans.stream().max()",
            Object.class);
}
 
Example 15
Source File: ExpressionLanguageTest.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@Test
public void testEnvironmentBean(){
    EnvironmentImpl environment = new EnvironmentImpl();
    environment.setAttribute( "testAtr1", true );
    environment.setAttribute( "testAtr2", "33" );
    environment.setAttribute( "testAtr3", null );

    ELProcessor processor = ElUtil.initNewELProcessor( environment, null );
    // this way all the parameters must be prefixed with "env.", but we will at least be able to check unmapped attributes without an exception
    processor.defineBean( "env", environment.getAttributes() );

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

    Assert.assertTrue( result );
}
 
Example 16
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testSubstreamStart01() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);

    Object result = processor.getValue(
            "beans.stream().substream(1).toList()",
            Object.class);

    List<TesterBeanA> expected = new ArrayList<>(2);
    expected.add(bean02);
    expected.add(bean03);

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

    Object result = processor.getValue(
            "beans.stream().limit(2).toList()",
            Object.class);

    List<TesterBeanA> expected = new ArrayList<>(2);
    expected.add(bean01);
    expected.add(bean02);

    Assert.assertEquals(expected, result);
}
 
Example 18
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testFlatMap01() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);
    Object result = processor.getValue(
            "beans.stream().flatMap(b->b.name.toCharArray().stream()).toList()",
            List.class);

    List<Character> expected = new ArrayList<>(18);
    expected.add(Character.valueOf('b'));
    expected.add(Character.valueOf('e'));
    expected.add(Character.valueOf('a'));
    expected.add(Character.valueOf('n'));
    expected.add(Character.valueOf('0'));
    expected.add(Character.valueOf('1'));
    expected.add(Character.valueOf('b'));
    expected.add(Character.valueOf('e'));
    expected.add(Character.valueOf('a'));
    expected.add(Character.valueOf('n'));
    expected.add(Character.valueOf('0'));
    expected.add(Character.valueOf('2'));
    expected.add(Character.valueOf('b'));
    expected.add(Character.valueOf('e'));
    expected.add(Character.valueOf('a'));
    expected.add(Character.valueOf('n'));
    expected.add(Character.valueOf('0'));
    expected.add(Character.valueOf('3'));

    Assert.assertEquals(expected, result);
}
 
Example 19
Source File: TestCollectionOperations.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testMap01() {
    ELProcessor processor = new ELProcessor();
    processor.defineBean("beans", beans);
    Object result = processor.getValue(
            "beans.stream().map(b->b.name).toList()",
            List.class);
    List<String> expected = new ArrayList<>(3);
    expected.add("bean01");
    expected.add("bean02");
    expected.add("bean03");

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

    processor.defineBean( "client", new Client( 1, "Heli Kopter" ) );
    boolean result = (boolean)processor.eval( "not empty client.name and client.id > 0" );

    Assert.assertTrue( result );
}