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.jexl.junit;
17  
18  import junit.framework.AssertionFailedError;
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import junit.textui.TestRunner;
23  
24  import org.apache.commons.jexl.Foo;
25  
26  /***
27   *  Simple testcases
28   *
29   *  @since 1.0
30   *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
31   *  @version $Id: AsserterTest.java 209171 2005-07-05 01:24:57Z dion $
32   */
33  public class AsserterTest extends TestCase {
34      
35      public static Test suite() {
36          return new TestSuite(AsserterTest.class);
37      }
38  
39      public static void main(String[] args) {
40          TestRunner.run(suite());
41      }
42  
43      public AsserterTest(String testName) {
44          super(testName);
45      }
46  
47      public void testThis() throws Exception {
48          Asserter asserter = new Asserter(new Foo());
49          
50          asserter.assertExpression("this.get('abc')", "Repeat : abc");
51          
52          try {
53              asserter.assertExpression("this.count", "Wrong Value");
54              fail("This method should have thrown an assertion exception");
55          }
56          catch (AssertionFailedError e) {
57              // it worked!
58          }
59      }
60  
61      public void testVariable() throws Exception {
62          Asserter asserter = new Asserter();
63          asserter.setVariable("foo", new Foo());
64          asserter.setVariable("person", "James");
65  
66          asserter.assertExpression("person", "James");
67          asserter.assertExpression("size(person)", new Integer(5));
68          
69          asserter.assertExpression("foo.getCount()", new Integer(5));
70          asserter.assertExpression("foo.count", new Integer(5));
71          
72          try {
73              asserter.assertExpression("bar.count", new Integer(5));
74              fail("This method should have thrown an assertion exception");
75          }
76          catch (AssertionFailedError e) {
77              // it worked!
78          }
79      }
80  }