1 | |
package org.apache.maven.wagon; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileWriter; |
24 | |
import java.io.IOException; |
25 | |
import java.io.Writer; |
26 | |
|
27 | |
import org.codehaus.plexus.util.FileUtils; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class FileTestUtils |
34 | |
{ |
35 | |
|
36 | |
public static final File createUniqueFile( final String dirname, final String name ) |
37 | |
throws IOException |
38 | |
{ |
39 | |
|
40 | 0 | final File dir = createDir( dirname ); |
41 | |
|
42 | 0 | final File retValue = new File( dir, name ); |
43 | |
|
44 | 0 | return retValue; |
45 | |
|
46 | |
} |
47 | |
|
48 | |
|
49 | |
public static final File createUniqueDir( final String name ) |
50 | |
throws IOException |
51 | |
{ |
52 | |
|
53 | 0 | String filename = name + System.currentTimeMillis(); |
54 | |
|
55 | 0 | return createDir( filename ); |
56 | |
|
57 | |
} |
58 | |
|
59 | |
|
60 | |
public static final File createDir( final String name ) |
61 | |
throws IOException |
62 | |
{ |
63 | |
|
64 | 0 | final File baseDirectory = getTestOutputDir(); |
65 | |
|
66 | 0 | final File retValue = new File( baseDirectory, name ); |
67 | |
|
68 | 0 | if ( retValue.exists() ) |
69 | |
{ |
70 | 0 | FileUtils.cleanDirectory( retValue ); |
71 | 0 | return retValue; |
72 | |
} |
73 | |
|
74 | 0 | retValue.mkdirs(); |
75 | |
|
76 | 0 | if ( !retValue.exists() ) |
77 | |
{ |
78 | 0 | throw new IOException( "Unable to create the directory for testdata " + retValue.getPath() ); |
79 | |
} |
80 | |
|
81 | 0 | return retValue; |
82 | |
} |
83 | |
|
84 | |
public static final File getTestOutputDir() |
85 | |
{ |
86 | 0 | final String tempDir = System.getProperty( "java.io.tmpdir" ); |
87 | |
|
88 | 0 | final String baseDir = System.getProperty( "basedir", tempDir ); |
89 | |
|
90 | 0 | final File base = new File( baseDir ).getAbsoluteFile(); |
91 | |
|
92 | 0 | final String pathname = base + File.separator + "target" + File.separator + "test-output"; |
93 | |
|
94 | 0 | final File retValue = new File( pathname ); |
95 | |
|
96 | 0 | retValue.mkdirs(); |
97 | |
|
98 | 0 | return retValue; |
99 | |
} |
100 | |
|
101 | |
public static File generateFile( String file, String content ) |
102 | |
throws IOException |
103 | |
{ |
104 | 0 | File f = new File( file ); |
105 | |
|
106 | 0 | f.getParentFile().mkdirs(); |
107 | |
|
108 | 0 | Writer writer = new FileWriter( f ); |
109 | |
|
110 | 0 | writer.write( content ); |
111 | |
|
112 | 0 | writer.close(); |
113 | |
|
114 | 0 | return f; |
115 | |
} |
116 | |
} |