1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.beanshell;
17  
18  import junit.framework.Test;
19  import junit.framework.TestCase;
20  import junit.framework.TestSuite;
21  import junit.textui.TestRunner;
22  
23  import org.apache.commons.jelly.JellyContext;
24  import org.apache.commons.jelly.expression.Expression;
25  import org.apache.commons.jelly.expression.ExpressionFactory;
26  import org.apache.commons.jelly.tags.beanshell.BeanShellExpressionFactory;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  
32  /*** Tests the BeanShell EL
33    *
34    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
35    * @version $Revision: 1.4 $
36    */
37  public class TestBeanShellEL extends TestCase {
38  
39      /*** The Log to which logging calls will be made. */
40      private static final Log log = LogFactory.getLog( TestBeanShellEL.class );
41  
42      /*** Jelly context */
43      protected JellyContext context;
44  
45      /*** The factory of Expression objects */
46      protected ExpressionFactory factory;
47  
48  
49      public static void main( String[] args ) {
50          TestRunner.run( suite() );
51      }
52  
53      public static Test suite() {
54          return new TestSuite(TestBeanShellEL.class);
55      }
56  
57      public TestBeanShellEL(String testName) {
58          super(testName);
59      }
60  
61      public void setUp() {
62          context = new JellyContext();
63          context.setVariable( "foo", "abc" );
64          context.setVariable( "bar", new Integer( 123 ) );
65          factory = new BeanShellExpressionFactory();
66      }
67  
68      public void testEL() throws Exception {
69          assertExpression( "foo", "abc" );
70          assertExpression( "bar * 2", new Integer( 246 ) );
71          assertExpression( "bar == 123", Boolean.TRUE );
72          assertExpression( "bar == 124", Boolean.FALSE );
73          assertExpression( "foo.equals( \"abc\" )", Boolean.TRUE );
74          assertExpression( "foo.equals( \"xyz\" )", Boolean.FALSE );
75      }
76  
77      /*** Evaluates the given expression text and tests it against the expected value */
78      protected void assertExpression( String expressionText, Object expectedValue ) throws Exception {
79          Expression expr = factory.createExpression( expressionText );
80          Object value = expr.evaluate( context );
81          assertEquals( "Value of expression: " + expressionText, expectedValue, value );
82      }
83  }
84