Author: rdonkin
Date: Tue Jul 31 01:40:44 2007
New Revision: 561266

URL: http://svn.apache.org/viewvc?view=rev&rev=561266
Log:
Factor out dependency on IMAP implementation.

Added:
    
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/HostSystem.java
Removed:
    james/server/sandbox/imap-functional/src/main/java/
    
james/server/sandbox/imap-functional/src/test/java/org/apache/james/imapserver/CommandParserTest.java
    
james/server/sandbox/imap-functional/src/test/java/org/apache/james/imapserver/ImapHostTest.java
    
james/server/sandbox/imap-functional/src/test/java/org/apache/james/imapserver/ImapMailboxTest.java
    
james/server/sandbox/imap-functional/src/test/java/org/apache/james/imapserver/ImapStoreTest.java
    
james/server/sandbox/imap-functional/src/test/java/org/apache/james/imapserver/InitialMail.java
Modified:
    
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/AbstractProtocolTest.java
    
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/ProtocolSession.java
    
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/SimpleFileProtocolTest.java

Modified: 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/AbstractProtocolTest.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/AbstractProtocolTest.java?view=diff&rev=561266&r1=561265&r2=561266
==============================================================================
--- 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/AbstractProtocolTest.java
 (original)
+++ 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/AbstractProtocolTest.java
 Tue Jul 31 01:40:44 2007
@@ -19,32 +19,12 @@
 
 package org.apache.james.test;
 
-import junit.framework.TestCase;
-import org.apache.james.imapserver.ImapHandler;
-import org.apache.james.imapserver.ImapHost;
-import org.apache.james.imapserver.ImapRequestHandler;
-import org.apache.james.imapserver.ImapResponse;
-import org.apache.james.imapserver.ImapSession;
-import org.apache.james.imapserver.ImapSessionImpl;
-import org.apache.james.imapserver.ImapTest;
-import org.apache.james.imapserver.JamesImapHost;
-import org.apache.james.imapserver.ProtocolException;
-import org.apache.james.imapserver.store.MailboxException;
-import org.apache.james.userrepository.AbstractUsersRepository;
-import org.apache.james.userrepository.DefaultUser;
-import org.apache.mailet.User;
-import org.apache.mailet.UsersRepository;
-
 import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
 import java.io.PrintWriter;
-import java.net.Socket;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.james.imapserver.ImapTest;
 
 /**
  * Abstract Protocol Test is the root of all of the James Imap Server test
@@ -73,14 +53,12 @@
     /** The timeout to set on the socket for socket-based testing. */
     protected int timeout = TIMEOUT;
 
-    /** A UsersRepository which all tests share. */
-    private static UsersRepository users;
-    /** An ImapHost instance which all tests share. */
-    private static ImapHost imapHost;
-
-    public AbstractProtocolTest( String s )
+    private final HostSystem hostSystem;
+    
+    public AbstractProtocolTest( String name, HostSystem hostSystem )
     {
-        super( s );
+        super(name);
+        this.hostSystem = hostSystem;
     }
 
     protected void setUp() throws Exception
@@ -89,68 +67,6 @@
         setUpEnvironment();
     }
 
-    /**
-     * Uses a system property to determine whether to run tests locally, or 
against
-     * a remote server.
-     */
-    protected void runSessions() throws Exception
-    {
-        String runLocal = System.getProperty( "runTestsLocal", "true" );
-        boolean local = Boolean.valueOf( runLocal ).booleanValue();
-        if ( local ) {
-            runLocalProtocolSessions();
-        }
-        else {
-            runSocketProtocolSessions();
-        }
-    }
-
-    /**
-     * Runs the pre,test and post protocol sessions against a running instance 
of James,
-     * by communicating via a socket connection. After a request is sent, the 
server response
-     * is parsed to determine if the actual response matches that expected.
-     */
-    private void runSocketProtocolSessions()
-            throws Exception {
-        Socket[] socket = new Socket[testElements.getSessionCount()];
-        PrintWriter[] out = new PrintWriter[socket.length];
-        BufferedReader[] in = new BufferedReader[socket.length];
-
-        for (int i = 0; i < socket.length; i++) {
-            socket[i] = new Socket(host, port);
-            socket[i].setSoTimeout(timeout);
-            out[i] = new PrintWriter(socket[i].getOutputStream());
-            in[i] = new BufferedReader(new 
InputStreamReader(socket[i].getInputStream()));
-        }
-
-        Exception failure = null;
-        try {
-            preElements.runLiveSession(out, in);
-            testElements.runLiveSession(out, in);
-        } catch (ProtocolSession.InvalidServerResponseException e) {
-            failure = e;
-        } finally {
-            // Try our best to do cleanup.
-            try {
-                postElements.runLiveSession(out, in);
-            } catch (ProtocolSession.InvalidServerResponseException e) {
-                // Don't overwrite real error with error on cleanup.
-                if (failure == null) {
-                    failure = e;
-                }
-            }
-        }
-        
-        if (failure != null) {
-            fail(failure.getMessage());
-        }
-
-        for (int i = 0; i < socket.length; i++) {
-            out[i].close();
-            in[i].close();
-            socket[i].close();
-        }
-    }
 
     /**
      * Runs the pre,test and post protocol sessions against a local copy of 
the ImapServer.
@@ -162,191 +78,65 @@
      * is required per protocol session/connection. These share the same 
underlying 
      * Mailboxes, because of the way [EMAIL PROTECTED] 
MockImapServer#getImapSession()} works.
      */
-    private void runLocalProtocolSessions() throws Exception
+    protected void runSessions() throws Exception
     {
-        MockImapServer[] socket = new 
MockImapServer[testElements.getSessionCount()];
+        HostSystem.Session[] socket = new 
HostSystem.Session[testElements.getSessionCount()];
         PrintWriter[] out = new PrintWriter[socket.length];
         BufferedReader[] in = new BufferedReader[socket.length];
 
         for (int i = 0; i < socket.length; i++) {
-            socket[i] = new MockImapServer();
-            out[i] = socket[i].getWriter();
-            in[i] = socket[i].getReader();
+            socket[i] = hostSystem.newSession();
+            out[i] = new PrintWriter(socket[i].getWriter());
+            in[i] = new BufferedReader(socket[i].getReader());
             socket[i].start();
         }
-
-        Exception failure = null;
-        try {
-             preElements.runLiveSession( out, in );
-             testElements.runLiveSession( out, in );
-        } catch (ProtocolSession.InvalidServerResponseException e) {
-             failure = e;
-             // Try our best to do cleanup.
-               for (int i = 0; i < in.length; i++) {
-                   BufferedReader reader = in[i];
-                   while (reader.ready()) {
-                       reader.read();
-                   }
-               }
-         } finally {
-             try {
-                 postElements.runLiveSession(out, in);
-             } catch (ProtocolSession.InvalidServerResponseException e) {
-                 // Don't overwrite real error with error on cleanup.
-                 if (failure == null) {
-                     failure = e;
-                 }
-             }
-         }
-        
-         if (failure != null) {
-             fail(failure.getMessage());
-         }
-
- 
-        for (int i = 0; i < socket.length; i++) {
-            out[i].close();
-            in[i].close();
-            socket[i].stopServer();
-        }
-
-    }
-
-    /**
-     * Initialises the UsersRepository and ImapHost on first call.
-     * TODO enable logging, set up components properly.
-     */
-    private void setUpEnvironment() throws MailboxException
-    {
-        if ( users == null || imapHost == null ) {
-            users = new InMemoryUsersRepository();
-            DefaultUser user = new DefaultUser( USER, "SHA" );
-            user.setPassword( PASSWORD );
-            users.addUser( user );
-
-            imapHost = new JamesImapHost();
-            imapHost.createPrivateMailAccount( user );
-        }
-    }
-
-    /**
-     * A simple, dummy implementation of UsersRepository to use for testing,
-     * which stored all users in memory.
-     */
-    private class InMemoryUsersRepository extends AbstractUsersRepository
-    {
-        private Map users = new HashMap();
-
-        protected Iterator listAllUsers()
-        {
-            return users.values().iterator();
-        }
-
-        protected void doAddUser( User user )
-        {
-            users.put( user.getUserName(), user );
-        }
-
-        protected void doRemoveUser( User user )
+        try
         {
-            users.remove( user.getUserName());
-        }
-
-        protected void doUpdateUser( User user )
-        {
-            users.put( user.getUserName(), user );
-        }
-    }
-
-    /**
-     * A simple test utility which allows testing of Imap commands without
-     * deployment of the full server. Piped input and output streams are used
-     * to provide the equivilant of a socket interface.
-     */ 
-    private class MockImapServer extends Thread {
-        private ImapRequestHandler handler = new ImapRequestHandler();
-
-        private PipedInputStream requestInputStream;
-        private PipedOutputStream requestOutputStream;
-
-        private PipedInputStream responseInputStream;
-        private PipedOutputStream responseOutputStream;
-
-        private ImapSession session;
-        private ProtocolException exception;
-
-        private boolean running;
-
-        /**
-         * Creates a MockImapServer, with a handler for the session provided.
-         */ 
-        public MockImapServer() throws IOException {
-            this.session = getImapSession();
-            requestOutputStream = new PipedOutputStream();
-            requestInputStream = new PipedInputStream(requestOutputStream);
-
-            responseInputStream = new PipedInputStream();
-            responseOutputStream = new PipedOutputStream(responseInputStream);
-
-        }
-
-        /**
-         * Core loop where Imap commands are handled
-         */ 
-        public void run() {
-            running = true;
+            Exception failure = null;
             try {
-                responseOutputStream.write( "* OK IMAP4rev1 Server XXX 
ready".getBytes() );
-                responseOutputStream.write( '\r' );
-                responseOutputStream.write( '\n' );
-            } catch (IOException e) {
-                throw new RuntimeException("Couldn't write welcome message", 
e);
-            }
-
-            while (running) {
+                preElements.runLiveSession( out, in );
+                testElements.runLiveSession( out, in );
+            } catch (ProtocolSession.InvalidServerResponseException e) {
+                failure = e;
+                // Try our best to do cleanup.
+                for (int i = 0; i < in.length; i++) {
+                    BufferedReader reader = in[i];
+                    while (reader.ready()) {
+                        reader.read();
+                    }
+                }
+            } finally {
                 try {
-                    handler.handleRequest(requestInputStream, 
responseOutputStream, session);
-                } catch (ProtocolException e) {
-                    exception = e;
-                    break;
+                    postElements.runLiveSession(out, in);
+                } catch (ProtocolSession.InvalidServerResponseException e) {
+                    // Don't overwrite real error with error on cleanup.
+                    if (failure == null) {
+                        failure = e;
+                    }
                 }
             }
-        }
-
-        /** 
-         * @return A writer which is used to send commands to the mock server.
-         */ 
-        public PrintWriter getWriter() {
-            return new PrintWriter(requestOutputStream);
-        }
 
-        /**
-         * @return A reader which is used to read responses from the mock 
server.
-         */ 
-        public BufferedReader getReader() {
-            return new BufferedReader(new 
InputStreamReader(responseInputStream));
-        }
+            if (failure != null) {
+                fail(failure.getMessage());
+            }
 
-        /** stop the running server thread.*/
-        public void stopServer() {
-            running = false;
         }
-
-        /**
-         * Provides an ImapSession to use for this test. An ImapSession is 
accosiated
-         * with a single client connection.
-         */
-        private ImapSession getImapSession()
+        finally
         {
-            ImapSession session = new ImapSessionImpl( imapHost, users, new 
MockImapHandler(), null, null );
-            return session;
-        }
-
-        private final class MockImapHandler extends ImapHandler {
-            public void forceConnectionClose(String message) {
-                ImapResponse response = new ImapResponse(responseOutputStream);
-                response.byeResponse(message);        
+            for (int i = 0; i < socket.length; i++) {
+                out[i].close();
+                in[i].close();
+                socket[i].stop();
             }
         }
+    }
+
+    /**
+     * Initialises the UsersRepository and ImapHost on first call.
+     */
+    private void setUpEnvironment() throws Exception
+    {
+        hostSystem.reset();
+        hostSystem.addUser( USER, PASSWORD );
     }
 }

Added: 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/HostSystem.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/HostSystem.java?view=auto&rev=561266
==============================================================================
--- 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/HostSystem.java
 (added)
+++ 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/HostSystem.java
 Tue Jul 31 01:40:44 2007
@@ -0,0 +1,60 @@
+/****************************************************************
+ * 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.james.test;
+
+import java.io.Reader;
+import java.io.Writer;
+
+/**
+ * Host system under test.
+ *
+ */
+public interface HostSystem {
+
+    /**
+     * Resets host system to initial state.
+     * @throws Exception
+     */
+    public void reset() throws Exception;
+    
+    /**
+     * Add a user for testing.
+     * @param user user name
+     * @param password user password
+     * @throws Exception
+     */
+    public void addUser(String user, String password) throws Exception;
+    
+    /**
+     * Creates a new session for functional testing.
+     * @return <code>Session</code>, not null
+     * @throws Exception
+     */
+    public Session newSession() throws Exception;
+    
+    public interface Session
+    {
+        public Reader getReader() throws Exception;
+        public Writer getWriter() throws Exception;
+        public void start() throws Exception;
+        public void stop() throws Exception;
+    }
+}

Modified: 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/ProtocolSession.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/ProtocolSession.java?view=diff&rev=561266&r1=561265&r2=561266
==============================================================================
--- 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/ProtocolSession.java
 (original)
+++ 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/ProtocolSession.java
 Tue Jul 31 01:40:44 2007
@@ -20,14 +20,13 @@
 
 package org.apache.james.test;
 
-import org.apache.oro.text.perl.Perl5Util;
-
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.regex.Pattern;
 
 /**
  * A protocol session which can be run against a reader and writer, which 
checks
@@ -42,7 +41,6 @@
 {
     private int maxSessionNumber;
     protected List testElements = new ArrayList();
-    private static final Perl5Util perl = new Perl5Util();
 
     /**
      * Returns the number of sessions required to run this ProtocolSession.
@@ -254,9 +252,9 @@
          * @return <code>true</code> if the actual matches the expected.
          */
         protected boolean match( String expected, String actual )
-        {
-            String pattern = "m/" + expected + "/";
-            return perl.match( pattern, actual );
+        {            
+            final boolean result = Pattern.matches( expected, actual );
+            return result;
         }
 
         /**

Modified: 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/SimpleFileProtocolTest.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/SimpleFileProtocolTest.java?view=diff&rev=561266&r1=561265&r2=561266
==============================================================================
--- 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/SimpleFileProtocolTest.java
 (original)
+++ 
james/server/sandbox/imap-functional/src/test/java/org/apache/james/test/SimpleFileProtocolTest.java
 Tue Jul 31 01:40:44 2007
@@ -41,9 +41,9 @@
      * in the same location as this test class.
      * @param fileName The name of the file to read protocol elements from.
      */
-    public SimpleFileProtocolTest( String fileName )
+    public SimpleFileProtocolTest( String fileName, HostSystem hostSystem  )
     {
-        super( fileName );
+        super( fileName, hostSystem );
     }
 
     /**



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

Reply via email to