Initial fake command-handler for USER command

git-svn-id: svn://svn.code.sf.net/p/mockftpserver/code@30 531de8e6-9941-0410-b38b-9a92acbe0330
diff --git a/MockFtpServer/.classpath b/MockFtpServer/.classpath
index 5188e0d..05ded44 100644
--- a/MockFtpServer/.classpath
+++ b/MockFtpServer/.classpath
@@ -21,5 +21,6 @@
 	<classpathentry kind="var" path="M2_REPO/easymock/easymock/1.2_Java1.3/easymock-1.2_Java1.3.jar"/>

 	<classpathentry kind="var" path="M2_REPO/xerces/xmlParserAPIs/2.6.2/xmlParserAPIs-2.6.2.jar"/>

 	<classpathentry exported="true" kind="con" path="GROOVY_SUPPORT"/>

+	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>

 	<classpathentry kind="output" path="bin-groovy"/>

 </classpath>

diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy
new file mode 100644
index 0000000..9aec814
--- /dev/null
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2008 the original author or authors.
+ * 
+ * Licensed 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.mockftpserver.core.session
+
+/**
+ * Constants for names of properties (attributes) stored in the session.
+ */
+ class SessionKeys {
+
+     static final USERNAME = "username"
+     static final USER_ACCOUNT = "userAccount"
+     static final CURRENT_DIRECTORY = "currentDirectory"
+     static final RENAME_FROM = "renameFrom"
+ 
+}
\ No newline at end of file
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfiguration.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfiguration.groovy
new file mode 100644
index 0000000..56f89c0
--- /dev/null
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfiguration.groovy
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2008 the original author or authors.
+ * 
+ * Licensed 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.mockftpserver.fake
+
+import org.mockftpserver.fake.filesystem.FileSystem
+import org.mockftpserver.fake.user.UserAccount
+
+/**
+ * Interface for objects that provide access to server-specific information.
+ */
+interface ServerConfiguration {
+    
+    FileSystem getFileSystem()
+    void setFileSystem(FileSystem fileSystem)
+
+    ResourceBundle getReplyTextBundle()
+    void setReplyTextBundle(ResourceBundle resourceBundle)
+    
+    UserAccount getUserAccount(String username)
+    
+    String getTextForReplyCode(int replyCode)
+    
+}
\ No newline at end of file
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfigurationAware.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfigurationAware.groovy
new file mode 100644
index 0000000..b977d8a
--- /dev/null
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfigurationAware.groovy
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2008 the original author or authors.
+ * 
+ * Licensed 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.mockftpserver.fake
+
+/**
+ * Interface for classes that provide setter and getter to access a ServerConfiguration instance.
+ */
+interface ServerConfigurationAware {
+     ServerConfiguration getServerConfiguration();
+     void setServerConfiguration(ServerConfiguration serverConfiguration);
+}
\ No newline at end of file
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractFakeCommandHandler.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractFakeCommandHandler.groovy
new file mode 100644
index 0000000..006ae0f
--- /dev/null
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractFakeCommandHandler.groovy
Binary files differ
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/UserCommandHandler.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/UserCommandHandler.groovy
new file mode 100644
index 0000000..e125739
--- /dev/null
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/UserCommandHandler.groovy
Binary files differ
diff --git a/MockFtpServer/src/main/java/org/mockftpserver/core/command/Command.java b/MockFtpServer/src/main/java/org/mockftpserver/core/command/Command.java
index c0dd720..32c16cb 100644
--- a/MockFtpServer/src/main/java/org/mockftpserver/core/command/Command.java
+++ b/MockFtpServer/src/main/java/org/mockftpserver/core/command/Command.java
@@ -1,5 +1,5 @@
 /*

- * Copyright 2007 the original author or authors.

+ * Copyright 20078 the original author or authors.

  * 

  * Licensed under the Apache License, Version 2.0 (the "License");

  * you may not use this file except in compliance with the License.

@@ -16,6 +16,7 @@
 package org.mockftpserver.core.command;

 

 import java.util.Arrays;

+import java.util.List;

 

 import org.mockftpserver.core.util.Assert;

 

@@ -35,7 +36,7 @@
     /**

      * Construct a new immutable instance with the specified command name and parameters

      * @param name - the command name; may not be null

-     * @param parameters - the command parameters; may be empty; may not benull

+     * @param parameters - the command parameters; may be empty; may not be null

      */

     public Command(String name, String[] parameters) {

         Assert.notNull(name, "name");

@@ -45,6 +46,15 @@
     }

 

     /**

+     * Construct a new immutable instance with the specified command name and parameters

+     * @param name - the command name; may not be null

+     * @param parameters - the command parameters; may be empty; may not be null

+     */

+    public Command(String name, List parameters) {

+        this(name, (String[]) parameters.toArray(new String[parameters.size()]));

+    }

+    

+    /**

      * @return the name

      */

     public String getName() {

diff --git a/MockFtpServer/src/main/java/org/mockftpserver/core/command/ReplyCodes.java b/MockFtpServer/src/main/java/org/mockftpserver/core/command/ReplyCodes.java
index 4bf03cf..8c0a437 100644
--- a/MockFtpServer/src/main/java/org/mockftpserver/core/command/ReplyCodes.java
+++ b/MockFtpServer/src/main/java/org/mockftpserver/core/command/ReplyCodes.java
@@ -53,6 +53,7 @@
     public static final int TYPE_OK = 200;

     public static final int USER_LOGGED_IN_OK = 230;

     public static final int USER_NEED_PASSWORD_OK = 331;

+    public static final int USER_NO_SUCH_USER = 530;

     

     public static final int SEND_DATA_INITIAL_OK = 150;

     public static final int SEND_DATA_FINAL_OK = 226;

diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/core/session/StubSession.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/core/session/StubSession.groovy
new file mode 100644
index 0000000..b498e19
--- /dev/null
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/core/session/StubSession.groovy
@@ -0,0 +1,153 @@
+/*

+ * Copyright 2008 the original author or authors.

+ * 

+ * Licensed 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.mockftpserver.core.session

+

+import java.net.InetAddress

+import java.util.Set

+

+/**

+ * Stub implementation of the {@link Session} interface for testing

+ * 

+ * @version $Revision: $ - $Date: $

+ *

+ * @author Chris Mair

+ */

+class StubSession implements Session {

+

+     Map attributes = [:]

+     List sentReplies = [ ]

+     

+    /**

+     * @see org.mockftpserver.core.session.Session#close()

+     */

+    public void close() {

+

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#closeDataConnection()

+     */

+    public void closeDataConnection() {

+

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#getAttribute(java.lang.String)

+     */

+    public Object getAttribute(String name) {

+        return attributes[name]

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#getAttributeNames()

+     */

+    public Set getAttributeNames() {

+        return attributes.keySet()

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#getClientHost()

+     */

+    public InetAddress getClientHost() {

+        return null

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#getServerHost()

+     */

+    public InetAddress getServerHost() {

+        return null

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#openDataConnection()

+     */

+    public void openDataConnection() {

+

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#readData()

+     */

+    public byte[] readData() {

+        return null

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#removeAttribute(java.lang.String)

+     */

+    public void removeAttribute(String name) {

+        attributes.remove(name)

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#sendData(byte[], int)

+     */

+    public void sendData(byte[] data, int numBytes) {

+

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#sendReply(int, java.lang.String)

+     */

+    public void sendReply(int replyCode, String replyText) {

+        sentReplies << [replyCode, replyText]

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#setAttribute(java.lang.String, java.lang.Object)

+     */

+    public void setAttribute(String name, Object value) {

+        attributes[name] = value

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#setClientDataHost(java.net.InetAddress)

+     */

+    public void setClientDataHost(InetAddress clientHost) {

+

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#setClientDataPort(int)

+     */

+    public void setClientDataPort(int clientDataPort) {

+

+    }

+

+    /**

+     * @see org.mockftpserver.core.session.Session#switchToPassiveMode()

+     */

+    public int switchToPassiveMode() {

+        return 0

+    }

+

+    /**

+     * @see java.lang.Runnable#run()

+     */

+    public void run() {

+

+    }

+

+    //-------------------------------------------------------------------------

+    // Stub-specific API - Helper methods not part of Session interface

+    //-------------------------------------------------------------------------

+    

+    String toString() {

+        "StubSession[sentReplies=$sentReplies]"

+    }

+    

+}

diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/StubServerConfiguration.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/StubServerConfiguration.groovy
new file mode 100644
index 0000000..3597de5
--- /dev/null
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/StubServerConfiguration.groovy
@@ -0,0 +1,55 @@
+/*

+ * Copyright 2008 the original author or authors.

+ * 

+ * Licensed 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.mockftpserver.fake

+

+import org.mockftpserver.fake.filesystem.FileSystem

+import org.mockftpserver.fake.ServerConfiguration

+import org.mockftpserver.fake.user.UserAccount

+

+/**

+ * Stub implementation of the {@link ServerConfiguration} interface for testing

+ * 

+ * @version $Revision: $ - $Date: $

+ *

+ * @author Chris Mair

+ */

+class StubServerConfiguration implements ServerConfiguration {

+

+     ResourceBundle replyTextBundle

+     Map userAccounts = [:]

+     

+     FileSystem getFileSystem() {

+         

+     }

+     

+     void setFileSystem(FileSystem fileSystem) {

+         

+     }

+

+     

+     UserAccount getUserAccount(String username) {

+         userAccounts[username]

+     }

+     

+     String getTextForReplyCode(int replyCode) {

+         return replyCode as String

+     }

+

+     //-------------------------------------------------------------------------

+     // Stub-specific API - Helper methods not part of ServerConfiguration interface

+     //-------------------------------------------------------------------------

+     

+}

diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AbstractCommandHandlerTest.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AbstractCommandHandlerTest.groovy
new file mode 100644
index 0000000..d5de79e
--- /dev/null
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AbstractCommandHandlerTest.groovy
Binary files differ
diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/UserCommandHandlerTest.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/UserCommandHandlerTest.groovy
new file mode 100644
index 0000000..975d273
--- /dev/null
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/UserCommandHandlerTest.groovy
Binary files differ
diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/test/AbstractGroovyTest.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/test/AbstractGroovyTest.groovy
index 7d9825a..9847bdb 100644
--- a/MockFtpServer/src/test/groovy/org/mockftpserver/test/AbstractGroovyTest.groovy
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/test/AbstractGroovyTest.groovy
@@ -46,7 +46,23 @@
          assert actualException.class == expectedExceptionClass, "Expected [${expectedExceptionClass.getName()}] but was [${actualException.class.name}]" 
          return actualException 
      } 
-         
+
+     /** 
+      * Assert that the specified code throws an exception with an error message
+      * containing the specified text. 
+      * @param text - the text expected within the exception message 
+      * @param code - the Closure containing the code to be executed, which is expected to throw an exception of the specified type
+      * @return the message from the thrown Exception 
+      * 
+      * @throws AssertionError - if no exception is thrown by the code or if the thrown 
+      * 	exception message does not contain the expected text 
+      */ 
+     protected String shouldFailWithMessageContaining(String text, Closure code) { 
+          def message = shouldFail(code)
+          assert message.contains(text)
+          return message
+     }
+          
      /** 
       * Write the specified message out to the log. Application-specific subclasses 
       * can override with application-specific logging behavior, if desired. 
diff --git a/MockFtpServer/src/test/java/org/mockftpserver/stub/command/CommandTest.java b/MockFtpServer/src/test/java/org/mockftpserver/stub/command/CommandTest.java
index d104885..a12175f 100644
--- a/MockFtpServer/src/test/java/org/mockftpserver/stub/command/CommandTest.java
+++ b/MockFtpServer/src/test/java/org/mockftpserver/stub/command/CommandTest.java
@@ -15,6 +15,8 @@
  */

 package org.mockftpserver.stub.command;

 

+import java.util.List;

+

 import org.apache.log4j.Logger;

 import org.mockftpserver.core.command.Command;

 import org.mockftpserver.core.util.AssertFailedException;

@@ -32,7 +34,7 @@
     private static final Logger LOG = Logger.getLogger(CommandTest.class);

     

     /**

-     * Test the constructor

+     * Test the Command(String,String[]) constructor

      */

     public void testConstructor() {

         final String[] PARAMETERS = array("123");

@@ -42,6 +44,17 @@
     }

     

     /**

+     * Test the Command(String,List) constructor

+     */

+    public void testConstructor_List() {

+        final List PARAMETERS_LIST = list("123");

+        final String[] PARAMETERS_ARRAY = array("123");

+        Command command = new Command("abc", PARAMETERS_LIST);

+        assertEquals("name", "abc", command.getName());

+        assertEquals("parameters String[]", PARAMETERS_ARRAY, command.getParameters());

+    }

+    

+    /**

      * Test the Constructor method, passing in a null name

      */

     public void testConstructor_NullName() {

@@ -59,7 +72,7 @@
      */

     public void testConstructor_NullParameters() {

         try {

-            new Command("OK", null);

+            new Command("OK", (String[])null);

             fail("Expected AssertFailedException");

         }

         catch (AssertFailedException expected) {