1b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair/*
2b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * Copyright 2008 the original author or authors.
3b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair *
4b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * you may not use this file except in compliance with the License.
6b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * You may obtain a copy of the License at
7b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair *
8b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair *
10b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * Unless required by applicable law or agreed to in writing, software
11b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * See the License for the specific language governing permissions and
14b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * limitations under the License.
15b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair */
16b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairpackage org.mockftpserver.fake.filesystem
17b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
18b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairimport org.apache.log4j.Logger
19b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairimport org.mockftpserver.core.util.IoUtil
20b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
21b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair/**
22b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * Tests for FileEntry
23b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair *
24b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * @version $Revision$ - $Date$
25b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair *
26b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * @author Chris Mair
27b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair */
28b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairpublic class FileEntryTest extends AbstractFileSystemEntryTest {
29b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
30b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    private static final LOG = Logger.getLogger(FileEntryTest)
31b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    private static final CONTENTS = "abc 123 %^& xxx"
32b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
33b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    private FileEntry entry
34b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
35b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void testConstructorWithStringContents() {
36b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry = new FileEntry(PATH, CONTENTS)
37b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(CONTENTS)
38b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
39b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
40b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void testSettingContentsFromString() {
41b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.setContents(CONTENTS)
42b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(CONTENTS)
43b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
44b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
45b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void testSettingContentsFromBytes() {
46b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        byte[] contents = CONTENTS.getBytes()
47b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.setContents(contents)
48b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        // Now corrupt the original byte array to make sure the file entry is not affected
49b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        contents[1] = (byte) '#'
50b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(CONTENTS)
51b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
52b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
53b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void testSetContents_NullString() {
54b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.setContents((String) null)
55b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert entry.size == 0
56b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
57b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
58b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void testSetContents_NullBytes() {
59b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.setContents((byte[]) null)
60b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert entry.size == 0
61b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
62b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
63b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void testCreateOutputStream() {
64b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        // New, empty file
65b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        OutputStream out = entry.createOutputStream(false)
66b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out.write(CONTENTS.getBytes())
67b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(CONTENTS)
68b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
69b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        // Another OutputStream, append=false
70b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out = entry.createOutputStream(false)
71b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out.write(CONTENTS.getBytes())
72b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(CONTENTS)
73b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
74b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        // Another OutputStream, append=true
75b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out = entry.createOutputStream(true)
76b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out.write(CONTENTS.getBytes())
77b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(CONTENTS + CONTENTS)
78b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
79b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        // Set contents directly
80b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        final String NEW_CONTENTS = ",./'\t\r[]-\n="
81b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.setContents(NEW_CONTENTS)
82b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(NEW_CONTENTS)
83b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
84b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        // New OutputStream, append=true (so should append to contents we set directly)
85b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out = entry.createOutputStream(true)
86b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out.write(CONTENTS.getBytes())
87b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(NEW_CONTENTS + CONTENTS)
88b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
89b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        // Yet another OutputStream, append=true (so should append to accumulated contents)
90b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        OutputStream out2 = entry.createOutputStream(true)
91b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out2.write(CONTENTS.getBytes())
92b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out2.close()       // should have no effect
93b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(NEW_CONTENTS + CONTENTS + CONTENTS)
94b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
95b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        // Write with the previous OutputStream (simulate 2 OututStreams writing "concurrently")
96b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        out.write(NEW_CONTENTS.getBytes())
97b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents(NEW_CONTENTS + CONTENTS + CONTENTS + NEW_CONTENTS)
98b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
99b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
100b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void testCreateInputStream_NullContents() {
101b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        verifyContents("")
102b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
103b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
104b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void testCloneWithNewPath() {
105b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.lastModified = LAST_MODIFIED
106b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.owner = USER
107b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.group = GROUP
108b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.permissions = PERMISSIONS
109b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry.setContents('abc')
110b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        def clone = entry.cloneWithNewPath(NEW_PATH)
111b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
112b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert !clone.is(entry)
113b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert clone.path == NEW_PATH
114b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert clone.lastModified == LAST_MODIFIED
115b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert clone.owner == USER
116b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert clone.group == GROUP
117b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert clone.permissions == PERMISSIONS
118b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert clone.createInputStream().text == 'abc'
119b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert !clone.directory
120b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
121b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
122b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void testCloneWithNewPath_WriteToOutputStream() {
123b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        def outputStream = entry.createOutputStream(false)
124b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        outputStream.withWriter { writer -> writer.write('ABCDEF') }
125b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        def clone = entry.cloneWithNewPath(NEW_PATH)
126b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
127b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert !clone.is(entry)
128b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert clone.path == NEW_PATH
129b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert clone.createInputStream().text == 'ABCDEF'
130b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert !clone.directory
131b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
132b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
133b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//    void testEquals() {
134b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert entry.equals(entry)
135b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert entry.equals(new FileEntry(path:PATH, lastModified:LAST_MODIFIED))
136b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert entry.equals(new FileEntry(path:PATH, lastModified:new Date())) // lastModified ignored
137b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//
138b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert !entry.equals(new FileEntry("xyz", lastModified:LAST_MODIFIED))
139b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert !entry.equals(new FileEntry(path:PATH, contents:'abc', lastModified:LAST_MODIFIED))
140b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert !entry.equals("ABC")
141b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert !entry.equals(null)
142b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//    }
143b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//
144b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//    void testHashCode() {
145b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert entry.hashCode() == entry.hashCode()
146b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert entry.hashCode() == new FileEntry(path:PATH, contents:'abc', lastModified:LAST_MODIFIED).hashCode()
147b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert entry.hashCode() == new FileEntry(path:PATH, contents:'abc', new Date()).hashCode()  // lastModified ignored
148b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//
149b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert entry.hashCode() != new FileEntry(path:PATH, contents:'abc', lastModified:LAST_MODIFIED).hashCode()
150b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert entry.hashCode() != new FileEntry(path:PATH, contents:'abcdef', lastModified:LAST_MODIFIED).hashCode()
151b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//
152b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//        assert entry.hashCode() == new DirectoryEntry(path:PATH, lastModified:LAST_MODIFIED).hashCode()
153b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair//    }
154b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
155b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    //-------------------------------------------------------------------------
156b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    // Implementation of Required Abstract Methods
157b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    //-------------------------------------------------------------------------
158b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
159b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    /**
160b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     * @see org.mockftpserver.fake.filesystem.AbstractFileSystemEntryTest#getImplementationClass()
161b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     */
162b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    protected Class getImplementationClass() {
163b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        return FileEntry.class
164b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
165b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
166b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    /**
167b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     * @see org.mockftpserver.fake.filesystem.AbstractFileSystemEntryTest#isDirectory()
168b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     */
169b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    protected boolean isDirectory() {
170b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        return false
171b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
172b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
173b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    //-------------------------------------------------------------------------
174b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    // Test setup
175b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    //-------------------------------------------------------------------------
176b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
177b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    /**
178b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     * @see org.mockftpserver.test.AbstractTest#setUp()
179b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     */
180b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    void setUp() {
181b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        super.setUp()
182b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        entry = new FileEntry(PATH)
183b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
184b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
185b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    //-------------------------------------------------------------------------
186b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    // Internal Helper Methods
187b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    //-------------------------------------------------------------------------
188b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
189b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    /**
190b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     * Verify the expected contents of the file entry, read from its InputSteam
191b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     * @param expectedContents - the expected contents
192b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     * @throws IOException
193b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair     */
194b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    private void verifyContents(String expectedContents) {
195b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        byte[] bytes = IoUtil.readBytes(entry.createInputStream())
196b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        LOG.info("bytes=[" + new String(bytes) + "]")
197b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assertEquals("contents: actual=[" + new String(bytes) + "]", expectedContents.getBytes(), bytes)
198b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        assert entry.getSize() == expectedContents.length()
199b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
200b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
201b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair}
202