View Javadoc
1   package org.codehaus.plexus.components.io.attributes;
2   
3   /*
4    * Copyright 2007 The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.codehaus.plexus.util.Os;
20  import org.codehaus.plexus.util.StringUtils;
21  import org.codehaus.plexus.util.cli.CommandLineException;
22  import org.codehaus.plexus.util.cli.CommandLineUtils;
23  import org.codehaus.plexus.util.cli.Commandline;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.net.URL;
28  import java.util.Map;
29  
30  import junit.framework.TestCase;
31  
32  import static org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributeUtils.getFileAttributes;
33  
34  public class PlexusIoResourceAttributeUtilsTest
35      extends TestCase
36  {
37  
38      public void testGetAttributesForThisTestClass()
39          throws IOException
40      {
41          if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
42          {
43              System.out.println( "WARNING: Unsupported OS, skipping test" );
44              return;
45          }
46  
47          URL resource = Thread.currentThread().getContextClassLoader().getResource(
48              getClass().getName().replace( '.', '/' ) + ".class" );
49  
50          if ( resource == null )
51          {
52              throw new IllegalStateException(
53                  "SOMETHING IS VERY WRONG. CANNOT FIND THIS TEST CLASS IN THE CLASSLOADER." );
54          }
55  
56          File f = new File( resource.getPath().replaceAll( "%20", " " ) );
57  
58          Map<String, PlexusIoResourceAttributes> attrs =
59              PlexusIoResourceAttributeUtils.getFileAttributesByPath( f, true );
60  
61          PlexusIoResourceAttributes fileAttrs = attrs.get( f.getAbsolutePath() );
62  
63          System.out.println( "Got attributes for: " + f.getAbsolutePath() + fileAttrs );
64  
65          assertNotNull( fileAttrs );
66          assertTrue( fileAttrs.isOwnerReadable() );
67          assertEquals( System.getProperty( "user.name" ), fileAttrs.getUserName() );
68      }
69  
70      public void testDirectory()
71          throws IOException, CommandLineException
72      {
73  
74          if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
75          {
76              return; // Nothing to do here.
77          }
78  
79          URL resource = Thread.currentThread().getContextClassLoader().getResource(
80              getClass().getName().replace( '.', '/' ) + ".class" );
81  
82          if ( resource == null )
83          {
84              throw new IllegalStateException(
85                  "SOMETHING IS VERY WRONG. CANNOT FIND THIS TEST CLASS IN THE CLASSLOADER." );
86          }
87  
88          File f = new File( resource.getPath().replaceAll( "%20", " " ) );
89          final File aDir = f.getParentFile().getParentFile().getParentFile();
90  
91          Commandline commandLine = new Commandline( "chmod" );
92          commandLine.addArguments( new String[] { "763", f.getAbsolutePath() } );
93  
94          CommandLineUtils.executeCommandLine( commandLine, null, null );
95          Map<String, PlexusIoResourceAttributes> attrs =
96              PlexusIoResourceAttributeUtils.getFileAttributesByPath( aDir, true );
97  
98          PlexusIoResourceAttributes fileAttrs = attrs.get( f.getAbsolutePath() );
99  
100         assertTrue( fileAttrs.isGroupReadable() );
101         assertTrue( fileAttrs.isGroupWritable() );
102         assertFalse( fileAttrs.isGroupExecutable() );
103 
104         assertTrue( fileAttrs.isOwnerExecutable() );
105         assertTrue( fileAttrs.isOwnerReadable() );
106         assertTrue( fileAttrs.isOwnerWritable() );
107 
108         assertTrue( fileAttrs.isWorldExecutable() );
109         assertFalse( fileAttrs.isWorldReadable() );
110         assertTrue( fileAttrs.isWorldWritable() );
111 
112         assertNotNull( fileAttrs );
113     }
114 
115     public void testSrcResource()
116         throws IOException
117     {
118         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
119         {
120             return; // Nothing to do here.
121         }
122 
123         File dir = new File( "src/test/resources/symlinks" );
124         final Map<String, PlexusIoResourceAttributes> fileAttributesByPathScreenScrape =
125             PlexusIoResourceAttributeUtils.getFileAttributesByPath( dir, true );
126         assertNotNull( fileAttributesByPathScreenScrape );
127         PlexusIoResourceAttributes pr = null;
128         for ( String s : fileAttributesByPathScreenScrape.keySet() )
129         {
130             if ( s.endsWith( "targetFile.txt" ) )
131                 pr = fileAttributesByPathScreenScrape.get( s );
132         }
133         assertNotNull( pr );
134 
135         assertTrue( pr.getOctalMode() > 0 );
136     }
137     public void testNonExistingDirectory()
138     {
139         File dir = new File( "src/test/noSuchDirectory" );
140         try
141         {
142             PlexusIoResourceAttributeUtils.getFileAttributesByPath( dir, true );
143             fail( "We were supposed to get an io exceptions" );
144         }
145         catch ( IOException ignore )
146         {
147             ignore.printStackTrace();
148         }
149     }
150 
151     public void testMergeAttributesWithNullBase()
152     {
153         PlexusIoResourceAttributes override =
154             new SimpleResourceAttributes( 1001, "myUser", 1001, "test", 0 );
155         PlexusIoResourceAttributes defaults =
156             new SimpleResourceAttributes( 1000, "defaultUser", 1000, "defaultTest", 0 );
157 
158         PlexusIoResourceAttributes attributes;
159         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( override, null, defaults );
160 
161         assertEquals( Integer.valueOf( 1001 ), attributes.getGroupId() );
162         assertEquals( Integer.valueOf( 1001 ), attributes.getUserId() );
163     }
164 
165     public void testMergeAttributesWithNullOverrideGroup()
166     {
167         final PlexusIoResourceAttributes override =
168             new SimpleResourceAttributes( 1001, "myUser", -1, null, 0 );
169         final PlexusIoResourceAttributes defaults =
170             new SimpleResourceAttributes( 1000, "defaultUser", 1000, "defaultGroup", 0 );
171 
172         PlexusIoResourceAttributes attributes =
173             PlexusIoResourceAttributeUtils.mergeAttributes( override, null, defaults );
174 
175         assertEquals( attributes.getGroupId(), Integer.valueOf( 1000 ) );
176         assertEquals( attributes.getUserId(), Integer.valueOf( 1001 ) );
177     }
178 
179     public void testMergeAttributesOverride()
180     {
181         final PlexusIoResourceAttributes blank = new SimpleResourceAttributes();
182         final PlexusIoResourceAttributes invalid = new SimpleResourceAttributes( -1, null, -1, null, -1 );
183         final PlexusIoResourceAttributes override =
184             new SimpleResourceAttributes( 1111, "testUser", 2222, "testGroup", 0777 );
185         final PlexusIoResourceAttributes defaults =
186             new SimpleResourceAttributes( 3333, "defaultUser", 4444, "defaultGroup", 0444 );
187         final PlexusIoResourceAttributes base =
188             new SimpleResourceAttributes( 5555, "baseUser", 6666, "baseGroup", 0111 );
189 
190         PlexusIoResourceAttributes attributes;
191 
192         // When override is null, base is returned verbatim
193         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( null, null, null );
194         assertNull( attributes );
195 
196         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( null, null, defaults );
197         assertNull( attributes );
198 
199         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( null, base, null );
200         assertSame( base, attributes );
201 
202         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( null, base, defaults );
203         assertSame( base, attributes );
204 
205         // Test cases when override is non-null
206         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( override, null, null );
207 
208         assertEquals( Integer.valueOf( 1111 ), attributes.getUserId() );
209         assertEquals( "testUser", attributes.getUserName() );
210         assertEquals( Integer.valueOf( 2222 ), attributes.getGroupId() );
211         assertEquals( "testGroup", attributes.getGroupName() );
212         assertEquals( 0777, attributes.getOctalMode() );
213 
214         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( override, base, null );
215 
216         assertEquals( Integer.valueOf( 1111 ), attributes.getUserId() );
217         assertEquals( "testUser", attributes.getUserName() );
218         assertEquals( Integer.valueOf( 2222 ), attributes.getGroupId() );
219         assertEquals( "testGroup", attributes.getGroupName() );
220         assertEquals( 0777, attributes.getOctalMode() );
221 
222         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( override, null, defaults );
223 
224         assertEquals( Integer.valueOf( 1111 ), attributes.getUserId() );
225         assertEquals( "testUser", attributes.getUserName() );
226         assertEquals( Integer.valueOf( 2222 ), attributes.getGroupId() );
227         assertEquals( "testGroup", attributes.getGroupName() );
228         assertEquals( 0777, attributes.getOctalMode() );
229 
230         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( override, base, defaults );
231 
232         assertEquals( Integer.valueOf( 1111 ), attributes.getUserId() );
233         assertEquals( "testUser", attributes.getUserName() );
234         assertEquals( Integer.valueOf( 2222 ), attributes.getGroupId() );
235         assertEquals( "testGroup", attributes.getGroupName() );
236         assertEquals( 0777, attributes.getOctalMode() );
237 
238         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( override, blank, null );
239 
240         assertEquals( Integer.valueOf( 1111 ), attributes.getUserId() );
241         assertEquals( "testUser", attributes.getUserName() );
242         assertEquals( Integer.valueOf( 2222 ), attributes.getGroupId() );
243         assertEquals( "testGroup", attributes.getGroupName() );
244         assertEquals( 0777, attributes.getOctalMode() );
245 
246         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( override, invalid, null );
247 
248         assertEquals( Integer.valueOf( 1111 ), attributes.getUserId() );
249         assertEquals( "testUser", attributes.getUserName() );
250         assertEquals( Integer.valueOf( 2222 ), attributes.getGroupId() );
251         assertEquals( "testGroup", attributes.getGroupName() );
252         assertEquals( 0777, attributes.getOctalMode() );
253 
254         // Test cases when override has only blank values
255         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( blank, base, null );
256 
257         assertEquals( Integer.valueOf( 5555 ), attributes.getUserId() );
258         assertEquals( "baseUser", attributes.getUserName() );
259         assertEquals( Integer.valueOf( 6666 ), attributes.getGroupId() );
260         assertEquals( "baseGroup", attributes.getGroupName() );
261         assertEquals( 0111, attributes.getOctalMode() );
262 
263         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( invalid, base, null );
264 
265         assertEquals( Integer.valueOf( 5555 ), attributes.getUserId() );
266         assertEquals( "baseUser", attributes.getUserName() );
267         assertEquals( Integer.valueOf( 6666 ), attributes.getGroupId() );
268         assertEquals( "baseGroup", attributes.getGroupName() );
269         assertEquals( 0111, attributes.getOctalMode() );
270 
271         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( blank, base, defaults );
272 
273         assertEquals( Integer.valueOf( 5555 ), attributes.getUserId() );
274         assertEquals( "baseUser", attributes.getUserName() );
275         assertEquals( Integer.valueOf( 6666 ), attributes.getGroupId() );
276         assertEquals( "baseGroup", attributes.getGroupName() );
277         assertEquals( 0111, attributes.getOctalMode() );
278 
279         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( invalid, base, defaults );
280 
281         assertEquals( Integer.valueOf( 5555 ), attributes.getUserId() );
282         assertEquals( "baseUser", attributes.getUserName() );
283         assertEquals( Integer.valueOf( 6666 ), attributes.getGroupId() );
284         assertEquals( "baseGroup", attributes.getGroupName() );
285         assertEquals( 0111, attributes.getOctalMode() );
286     }
287 
288     public void testFileAttributes()
289         throws IOException
290     {
291         PlexusIoResourceAttributes attrs = getFileAttributes( new File( "src/test/resources/symlinks/src/fileW.txt" ) );
292         assertFalse( attrs.isSymbolicLink() );
293         assertTrue( StringUtils.isNotEmpty( attrs.getUserName() ) );
294         if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
295         {
296             assertTrue( StringUtils.isNotEmpty( attrs.getGroupName() ) );
297             assertNotNull( attrs.getGroupId() );
298             assertNotNull( attrs.getUserId() );
299         }
300     }
301 
302     public void testMergeAttributesDefault()
303     {
304         final PlexusIoResourceAttributes blank = new SimpleResourceAttributes( null, null, null, null, 0 );
305         final PlexusIoResourceAttributes invalid = new SimpleResourceAttributes( -1, null, -1, null, -1 );
306         final PlexusIoResourceAttributes defaults =
307             new SimpleResourceAttributes( 3333, "defaultUser", 4444, "defaultGroup", 0444 );
308 
309         PlexusIoResourceAttributes attributes;
310 
311         // Test cases when override and base have blank values
312         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( blank, blank, defaults );
313 
314         assertEquals( Integer.valueOf( 3333 ), attributes.getUserId() );
315         assertEquals( "defaultUser", attributes.getUserName() );
316         assertEquals( Integer.valueOf( 4444 ), attributes.getGroupId() );
317         assertEquals( "defaultGroup", attributes.getGroupName() );
318         // 0 is a borderline case, for backwards compatibility it is not overridden by value from defaults
319         assertEquals( 0, attributes.getOctalMode() );
320 
321         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( invalid, blank, defaults );
322 
323         assertEquals( Integer.valueOf( 3333 ), attributes.getUserId() );
324         assertEquals( "defaultUser", attributes.getUserName() );
325         assertEquals( Integer.valueOf( 4444 ), attributes.getGroupId() );
326         assertEquals( "defaultGroup", attributes.getGroupName() );
327         // 0 is a borderline case, for backwards compatibility it is not overridden by value from defaults
328         // Not just that, but 0 is correct.
329         assertEquals( 0, attributes.getOctalMode() );
330 
331         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( blank, invalid, defaults );
332 
333         assertEquals( Integer.valueOf( 3333 ), attributes.getUserId() );
334         assertEquals( "defaultUser", attributes.getUserName() );
335         assertEquals( Integer.valueOf( 4444 ), attributes.getGroupId() );
336         assertEquals( "defaultGroup", attributes.getGroupName() );
337         assertEquals( 0444, attributes.getOctalMode() );
338 
339         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( invalid, invalid, defaults );
340 
341         assertEquals( Integer.valueOf( 3333 ), attributes.getUserId() );
342         assertEquals( "defaultUser", attributes.getUserName() );
343         assertEquals( Integer.valueOf( 4444 ), attributes.getGroupId() );
344         assertEquals( "defaultGroup", attributes.getGroupName() );
345         assertEquals( 0444, attributes.getOctalMode() );
346 
347         // Test cases when invalid defaults should not override blank values
348         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( blank, blank, invalid );
349 
350         assertNull( attributes.getUserId() );
351         assertNull( attributes.getUserName() );
352         assertNull( attributes.getGroupId() );
353         assertNull( attributes.getGroupName() );
354         assertEquals( 0, attributes.getOctalMode() );
355 
356         attributes = PlexusIoResourceAttributeUtils.mergeAttributes( invalid, blank, invalid );
357 
358         assertNull( attributes.getUserId() );
359         assertNull( attributes.getUserName() );
360         assertNull( attributes.getGroupId() );
361         assertNull( attributes.getGroupName() );
362         assertEquals( 0, attributes.getOctalMode() );
363     }
364 
365 }