View Javadoc
1   package org.apache.maven.wagon;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.plexus.util.FileUtils;
23  
24  import java.io.File;
25  import java.io.FileWriter;
26  import java.io.IOException;
27  import java.io.Writer;
28  
29  /**
30   * @author <a href="michal@apache.org>Michal Maczka</a>
31   *
32   */
33  public class FileTestUtils
34  {
35  
36      public static File createUniqueFile( final String dirname, final String name )
37          throws IOException
38      {
39  
40          final File dir = createDir( dirname );
41  
42          final File retValue = new File( dir, name );
43  
44          return retValue;
45  
46      }
47  
48  
49      public static File createUniqueDir( final String name )
50          throws IOException
51      {
52  
53          String filename = name + System.currentTimeMillis();
54  
55          return createDir( filename );
56  
57      }
58  
59  
60      public static File createDir( final String name )
61          throws IOException
62      {
63  
64          final File baseDirectory = getTestOutputDir();
65  
66          final File retValue = new File( baseDirectory, name );
67         
68          if ( retValue.exists() )
69          {
70              FileUtils.cleanDirectory( retValue );
71              return retValue;
72          }
73          
74          retValue.mkdirs();
75  
76          if ( !retValue.exists() )
77          {
78              throw new IOException( "Unable to create the directory for testdata " + retValue.getPath() );
79          }
80  
81          return retValue;
82      }
83  
84      public static File getTestOutputDir()
85      {
86          final String tempDir = System.getProperty( "java.io.tmpdir" );
87  
88          final String baseDir = System.getProperty( "basedir", tempDir );
89  
90          final File base = new File( baseDir ).getAbsoluteFile();
91  
92          final String pathname = base + File.separator + "target" + File.separator + "test-output";
93  
94          final File retValue = new File( pathname );
95  
96          retValue.mkdirs();
97  
98          return retValue;
99      }
100 
101     public static File generateFile( String file, String content )
102         throws IOException
103     {
104         File f = new File( file );
105 
106         f.getParentFile().mkdirs();
107 
108         Writer writer = new FileWriter( f );
109 
110         try
111         {
112             writer.write( content );
113         }
114         finally
115         {
116             writer.close();
117         }
118 
119         return f;
120     }
121 }