summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchrismair <chrismair@531de8e6-9941-0410-b38b-9a92acbe0330>2008-03-26 01:48:22 +0000
committerchrismair <chrismair@531de8e6-9941-0410-b38b-9a92acbe0330>2008-03-26 01:48:22 +0000
commit78795c9e5e3c55fc294f011bcc129b7aab0f49b7 (patch)
tree267f65de7f7539e39df7b87fc479bdb76a185c3e
parentabd32a990e145862f22d15316feedced5ad246aa (diff)
downloadmockftpserver-78795c9e5e3c55fc294f011bcc129b7aab0f49b7.tar.gz
Initial fake command-handler for USER command
git-svn-id: svn://svn.code.sf.net/p/mockftpserver/code@30 531de8e6-9941-0410-b38b-9a92acbe0330
-rw-r--r--MockFtpServer/.classpath1
-rw-r--r--MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy28
-rw-r--r--MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfiguration.groovy36
-rw-r--r--MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfigurationAware.groovy24
-rw-r--r--MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractFakeCommandHandler.groovy114
-rw-r--r--MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/UserCommandHandler.groovy30
-rw-r--r--MockFtpServer/src/main/java/org/mockftpserver/core/command/Command.java14
-rw-r--r--MockFtpServer/src/main/java/org/mockftpserver/core/command/ReplyCodes.java1
-rw-r--r--MockFtpServer/src/test/groovy/org/mockftpserver/core/session/StubSession.groovy153
-rw-r--r--MockFtpServer/src/test/groovy/org/mockftpserver/fake/StubServerConfiguration.groovy55
-rw-r--r--MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AbstractCommandHandlerTest.groovy58
-rw-r--r--MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/UserCommandHandlerTest.groovy86
-rw-r--r--MockFtpServer/src/test/groovy/org/mockftpserver/test/AbstractGroovyTest.groovy18
-rw-r--r--MockFtpServer/src/test/java/org/mockftpserver/stub/command/CommandTest.java17
14 files changed, 630 insertions, 5 deletions
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
@@ -0,0 +1,114 @@
+/*
+ * 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.command
+
+import org.mockftpserver.fake.ServerConfigurationAware
+import org.mockftpserver.fake.ServerConfiguration
+import org.mockftpserver.core.session.Session import org.apache.log4j.Logger import org.apache.log4j.Logger import java.text.MessageFormat import org.mockftpserver.core.command.Command
+
+/**
+ * Abstract superclass for CommandHandler classes for the "Fake" server.
+ */
+abstract class AbstractFakeCommandHandler implements ServerConfigurationAware {
+
+ final Logger LOG = Logger.getLogger(this.class)
+ ServerConfiguration serverConfiguration
+
+ /**
+ * Use template method to centralize and ensure common validation
+ */
+ protected final void handleCommand(Command command, Session session) {
+ assert serverConfiguration != null
+ assert command != null
+ assert session != null
+
+ handle(command, session)
+ }
+
+ /**
+ * Subclasses must implement this
+ */
+ protected abstract void handle(Command command, Session session)
+
+ // -------------------------------------------------------------------------
+ // Utility methods for subclasses
+ // -------------------------------------------------------------------------
+
+ /**
+ * Send a reply for this command on the control connection.
+ *
+ * The reply code is designated by the <code>replyCode</code> property, and the reply text
+ * is retrieved from the <code>replyText</code> ResourceBundle, using the reply code as the key.
+ *
+ * @param session - the Session
+ * @param replyCode - the reply code
+ *
+ * @throws AssertionError - if session is null
+ *
+ * @see MessageFormat
+ */
+ protected void sendReply(Session session, int replyCode) {
+ assert session
+ assertValidReplyCode(replyCode);
+
+ //String key = (replyMessageKey != null) ? replyMessageKey : Integer.toString(replyCode);
+ String key = Integer.toString(replyCode);
+ //String text = getTextForReplyCode(replyCode, key, null);
+ String text = serverConfiguration.getTextForReplyCode(replyCode)
+ String replyTextToLog = (text == null) ? "" : " " + text;
+ LOG.debug("Sending reply [" + replyCode + replyTextToLog + "]");
+ session.sendReply(replyCode, text);
+ }
+
+ /**
+ * Return the text for the specified reply code, formatted using the message arguments, if
+ * supplied. Return the text mapped to the code from the replyText ResourceBundle. If the
+ * ResourceBundle contains no mapping, then return null.
+ * <p>
+ * If arguments is not null, then the returned reply text if formatted using the
+ * {@link MessageFormat} class.
+ *
+ * @param code - the reply code
+ * @param messageKey - the key used to retrieve the reply text from the replyTextBundle
+ * @param arguments - the array of arguments to be formatted and substituted within the reply
+ * text; may be null
+ * @return the text for the reply code; may be null
+ */
+ private String getTextForReplyCode(int code, String messageKey, Object[] arguments) {
+ try {
+ String t = serverConfiguration.getTextForReplyCode(code)
+ String formattedMessage = MessageFormat.format(t, arguments);
+ return (formattedMessage == null) ? null : formattedMessage.trim();
+ }
+ catch (MissingResourceException e) {
+ // No reply text is mapped for the specified key
+ LOG.warn("No reply text defined for reply code [" + code + "]");
+ return null;
+ }
+ }
+
+ /**
+ * Assert that the specified number is a valid reply code
+ * @param replyCode - the reply code to check
+ *
+ * @throws AssertionError - if the replyCode is invalid
+ */
+ protected void assertValidReplyCode(int replyCode) {
+ assert replyCode > 0, "The number [" + replyCode + "] is not a valid reply code"
+ }
+
+
+} \ No newline at end of file
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
@@ -0,0 +1,30 @@
+/*
+ * 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.command
+
+import org.mockftpserver.fake.command.AbstractFakeCommandHandler import org.mockftpserver.core.command.Command import org.mockftpserver.core.session.Session import org.mockftpserver.core.session.SessionKeys
+import org.mockftpserver.core.command.ReplyCodes
+
+class UserCommandHandler extends AbstractFakeCommandHandler {
+
+ protected void handle(Command command, Session session) {
+ def username = command.getRequiredString(0)
+ assert username.size() > 0
+ session.setAttribute(SessionKeys.USERNAME, username)
+ sendReply(session, ReplyCodes.USER_NEED_PASSWORD_OK)
+ }
+
+} \ No newline at end of file
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 @@ public final class Command {
/**
* 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 @@ public final class Command {
}
/**
+ * 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 final class ReplyCodes {
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
@@ -0,0 +1,58 @@
+/*
+ * 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.command
+
+import org.mockftpserver.test.AbstractGroovyTest
+import org.mockftpserver.core.command.Command
+import org.mockftpserver.core.command.CommandNames import org.mockftpserver.core.session.StubSession
+import org.mockftpserver.fake.StubServerConfiguration
+import org.apache.log4j.Logger import org.mockftpserver.core.command.ReplyCodes
+/**
+ * Abstract superclass for CommandHandler tests
+ *
+ * @version $Revision: $ - $Date: $
+ *
+ * @author Chris Mair
+ */
+abstract class AbstractCommandHandlerTest extends AbstractGroovyTest {
+ protected final Logger LOG = Logger.getLogger(this.class)
+
+ def session
+ def serverConfiguration
+ def commandHandler
+
+ /**
+ * Create and return a new instance of the CommandHandler class under test. Concrete subclasses must implement.
+ */
+ abstract createCommandHandler()
+
+ void setUp() {
+ super.setUp()
+ session = new StubSession()
+ serverConfiguration = new StubServerConfiguration()
+ commandHandler = createCommandHandler()
+ commandHandler.serverConfiguration = serverConfiguration
+ }
+
+ /**
+ * Assert that the specified reply code (and default message) was sent to the session.
+ */
+ void assertSessionReply(int replyCode) {
+ LOG.info(session)
+ assert session.sentReplies[0] == [replyCode, replyCode as String]
+ }
+
+ } \ No newline at end of file
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
@@ -0,0 +1,86 @@
+/*
+ * 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.command
+
+import org.mockftpserver.test.AbstractGroovyTest
+import org.mockftpserver.core.command.Command
+import org.mockftpserver.core.command.CommandNames import org.mockftpserver.core.session.StubSession
+import org.mockftpserver.core.session.SessionKeys
+import org.mockftpserver.fake.StubServerConfiguration
+import org.mockftpserver.fake.user.UserAccount
+import org.apache.log4j.Logger import org.mockftpserver.core.command.ReplyCodes import com.sun.corba.se.impl.activation.CommandHandler import org.mockftpserver.core.util.AssertFailedException
+
+/**
+ * Tests for UserCommandHandler
+ *
+ * @version $Revision: $ - $Date: $
+ *
+ * @author Chris Mair
+ */
+class UserCommandHandlerTest extends AbstractCommandHandlerTest {
+
+ def USERNAME = "user123"
+
+ void testHandleCommand_UserExists() {
+ serverConfiguration.userAccounts[USERNAME] = new UserAccount()
+ def command = new Command(CommandNames.USER, [USERNAME]);
+ commandHandler.handleCommand(command, session)
+ assertSessionReply(ReplyCodes.USER_NEED_PASSWORD_OK)
+ assert session.getAttribute(SessionKeys.USERNAME) == USERNAME
+ }
+
+ void testHandleCommand_NoSuchUser() {
+ def command = new Command(CommandNames.USER, [USERNAME]);
+ commandHandler.handleCommand(command, session)
+ // Will return OK, even if username is not recognized
+ assertSessionReply(ReplyCodes.USER_NEED_PASSWORD_OK)
+ assert session.getAttribute(SessionKeys.USERNAME) == USERNAME
+ }
+
+ void testHandleCommand_MissingUsernameParameter() {
+ def command = new Command(CommandNames.USER, []);
+ shouldFail(AssertFailedException) { commandHandler.handleCommand(command, session) }
+ }
+
+ void testHandleCommand_EmptyUsernameParameter() {
+ def command = new Command(CommandNames.USER, [""]);
+ shouldFail { commandHandler.handleCommand(command, session) }
+ }
+
+ void testHandleCommand_ServerConfigurationIsNull() {
+ commandHandler.serverConfiguration = null
+ def command = new Command(CommandNames.USER, [USERNAME]);
+ shouldFailWithMessageContaining("serverConfiguration") { commandHandler.handleCommand(command, session) }
+ }
+
+ void testHandleCommand_CommandIsNull() {
+ shouldFailWithMessageContaining("command") { commandHandler.handleCommand(null, session) }
+ }
+
+ void testHandleCommand_SessionIsNull() {
+ def command = new Command(CommandNames.USER, [USERNAME]);
+ shouldFailWithMessageContaining("session") { commandHandler.handleCommand(command, null) }
+ }
+
+ void setUp() {
+ super.setUp()
+ }
+
+ protected createCommandHandler() {
+ new UserCommandHandler()
+ }
+
+ } \ No newline at end of file
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 @@ abstract class AbstractGroovyTest extends GroovyTestCase {
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 @@ public final class CommandTest extends AbstractTest {
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 @@ public final class CommandTest extends AbstractTest {
}
/**
+ * 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 final class CommandTest extends AbstractTest {
*/
public void testConstructor_NullParameters() {
try {
- new Command("OK", null);
+ new Command("OK", (String[])null);
fail("Expected AssertFailedException");
}
catch (AssertFailedException expected) {