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 2007/03/26 22:33:09 UTC

svn commit: r522620 - in /james/jsieve/trunk/src: main/java/org/apache/jsieve/util/ main/java/org/apache/jsieve/util/check/ test/java/org/apache/jsieve/javaxmail/ test/java/org/apache/jsieve/util/ test/java/org/apache/jsieve/util/check/

Author: rdonkin
Date: Mon Mar 26 13:33:08 2007
New Revision: 522620

URL: http://svn.apache.org/viewvc?view=rev&rev=522620
Log:
Utility that helps test sieve scripts before upload.

Added:
    james/jsieve/trunk/src/main/java/org/apache/jsieve/util/
    james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/
    james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
    james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptChecker.java
    james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/package.html
    james/jsieve/trunk/src/test/java/org/apache/jsieve/javaxmail/
    james/jsieve/trunk/src/test/java/org/apache/jsieve/javaxmail/MockMimeMessage.java
      - copied, changed from r521990, james/server/trunk/phoenix-deployment/src/test/org/apache/james/test/mock/javaxmail/MockMimeMessage.java
    james/jsieve/trunk/src/test/java/org/apache/jsieve/util/
    james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/
    james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/MockAction.java
    james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterActionsTest.java
    james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterHeadersTest.java
    james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterMailTest.java
    james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterNoMessageSetTest.java

Added: james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java?view=auto&rev=522620
==============================================================================
--- james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java (added)
+++ james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java Mon Mar 26 13:33:08 2007
@@ -0,0 +1,230 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.jsieve.util.check;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.ListIterator;
+
+import javax.mail.Header;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+
+import org.apache.jsieve.SieveException;
+import org.apache.jsieve.mail.Action;
+import org.apache.jsieve.mail.MailAdapter;
+import org.apache.jsieve.mail.SieveMailException;
+
+/**
+ * Checks script execution for an email.
+ * The wrapped email is set by called {@link #setMail}.
+ * Actions are recorded on {@link #executedActions}
+ * and can be retrieved by {@link #getExecutedActions().
+ */
+public class ScriptCheckMailAdapter implements MailAdapter {
+
+    private final List actions;
+    private final List executedActions;
+    
+    private Message mail = null;
+    
+    public ScriptCheckMailAdapter() {
+        actions = new ArrayList();
+        executedActions = new ArrayList();
+    }
+    
+    /**
+     * Gets the wrapped email.
+     * @return <code>Message</code>, possibly null
+     */
+    public Message getMail() {
+        return mail;
+    }
+
+    /**
+     * Sets the wrapped email and {@link #reset}s the adapter
+     * ready for another execution.
+     * @param mail <code>Message</code>, possibly null
+     */
+    public void setMail(Message mail) {
+        this.mail = mail;
+        reset();
+    }
+
+
+
+    /**
+     * Method addAction adds an Action to the List of Actions to be performed by the
+     * receiver.
+     * @param action
+     */
+    public void addAction(final Action action) {
+        actions.add(action);
+    }
+
+    /**
+     * Method executeActions. Applies the Actions accumulated by the receiver.
+     */
+    public void executeActions() throws SieveException {
+        executedActions.clear();
+        executedActions.addAll(actions);
+    }
+
+    /**
+     * Gets the actions accumulated when {@link #executedActions}
+     * was last called.
+     * @return <code>List</code> of {@link Action}s, not null.
+     * This list is a modifiable copy
+     */
+    public List getExecutedActions() {
+        final ArrayList result = new ArrayList(executedActions);
+        return result;
+    }
+
+    /**
+     * Method getActions answers the List of Actions accumulated by the receiver.
+     * Implementations may elect to supply an unmodifiable collection.
+     * @return <code>List</code> of {@link Action}'s, not null, possibly unmodifiable
+     */
+    public List getActions() {
+        final List result = Collections.unmodifiableList(actions);
+        return result;
+    }
+
+    /**
+     * Method getActionIteraror answers an Iterator over the List of Actions
+     * accumulated by the receiver. Implementations may elect to supply
+     * an unmodifiable iterator.
+     * @return <code>ListIterator</code>, not null, possibly unmodifiable
+     */
+    public ListIterator getActionsIterator() {
+        final List unmodifiableActions = getActions();
+        final ListIterator result = unmodifiableActions.listIterator();
+        return result;
+    }
+    
+    /**
+     * Resets executed and accumlated actions.
+     * An instance may be safely reused to check a script
+     * once this method has been called.
+     */
+    public void reset() {
+        executedActions.clear();
+        actions.clear();
+    }
+    
+    /**
+     * Method getHeader answers a List of all of the headers in the receiver whose 
+     * name is equal to the passed name. If no headers are found an empty List is 
+     * returned.
+     * 
+     * @param name
+     * @return <code>List</code> not null, possibly empty
+     * @throws SieveMailException
+     */
+    public List getHeader(String name) throws SieveMailException {
+        List result = Collections.EMPTY_LIST;
+        if (mail != null) {
+            try {
+                String[] values = mail.getHeader(name);
+                if (values != null)
+                {
+                    result = Arrays.asList(values);
+                }
+            } catch (MessagingException e) {
+                throw new SieveMailException(e);
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Method getHeaderNames answers a List of all of the headers in the receiver.
+     * No duplicates are allowed.
+     * @return <code>List</code>, not null possible empty, possible unmodifiable
+     * @throws SieveMailException
+     */
+    public List getHeaderNames() throws SieveMailException {
+        List results = Collections.EMPTY_LIST;
+        if (mail != null)
+        {
+            try {
+                results = new ArrayList();
+                for (final Enumeration en=mail.getAllHeaders();en.hasMoreElements();) {
+                    final Header header = (Header) en.nextElement();
+                    final String name = header.getName();
+                    if (!results.contains(name)) {
+                        results.add(name);
+                    }
+                }
+            } catch (MessagingException e) {
+                throw new SieveMailException(e);
+            }
+        }
+        return results;
+    }
+
+    /**
+     * <p>Method getMatchingHeader answers a List of all of the headers in the 
+     * receiver with the passed name. If no headers are found an empty List is 
+     * returned.
+     * </p>
+     * 
+     * <p>This method differs from getHeader(String) in that it ignores case and the 
+     * whitespace prefixes and suffixes of a header name when performing the
+     * match, as required by RFC 3028. Thus "From", "from ", " From" and " from "
+     * are considered equal.
+     * </p>
+     * 
+     * @param name
+     * @return <code>List</code>, not null possibly empty
+     * @throws SieveMailException
+     */
+    public List getMatchingHeader(String name) throws SieveMailException {
+        List result = Collections.EMPTY_LIST;
+        if (mail != null)
+        {
+            // TODO: implementation
+        }
+        return result;
+    }
+
+    /**
+     * Method getSize answers the receiver's message size in octets.
+     * @return int
+     * @throws SieveMailException
+     */
+    public int getSize() throws SieveMailException {
+        int result = 0;
+        if (mail != null)
+        {
+            try {
+                result = mail.getSize();
+            } catch (MessagingException e) {
+                throw new SieveMailException(e);
+            }
+        }
+        return result;
+    }
+
+}

Added: james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptChecker.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptChecker.java?view=auto&rev=522620
==============================================================================
--- james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptChecker.java (added)
+++ james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptChecker.java Mon Mar 26 13:33:08 2007
@@ -0,0 +1,159 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.jsieve.util.check;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.jsieve.SieveException;
+import org.apache.jsieve.SieveFactory;
+import org.apache.jsieve.parser.generated.ParseException;
+
+/**
+ * Checks a <code>sieve</code> script by executing it against a given mail
+ * and reporting the results. 
+ */
+public class ScriptChecker {
+
+    private final ScriptCheckMailAdapter adapter;
+    
+    public ScriptChecker() {
+        adapter = new ScriptCheckMailAdapter();
+    }
+    
+    /**
+     * Checks the <code>sieve</code> script contained in the given file
+     * by executing it against the given message.
+     * @param message <code>File</code> containing the mail message to be fed to the script,
+     * not null
+     * @param script <code>File</code> containing the script to be checked
+     * @return <code>Results</code> of that execution
+     * @throws IOException
+     * @throws MessageException
+     */
+    public Results check(final File message, final File script) throws IOException, MessagingException {
+        final FileInputStream messageStream = new FileInputStream(message);
+        final FileInputStream scriptStream = new FileInputStream(script);
+        final Results results = check(messageStream, scriptStream);
+        return results;
+    }
+    
+    /**
+     * Checks the <code>sieve</code> script contained in the given file
+     * by executing it against the given message.
+     * @param in <code>InputStream</code>, not null
+     * @return <code>Results</code> of the check, not null
+     * @throws IOException
+     * @throws MessagingException 
+     */
+    public Results check(final InputStream message, final InputStream script) throws IOException, MessagingException {
+        MimeMessage mimeMessage = new MimeMessage(null, message);
+        adapter.setMail(mimeMessage);
+        Results results;
+        try {
+            SieveFactory.getInstance().interpret(adapter, script);
+            final List executedActions = adapter.getExecutedActions();
+            results = new Results(executedActions);
+        } catch (ParseException e) {
+            e.printStackTrace();
+            results = new Results(e);
+        } catch (SieveException e) {
+            e.printStackTrace();
+            results = new Results(e);
+        }
+        return results;
+    }
+    
+    /**
+     * Contains results of script execution.
+     */
+    public final static class Results {
+        private final boolean pass;
+        private final Exception exception;
+        private final List actionsExecuted;
+        
+        private Results(final Exception ex) {
+            this.exception = ex;
+            pass = false;
+            actionsExecuted = null;
+        }
+        
+        private Results(final List actions) {
+            this.pass = true;
+            exception = null;
+            this.actionsExecuted = actions;
+        }
+        
+        /**
+         * Is this a pass?
+         * @return true if the script executed without error,
+         * false if errors were encountered
+         */
+        public boolean isPass() {
+            return pass;
+        }
+
+        /**
+         * Gets the exception which was thrown during execution.
+         * @return <code>Exception</code> or null if no exception
+         * was thrown
+         */
+        public Exception getException() {
+            return exception;
+        }
+        
+        
+        /**
+         * Gets the actions executed by the script.
+         * @return <code>List</code> of actions
+         * or null if the script failed
+         */
+        public List getActionsExecuted() {
+            return actionsExecuted;
+        }
+
+        /**
+         * Prints out details of results.
+         */
+        public String toString() {
+            StringBuffer buffer = new StringBuffer("Results: ");
+            if (pass) {
+                buffer.append("PASS");
+            } else { 
+                buffer.append("FAIL: ");
+                if (exception != null){
+                    if (exception instanceof ParseException) {
+                        buffer.append("Cannot parse script");
+                    } else {
+                        buffer.append("Cannot excute script");
+                    }
+                    buffer.append(exception.getMessage());
+                }
+            }
+            return buffer.toString();
+        }
+    }
+}

Added: james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/package.html
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/package.html?view=auto&rev=522620
==============================================================================
--- james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/package.html (added)
+++ james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/package.html Mon Mar 26 13:33:08 2007
@@ -0,0 +1,34 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<HTML>
+<HEAD>
+<!--
+
+  @(#)package.html
+
+  Licensed to the Apache Software Foundation (ASF) under one   
+  or more contributor license agreements.  See the NOTICE file 
+  distributed with this work for additional information        
+  regarding copyright ownership.  The ASF licenses this file   
+  to you under the Apache License, Version 2.0 (the            
+  "License"); you may not use this file except in compliance   
+  with the License.  You may obtain a copy of the License at   
+                                                               
+    http://www.apache.org/licenses/LICENSE-2.0                 
+                                                               
+  Unless required by applicable law or agreed to in writing,   
+  software distributed under the License is distributed on an  
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       
+  KIND, either express or implied.  See the License for the    
+  specific language governing permissions and limitations      
+  under the License.       
+-->
+
+</HEAD>
+<BODY>
+
+<p>This package contains the <code>ScriptCheckMailAdapter</code> 
+MailAdapter implementation. This adapter wraps a standard JavaMail
+message and records the actions to be executed.
+</p>
+</BODY>
+</HTML>

Copied: james/jsieve/trunk/src/test/java/org/apache/jsieve/javaxmail/MockMimeMessage.java (from r521990, james/server/trunk/phoenix-deployment/src/test/org/apache/james/test/mock/javaxmail/MockMimeMessage.java)
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/src/test/java/org/apache/jsieve/javaxmail/MockMimeMessage.java?view=diff&rev=522620&p1=james/server/trunk/phoenix-deployment/src/test/org/apache/james/test/mock/javaxmail/MockMimeMessage.java&r1=521990&p2=james/jsieve/trunk/src/test/java/org/apache/jsieve/javaxmail/MockMimeMessage.java&r2=522620
==============================================================================
--- james/server/trunk/phoenix-deployment/src/test/org/apache/james/test/mock/javaxmail/MockMimeMessage.java (original)
+++ james/jsieve/trunk/src/test/java/org/apache/jsieve/javaxmail/MockMimeMessage.java Mon Mar 26 13:33:08 2007
@@ -19,7 +19,7 @@
 
 
 
-package org.apache.james.test.mock.javaxmail;
+package org.apache.jsieve.javaxmail;
 
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.InternetHeaders;
@@ -34,6 +34,9 @@
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 
+/**
+ * Forked from JAMES server.
+ */
 public class MockMimeMessage extends MimeMessage {
 
     private final List m_fromAddresses = new ArrayList();
@@ -53,6 +56,7 @@
     private HashMap m_contentHeaders = new HashMap();
     private Flags m_setFlags = new Flags();
     private boolean m_doMatch;
+    private int m_size = -1;
 
     public MockMimeMessage() throws MessagingException {
         super((Session)null);
@@ -176,7 +180,7 @@
     }
 
     public int getSize() throws MessagingException {
-        return -1; // trivial implementation
+        return m_size ; // trivial implementation
     }
 
     public int getLineCount() throws MessagingException {
@@ -315,9 +319,15 @@
     }
 
     public String[] getHeader(String name) throws MessagingException {
-        String value = (String) m_contentHeaders.get(name);
+        Object value = m_contentHeaders.get(name);
         if (value == null) return null;
-        return new String[] {value};
+        if (value instanceof String) {
+            String stringValue = (String) value;
+            return new String[] {stringValue};
+        } else {
+            Collection values = (Collection) value;
+            return (String[]) values.toArray(new String[values.size()]);
+        }
     }
 
     public String getHeader(String name, String delimiter) throws MessagingException {
@@ -331,7 +341,22 @@
     }
 
     public void addHeader(String name, String value) throws MessagingException {
-        m_contentHeaders.put(name, value);
+        Object newValue;
+        Object existingValue = m_contentHeaders.get(name);
+        if (existingValue == null) {
+            newValue = value;
+        }
+        else if (existingValue instanceof String) {
+            List values = new ArrayList();
+            values.add(existingValue);
+            values.add(value);
+            newValue = values;
+        } else {
+            List values = (List) existingValue;
+            values.add(value);
+            newValue = values;
+        }
+        m_contentHeaders.put(name, newValue);
     }
 
     public void removeHeader(String name) throws MessagingException {
@@ -339,7 +364,25 @@
     }
 
     public Enumeration getAllHeaders() throws MessagingException {
-        return Collections.enumeration(m_contentHeaders.values());
+        final Collection results = new ArrayList();
+        final Collection entries = m_contentHeaders.entrySet();
+        for (Iterator it = entries.iterator(); it.hasNext();) {
+            Map.Entry entry = (Map.Entry) it.next();
+            String name = entry.getKey().toString();
+            Object value = entry.getValue();
+            if (value == null) {
+                // ignore
+            } else if (value instanceof Collection) {
+                Collection values = (Collection) value;
+                for (Iterator iterValues = values.iterator();iterValues.hasNext();) {
+                    String stringValue = (String) iterValues.next();
+                    results.add(new Header(name, stringValue));
+                }
+            } else {
+                results.add(new Header(name, value.toString()));
+            }
+        }
+        return Collections.enumeration(results);
     }
 
     public Enumeration getMatchingHeaders(String[] names) throws MessagingException {
@@ -482,5 +525,9 @@
     
     public boolean match(SearchTerm searchTerm) throws MessagingException {
         return m_doMatch; 
+    }
+    
+    public void setSize(int size) {
+        this.m_size = size;
     }
 }

Added: james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/MockAction.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/MockAction.java?view=auto&rev=522620
==============================================================================
--- james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/MockAction.java (added)
+++ james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/MockAction.java Mon Mar 26 13:33:08 2007
@@ -0,0 +1,26 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.jsieve.util.check;
+
+import org.apache.jsieve.mail.Action;
+
+public class MockAction implements Action {
+
+}

Added: james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterActionsTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterActionsTest.java?view=auto&rev=522620
==============================================================================
--- james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterActionsTest.java (added)
+++ james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterActionsTest.java Mon Mar 26 13:33:08 2007
@@ -0,0 +1,146 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.jsieve.util.check;
+
+import java.util.ListIterator;
+
+import junit.framework.TestCase;
+
+import org.apache.jsieve.mail.Action;
+
+public class ScriptCheckMailAdapterActionsTest extends TestCase {
+
+    ScriptCheckMailAdapter adapter;
+    Action action;
+    Action anotherAction;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        adapter = new ScriptCheckMailAdapter();
+        action = new MockAction();
+        anotherAction = new MockAction();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testAddAction() {
+        adapter.addAction(action);
+        assertEquals("Running total updated", 1, adapter.getActions().size());
+        assertEquals("Running total updated", action, adapter.getActions().get(0));
+        adapter.addAction(anotherAction);
+        assertEquals("Order preserved", 2, adapter.getActions().size());
+        assertEquals("Order preserved", anotherAction, adapter.getActions().get(1));
+    }
+
+    public void testExecuteActions() throws Exception {
+        assertNotNull(adapter.getExecutedActions());
+        assertEquals("No actions executed", 0, adapter.getExecutedActions().size());
+        adapter.addAction(action);
+        assertNotNull(adapter.getExecutedActions());
+        assertEquals("No actions executed", 0, adapter.getExecutedActions().size());
+        adapter.executeActions();
+        assertNotNull(adapter.getExecutedActions());
+        assertEquals("One action executed", 1, adapter.getExecutedActions().size());
+    }
+
+    public void testGetActions() {
+        assertNotNull(adapter.getActions());
+        try {
+            adapter.getActions().add("A Little Extra");
+            fail("Should not be able to modify collection");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+        adapter.addAction(action);
+        assertNotNull(adapter.getActions());
+        assertEquals("Running total updated", 1, adapter.getActions().size());
+        assertEquals("Running total updated", action, adapter.getActions().get(0));
+        adapter.addAction(anotherAction);
+        assertNotNull(adapter.getActions());
+        assertEquals("Order preserved", 2, adapter.getActions().size());
+        assertEquals("Order preserved", anotherAction, adapter.getActions().get(1));
+    }
+
+    public void testGetActionsIterator() {
+        ListIterator iterator = adapter.getActionsIterator();
+        assertNotNull(iterator);
+        assertFalse("No actions", iterator.hasNext());
+        adapter.addAction(action);
+        iterator = adapter.getActionsIterator();
+        assertNotNull(iterator);
+        assertTrue("One action", iterator.hasNext());
+        assertEquals("One action", action, iterator.next());
+        assertFalse("One action", iterator.hasNext());
+        adapter.addAction(anotherAction);
+        iterator = adapter.getActionsIterator();
+        assertNotNull(iterator);
+        assertTrue("Two actions", iterator.hasNext());
+        assertEquals("Two actions", action, iterator.next());
+        assertTrue("Two actions", iterator.hasNext());
+        assertEquals("Two actions", anotherAction, iterator.next());
+        assertTrue("Two actions", iterator.hasPrevious());
+        assertFalse("Two actions", iterator.hasNext());
+        try {
+            iterator.remove();
+            fail("Should not be able to modify collection");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+    }
+
+    public void testGetExecutedActions() throws Exception {
+        assertNotNull(adapter.getExecutedActions());
+        assertEquals("No actions executed", 0, adapter.getExecutedActions().size());
+        adapter.addAction(action);
+        assertNotNull(adapter.getExecutedActions());
+        assertEquals("No actions executed", 0, adapter.getExecutedActions().size());
+        adapter.executeActions();
+        assertEquals("One action executed", 1, adapter.getExecutedActions().size());
+        assertEquals("One action executed", action, adapter.getExecutedActions().get(0));
+        adapter.addAction(anotherAction);
+        assertEquals("One action executed", 1, adapter.getExecutedActions().size());
+        assertEquals("One action executed", action, adapter.getExecutedActions().get(0));
+        adapter.executeActions();
+        assertEquals("Two actions executed", 2, adapter.getExecutedActions().size());
+        assertEquals("Two actions executed", action, adapter.getExecutedActions().get(0));
+        assertEquals("Two actions executed", anotherAction, adapter.getExecutedActions().get(1));
+        adapter.getExecutedActions().add("Whatever");
+        assertEquals("Two actions executed", 2, adapter.getExecutedActions().size());
+        assertEquals("Two actions executed", action, adapter.getExecutedActions().get(0));
+        assertEquals("Two actions executed", anotherAction, adapter.getExecutedActions().get(1));
+        adapter.executeActions();
+        assertEquals("Two actions executed", 2, adapter.getExecutedActions().size());
+        assertEquals("Two actions executed", action, adapter.getExecutedActions().get(0));
+        assertEquals("Two actions executed", anotherAction, adapter.getExecutedActions().get(1));
+    }
+    
+    public void testReset() throws Exception {
+        adapter.addAction(action);
+        adapter.addAction(anotherAction);
+        adapter.executeActions();
+        assertEquals("Two actions executed", 2, adapter.getExecutedActions().size());
+        assertEquals("Two actions", 2, adapter.getActions().size());
+        adapter.reset();
+        assertEquals("Two actions executed", 0, adapter.getExecutedActions().size());
+        assertEquals("Two actions", 0, adapter.getActions().size());
+    }
+}

Added: james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterHeadersTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterHeadersTest.java?view=auto&rev=522620
==============================================================================
--- james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterHeadersTest.java (added)
+++ james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterHeadersTest.java Mon Mar 26 13:33:08 2007
@@ -0,0 +1,59 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.jsieve.util.check;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class ScriptCheckMailAdapterHeadersTest extends TestCase {
+
+    ScriptCheckMailAdapter adapter;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        adapter = new ScriptCheckMailAdapter();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testGetHeader() throws Exception {
+        List headers = adapter.getHeader("From");
+        assertNotNull(headers);
+    }
+
+    public void testGetHeaderNames() throws Exception {
+        List headers = adapter.getHeaderNames();
+        assertNotNull(headers);
+    }
+
+    public void testGetMatchingHeader() throws Exception {
+        List headers = adapter.getMatchingHeader("From");
+        assertNotNull(headers);
+    }
+
+    public void tesGetSize() throws Exception {
+        int size = adapter.getSize();
+        assertEquals("When mail not set, size is zero", 0, size);
+    }
+    
+}

Added: james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterMailTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterMailTest.java?view=auto&rev=522620
==============================================================================
--- james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterMailTest.java (added)
+++ james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterMailTest.java Mon Mar 26 13:33:08 2007
@@ -0,0 +1,54 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.jsieve.util.check;
+
+import junit.framework.TestCase;
+
+import org.apache.jsieve.mail.Action;
+
+public class ScriptCheckMailAdapterMailTest extends TestCase {
+
+    ScriptCheckMailAdapter adapter;
+    Action action;
+    Action anotherAction;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        adapter = new ScriptCheckMailAdapter();
+        action = new MockAction();
+        anotherAction = new MockAction();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testSetMail() throws Exception {
+        adapter.addAction(action);
+        adapter.addAction(anotherAction);
+        adapter.executeActions();
+        assertEquals("Two actions executed", 2, adapter.getExecutedActions().size());
+        assertEquals("Two actions", 2, adapter.getActions().size());
+        adapter.setMail(null);
+        assertEquals("Set mail resets", 0, adapter.getExecutedActions().size());
+        assertEquals("Set mail resets", 0, adapter.getActions().size());
+    }
+
+}

Added: james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterNoMessageSetTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterNoMessageSetTest.java?view=auto&rev=522620
==============================================================================
--- james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterNoMessageSetTest.java (added)
+++ james/jsieve/trunk/src/test/java/org/apache/jsieve/util/check/ScriptCheckMailAdapterNoMessageSetTest.java Mon Mar 26 13:33:08 2007
@@ -0,0 +1,105 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.jsieve.util.check;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.jsieve.javaxmail.MockMimeMessage;
+
+public class ScriptCheckMailAdapterNoMessageSetTest extends TestCase {
+
+    private static final String BCC = "Bcc";
+    private static final String TO = "To";
+    private static final String FROM = "From";
+    private static final int MESSAGE_SIZE = 100;
+    private static final String BCC_ADDRESS_ONE = "bugs@toons.example.org";
+    private static final String BCC_ADDRESS_TWO = "daffy@toons.example.org";
+    private static final String TO_ADDRESS = "roadrunner@acme.example.com";
+    private static final String X_HEADER_NAME = "X-Toon";
+    private static final String X_HEADER_WITH_WS = "   " + X_HEADER_NAME.toLowerCase();
+    private static final String X_HEADER_VALUE = "Road Runner";
+    private static final String X_HEADER_VALUE_ALT = "Wile E. Coyote And Road Runner";
+    private static final String FROM_ADDRESS = "coyote@desert.example.org";
+    ScriptCheckMailAdapter adapter;
+    MockMimeMessage message;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        adapter = new ScriptCheckMailAdapter();
+        message = new MockMimeMessage();
+        message.addHeader(FROM, FROM_ADDRESS);
+        message.addHeader(TO, TO_ADDRESS);
+        message.addHeader(BCC, BCC_ADDRESS_ONE);
+        message.addHeader(BCC, BCC_ADDRESS_TWO);
+        message.addHeader(X_HEADER_NAME, X_HEADER_VALUE);
+        message.addHeader(X_HEADER_NAME.toLowerCase(), X_HEADER_VALUE.toLowerCase());
+        message.addHeader(X_HEADER_WITH_WS, X_HEADER_VALUE_ALT);
+        message.setSize(MESSAGE_SIZE);
+        adapter.setMail(message);
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testGetHeader() throws Exception {
+        List headers = adapter.getHeader(FROM);
+        assertNotNull(headers);
+        assertEquals("From header", 1, headers.size());
+        assertEquals("From header", FROM_ADDRESS, headers.get(0));
+        headers = adapter.getHeader(BCC);
+        assertEquals("Bcc headers", 2, headers.size());
+        assertTrue("Bcc headers", headers.contains(BCC_ADDRESS_ONE));
+        assertTrue("Bcc headers", headers.contains(BCC_ADDRESS_TWO));
+        headers = adapter.getHeader(X_HEADER_NAME);
+        assertEquals("Case and whitespace sensitive", 1, headers.size());
+        assertEquals("Case and whitespace sensitive", X_HEADER_VALUE, headers.get(0));
+        headers = adapter.getHeader(X_HEADER_NAME.toLowerCase());
+        assertEquals("Case and whitespace sensitive", 1, headers.size());
+        assertEquals("Case and whitespace sensitive", X_HEADER_VALUE.toLowerCase(), headers.get(0));
+        headers = adapter.getHeader(X_HEADER_WITH_WS);
+        assertEquals("Case and whitespace sensitive", 1, headers.size());
+        assertEquals("Case and whitespace sensitive", X_HEADER_VALUE_ALT, headers.get(0));
+    }
+
+    public void testGetHeaderNames() throws Exception {
+        List headers = adapter.getHeaderNames();
+        assertNotNull(headers);
+        assertEquals("All headers set returned", 6, headers.size());
+        assertTrue("All headers set returned", headers.contains(BCC));
+        assertTrue("All headers set returned", headers.contains(TO));
+        assertTrue("All headers set returned", headers.contains(FROM));
+        assertTrue("All headers set returned", headers.contains(X_HEADER_NAME));
+        assertTrue("All headers set returned", headers.contains(X_HEADER_NAME.toLowerCase()));
+        assertTrue("All headers set returned", headers.contains(X_HEADER_WITH_WS));
+    }
+
+    public void testGetMatchingHeader() throws Exception {
+        List headers = adapter.getMatchingHeader(FROM);
+        assertNotNull(headers);
+    }
+
+    public void testGetSize() throws Exception {
+        int size = adapter.getSize();
+        assertEquals("Message size set", MESSAGE_SIZE, size);
+    }
+}



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