1 package org.apache.maven.shared.utils.testhelpers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.shared.utils.io.FileUtils;
23 import org.apache.maven.shared.utils.io.IOUtil;
24 import org.junit.rules.TemporaryFolder;
25
26 import java.io.*;
27
28
29
30
31 public final class FileTestHelper
32 {
33
34 private FileTestHelper()
35 {
36
37 }
38
39 public static void generateTestData( OutputStream out, long size )
40 throws IOException
41 {
42 for ( int i = 0; i < size; i++ )
43 {
44
45 out.write( (byte) ( ( i % 127 ) + 1 ) );
46 }
47 }
48
49 public static void generateTestFile( File testfile, int size )
50 throws IOException
51 {
52 if ( testfile.exists() )
53 {
54
55 testfile.delete();
56 }
57
58 OutputStream os = new FileOutputStream( testfile );
59 generateTestData( os, size );
60 os.flush();
61 os.close();
62 }
63
64
65
66 public static void createLineBasedFile( File file, String[] data )
67 throws IOException
68 {
69 if ( file.getParentFile() != null && !file.getParentFile().exists() )
70 {
71 throw new IOException( "Cannot create file " + file + " as the parent directory does not exist" );
72 }
73 PrintWriter output = new PrintWriter( new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ) );
74 try
75 {
76 for ( String aData : data )
77 {
78 output.println( aData );
79 }
80 }
81 finally
82 {
83 IOUtil.close( output );
84 }
85 }
86
87
88
89
90
91
92
93 public static File newFile( TemporaryFolder folder, String filename )
94 throws IOException
95 {
96 File destination = folder.newFile( filename );
97
98 if ( destination.exists() )
99 {
100 FileUtils.forceDelete( destination );
101 }
102 return destination;
103 }
104 }