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 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
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 }