Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright the original author or authors.
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package uelbox;
 
 import java.util.List;
 import java.util.Map;
 
 import  javax.el.ELContext;
 import  javax.el.ELResolver;
 import  javax.el.ExpressionFactory;
 import  javax.el.ValueExpression;
 
 import org.junit.Test;
 
 import uelbox.UEL;

Test HelperELContext / HelperELResolver.
 
 public class HelperELContextTest {
     public static class Foo {
         private Map<StringBazbar = new HashMap<StringBaz>();
 
         public Map<StringBazgetBar() {
             return ;
         }
 
         public void setBar(Map<StringBazbar) {
             this. = bar;
         }
 
     }
 
     public static class Baz {
         private Object value;
 
         public Object getValue() {
             return ;
         }
 
         public void setValue(Object value) {
             this. = value;
         }
 
         public void go() {
             throw new UnsupportedOperationException("Shouldn't actually call me!");
         }
     }
 
     private ELContext context;
     private ExpressionFactory expressionFactory;
     private Foo foo;
 
     @Before
     public void setup() {
          = new Foo();
         .getBar().put("baz"new Baz());
          = new SimpleELContext();
         .getVariableMapper().setVariable("foo",
             UEL.getExpressionFactory().createValueExpression(Foo.class));
     }
 
     public static class TestResult {
         final Object base;
         final List<Objectproperties = new ArrayList<Object>();
 
         public TestResult(Object base) {
             this. = base;
         }
 
     }
 
     @Test
     public void testHelperFunctionality() {
 
         HelperELContext<TestResulthelper = new HelperELContext<TestResult>() {
 
            @Override
            protected HelperELResolver<TestResultwrap(ELResolver elResolver) {
                return new HelperELResolver.WithWorkingStorage.AsResult<TestResult>(elResolver) {
                    @Override
                    protected TestResult createWorkingStorage(ELContext contextObject base) {
                        return new TestResult(base);
                    }
                    @Override
                    protected void afterGetValue(ELContext contextObject baseObject propertyObject value,
                        TestResult workingStorage) {
                        workingStorage.properties.add(property);
                    }
                    @Override
                    protected TestResult afterSetValue(ELContext contextObject baseObject property,
                        TestResult workingStorage) {
                        workingStorage.properties.add(property);
                        return workingStorage;
                    }
                };
            }
        };
        doValueExpressionTestResultAssertions(helper.evaluate(createValueExpression("foo.bar['baz'].value")), ,
            "bar""baz""value");
        doValueExpressionTestResultAssertions(helper.evaluate(createValueExpression("foo.bar['baz']")), "bar",
            "baz");
        doValueExpressionTestResultAssertions(helper.evaluate(createValueExpression("foo.bar")), "bar");
    }
    private ValueExpression createValueExpression(String expression) {
        return createValueExpression(expressionObject.class);
    }
    private ValueExpression createValueExpression(String expressionClass<?> expectedType) {
        return .createValueExpression(, UEL.embed(expression), expectedType);
    }
    private void doValueExpressionTestResultAssertions(TestResult resultObject baseObject... properties) {
        Assert.assertSame(baseresult.base);
        Assert.assertEquals(Arrays.asList(properties), result.properties);
    }
New to GrepCode? Check out our FAQ X