View Javadoc
1   package org.codehaus.plexus.resource;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  import java.io.InputStream;
29  
30  import org.codehaus.plexus.PlexusTestCase;
31  import org.codehaus.plexus.util.IOUtil;
32  import org.codehaus.plexus.util.FileUtils;
33  
34  /**
35   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
36   * @version $Id$
37   */
38  public class ResourceManagerTest
39      extends PlexusTestCase
40  {
41  
42      public void testResourceManagerRetrievingInputStreams()
43          throws Exception
44      {
45          ResourceManager resourceManager = (ResourceManager) lookup( ResourceManager.ROLE );
46  
47          InputStream in;
48  
49          File absoluteFile = new File( getBasedir(), "src/test/file-resources/dir/file.txt" ).getAbsoluteFile();
50          assertTrue( absoluteFile.isFile() );
51          assertTrue( absoluteFile.isAbsolute() );
52          in = resourceManager.getResourceAsInputStream( absoluteFile.getAbsolutePath() );
53          assertEquals( "file.txt", IOUtil.toString( in, "UTF-8" ) );
54  
55          in = resourceManager.getResourceAsInputStream( "/dir/file.txt" );
56          assertEquals( "file.txt", IOUtil.toString( in, "UTF-8" ) );
57  
58          in = resourceManager.getResourceAsInputStream( "dir/file.txt" );
59          assertEquals( "file.txt", IOUtil.toString( in, "UTF-8" ) );
60  
61          in = resourceManager.getResourceAsInputStream( "/dir/classpath.txt" );
62          assertEquals( "classpath.txt", IOUtil.toString( in, "UTF-8" ) );
63  
64          in = resourceManager.getResourceAsInputStream( "dir/classpath.txt" );
65          assertEquals( "classpath.txt", IOUtil.toString( in, "UTF-8" ) );
66      }
67  
68      public void testResourceManagerRetrievingFiles()
69          throws Exception
70      {
71          ResourceManager resourceManager = (ResourceManager) lookup( ResourceManager.ROLE );
72  
73          File f;
74  
75          File absoluteFile = new File( getBasedir(), "src/test/file-resources/dir/file.txt" ).getAbsoluteFile();
76          assertTrue( absoluteFile.isFile() );
77          assertTrue( absoluteFile.isAbsolute() );
78          f = resourceManager.getResourceAsFile( absoluteFile.getAbsolutePath() );
79          assertEquals( "file.txt", FileUtils.fileRead( f, "UTF-8" ) );
80  
81          f = resourceManager.getResourceAsFile( "/dir/file.txt" );
82          assertEquals( "file.txt", FileUtils.fileRead( f, "UTF-8" ) );
83  
84          f = resourceManager.getResourceAsFile( "dir/file.txt" );
85          assertEquals( "file.txt", FileUtils.fileRead( f, "UTF-8" ) );
86  
87          f = resourceManager.getResourceAsFile( "/dir/classpath.txt" );
88          assertEquals( "classpath.txt", FileUtils.fileRead( f, "UTF-8" ) );
89  
90          f = resourceManager.getResourceAsFile( "dir/classpath.txt" );
91          assertEquals( "classpath.txt", FileUtils.fileRead( f, "UTF-8" ) );
92      }
93  
94      public void testResourceManagerRetrievingFilesToSpecificLocation()
95          throws Exception
96      {
97          File outDir = new File( getBasedir(), "target/test/unit/output-directory" );
98  
99          ResourceManager resourceManager = (ResourceManager) lookup( ResourceManager.ROLE );
100 
101         resourceManager.setOutputDirectory( outDir );
102 
103         File ef = new File( outDir, "test/f.txt" );
104         FileUtils.forceDelete( ef );
105         assertFalse( ef.exists() );
106         File f = resourceManager.getResourceAsFile( "dir/file.txt", "test/f.txt" );
107         assertEquals( "file.txt", FileUtils.fileRead( f, "UTF-8" ) );
108         assertEquals( ef, f );
109 
110         File ec = new File( outDir, "test/c.txt" );
111         FileUtils.forceDelete( ec );
112         assertFalse( ec.exists() );
113         File c = resourceManager.getResourceAsFile( "dir/classpath.txt", "test/c.txt" );
114         assertEquals( "classpath.txt", FileUtils.fileRead( c, "UTF-8" ) );
115         assertEquals( ec, c );
116     }
117 
118 }