leosutic    2004/01/18 09:14:04

  Modified:    attributes/compiler project.xml
               attributes/compiler/src/java/org/apache/commons/attributes/compiler
                        AttributeExpressionParser.java
  Added:       attributes/compiler/src/test/org/apache/commons/attributes/compiler/test
                        AttributeExpressionParserTestCase.java
  Log:
  Added test cases for the parser.
  
  Revision  Changes    Path
  1.3       +4 -0      jakarta-commons-sandbox/attributes/compiler/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/attributes/compiler/project.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- project.xml       22 Aug 2003 23:48:03 -0000      1.2
  +++ project.xml       18 Jan 2004 17:14:03 -0000      1.3
  @@ -12,6 +12,10 @@
           An Ant task that will precompile Java source.
       </description>
       
  +    <build>
  +        <unitTestSourceDirectory>${basedir}/src/test</unitTestSourceDirectory>
  +    </build>
  +    
       <dependencies>
           <dependency>
               <groupId>ant</groupId>
  
  
  
  1.3       +44 -15    
jakarta-commons-sandbox/attributes/compiler/src/java/org/apache/commons/attributes/compiler/AttributeExpressionParser.java
  
  Index: AttributeExpressionParser.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/attributes/compiler/src/java/org/apache/commons/attributes/compiler/AttributeExpressionParser.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AttributeExpressionParser.java    15 Jan 2004 21:04:13 -0000      1.2
  +++ AttributeExpressionParser.java    18 Jan 2004 17:14:03 -0000      1.3
  @@ -64,21 +64,55 @@
   /**
    * Parser for attribute expressions.
    */
  -class AttributeExpressionParser {
  +public class AttributeExpressionParser {
       
  -    static class Argument {
  -        public String field;
  -        public String text;
  -        public int length;
  +    public static class Argument {
  +        public final String field;
  +        public final String text;
  +        public final int length;
  +        
  +        public Argument (String field, String text, int length) {
  +            this.field = field;
  +            this.text = text;
  +            this.length = length;
  +        }
  +        
  +        public boolean equalsOrNull (String a, String b) {
  +            if (a == null) {
  +                return b == null;
  +            } else {
  +                return b != null && a.equals (b);
  +            }
  +        }
  +        
  +        public boolean equals (Object o) {
  +            return o instanceof Argument &&
  +                equalsOrNull (field, ((Argument) o).field) &&
  +                ((Argument) o).text.equals (text);
  +        }
           
           public String toString () {
               return "[Argument: " + field + ", " + text + ", " + length + "]";
           }
       }
       
  -    static class ParseResult {
  -        public List arguments;
  -        public String className;
  +    public static class ParseResult {
  +        public final List arguments = new ArrayList ();
  +        public final String className;
  +        
  +        public ParseResult (String className) {
  +            this.className = className;
  +        }
  +        
  +        public boolean equals (Object o) {
  +            return o instanceof ParseResult &&
  +                className.equals (((ParseResult) o).className) &&
  +                arguments.equals (((ParseResult) o).arguments);
  +        }
  +        
  +        public String toString () {
  +            return "[ParseResult: " + className + ", " + arguments + "]";
  +        }
       }
       
       protected static Argument nextArgument (String string, int startPos, String 
filename, int line) {
  @@ -155,10 +189,7 @@
               }
           }
           
  -        Argument arg = new Argument ();
  -        arg.text = text;
  -        arg.field = field;
  -        arg.length = i - startPos;
  +        Argument arg = new Argument (field, text, i - startPos);
           
           return arg;
       }
  @@ -175,9 +206,7 @@
               throw new BuildException (filename + ":" + line + ": Illegal 
expression: " + string);
           }
           
  -        ParseResult result = new ParseResult ();
  -        result.className = sb.toString ();
  -        result.arguments = new ArrayList ();
  +        ParseResult result = new ParseResult (sb.toString ());
           
           i++;
           Argument arg = null;
  
  
  
  1.1                  
jakarta-commons-sandbox/attributes/compiler/src/test/org/apache/commons/attributes/compiler/test/AttributeExpressionParserTestCase.java
  
  Index: AttributeExpressionParserTestCase.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-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 acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 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 Software Foundation.
   *
   * 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.attributes.compiler.test;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.lang.reflect.Constructor;
  import java.io.File;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.Collection;
  import java.util.Iterator;
  import org.apache.commons.attributes.compiler.AttributeExpressionParser;
  import junit.framework.TestCase;
  
  public class AttributeExpressionParserTestCase extends TestCase {
      
      private void singleTest (String expression, String className, 
AttributeExpressionParser.Argument[] args) {
          AttributeExpressionParser.ParseResult result = 
AttributeExpressionParser.parse (expression, "noFile", 1);
          AttributeExpressionParser.ParseResult expected = new 
AttributeExpressionParser.ParseResult (className);
          for (int i = 0; i < args.length; i++) {
              expected.arguments.add (args[i]);
          }
          
          assertEquals (expected, result);
      }
      
      public void testExpressions () throws Exception {
          singleTest ("Inherited()", "Inherited", new 
AttributeExpressionParser.Argument[] {});
          
          singleTest ("AnAttribute(1,2)", "AnAttribute", new 
AttributeExpressionParser.Argument[] {
                  new AttributeExpressionParser.Argument(null, "1", 0),
                  new AttributeExpressionParser.Argument(null, "2", 0)
                  });
          
          singleTest ("AnAttribute(\"sometext,1,2\",'a',',')", "AnAttribute", new 
AttributeExpressionParser.Argument[] {
                  new AttributeExpressionParser.Argument(null, "\"sometext,1,2\"", 0),
                  new AttributeExpressionParser.Argument(null, "'a'", 0),
                  new AttributeExpressionParser.Argument(null, "','", 0)
                  });
          
          singleTest 
("AnAttribute(\"sometext,1,2\",'a',',',alpha='\"',beta=\"\\\'\")", "AnAttribute", new 
AttributeExpressionParser.Argument[] {
                  new AttributeExpressionParser.Argument(null, "\"sometext,1,2\"", 0),
                  new AttributeExpressionParser.Argument(null, "'a'", 0),
                  new AttributeExpressionParser.Argument(null, "','", 0),
                  new AttributeExpressionParser.Argument("alpha", "'\"'", 0),
                  new AttributeExpressionParser.Argument("beta", "\"\\\'\"", 0)
                  });
          
          singleTest ("my.package. 
AnAttribute(\"sometext,1,2\",'a',',',alpha='\"',beta=\"\\\'\")", 
              "my.package. AnAttribute", new AttributeExpressionParser.Argument[] {
                  new AttributeExpressionParser.Argument(null, "\"sometext,1,2\"", 0),
                  new AttributeExpressionParser.Argument(null, "'a'", 0),
                  new AttributeExpressionParser.Argument(null, "','", 0),
                  new AttributeExpressionParser.Argument("alpha", "'\"'", 0),
                  new AttributeExpressionParser.Argument("beta", "\"\\\'\"", 0)
                  });
      }
      
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to