ChmodFilePermissions { public static String toMode(PermissionChange change, Set<PosixFilePermission> permissions) { checkArgument(!permissions.isEmpty() || change == PermissionChange.SET, "permission set is empty but change type is not SET"); StringBuilder modes = new StringBuilder(); int bits = toBits(permissions); int ubits = (bits >> U_SHIFT) & MASK; if (ubits != 0) { writeMode(modes.append("u").append(change.getOperator()), ubits); } int gbits = (bits >> G_SHIFT) & MASK; if (gbits != 0) { if (modes.length() > 0) { modes.append(','); } writeMode(modes.append("g").append(change.getOperator()), gbits); } int obits = (bits >> O_SHIFT) & MASK; if (obits != 0) { if (modes.length() > 0) { modes.append(','); } writeMode(modes.append("o").append(change.getOperator()), obits); } return modes.toString(); } private  ChmodFilePermissions(); static Set<PosixFilePermission> toPermissions(int bits); static int toBits(Set<PosixFilePermission> permissions); static String toMode(PermissionChange change, Set<PosixFilePermission> permissions);  }
@Test public void toOtherMode() { String mode = ChmodFilePermissions.toMode(PermissionChange.SET, EnumSet.of(PosixFilePermission.OTHERS_READ)); assertEquals("incorrect mode", "o=r", mode); }
@Test public void toCombinedMode() { String mode = ChmodFilePermissions.toMode(PermissionChange.ADD, EnumSet.of( PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_EXECUTE)); assertEquals("incorrect mode", "u+x,g+wx,o+x", mode); }
@Test public void toOwnerMode() { String mode = ChmodFilePermissions.toMode(PermissionChange.ADD, EnumSet.of(PosixFilePermission.OWNER_EXECUTE)); assertEquals("incorrect mode", "u+x", mode); }
@Test public void toGroupMode() { String mode = ChmodFilePermissions.toMode(PermissionChange.REMOVE, EnumSet.of(PosixFilePermission.GROUP_WRITE)); assertEquals("incorrect mode", "g-w", mode); }