19d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/*
29d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Copyright 2008 the original author or authors.
39d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
49d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Licensed under the Apache License, Version 2.0 (the "License");
59d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * you may not use this file except in compliance with the License.
69d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * You may obtain a copy of the License at
79d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
89d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *      http://www.apache.org/licenses/LICENSE-2.0
99d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Unless required by applicable law or agreed to in writing, software
119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * distributed under the License is distributed on an "AS IS" BASIS,
129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * See the License for the specific language governing permissions and
149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * limitations under the License.
159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpackage org.mockftpserver.fake.command
179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.test.AbstractGroovyTest
199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.Command
209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.CommandHandler
219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.CommandNames
229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.session.StubSession
239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.session.SessionKeys
249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.StubServerConfiguration
259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.user.UserAccount
269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.apache.log4j.Logger
279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.ReplyCodes
289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.filesystem.FileSystem
299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.filesystem.FileSystemException
309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.filesystem.FakeUnixFileSystem
319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.filesystem.FakeUnixFileSystem
329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/**
349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Tests for StorCommandHandler
359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @version $Revision: $ - $Date: $
379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @author Chris Mair
399d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
409d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairclass StorCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
419d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
429d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    def DIR = "/"
439d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    def FILENAME = "file.txt"
449d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    def FILE = p(DIR,FILENAME)
459d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    def CONTENTS = "abc"
469d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
479d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    void testHandleCommand_MissingPathParameter() {
489d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        testHandleCommand_MissingRequiredParameter([])
499d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
509d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
519d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    void testHandleCommand_AbsolutePath() {
529d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        session.dataToRead = CONTENTS.bytes
539d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        handleCommandAndVerifySendDataReplies([FILE])
549d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assert fileSystem.isFile(FILE)
559d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
569d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        def contents = fileSystem.createInputStream(FILE).text
579d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assert contents == CONTENTS
589d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
599d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
609d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    void testHandleCommand_RelativePath() {
619d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        setCurrentDirectory(DIR)
629d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        session.dataToRead = CONTENTS.bytes
639d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        handleCommandAndVerifySendDataReplies([FILENAME])
649d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assert fileSystem.isFile(FILE)
659d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
669d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        def contents = fileSystem.createInputStream(FILE).text
679d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assert contents == CONTENTS
689d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
699d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
709d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    void testHandleCommand_PathSpecifiesAnExistingDirectory() {
719d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assert fileSystem.createDirectory(FILE)
729d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        commandHandler.handleCommand(createCommand([FILE]), session)
739d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, FILE)
749d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
759d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
769d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    void testHandleCommand_ParentDirectoryDoesNotExist() {
779d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        def NO_SUCH_DIR = "/path/DoesNotExist"
789d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        handleCommand([p(NO_SUCH_DIR,FILENAME)])
799d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, NO_SUCH_DIR)
809d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
819d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
829d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    void testHandleCommand_CreateOutputStreamThrowsException() {
839d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        // Override createOutputStream() method to throw exception
849d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        def emc = new ExpandoMetaClass(fileSystem.class, false)
859d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        emc.createOutputStream = { String path, boolean append ->
869d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            println "Calling createOutputStream() - throwing exception"
879d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            throw new FileSystemException("bad")
889d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
899d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        emc.initialize()
909d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        fileSystem.metaClass = emc
919d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
929d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        handleCommand([FILE])
939d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assertSessionReply(0, ReplyCodes.SEND_DATA_INITIAL_OK)
949d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assertSessionReply(1, ReplyCodes.NEW_FILE_ERROR)
959d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
969d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
979d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    //-------------------------------------------------------------------------
989d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    // Helper Methods
999d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    //-------------------------------------------------------------------------
1009d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1019d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	CommandHandler createCommandHandler() {
1029d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	    new StorCommandHandler()
1039d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
1049d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1059d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    Command createValidCommand() {
1069d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        return new Command(CommandNames.STOR, [FILE])
1079d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
108
109    void setUp() {
110        super.setUp()
111        assert fileSystem.createDirectory(DIR)
112    }
113
114}