View Javadoc
1   package org.codehaus.plexus.components.io.attributes;
2   
3   import junit.framework.TestCase;
4   import org.codehaus.plexus.util.Os;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.nio.file.attribute.PosixFilePermission;
9   import java.util.HashMap;
10  import java.util.Set;
11  
12  /**
13   * @author Kristian Rosenvold
14   */
15  public class AttributeUtilsTest
16      extends TestCase
17  {
18      public void testMiscPatterns()
19          throws Exception
20      {
21          final Set<PosixFilePermission> permissions = AttributeUtils.getPermissions( 0124 );
22          assertTrue( permissions.contains( PosixFilePermission.OWNER_EXECUTE ) );
23          assertTrue( permissions.contains( PosixFilePermission.GROUP_WRITE ) );
24          assertTrue( permissions.contains( PosixFilePermission.OTHERS_READ ) );
25      }
26  
27      public void testMorePatterns() throws Exception
28      {
29          final Set<PosixFilePermission> permissions = AttributeUtils.getPermissions( 0241 );
30          assertTrue( permissions.contains( PosixFilePermission.OWNER_WRITE ) );
31          assertTrue( permissions.contains( PosixFilePermission.GROUP_READ ) );
32          assertTrue( permissions.contains( PosixFilePermission.OTHERS_EXECUTE ) );
33      }
34  
35      public void testEvenMorePatterns() throws Exception
36      {
37          final Set<PosixFilePermission> permissions = AttributeUtils.getPermissions( 0412 );
38          assertTrue( permissions.contains( PosixFilePermission.OWNER_READ ) );
39          assertTrue( permissions.contains( PosixFilePermission.GROUP_EXECUTE ) );
40          assertTrue( permissions.contains( PosixFilePermission.OTHERS_WRITE ) );
41      }
42  
43      public void test777()
44          throws Exception
45      {
46          final Set<PosixFilePermission> permissions = AttributeUtils.getPermissions( 0777 );
47          assertTrue( permissions.size() == 9 );
48      }
49  
50      public void testChmodBackAndForth()
51          throws IOException
52      {
53          if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
54              return;
55          final File bxx = File.createTempFile( "bxx", "ff" );
56          AttributeUtils.chmod( bxx, 0422 );
57          PlexusIoResourceAttributes firstAttrs =
58              new FileAttributes( bxx, new HashMap<Integer, String>(), new HashMap<Integer, String>() );
59          assertTrue( firstAttrs.isOwnerReadable() );
60          assertFalse( firstAttrs.isOwnerWritable() );
61          assertFalse( firstAttrs.isOwnerExecutable() );
62          AttributeUtils.chmod( bxx, 0777 );
63          PlexusIoResourceAttributes secondAttrs =
64              new FileAttributes( bxx, new HashMap<Integer, String>(), new HashMap<Integer, String>() );
65          assertTrue( secondAttrs.isOwnerReadable() );
66          assertTrue( secondAttrs.isOwnerWritable() );
67          assertTrue( secondAttrs.isOwnerExecutable() );
68      }
69  }