blob: fb10e5afde903f2264082e07697a22d66639ee09 [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 java.text.SimpleDateFormat
19import org.mockftpserver.test.AbstractGroovyTestCase
20
21/**
22 * Tests for UnixDirectoryListingFormatter
23 *
24 * @version $Revision$ - $Date$
25 *
26 * @author Chris Mair
27 */
28class UnixDirectoryListingFormatterTest extends AbstractGroovyTestCase {
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 lastModifiedFormatted
43 private defaultLocale
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.toString())
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.toString())
61 verifyFormat(fileSystemEntry, "-rwxrwxrwx 1 none none 11 $lastModifiedFormatted def.txt")
62 }
63
64 void testFormat_File_NonEnglishDefaultLocale() {
65 Locale.setDefault(Locale.GERMAN)
66 def fileSystemEntry = new FileEntry(path: FILE_PATH, contents: '12345678901', lastModified: LAST_MODIFIED)
67 LOG.info(fileSystemEntry.toString())
68 verifyFormat(fileSystemEntry, "-rwxrwxrwx 1 none none 11 $lastModifiedFormatted def.txt")
69 }
70
71 void testFormat_File_NonEnglishLocale() {
72 formatter.setLocale(Locale.FRENCH)
73 def fileSystemEntry = new FileEntry(path: FILE_PATH, contents: '12345678901', lastModified: LAST_MODIFIED)
74 LOG.info(fileSystemEntry.toString())
75 def dateFormat = new SimpleDateFormat(UnixDirectoryListingFormatter.DATE_FORMAT, Locale.FRENCH)
76 def formattedDate = dateFormat.format(LAST_MODIFIED)
77 def result = formatter.format(fileSystemEntry)
78 assert result.contains(formattedDate)
79 }
80
81 void testFormat_Directory() {
82 def fileSystemEntry = new DirectoryEntry(path: DIR_PATH, lastModified: LAST_MODIFIED,
83 owner: OWNER, group: GROUP, permissions: DIR_PERMISSIONS)
84 LOG.info(fileSystemEntry.toString())
85 verifyFormat(fileSystemEntry, "drwxr-xr-x 1 owner123 group456 0 $lastModifiedFormatted etc")
86 }
87
88 void testFormat_Directory_Defaults() {
89 def fileSystemEntry = new DirectoryEntry(path: DIR_PATH, lastModified: LAST_MODIFIED)
90 LOG.info(fileSystemEntry.toString())
91 verifyFormat(fileSystemEntry, "drwxrwxrwx 1 none none 0 $lastModifiedFormatted etc")
92 }
93
94 void setUp() {
95 super.setUp()
96 formatter = new UnixDirectoryListingFormatter()
97 def dateFormat = new SimpleDateFormat(UnixDirectoryListingFormatter.DATE_FORMAT, Locale.ENGLISH)
98 lastModifiedFormatted = dateFormat.format(LAST_MODIFIED)
99 defaultLocale = Locale.default
100 }
101
102 void tearDown() {
103 super.tearDown()
104 Locale.setDefault(defaultLocale)
105 }
106
107 private void verifyFormat(FileSystemEntry fileSystemEntry, String expectedResult) {
108 def result = formatter.format(fileSystemEntry)
109 LOG.info("result= [$result]")
110 LOG.info("expected=[$expectedResult]")
111 assert result == expectedResult
112 }
113
114}