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.apache.log4j.Logger
1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.util.IoUtil
2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/**
2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Tests for FileEntry
2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$
2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair
2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpublic class FileEntryTest extends AbstractFileSystemEntryTest {
2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final LOG = Logger.getLogger(FileEntryTest)
3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final CONTENTS = "abc 123 %^& xxx"
3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private FileEntry entry
3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testConstructorWithStringContents() {
3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry = new FileEntry(PATH, CONTENTS)
3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(CONTENTS)
3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testSettingContentsFromString() {
4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.setContents(CONTENTS)
4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(CONTENTS)
4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testSettingContentsFromBytes() {
4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        byte[] contents = CONTENTS.getBytes()
4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.setContents(contents)
4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // Now corrupt the original byte array to make sure the file entry is not affected
4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        contents[1] = (byte) '#'
5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(CONTENTS)
5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testSetContents_NullString() {
5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.setContents((String) null)
5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert entry.size == 0
5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testSetContents_NullBytes() {
5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.setContents((byte[]) null)
6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert entry.size == 0
6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testCreateOutputStream() {
6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // New, empty file
6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        OutputStream out = entry.createOutputStream(false)
6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out.write(CONTENTS.getBytes())
6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(CONTENTS)
6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // Another OutputStream, append=false
7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out = entry.createOutputStream(false)
7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out.write(CONTENTS.getBytes())
7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(CONTENTS)
7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // Another OutputStream, append=true
7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out = entry.createOutputStream(true)
7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out.write(CONTENTS.getBytes())
7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(CONTENTS + CONTENTS)
7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // Set contents directly
8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        final String NEW_CONTENTS = ",./'\t\r[]-\n="
8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.setContents(NEW_CONTENTS)
8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(NEW_CONTENTS)
8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // New OutputStream, append=true (so should append to contents we set directly)
8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out = entry.createOutputStream(true)
8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out.write(CONTENTS.getBytes())
8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(NEW_CONTENTS + CONTENTS)
8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // Yet another OutputStream, append=true (so should append to accumulated contents)
9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        OutputStream out2 = entry.createOutputStream(true)
9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out2.write(CONTENTS.getBytes())
9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out2.close()       // should have no effect
9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(NEW_CONTENTS + CONTENTS + CONTENTS)
9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // Write with the previous OutputStream (simulate 2 OututStreams writing "concurrently")
9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        out.write(NEW_CONTENTS.getBytes())
9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents(NEW_CONTENTS + CONTENTS + CONTENTS + NEW_CONTENTS)
9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testCreateInputStream_NullContents() {
10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyContents("")
10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
10360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
10460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testCloneWithNewPath() {
10560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.lastModified = LAST_MODIFIED
10660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.owner = USER
10760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.group = GROUP
10860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.permissions = PERMISSIONS
10960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry.setContents('abc')
11060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def clone = entry.cloneWithNewPath(NEW_PATH)
11160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
11260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert !clone.is(entry)
11360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert clone.path == NEW_PATH
11460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert clone.lastModified == LAST_MODIFIED
11560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert clone.owner == USER
11660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert clone.group == GROUP
11760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert clone.permissions == PERMISSIONS
11860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert Arrays.equals(clone.bytes, entry.bytes)
11960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert !clone.directory
12060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
12160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
12260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//    void testEquals() {
12360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert entry.equals(entry)
12460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert entry.equals(new FileEntry(path:PATH, lastModified:LAST_MODIFIED))
12560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert entry.equals(new FileEntry(path:PATH, lastModified:new Date())) // lastModified ignored
12660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//
12760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert !entry.equals(new FileEntry("xyz", lastModified:LAST_MODIFIED))
12860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert !entry.equals(new FileEntry(path:PATH, contents:'abc', lastModified:LAST_MODIFIED))
12960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert !entry.equals("ABC")
13060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert !entry.equals(null)
13160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//    }
13260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//
13360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//    void testHashCode() {
13460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert entry.hashCode() == entry.hashCode()
13560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert entry.hashCode() == new FileEntry(path:PATH, contents:'abc', lastModified:LAST_MODIFIED).hashCode()
13660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert entry.hashCode() == new FileEntry(path:PATH, contents:'abc', new Date()).hashCode()  // lastModified ignored
13760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//
13860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert entry.hashCode() != new FileEntry(path:PATH, contents:'abc', lastModified:LAST_MODIFIED).hashCode()
13960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert entry.hashCode() != new FileEntry(path:PATH, contents:'abcdef', lastModified:LAST_MODIFIED).hashCode()
14060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//
14160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//        assert entry.hashCode() == new DirectoryEntry(path:PATH, lastModified:LAST_MODIFIED).hashCode()
14260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//    }
14360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
14460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
14560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Implementation of Required Abstract Methods
14660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
14760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
14860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
14960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @see org.mockftpserver.fake.filesystem.AbstractFileSystemEntryTest#getImplementationClass()
15060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
15160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected Class getImplementationClass() {
15260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        return FileEntry.class
15360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
15460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
15560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
15660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @see org.mockftpserver.fake.filesystem.AbstractFileSystemEntryTest#isDirectory()
15760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
15860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected boolean isDirectory() {
15960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        return false
16060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
16160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
16260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
16360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Test setup
16460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
16560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
16660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
16760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @see org.mockftpserver.test.AbstractTest#setUp()
16860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
16960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void setUp() {
17060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        super.setUp()
17160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        entry = new FileEntry(PATH)
17260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
17360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
17460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
17560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Internal Helper Methods
17660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
17760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
17860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
17960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Verify the expected contents of the file entry, read from its InputSteam
18060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @param expectedContents - the expected contents
18160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @throws IOException
18260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
18360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private void verifyContents(String expectedContents) {
18460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        byte[] bytes = IoUtil.readBytes(entry.createInputStream())
18560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        LOG.info("bytes=[" + new String(bytes) + "]")
18660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertEquals("contents: actual=[" + new String(bytes) + "]", expectedContents.getBytes(), bytes)
18760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert entry.getSize() == expectedContents.length()
18860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
18960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
19060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}
191