1 package org.apache.maven.shared.dependency.analyzer;
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.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.util.jar.JarOutputStream;
27 import java.util.zip.ZipEntry;
28
29 import org.codehaus.plexus.util.FileUtils;
30 import org.codehaus.plexus.util.IOUtil;
31 import org.jmock.MockObjectTestCase;
32
33
34
35
36
37
38
39 public abstract class AbstractFileTest extends MockObjectTestCase
40 {
41
42
43 protected File createJar() throws IOException
44 {
45 File file = File.createTempFile( "test", ".jar" );
46 file.deleteOnExit();
47
48 return file;
49 }
50
51 protected File createDir() throws IOException
52 {
53 File file = File.createTempFile( "test", null );
54 file.delete();
55
56 if ( !file.mkdir() )
57 throw new IOException( "Cannot create temporary directory: " + file );
58
59 return file;
60 }
61
62 protected File createFile( File parent, String child, String data ) throws IOException
63 {
64 File file = new File( parent, child );
65
66 OutputStream out = new FileOutputStream( file );
67 IOUtil.copy( data, out );
68 out.close();
69
70 return file;
71 }
72
73 protected File mkdirs( File parent, String child ) throws IOException
74 {
75 File dir = new File( parent, child );
76
77 FileUtils.forceMkdir( dir );
78
79 return dir;
80 }
81
82 protected void writeEntry( JarOutputStream out, String path, String data ) throws IOException
83 {
84 out.putNextEntry( new ZipEntry( path ) );
85
86 byte[] bytes = data.getBytes( "UTF-8" );
87
88 out.write( bytes, 0, bytes.length );
89 }
90 }