
package org.apache.camel.language.spel;

import org.apache.camel.CamelContext;
import org.apache.camel.ExpressionEvaluationException;
import org.apache.camel.LanguageTestSupport;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.camel.language.spel.bean.Dummy;
import org.junit.Test;

/**
 * Test access to beans defined in non-Spring context from SpEL expressions/predicates.
 */
public class SpelNonSpringTest extends LanguageTestSupport {

    @Override
    protected CamelContext createCamelContext() throws Exception {
        SimpleRegistry registry = new SimpleRegistry();
        registry.put("myDummy", new Dummy());
        return new DefaultCamelContext(registry);
    }

    @Test
    public void testSpelBeanExpressions() throws Exception {
        assertExpression("#{@myDummy.foo == 'xyz'}", true);
        assertExpression("#{@myDummy.bar == 789}", true);
        assertExpression("#{@myDummy.bar.toString()}", "789");
        try {
            assertExpression("#{@notFound}", null);
        } catch (ExpressionEvaluationException ex) {
            assertStringContains(ex.getMessage(), "Could not resolve bean reference against Registry");
        }
    }
    
    @Test
    public void testSpelBeanPredicates() throws Exception {
        assertPredicate("@myDummy.foo == 'xyz'");
        assertPredicate("@myDummy.bar == 789");
        assertPredicate("@myDummy.bar instanceof T(Integer)");
    }

    @Override
    protected String getLanguageName() {
        return "spel";
    }
}
