Coverage Report - org.mockftpserver.fake.filesystem.Permissions
 
Classes in this File Line Coverage Branch Coverage Complexity
Permissions
95%
23/24
100%
26/26
1.133
 
 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  
  */
 16  
 package org.mockftpserver.fake.filesystem;
 17  
 
 18  
 import org.mockftpserver.core.util.Assert;
 19  
 
 20  
 /**
 21  
  * Represents and encapsulates the read/write/execute permissions for a file or directory.
 22  
  * This is conceptually (and somewhat loosely) based on the permissions flags within the Unix
 23  
  * file system. An instance of this class is immutable.
 24  
  *
 25  
  * @author Chris Mair
 26  
  * @version $Revision: 182 $ - $Date: 2008-11-30 21:37:49 -0500 (Sun, 30 Nov 2008) $
 27  
  */
 28  
 public class Permissions {
 29  1
     public static final Permissions ALL = new Permissions("rwxrwxrwx");
 30  1
     public static final Permissions NONE = new Permissions("---------");
 31  1
     public static final Permissions DEFAULT = ALL;
 32  
 
 33  
     private static final char READ_CHAR = 'r';
 34  
     private static final char WRITE_CHAR = 'w';
 35  
     private static final char EXECUTE_CHAR = 'x';
 36  
 
 37  
     private String rwxString;
 38  
 
 39  
     /**
 40  
      * Costruct a new instance for the specified read/write/execute specification String
 41  
      *
 42  
      * @param rwxString - the read/write/execute specification String; must be 9 characters long, with chars
 43  
      *                  at index 0,3,6 == '-' or 'r', chars at index 1,4,7 == '-' or 'w' and chars at index 2,5,8 == '-' or 'x'.
 44  
      */
 45  87
     public Permissions(String rwxString) {
 46  87
         Assert.isTrue(rwxString.length() == 9, "The permissions string must be exactly 9 characters");
 47  81
         final String RWX = "(-|r)(-|w)(-|x)";
 48  81
         final String PATTERN = RWX + RWX + RWX;
 49  81
         Assert.isTrue(rwxString.matches(PATTERN), "The permissions string must match [" + PATTERN + "]");
 50  78
         this.rwxString = rwxString;
 51  78
     }
 52  
 
 53  
     /**
 54  
      * Return the read/write/execute specification String representing the set of permissions. For example:
 55  
      * "rwxrwxrwx" or "rw-r-----".
 56  
      *
 57  
      * @return the String containing 9 characters that represent the read/write/execute permissions.
 58  
      */
 59  
     public String asRwxString() {
 60  12
         return rwxString;
 61  
     }
 62  
 
 63  
     /**
 64  
      * @return the RWX string for this instance
 65  
      */
 66  
     public String getRwxString() {
 67  0
         return rwxString;
 68  
     }
 69  
 
 70  
     /**
 71  
      * @see java.lang.Object#equals(java.lang.Object)
 72  
      */
 73  
     public boolean equals(Object object) {
 74  9
         return (object != null)
 75  
                 && (object.getClass() == this.getClass())
 76  
                 && (object.hashCode() == hashCode());
 77  
     }
 78  
 
 79  
     /**
 80  
      * Return the hash code for this object.
 81  
      *
 82  
      * @see java.lang.Object#hashCode()
 83  
      */
 84  
     public int hashCode() {
 85  18
         return rwxString.hashCode();
 86  
     }
 87  
 
 88  
     /**
 89  
      * @return true if and only if the user has read permission
 90  
      */
 91  
     public boolean canUserRead() {
 92  13
         return rwxString.charAt(0) == READ_CHAR;
 93  
     }
 94  
 
 95  
     /**
 96  
      * @return true if and only if the user has write permission
 97  
      */
 98  
     public boolean canUserWrite() {
 99  19
         return rwxString.charAt(1) == WRITE_CHAR;
 100  
     }
 101  
 
 102  
     /**
 103  
      * @return true if and only if the user has execute permission
 104  
      */
 105  
     public boolean canUserExecute() {
 106  15
         return rwxString.charAt(2) == EXECUTE_CHAR;
 107  
     }
 108  
 
 109  
     /**
 110  
      * @return true if and only if the group has read permission
 111  
      */
 112  
     public boolean canGroupRead() {
 113  6
         return rwxString.charAt(3) == READ_CHAR;
 114  
     }
 115  
 
 116  
     /**
 117  
      * @return true if and only if the group has write permission
 118  
      */
 119  
     public boolean canGroupWrite() {
 120  6
         return rwxString.charAt(4) == WRITE_CHAR;
 121  
     }
 122  
 
 123  
     /**
 124  
      * @return true if and only if the group has execute permission
 125  
      */
 126  
     public boolean canGroupExecute() {
 127  6
         return rwxString.charAt(5) == EXECUTE_CHAR;
 128  
     }
 129  
 
 130  
     /**
 131  
      * @return true if and only if the world has read permission
 132  
      */
 133  
     public boolean canWorldRead() {
 134  10
         return rwxString.charAt(6) == READ_CHAR;
 135  
     }
 136  
 
 137  
     /**
 138  
      * @return true if and only if the world has write permission
 139  
      */
 140  
     public boolean canWorldWrite() {
 141  8
         return rwxString.charAt(7) == WRITE_CHAR;
 142  
     }
 143  
 
 144  
     /**
 145  
      * @return true if and only if the world has execute permission
 146  
      */
 147  
     public boolean canWorldExecute() {
 148  9
         return rwxString.charAt(8) == EXECUTE_CHAR;
 149  
     }
 150  
 
 151  
     /**
 152  
      * @return the String representation of this object.
 153  
      */
 154  
     public String toString() {
 155  15
         return "Permissions[" + rwxString + "]";
 156  
     }
 157  
 }