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.mockftpserver.test.AbstractGroovyTestCase
19
20/**
21 * Tests for the Permissions class
22 *
23 * @version $Revision$ - $Date$
24 *
25 * @author Chris Mair
26 */
27class PermissionsTest extends AbstractGroovyTestCase {
28
29    void testConstructor() {
30        testConstructorWithValidString('rwxrwxrwx')
31        testConstructorWithValidString('rwxr--r--')
32        testConstructorWithValidString('---------')
33    }
34
35    void testConstructor_InvalidString() {
36        testConstructorWithInvalidString('')
37        testConstructorWithInvalidString('------')
38        testConstructorWithInvalidString('-')
39        testConstructorWithInvalidString('r')
40        testConstructorWithInvalidString('rwx')
41        testConstructorWithInvalidString('rwxrwxrw')
42        testConstructorWithInvalidString('123456789')
43        testConstructorWithInvalidString('rwxrZxrwx')
44        testConstructorWithInvalidString('--------Z')
45    }
46
47    void testCanReadWriteExecute() {
48        testCanReadWriteExecute('rwxrwxrwx', true, true, true, true, true, true, true, true, true)
49        testCanReadWriteExecute('r--r--r--', true, false, false, true, false, false, true, false, false)
50        testCanReadWriteExecute('-w-r----x', false, true, false, true, false, false, false, false, true)
51        testCanReadWriteExecute('---------', false, false, false, false, false, false, false, false, false)
52    }
53
54    void testHashCode() {
55        assert new Permissions('rwxrwxrwx').hashCode() == Permissions.DEFAULT.hashCode()
56        assert new Permissions('---------').hashCode() == Permissions.NONE.hashCode()
57    }
58
59    void testEquals() {
60        assert new Permissions('rwxrwxrwx').equals(Permissions.DEFAULT)
61        assert new Permissions('---------').equals(Permissions.NONE)
62        assert Permissions.NONE.equals(Permissions.NONE)
63
64        assert !(new Permissions('------rwx').equals(Permissions.NONE))
65        assert !Permissions.NONE.equals(null)
66        assert !Permissions.NONE.equals(123)
67    }
68
69    //--------------------------------------------------------------------------
70    // Helper Methods
71    //--------------------------------------------------------------------------
72
73    private testCanReadWriteExecute(rwxString,
74                                    canUserRead, canUserWrite, canUserExecute,
75                                    canGroupRead, canGroupWrite, canGroupExecute,
76                                    canWorldRead, canWorldWrite, canWorldExecute) {
77
78        def permissions = new Permissions(rwxString)
79        LOG.info("Testing can read/write/execute for $permissions")
80        assert permissions.canUserRead() == canUserRead
81        assert permissions.canUserWrite() == canUserWrite
82        assert permissions.canUserExecute() == canUserExecute
83        assert permissions.canGroupRead() == canGroupRead
84        assert permissions.canGroupWrite() == canGroupWrite
85        assert permissions.canGroupExecute() == canGroupExecute
86        assert permissions.canWorldRead() == canWorldRead
87        assert permissions.canWorldWrite() == canWorldWrite
88        assert permissions.canWorldExecute() == canWorldExecute
89    }
90
91    private testConstructorWithInvalidString(String string) {
92        LOG.info("Verifying invalid: [$string]")
93        shouldFail { new Permissions(string) }
94    }
95
96    private testConstructorWithValidString(String string) {
97        LOG.info("Verifying valid: [$string]")
98        def permissions = new Permissions(string)
99        LOG.info(permissions)
100        assert permissions.asRwxString() == string
101    }
102}