blob: 440c9d5882b4b351c6422599f7ab462f6f558a85 [file] [log] [blame]
chrismair00dc7bd2014-05-11 21:21:28 +00001/*
2 * Copyright 2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.fake.filesystem
17
18import org.mockftpserver.core.util.IoUtil
19
20/**
21 * Tests for subclasses of AbstractFakeFileSystem. Subclasses must define
22 *
23 * @version $Revision$ - $Date$
24 *
25 * @author Chris Mair
26 */
27abstract class AbstractFakeFileSystemTestCase extends AbstractFileSystemTestCase {
28
29 // -------------------------------------------------------------------------
30 // Tests
31 // -------------------------------------------------------------------------
32
33 void testDefaultDirectoryListingFormatterClass() {
34 assert fileSystem.directoryListingFormatter.class == expectedDirectoryListingFormatterClass
35 }
36
37 void testAdd_PathLocked() {
38 def dirEntry = new DirectoryEntry(NEW_DIR)
39 fileSystem.add(dirEntry)
40 def fileEntry = new FileEntry(NEW_FILE)
41 fileSystem.add(fileEntry)
42
43 // The path should be locked for both entries
44 shouldFail { dirEntry.setPath('abc') }
45 shouldFail { fileEntry.setPath('abc') }
46 }
47
48 void testAdd_Directory_CreateParentDirectoriesAutomatically() {
49 final NEW_SUBDIR = fileSystem.path(NEW_DIR, "sub")
50 assert !fileSystem.exists(NEW_DIR), "Before createDirectory"
51 assert !fileSystem.exists(NEW_SUBDIR), "Before createDirectory"
52
53 fileSystem.createParentDirectoriesAutomatically = true
54 fileSystem.add(new DirectoryEntry(NEW_SUBDIR))
55 assert fileSystem.exists(NEW_DIR), "After createDirectory"
56 assert fileSystem.exists(NEW_SUBDIR), "$NEW_SUBDIR: After createDirectory"
57 }
58
59 void testAdd_File_CreateParentDirectoriesAutomatically() {
60 final NEW_FILE_IN_SUBDIR = fileSystem.path(NEW_DIR, "abc.txt")
61 assert !fileSystem.exists(NEW_DIR), "Before createDirectory"
62 assert !fileSystem.exists(NEW_FILE_IN_SUBDIR), "Before createDirectory"
63
64 fileSystem.createParentDirectoriesAutomatically = true
65 fileSystem.add(new FileEntry(NEW_FILE_IN_SUBDIR))
66 assert fileSystem.exists(NEW_DIR), "After createDirectory"
67 assert fileSystem.exists(NEW_FILE_IN_SUBDIR), "$NEW_FILE_IN_SUBDIR: After createDirectory"
68 }
69
70 void testAdd_File_CreateParentDirectoriesAutomatically_False() {
71 fileSystem.createParentDirectoriesAutomatically = false
72 final NEW_FILE_IN_SUBDIR = fileSystem.path(NEW_DIR, "abc.txt")
73 assert !fileSystem.exists(NEW_DIR), "Before createDirectory"
74
75 shouldFail(FileSystemException) { fileSystem.add(new FileEntry(NEW_FILE_IN_SUBDIR)) }
76 assert !fileSystem.exists(NEW_DIR), "After createDirectory"
77 }
78
79 void testSetEntries() {
80 fileSystem.createParentDirectoriesAutomatically = false
81 def entries = [new FileEntry(NEW_FILE), new DirectoryEntry(NEW_DIR)]
82 fileSystem.setEntries(entries)
83 assert fileSystem.exists(NEW_DIR)
84 assert fileSystem.exists(NEW_FILE)
85 }
86
87 void testToString() {
88 String toString = fileSystem.toString()
89 LOG.info("toString=" + toString)
90 assert toString.contains(EXISTING_DIR)
91 assert toString.contains(EXISTING_FILE)
92 }
93
94 void testFormatDirectoryListing() {
95 def fileEntry = new FileEntry(path: 'abc')
96 def formatter = [format: {f ->
97 assert f == fileEntry
98 return 'abc'
99 }] as DirectoryListingFormatter
100 fileSystem.directoryListingFormatter = formatter
101 assert fileSystem.formatDirectoryListing(fileEntry) == 'abc'
102 }
103
104 void testFormatDirectoryListing_NullDirectoryListingFormatter() {
105 fileSystem.directoryListingFormatter = null
106 def fileEntry = new FileEntry('abc')
107 shouldFailWithMessageContaining('directoryListingFormatter') { assert fileSystem.formatDirectoryListing(fileEntry) }
108 }
109
110 void testFormatDirectoryListing_NullFileSystemEntry() {
111 def formatter = [format: {f -> }] as DirectoryListingFormatter
112 fileSystem.directoryListingFormatter = formatter
113 shouldFailWithMessageContaining('fileSystemEntry') { assert fileSystem.formatDirectoryListing(null) }
114 }
115
116 void testGetEntry() {
117 assert fileSystem.getEntry(NO_SUCH_DIR) == null
118 assert fileSystem.getEntry(NO_SUCH_FILE) == null
119
120 assert fileSystem.getEntry(EXISTING_FILE).path == EXISTING_FILE
121 assert fileSystem.getEntry(EXISTING_DIR).path == EXISTING_DIR
122
123 def permissions = new Permissions('-wxrwx---')
124 def fileEntry = new FileEntry(path: NEW_FILE, lastModified: DATE, contents: 'abc', owner: 'owner',
125 group: 'group', permissions: permissions)
126 fileSystem.add(fileEntry)
127 def entry = fileSystem.getEntry(NEW_FILE)
128 LOG.info(entry.toString())
129 assert entry.path == NEW_FILE
130 assert !entry.directory
131 assert entry.size == 3
132 assert entry.owner == 'owner'
133 assert entry.group == 'group'
134 assert entry.permissions == permissions
135 }
136
137 void testNormalize_Null() {
138 shouldFailWithMessageContaining("path") { fileSystem.normalize(null) }
139 }
140
141 void testGetName_Null() {
142 shouldFailWithMessageContaining("path") { fileSystem.getName(null) }
143 }
144
145 //--------------------------------------------------------------------------
146 // Abstract Methods
147 //--------------------------------------------------------------------------
148
149 protected abstract Class getExpectedDirectoryListingFormatterClass()
150
151 //--------------------------------------------------------------------------
152 // Internal Helper Methods
153 //--------------------------------------------------------------------------
154
155 /**
156 * Verify the contents of the file at the specified path read from its InputSteam
157 *
158 * @param fileSystem - the FileSystem instance
159 * @param expectedContents - the expected contents
160 * @throws IOException
161 * @see org.mockftpserver.fake.filesystem.AbstractFileSystemTestCase#verifyFileContents(FileSystem,String,String )
162 */
163 protected void verifyFileContents(FileSystem fileSystem, String path, String expectedContents) throws IOException {
164 def fileEntry = fileSystem.getEntry(path)
165 InputStream input = fileEntry.createInputStream()
166 byte[] bytes = IoUtil.readBytes(input)
167 LOG.info("bytes=[" + new String(bytes) + "]")
168 assertEquals("contents: actual=[" + new String(bytes) + "]", expectedContents.getBytes(), bytes)
169 }
170
171}