You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rd...@apache.org on 2009/08/04 16:54:14 UTC

svn commit: r800828 - in /james/jsieve/trunk/main/src: main/java/org/apache/jsieve/comparators/ test/java/org/apache/jsieve/comparators/

Author: rdonkin
Date: Tue Aug  4 14:54:14 2009
New Revision: 800828

URL: http://svn.apache.org/viewvc?rev=800828&view=rev
Log:
JSIEVE-69 Create tests based on specification and then fix bugs in implementation https://issues.apache.org/jira/browse/JSIEVE-69

Modified:
    james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/AsciiNumeric.java
    james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/ComparatorUtils.java
    james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/Contains.java
    james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/AsciiNumericTest.java

Modified: james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/AsciiNumeric.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/AsciiNumeric.java?rev=800828&r1=800827&r2=800828&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/AsciiNumeric.java (original)
+++ james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/AsciiNumeric.java Tue Aug  4 14:54:14 2009
@@ -19,7 +19,9 @@
 
 package org.apache.jsieve.comparators;
 
-import org.apache.jsieve.exception.SievePatternException;
+import java.math.BigInteger;
+
+import org.apache.jsieve.exception.FeatureException;
 
 /**
  * Class AsciiNumeric implements the EQUALITY operation of the i;ascii-numeric
@@ -38,8 +40,58 @@
      * @see org.apache.jsieve.comparators.Equals#equals(String, String)
      */
     public boolean equals(String string1, String string2) {
-        return ComparatorUtils.equals(computeCompareString(string1),
-                computeCompareString(string2));
+        final boolean result;
+        if (isPositiveInfinity(string1)) {
+            if (isPositiveInfinity(string2)) {
+                result = true;
+            } else {
+                result = false;
+            }
+        } else {
+            if (isPositiveInfinity(string2)) {
+                result = false;
+            } else {
+                final BigInteger integer1 = toInteger(string1);
+                final BigInteger integer2 = toInteger(string2);
+                result = integer1.equals(integer2);
+            }
+        }
+        return result;
+    }
+    
+    private BigInteger toInteger(final String value) {
+        int i;
+        for (i=0;i<value.length();i++) {
+            final char next = value.charAt(i);
+            if (!isDigit(next)) {
+                break;
+            }
+        }
+        final BigInteger result = new BigInteger(value.substring(0,i));
+        return result;
+    }
+    
+    /**
+     * Does the given string to be handled as positive infinity?
+     * See <a href='http://tools.ietf.org/html/rfc4790#section-9.1.1'>RFC4790</a>
+     * @param value not null
+     * @return true when the value should represent positive infinity,
+     * false otherwise
+     */
+    private boolean isPositiveInfinity(final String value) {
+       final char initialCharacter = value.charAt(0);
+       final boolean result = !isDigit(initialCharacter);
+       return result;
+    }
+
+    /**
+     * Is the given character an ASCII digit?
+     * @param character character to be tested
+     * @return true when the given character is an ASCII digit,
+     * false otherwise 
+     */
+    private boolean isDigit(final char character) {
+        return character>=0x30 && character<=0x39;
     }
 
     /**
@@ -59,26 +111,26 @@
     }
 
     /**
+     * Unsupported, see <a href='http://tools.ietf.org/html/rfc4790#section-9.1.1'>RFC4790</a>.
      * @see org.apache.jsieve.comparators.Contains#contains(String, String)
      */
-    public boolean contains(String container, String content) {
-        return ComparatorUtils.contains(computeCompareString(container),
-                computeCompareString(content));
+    public boolean contains(String container, String content) throws FeatureException {
+        // TODO: Consider using finer grained exception
+        throw new FeatureException("Substring match unsupported by ascii-numeric");
     }
 
     /**
+     * Unsupported operation.
+     * <a href='http://tools.ietf.org/html/rfc5228#section-2.7.1'>RFC5228</a> limits
+     * support to comparators that support <code>:contains</code>. 
+     * <a href='http://tools.ietf.org/html/rfc4790#section-9.1.1'>RFC4790</a> states
+     * that substring matches are not supported.
      * @see org.apache.jsieve.comparators.Matches#matches(String, String)
      */
     public boolean matches(String string, String glob)
-            throws SievePatternException {
-        // TODO: Review comments and either correct or fix
-        // return computeCompareString(string).matches(regex);
-        
-        // Still to fix: computeCompareString(glob) will remove glob characters!
-        // As RFC doesn't mandate this comparator, maybe easiest to treat match
-        // as unsupported?
-        return ComparatorUtils.matches(computeCompareString(string),
-                computeCompareString(glob));
+            throws FeatureException {
+        // TODO: Consider using finer grained exception
+        throw new FeatureException("Substring match unsupported by ascii-numeric");
     }
 
 }

Modified: james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/ComparatorUtils.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/ComparatorUtils.java?rev=800828&r1=800827&r2=800828&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/ComparatorUtils.java (original)
+++ james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/ComparatorUtils.java Tue Aug  4 14:54:14 2009
@@ -192,7 +192,7 @@
      * @return boolean
      */
     public static boolean contains(String comparatorName, String container,
-            String contents, SieveContext context) throws LookupException {
+            String contents, SieveContext context) throws SieveException {
         Contains comparatorObj = context.getComparatorManager().getComparator(comparatorName);
         return comparatorObj.contains(container, contents);
     }

Modified: james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/Contains.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/Contains.java?rev=800828&r1=800827&r2=800828&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/Contains.java (original)
+++ james/jsieve/trunk/main/src/main/java/org/apache/jsieve/comparators/Contains.java Tue Aug  4 14:54:14 2009
@@ -19,6 +19,9 @@
 
 package org.apache.jsieve.comparators;
 
+import org.apache.jsieve.exception.FeatureException;
+
+
 /**
  * Interface Contains defines the method signatures for contains comparators.
  */
@@ -32,7 +35,8 @@
      * @param container
      * @param content
      * @return boolean
+     * @throws FeatureException when substring is unsupported
      */
-    public boolean contains(String container, String content);
+    public boolean contains(String container, String content) throws FeatureException;
 
 }

Modified: james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/AsciiNumericTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/AsciiNumericTest.java?rev=800828&r1=800827&r2=800828&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/AsciiNumericTest.java (original)
+++ james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/AsciiNumericTest.java Tue Aug  4 14:54:14 2009
@@ -20,13 +20,17 @@
 
 import junit.framework.TestCase;
 
+import org.apache.jsieve.exception.FeatureException;
 import org.apache.jsieve.parser.generated.ParseException;
 import org.apache.jsieve.utils.JUnitUtils;
 
 public class AsciiNumericTest extends TestCase {
+    
+    AsciiNumeric subject;
 
     protected void setUp() throws Exception {
         super.setUp();
+        subject = new AsciiNumeric();
     }
 
     protected void tearDown() throws Exception {
@@ -44,7 +48,48 @@
     }
     
     public void testVerificationPassesWhenAsciiNumericIsRequired() throws Exception {
-        String script = "require [\"comparator-i;ascii-numeric\"]; if header :contains :comparator \"i;ascii-numeric\" \"Subject\" \"69\" {stop;}";
+        String script = "require [\"comparator-i;ascii-numeric\"]; if header :is :comparator \"i;ascii-numeric\" \"Subject\" \"69\" {stop;}";
         JUnitUtils.interpret(JUnitUtils.createMail(), script);
     }
+    
+    public void testBasicNumbericEquality() throws Exception {
+        assertFalse(subject.equals("1", "2"));
+        assertTrue(subject.equals("1", "1"));
+    }
+    
+    public void testEqualityShouldIgnoreTrailingCharacters() throws Exception {
+        assertTrue(subject.equals("01", "1A"));
+        assertTrue(subject.equals("1", "00000000000000001A"));
+        assertTrue(subject.equals("234S", "234YTGSDBBSD"));
+    }
+    
+    public void testEqualityShouldIgnoreLeadingZeros() throws Exception {
+        assertTrue(subject.equals("01", "1"));
+        assertTrue(subject.equals("000001", "1"));
+        assertFalse(subject.equals("000001", "10"));
+    }
+    
+    public void testStingsThatDoNotStartWithADigitRepresentPositiveInfinityWhenUsedInEquality() throws Exception {
+        assertFalse(subject.equals("1", "A4"));
+        assertFalse(subject.equals("x", "4"));
+        assertTrue(subject.equals("GT", "A4"));
+    }
+    
+    public void testSubstringIsNotSupported() throws Exception {
+        try {
+            subject.contains("234234", "34");
+            fail("Substring is unsupported");
+        } catch (FeatureException e) {
+            // Expected
+        }
+    }
+    
+    public void testMatchNotSupported() throws Exception {
+        try {
+            subject.matches("234234", "34");
+            fail("Substring is unsupported");
+        } catch (FeatureException e) {
+            // Expected
+        }
+    }
 }



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