james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r800770 - in /james/jsieve/trunk/main/src: main/java/org/apache/jsieve/ main/java/org/apache/jsieve/commands/ test/java/org/apache/jsieve/comparators/
Date Tue, 04 Aug 2009 13:12:46 GMT
Author: rdonkin
Date: Tue Aug  4 13:12:45 2009
New Revision: 800770

URL: http://svn.apache.org/viewvc?rev=800770&view=rev
Log:
JSIEVE-72 Pass those comparators which are supported and have been explicitly defined https://issues.apache.org/jira/browse/JSIEVE-72

Modified:
    james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManager.java
    james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManagerImpl.java
    james/jsieve/trunk/main/src/main/java/org/apache/jsieve/Constants.java
    james/jsieve/trunk/main/src/main/java/org/apache/jsieve/SieveValidationVisitor.java
    james/jsieve/trunk/main/src/main/java/org/apache/jsieve/commands/Require.java
    james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/AsciiNumericTest.java
    james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/RequireComparatorTest.java

Modified: james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManager.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManager.java?rev=800770&r1=800769&r2=800770&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManager.java (original)
+++ james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManager.java Tue Aug
 4 13:12:45 2009
@@ -30,7 +30,7 @@
  * </p>
  */
 public interface ComparatorManager {
-
+    
     /**
      * <p>Gets a comparator by name.</p>
      * 
@@ -49,4 +49,11 @@
      * false when any usage of this comparator must be declared in a <code>require</code>
statement
      */
     public boolean isImplicitlyDeclared(final String comparatorName);
+    
+    /**
+     * Is the comparator with the given name supported?
+     * @param name not null
+     * @return true when supported, false otherwise
+     */
+    public boolean isSupported(String name);
 }

Modified: james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManagerImpl.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManagerImpl.java?rev=800770&r1=800769&r2=800770&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManagerImpl.java (original)
+++ james/jsieve/trunk/main/src/main/java/org/apache/jsieve/ComparatorManagerImpl.java Tue
Aug  4 13:12:45 2009
@@ -150,4 +150,16 @@
                     + "' not mapped.");
         return className;
     }
+
+    /**
+     * @see ComparatorManager#isSupported(String)
+     */
+    public boolean isSupported(String name) {
+        try {
+            getComparator(name);
+            return true;
+        } catch (LookupException e) {
+            return false;
+        }
+    }
 }

Modified: james/jsieve/trunk/main/src/main/java/org/apache/jsieve/Constants.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/main/java/org/apache/jsieve/Constants.java?rev=800770&r1=800769&r2=800770&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/main/java/org/apache/jsieve/Constants.java (original)
+++ james/jsieve/trunk/main/src/main/java/org/apache/jsieve/Constants.java Tue Aug  4 13:12:45
2009
@@ -26,5 +26,10 @@
     public static final String COMPARATOR_ASCII_CASEMAP_NAME = "i;ascii-casemap";
     /** Name of the standard octet comparator. See <a href='http://tools.ietf.org/html/rfc4790'>RFC4790</a>.
*/
     public static final String COMPARATOR_OCTET_NAME = "i;octet";
-
+    /** Prefix used to identify extension groups. See <a href='http://tools.ietf.org/html/rfc5228#section-6.1'>RFC5228,
6.1. Capability String</a> */
+    public static final char REQUIRE_EXTENSION_PREFIX = '-';
+    /** Prefix used to identify comparator extensions. See <a href='http://tools.ietf.org/html/rfc5228#section-6.1'>RFC5228,
6.1. Capability String</a> */
+    public static final String COMPARATOR_PREFIX = "comparator" + REQUIRE_EXTENSION_PREFIX;
+    /** Number of characters in {@link #COMPARATOR_PREFIX} */
+    public static final int COMPARATOR_PREFIX_LENGTH = COMPARATOR_PREFIX.length();
 }

Modified: james/jsieve/trunk/main/src/main/java/org/apache/jsieve/SieveValidationVisitor.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/main/java/org/apache/jsieve/SieveValidationVisitor.java?rev=800770&r1=800769&r2=800770&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/main/java/org/apache/jsieve/SieveValidationVisitor.java (original)
+++ james/jsieve/trunk/main/src/main/java/org/apache/jsieve/SieveValidationVisitor.java Tue
Aug  4 13:12:45 2009
@@ -1,7 +1,9 @@
 package org.apache.jsieve;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.apache.jsieve.exception.LookupException;
 import org.apache.jsieve.exception.SieveException;
@@ -18,6 +20,8 @@
 import org.apache.jsieve.parser.generated.SieveParserVisitor;
 import org.apache.jsieve.parser.generated.SimpleNode;
 
+import static org.apache.jsieve.Constants.*;
+
 /****************************************************************
  * Licensed to the Apache Software Foundation (ASF) under one   *
  * or more contributor license agreements.  See the NOTICE file *
@@ -47,6 +51,8 @@
     private final TestManager testManager;
     private final ComparatorManager comparatorManager;
 
+    private final Set<String> declaredComparators;
+    
     private boolean requireAllowed = true;
     /** Is the visitor within a <code>require</code>? */
     private boolean isInRequire = false;
@@ -61,6 +67,7 @@
         this.commandManager = commandManager;
         this.testManager = testManager;
         this.comparatorManager = comparatorManager;
+        declaredComparators = new HashSet<String>();
     }
 
     public Object visit(SimpleNode node, Object data) throws SieveException {
@@ -79,6 +86,7 @@
     }
 
     public Object visit(ASTcommands node, Object data) throws SieveException {
+        declaredComparators.clear();
         return visitNode(node, data);
     }
 
@@ -152,22 +160,35 @@
         final Object value = node.getValue();
         if (value != null && value instanceof String) {
             final String name = (String) value;
+            // Comparators must either be declared (either implicitly or explicitly)
             if (!comparatorManager.isImplicitlyDeclared(name)) {
-                // TODO: replace with better exception
-                throw new SieveException("Comparator must be explicitly declared in a require
statement.");
+                if (!declaredComparators.contains(name)) {
+                    // TODO: replace with better exception
+                    throw new SieveException("Comparator must be explicitly declared in a
require statement.");
+                }
             }
         }
     }
 
-    private void requirements(ASTstring node) throws LookupException {
+    private void requirements(ASTstring node) throws SieveException {
         final Object value = node.getValue();
         if (value != null && value instanceof String) {
             final String name = (String) value;
-            try {
-                commandManager.getCommand(name);
-            } catch (LookupException e) {
-                // TODO: catching is inefficient, should just check
-                testManager.getTest(name);
+            if (name.startsWith(COMPARATOR_PREFIX)) {
+                final String comparatorName = name.substring(COMPARATOR_PREFIX_LENGTH);
+                if (comparatorManager.isSupported(comparatorName)) {
+                    declaredComparators.add(comparatorName);
+                } else {
+//                  TODO: Replace with more finely grained exception
+                    throw new SieveException("Comparator " + comparatorName + " is not supported");
+                }
+            } else {
+                try {
+                    commandManager.getCommand(name);
+                } catch (LookupException e) {
+                    // TODO: catching is inefficient, should just check
+                    testManager.getTest(name);
+                }
             }
         }
     }

Modified: james/jsieve/trunk/main/src/main/java/org/apache/jsieve/commands/Require.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/main/java/org/apache/jsieve/commands/Require.java?rev=800770&r1=800769&r2=800770&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/main/java/org/apache/jsieve/commands/Require.java (original)
+++ james/jsieve/trunk/main/src/main/java/org/apache/jsieve/commands/Require.java Tue Aug
 4 13:12:45 2009
@@ -19,6 +19,9 @@
 
 package org.apache.jsieve.commands;
 
+import static org.apache.jsieve.Constants.COMPARATOR_PREFIX;
+import static org.apache.jsieve.Constants.COMPARATOR_PREFIX_LENGTH;
+
 import java.util.List;
 
 import org.apache.jsieve.Argument;
@@ -76,20 +79,28 @@
      */
     protected void validateFeature(String name, MailAdapter mail,
             SieveContext context) throws FeatureException {
-        // Validate as a Command
-        try {
-            validateCommand(name, context);
-            return;
-        } catch (LookupException e) {
-            // Not a command
-        }
-
-        // Validate as a Test
-        try {
-            validateTest(name, context);
-        } catch (LookupException e) {
-            throw new FeatureException("Feature \"" + name
-                    + "\" is not supported.");
+        if (name.startsWith(COMPARATOR_PREFIX)) {
+            final String comparatorName = name.substring(COMPARATOR_PREFIX_LENGTH);
+            if (!context.getComparatorManager().isSupported(comparatorName)) {
+                throw new FeatureException("Comparator \"" + comparatorName
+                        + "\" is not supported.");
+            }
+        } else {
+            // Validate as a Command
+            try {
+                validateCommand(name, context);
+                return;
+            } catch (LookupException e) {
+                // Not a command
+            }
+    
+            // Validate as a Test
+            try {
+                validateTest(name, context);
+            } catch (LookupException e) {
+                throw new FeatureException("Feature \"" + name
+                        + "\" is not supported.");
+            }
         }
     }
 

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=800770&r1=800769&r2=800770&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 13:12:45 2009
@@ -42,4 +42,9 @@
             // Expected
         }
     }
+    
+    public void testVerificationPassesWhenAsciiNumericIsRequired() throws Exception {
+        String script = "require [\"comparator-i;ascii-numeric\"]; if header :contains :comparator
\"i;ascii-numeric\" \"Subject\" \"69\" {stop;}";
+        JUnitUtils.interpret(JUnitUtils.createMail(), script);
+    }
 }

Modified: james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/RequireComparatorTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/RequireComparatorTest.java?rev=800770&r1=800769&r2=800770&view=diff
==============================================================================
--- james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/RequireComparatorTest.java
(original)
+++ james/jsieve/trunk/main/src/test/java/org/apache/jsieve/comparators/RequireComparatorTest.java
Tue Aug  4 13:12:45 2009
@@ -20,6 +20,7 @@
 
 import junit.framework.TestCase;
 
+import org.apache.jsieve.parser.generated.ParseException;
 import org.apache.jsieve.utils.JUnitUtils;
 
 public class RequireComparatorTest extends TestCase {
@@ -32,13 +33,24 @@
         super.tearDown();
     }
     
-    public void testAsciiCasemapShouldBeImplicitlyDelcared() throws Exception {
+    public void testAsciiCasemapShouldBeImplicitlyDeclared() throws Exception {
         String script = "if header :contains :comparator \"i;ascii-casemap\" \"Subject\"
\"69\" {stop;}";
         JUnitUtils.interpret(JUnitUtils.createMail(), script);
     }
     
-    public void testOctetShouldBeImplicitlyDelcared() throws Exception {
+    public void testOctetShouldBeImplicitlyDeclared() throws Exception {
         String script = "if header :contains :comparator \"i;octet\" \"Subject\" \"69\" {stop;}";
         JUnitUtils.interpret(JUnitUtils.createMail(), script);
     }
+    
+    public void testBogusComparatorShouldFailAtParseTime() throws Exception {
+        String script = "if header :contains :comparator \"i;bogus\" \"Subject\" \"69\" {stop;}";
+        try {
+            JUnitUtils.interpret(JUnitUtils.createMail(), script);
+            fail("Bogus comparator should fail");
+        } catch (ParseException e) {
+            // TODO: catch more finely grained exception
+            // Expected
+        }
+    }
 }



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


Mime
View raw message