FileEntryTest.groovy revision 09415075d96852489d096aea7ae07dd156b58643
1/*
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.apache.log4j.Logger
19import org.mockftpserver.core.util.IoUtil
20
21/**
22 * Tests for FileEntry
23 *
24 * @version $Revision$ - $Date$
25 *
26 * @author Chris Mair
27 */
28public class FileEntryTest extends AbstractFileSystemEntryTest {
29
30    private static final LOG = Logger.getLogger(FileEntryTest)
31    private static final CONTENTS = "abc 123 %^& xxx"
32
33    private FileEntry entry
34
35    void testConstructorWithStringContents() {
36        entry = new FileEntry(PATH, CONTENTS)
37        verifyContents(CONTENTS)
38    }
39
40    void testSettingContentsFromString() {
41        entry.setContents(CONTENTS)
42        verifyContents(CONTENTS)
43    }
44
45    void testSettingContentsFromBytes() {
46        byte[] contents = CONTENTS.getBytes()
47        entry.setContents(contents)
48        // Now corrupt the original byte array to make sure the file entry is not affected
49        contents[1] = (byte) '#'
50        verifyContents(CONTENTS)
51    }
52
53    void testSetContents_NullString() {
54        shouldFailWithMessageContaining("contents") { entry.setContents((String) null) }
55    }
56
57    void testSetContents_NullBytes() {
58        shouldFailWithMessageContaining("contents") { entry.setContents((byte[]) null) }
59    }
60
61    void testCreateOutputStream() {
62        // New, empty file
63        OutputStream out = entry.createOutputStream(false)
64        out.write(CONTENTS.getBytes())
65        verifyContents(CONTENTS)
66
67        // Another OutputStream, append=false
68        out = entry.createOutputStream(false)
69        out.write(CONTENTS.getBytes())
70        verifyContents(CONTENTS)
71
72        // Another OutputStream, append=true
73        out = entry.createOutputStream(true)
74        out.write(CONTENTS.getBytes())
75        verifyContents(CONTENTS + CONTENTS)
76
77        // Set contents directly
78        final String NEW_CONTENTS = ",./'\t\r[]-\n="
79        entry.setContents(NEW_CONTENTS)
80        verifyContents(NEW_CONTENTS)
81
82        // New OutputStream, append=true (so should append to contents we set directly)
83        out = entry.createOutputStream(true)
84        out.write(CONTENTS.getBytes())
85        verifyContents(NEW_CONTENTS + CONTENTS)
86
87        // Yet another OutputStream, append=true (so should append to accumulated contents)
88        OutputStream out2 = entry.createOutputStream(true)
89        out2.write(CONTENTS.getBytes())
90        out2.close()       // should have no effect
91        verifyContents(NEW_CONTENTS + CONTENTS + CONTENTS)
92
93        // Write with the previous OutputStream (simulate 2 OututStreams writing "concurrently")
94        out.write(NEW_CONTENTS.getBytes())
95        verifyContents(NEW_CONTENTS + CONTENTS + CONTENTS + NEW_CONTENTS)
96    }
97
98    void testCreateInputStream_NullContents() {
99        verifyContents("")
100    }
101
102    void testCloneWithNewPath() {
103        entry.lastModified = LAST_MODIFIED
104        entry.owner = USER
105        entry.group = GROUP
106        entry.permissions = PERMISSIONS
107        entry.setContents('abc')
108        def clone = entry.cloneWithNewPath(NEW_PATH)
109
110        assert !clone.is(entry)
111        assert clone.path == NEW_PATH
112        assert clone.lastModified == LAST_MODIFIED
113        assert clone.owner == USER
114        assert clone.group == GROUP
115        assert clone.permissions == PERMISSIONS
116        assert Arrays.equals(clone.bytes, entry.bytes)
117        assert !clone.directory
118    }
119
120    //-------------------------------------------------------------------------
121    // Implementation of Required Abstract Methods
122    //-------------------------------------------------------------------------
123
124    /**
125     * @see org.mockftpserver.fake.filesystem.AbstractFileSystemEntryTest#getImplementationClass()
126     */
127    protected Class getImplementationClass() {
128        return FileEntry.class
129    }
130
131    /**
132     * @see org.mockftpserver.fake.filesystem.AbstractFileSystemEntryTest#isDirectory()
133     */
134    protected boolean isDirectory() {
135        return false
136    }
137
138    //-------------------------------------------------------------------------
139    // Test setup
140    //-------------------------------------------------------------------------
141
142    /**
143     * @see org.mockftpserver.test.AbstractTest#setUp()
144     */
145    void setUp() {
146        super.setUp()
147        entry = new FileEntry(PATH)
148    }
149
150    //-------------------------------------------------------------------------
151    // Internal Helper Methods
152    //-------------------------------------------------------------------------
153
154    /**
155     * Verify the expected contents of the file entry, read from its InputSteam
156     * @param expectedContents - the expected contents
157     * @throws IOException
158     */
159    private void verifyContents(String expectedContents) {
160        byte[] bytes = IoUtil.readBytes(entry.createInputStream())
161        LOG.info("bytes=[" + new String(bytes) + "]")
162        assertEquals("contents: actual=[" + new String(bytes) + "]", expectedContents.getBytes(), bytes)
163        assert entry.getSize() == expectedContents.length()
164    }
165
166}
167