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 java.text.SimpleDateFormat
19import org.mockftpserver.test.AbstractGroovyTest
20
21/**
22 * Tests for UnixDirectoryListingFormatter
23 *
24 * @version $Revision$ - $Date$
25 *
26 * @author Chris Mair
27 */
28class UnixDirectoryListingFormatterTest extends AbstractGroovyTest {
29
30    static final FILE_NAME = "def.txt"
31    static final FILE_PATH = "/dir/$FILE_NAME"
32    static final DIR_NAME = "etc"
33    static final DIR_PATH = "/dir/$DIR_NAME"
34    static final OWNER = 'owner123'
35    static final GROUP = 'group456'
36    static final SIZE = 11L
37    static final LAST_MODIFIED = new Date()
38    static final FILE_PERMISSIONS = new Permissions('rw-r--r--')
39    static final DIR_PERMISSIONS = new Permissions('rwxr-xr-x')
40
41    private formatter
42    private dateFormat
43    private lastModifiedFormatted
44
45    // "-rw-rw-r--    1 ftp      ftp           254 Feb 23  2007 robots.txt"
46    // "-rw-r--r--    1 ftp      ftp      30014925 Apr 15 00:19 md5.sums.gz"
47    // "-rwxr-xr-x   1 c096336  iawebgrp    5778 Dec  1  2005 FU_WyCONN_updateplanaccess.sql"
48    // "drwxr-xr-x   2 c096336  iawebgrp    8192 Nov  7  2006 tmp"
49    // "drwxr-xr-x   39 ftp      ftp          4096 Mar 19  2004 a"
50
51    void testFormat_File() {
52        def fileSystemEntry = new FileEntry(path: FILE_PATH, contents: '12345678901', lastModified: LAST_MODIFIED,
53                owner: OWNER, group: GROUP, permissions: FILE_PERMISSIONS)
54        LOG.info(fileSystemEntry)
55        verifyFormat(fileSystemEntry, "-rw-r--r--  1 owner123 group456              11 $lastModifiedFormatted def.txt")
56    }
57
58    void testFormat_File_Defaults() {
59        def fileSystemEntry = new FileEntry(path: FILE_PATH, contents: '12345678901', lastModified: LAST_MODIFIED)
60        LOG.info(fileSystemEntry)
61        verifyFormat(fileSystemEntry, "-rwxrwxrwx  1 none     none                  11 $lastModifiedFormatted def.txt")
62    }
63
64    void testFormat_Directory() {
65        def fileSystemEntry = new DirectoryEntry(path: DIR_PATH, lastModified: LAST_MODIFIED,
66                owner: OWNER, group: GROUP, permissions: DIR_PERMISSIONS)
67        LOG.info(fileSystemEntry)
68        verifyFormat(fileSystemEntry, "drwxr-xr-x  1 owner123 group456               0 $lastModifiedFormatted etc")
69    }
70
71    void testFormat_Directory_Defaults() {
72        def fileSystemEntry = new DirectoryEntry(path: DIR_PATH, lastModified: LAST_MODIFIED)
73        LOG.info(fileSystemEntry)
74        verifyFormat(fileSystemEntry, "drwxrwxrwx  1 none     none                   0 $lastModifiedFormatted etc")
75    }
76
77    void setUp() {
78        super.setUp()
79        formatter = new UnixDirectoryListingFormatter()
80        dateFormat = new SimpleDateFormat(UnixDirectoryListingFormatter.DATE_FORMAT)
81        lastModifiedFormatted = dateFormat.format(LAST_MODIFIED)
82    }
83
84    private void verifyFormat(FileSystemEntry fileSystemEntry, String expectedResult) {
85        def result = formatter.format(fileSystemEntry)
86        LOG.info("result=  [$result]")
87        LOG.info("expected=[$expectedResult]")
88        assert result == expectedResult
89    }
90
91}