160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/*
260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Copyright 2008 the original author or authors.
360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Licensed under the Apache License, Version 2.0 (the "License");
560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * you may not use this file except in compliance with the License.
660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * You may obtain a copy of the License at
760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *      http://www.apache.org/licenses/LICENSE-2.0
960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
1060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Unless required by applicable law or agreed to in writing, software
1160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * See the License for the specific language governing permissions and
1460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * limitations under the License.
1560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
1660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpackage org.mockftpserver.fake.filesystem
1760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
1860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.util.IoUtil
1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/**
2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Tests for subclasses of AbstractFakeFileSystem. Subclasses must define
2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$
2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair
2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairabstract class AbstractFakeFileSystemTest extends AbstractFileSystemTest {
2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // -------------------------------------------------------------------------
3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Tests
3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // -------------------------------------------------------------------------
3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testDefaultDirectoryListingFormatterClass() {
3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.directoryListingFormatter.class == expectedDirectoryListingFormatterClass
3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testAdd_PathLocked() {
3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def dirEntry = new DirectoryEntry(NEW_DIR)
3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.add(dirEntry)
4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def fileEntry = new FileEntry(NEW_FILE)
4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.add(fileEntry)
4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // The path should be locked for both entries
4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        shouldFail { dirEntry.setPath('abc') }
4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        shouldFail { fileEntry.setPath('abc') }
4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testAdd_Directory_CreateParentDirectoriesAutomatically() {
4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        final NEW_SUBDIR = fileSystem.path(NEW_DIR, "sub")
5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert !fileSystem.exists(NEW_DIR), "Before createDirectory"
5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert !fileSystem.exists(NEW_SUBDIR), "Before createDirectory"
5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.createParentDirectoriesAutomatically = true
5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.add(new DirectoryEntry(NEW_SUBDIR))
5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.exists(NEW_DIR), "After createDirectory"
5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.exists(NEW_SUBDIR), "$NEW_SUBDIR: After createDirectory"
5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testAdd_File_CreateParentDirectoriesAutomatically() {
6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        final NEW_FILE_IN_SUBDIR = fileSystem.path(NEW_DIR, "abc.txt")
6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert !fileSystem.exists(NEW_DIR), "Before createDirectory"
6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert !fileSystem.exists(NEW_FILE_IN_SUBDIR), "Before createDirectory"
6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.createParentDirectoriesAutomatically = true
6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.add(new FileEntry(NEW_FILE_IN_SUBDIR))
6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.exists(NEW_DIR), "After createDirectory"
6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.exists(NEW_FILE_IN_SUBDIR), "$NEW_FILE_IN_SUBDIR: After createDirectory"
6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testAdd_File_CreateParentDirectoriesAutomatically_False() {
7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.createParentDirectoriesAutomatically = false
7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        final NEW_FILE_IN_SUBDIR = fileSystem.path(NEW_DIR, "abc.txt")
7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert !fileSystem.exists(NEW_DIR), "Before createDirectory"
7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        shouldFail(FileSystemException) { fileSystem.add(new FileEntry(NEW_FILE_IN_SUBDIR)) }
7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert !fileSystem.exists(NEW_DIR), "After createDirectory"
7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testSetEntries() {
8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.createParentDirectoriesAutomatically = false
8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def entries = [new FileEntry(NEW_FILE), new DirectoryEntry(NEW_DIR)]
8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.setEntries(entries)
8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.exists(NEW_DIR)
8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.exists(NEW_FILE)
8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testToString() {
8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        String toString = fileSystem.toString()
8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        LOG.info("toString=" + toString)
9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert toString.contains(EXISTING_DIR)
9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert toString.contains(EXISTING_FILE)
9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testFormatDirectoryListing() {
9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def fileEntry = new FileEntry(path: 'abc')
9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def formatter = [format: {f ->
9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            assert f == fileEntry
9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            return 'abc'
9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        }] as DirectoryListingFormatter
10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.directoryListingFormatter = formatter
10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.formatDirectoryListing(fileEntry) == 'abc'
10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
10360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
10460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testFormatDirectoryListing_NullDirectoryListingFormatter() {
10560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.directoryListingFormatter = null
10660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def fileEntry = new FileEntry('abc')
10760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        shouldFailWithMessageContaining('directoryListingFormatter') { assert fileSystem.formatDirectoryListing(fileEntry) }
10860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
10960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
11060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testFormatDirectoryListing_NullFileSystemEntry() {
11160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def formatter = [format: {f -> }] as DirectoryListingFormatter
11260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.directoryListingFormatter = formatter
11360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        shouldFailWithMessageContaining('fileSystemEntry') { assert fileSystem.formatDirectoryListing(null) }
11460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
11560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
11660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testGetEntry() {
11760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.getEntry(NO_SUCH_DIR) == null
11860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.getEntry(NO_SUCH_FILE) == null
11960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
12060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.getEntry(EXISTING_FILE).path == EXISTING_FILE
12160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert fileSystem.getEntry(EXISTING_DIR).path == EXISTING_DIR
12260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
12360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def permissions = new Permissions('-wxrwx---')
12460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def fileEntry = new FileEntry(path: NEW_FILE, lastModified: DATE, contents: 'abc', owner: 'owner',
12560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair                group: 'group', permissions: permissions)
12660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.add(fileEntry)
12760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def entry = fileSystem.getEntry(NEW_FILE)
12860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        LOG.info(entry)
12960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert entry.path == NEW_FILE
13060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert !entry.directory
13160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert entry.size == 3
13260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert entry.owner == 'owner'
13360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert entry.group == 'group'
13460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert entry.permissions == permissions
13560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
13660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
13760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testNormalize_Null() {
13860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        shouldFailWithMessageContaining("path") { fileSystem.normalize(null) }
13960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
14060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
14160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testGetName_Null() {
14260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        shouldFailWithMessageContaining("path") { fileSystem.getName(null) }
14360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
14460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
14560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //--------------------------------------------------------------------------
14660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Abstract Methods
14760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //--------------------------------------------------------------------------
14860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
14960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected abstract Class getExpectedDirectoryListingFormatterClass()
15060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
15160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //--------------------------------------------------------------------------
15260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Internal Helper Methods
15360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //--------------------------------------------------------------------------
15460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
15560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
15660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Verify the contents of the file at the specified path read from its InputSteam
15760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     *
15860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @param fileSystem - the FileSystem instance
15960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @param expectedContents - the expected contents
16060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @throws IOException
16160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @see org.mockftpserver.fake.filesystem.AbstractFileSystemTest#verifyFileContents(FileSystem,
16260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * String , String )
16360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
16460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected void verifyFileContents(FileSystem fileSystem, String path, String expectedContents) throws IOException {
16560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def fileEntry = fileSystem.getEntry(path)
16660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        InputStream input = fileEntry.createInputStream()
16760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        byte[] bytes = IoUtil.readBytes(input)
16860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        LOG.info("bytes=[" + new String(bytes) + "]")
16960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertEquals("contents: actual=[" + new String(bytes) + "]", expectedContents.getBytes(), bytes)
17060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
17160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
17260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}