You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2003/05/09 14:22:29 UTC

cvs commit: jakarta-commons/jexl/src/test/org/apache/commons/jexl/junit AsserterTest.java

jstrachan    2003/05/09 05:22:29

  Modified:    jexl/src/test/org/apache/commons/jexl JexlTest.java
               jexl     project.xml .cvsignore
  Added:       jexl/src/java/org/apache/commons/jexl/junit Asserter.java
               jexl/src/test/org/apache/commons/jexl Foo.java
               jexl/src/test/org/apache/commons/jexl/junit
                        AsserterTest.java
  Log:
  Added a simple Asserter helper class which can make using expression languages in JUnit test cases easier.

Often you end up with a graph of objects which can often take some clumsy casting or navigation code in your unit tests.

Asserter asserter = new Asserter( new SomeBean() );
asserter.assert("this.foo.bar[1].something", expectedValue);

or

asserter.setVariable("foo", new SomeOtherBean());
asserter.assert("foo.something.calculateSomething(a,12)", expected);
  
  Revision  Changes    Path
  1.1                  jakarta-commons/jexl/src/java/org/apache/commons/jexl/junit/Asserter.java
  
  Index: Asserter.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", "Jexl" and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.jexl.junit;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import junit.framework.Assert;
  
  import org.apache.commons.jexl.Expression;
  import org.apache.commons.jexl.ExpressionFactory;
  import org.apache.commons.jexl.JexlContext;
  import org.apache.commons.jexl.JexlHelper;
  
  /**
   * A utility class for performing JUnit based assertions using Jexl
   * expressions. This class can make it easier to do unit tests using
   * Jexl navigation expressions.
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.1 $
   */
  public class Asserter extends Assert {
  
      private Map variables = new HashMap();
      private JexlContext context = JexlHelper.createContext();
  
      public Asserter() {
      }
  
      /**
       * This consstructor will register the given variableValue as the
       * "this" variable.
       * 
       * @param variableValue
       */
      public Asserter(Object variableValue) {
          setVariable("this", variableValue);
      }
  
      /**
       * Performs an assertion that the value of the given Jexl expression 
       * evaluates to the given expected value
       * 
       * @param expression is the Jexl expression to evaluate
       * @param expected is the expected value of the expression
       * @throws Exception if the expression could not be evaluationed or an assertion
       * fails
       */
      public void assertExpression(String expression, Object expected) throws Exception {
          Expression exp = ExpressionFactory.createExpression(expression);
  
          context.setVars(variables);
          Object value = exp.evaluate(context);
  
          assertEquals("expression: " + expression, expected, value);
      }
  
      /**
       * Puts a variable of a certain name in the context so that it can be used from
       * assertion expressions.
       * 
       * @param name
       * @param value
       */
      public void setVariable(String name, Object value) {
          variables.put(name, value);
      }
  
  }
  
  
  
  1.28      +1 -63     jakarta-commons/jexl/src/test/org/apache/commons/jexl/JexlTest.java
  
  Index: JexlTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jexl/src/test/org/apache/commons/jexl/JexlTest.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- JexlTest.java	18 Feb 2003 20:22:41 -0000	1.27
  +++ JexlTest.java	9 May 2003 12:22:29 -0000	1.28
  @@ -998,68 +998,6 @@
   
       }
   
  -    public class Foo
  -    {
  -        public String bar()
  -        {
  -            return METHOD_STRING;
  -        }
  -
  -        public String getBar()
  -        {
  -            return GET_METHOD_STRING;
  -        }
  -
  -        public Foo getInnerFoo()
  -        {
  -            return new Foo();
  -        }
  -
  -        public String get(String arg)
  -        {
  -            return "Repeat : " + arg;
  -        }
  -
  -        public String convertBoolean(boolean b)
  -        {
  -            return "Boolean : " + b;
  -        }
  -
  -        public int getCount() {
  -            return 5;
  -        }
  -
  -        public List getCheeseList()
  -        {
  -            ArrayList answer = new ArrayList();
  -            answer.add("cheddar");
  -            answer.add("edam");
  -            answer.add("brie");
  -            return answer;
  -        }
  -
  -        public String[] getArray()
  -        {
  -            return GET_METHOD_ARRAY;
  -        }
  -
  -        public String[][] getArray2()
  -        {
  -            return GET_METHOD_ARRAY2;
  -        }
  -
  -        public boolean isSimple()
  -        {
  -            return true;
  -        }
  -
  -        public int square(int value)
  -        {
  -            return value * value;
  -        }
  -    }
  -
  -
       /**
        * Asserts that the given expression returns the given value when applied to the
        * given context
  
  
  
  1.1                  jakarta-commons/jexl/src/test/org/apache/commons/jexl/Foo.java
  
  Index: Foo.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", "Jexl" and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.jexl;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * A simple bean used for testing purposes
   * 
   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
   * @version $Revision: 1.1 $
   */
  public class Foo {
      
      public String bar()
      {
          return JexlTest.METHOD_STRING;
      }
  
      public String getBar()
      {
          return JexlTest.GET_METHOD_STRING;
      }
  
      public Foo getInnerFoo()
      {
          return new Foo();
      }
  
      public String get(String arg)
      {
          return "Repeat : " + arg;
      }
  
      public String convertBoolean(boolean b)
      {
          return "Boolean : " + b;
      }
  
      public int getCount() {
          return 5;
      }
  
      public List getCheeseList()
      {
          ArrayList answer = new ArrayList();
          answer.add("cheddar");
          answer.add("edam");
          answer.add("brie");
          return answer;
      }
  
      public String[] getArray()
      {
          return JexlTest.GET_METHOD_ARRAY;
      }
  
      public String[][] getArray2()
      {
          return JexlTest.GET_METHOD_ARRAY2;
      }
  
      public boolean isSimple()
      {
          return true;
      }
  
      public int square(int value)
      {
          return value * value;
      }
  
  }
  
  
  
  1.11      +3 -3      jakarta-commons/jexl/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jexl/project.xml,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- project.xml	5 Feb 2003 19:04:49 -0000	1.10
  +++ project.xml	9 May 2003 12:22:29 -0000	1.11
  @@ -4,7 +4,7 @@
     <pomVersion>3</pomVersion>
     <name>commons-jexl</name>
     <id>commons-jexl</id>
  -  <currentVersion>1.0-beta-1</currentVersion>
  +  <currentVersion>1.0-beta-2</currentVersion>
   
     <organization>
       <name>Apache Software Foundation</name>
  @@ -70,11 +70,11 @@
     <dependencies>
       <dependency>
         <id>junit</id>
  -      <version>3.7</version>
  +      <version>3.8.1</version>
       </dependency>
       <dependency>
         <id>commons-logging</id>
  -      <version>1.0</version>
  +      <version>1.0.2</version>
       </dependency>
     </dependencies>
   
  
  
  
  1.2       +1 -0      jakarta-commons/jexl/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jexl/.cvsignore,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- .cvsignore	10 Oct 2002 10:29:32 -0000	1.1
  +++ .cvsignore	9 May 2003 12:22:29 -0000	1.2
  @@ -7,3 +7,4 @@
   *.gz
   tmp
   lib
  +bin
  
  
  
  1.1                  jakarta-commons/jexl/src/test/org/apache/commons/jexl/junit/AsserterTest.java
  
  Index: AsserterTest.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", "Jexl" and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.jexl.junit;
  
  import junit.framework.AssertionFailedError;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.jexl.Foo;
  
  /**
   *  Simple testcases
   *
   *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
   *  @version $Id: AsserterTest.java,v 1.1 2003/05/09 12:22:29 jstrachan Exp $
   */
  public class AsserterTest extends TestCase {
      
      public static Test suite() {
          return new TestSuite(AsserterTest.class);
      }
  
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      public AsserterTest(String testName) {
          super(testName);
      }
  
      public void testThis() throws Exception {
          Asserter asserter = new Asserter(new Foo());
          
          asserter.assertExpression("this.get('abc')", "Repeat : abc");
          
          try {
              asserter.assertExpression("this.count", "Wrong Value");
              fail("This method should have thrown an assertion exception");
          }
          catch (AssertionFailedError e) {
              // it worked!
          }
      }
  
      public void testVariable() throws Exception {
          Asserter asserter = new Asserter();
          asserter.setVariable("foo", new Foo());
          asserter.setVariable("person", "James");
  
          asserter.assertExpression("person", "James");
          asserter.assertExpression("size(person)", new Integer(5));
          
          asserter.assertExpression("foo.getCount()", new Integer(5));
          asserter.assertExpression("foo.count", new Integer(5));
          
          try {
              asserter.assertExpression("bar.count", new Integer(5));
              fail("This method should have thrown an assertion exception");
          }
          catch (AssertionFailedError e) {
              // it worked!
          }
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org