1 package org.apache.maven.plugin.testing.resources;
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.IOException;
24 import java.util.Collection;
25 import java.util.Set;
26 import java.util.TreeSet;
27
28 import org.codehaus.plexus.util.DirectoryScanner;
29 import org.codehaus.plexus.util.FileUtils;
30 import org.junit.Assert;
31 import org.junit.Rule;
32 import org.junit.rules.TestWatcher;
33 import org.junit.runner.Description;
34
35
36
37
38
39
40 public class TestResources
41 extends TestWatcher
42 {
43
44 private final String projectsDir;
45
46 private final String workDir;
47
48 public TestResources()
49 {
50 this( "src/test/projects", "target/ut/" );
51 }
52
53 public TestResources( String projectsDir, String workDir )
54 {
55 this.projectsDir = projectsDir;
56 this.workDir = workDir;
57 }
58
59 private String name;
60
61 @Override
62 protected void starting( Description d )
63 {
64 name = d.getTestClass().getSimpleName() + "_" + d.getMethodName();
65 }
66
67
68
69
70
71
72
73 public File getBasedir( String project )
74 throws IOException
75 {
76 File src = new File( projectsDir, project ).getCanonicalFile();
77 Assert.assertTrue( "Test project directory does not exist: " + src.getPath(), src.isDirectory() );
78 File basedir = new File( workDir, name + "_" + project ).getCanonicalFile();
79 FileUtils.deleteDirectory( basedir );
80 Assert.assertTrue( "Test project working directory created", basedir.mkdirs() );
81 FileUtils.copyDirectoryStructure( src, basedir );
82 return basedir;
83 }
84
85
86
87 public static void cp( File basedir, String from, String to )
88 throws IOException
89 {
90
91 FileUtils.copyFile( new File( basedir, from ), new File( basedir, to ) );
92 }
93
94 public static void assertFileContents( File basedir, String expectedPath, String actualPath )
95 throws IOException
96 {
97 String expected = FileUtils.fileRead( new File( basedir, expectedPath ) );
98 String actual = FileUtils.fileRead( new File( basedir, actualPath ) );
99 Assert.assertEquals( expected, actual );
100 }
101
102 public static void assertDirectoryContents( File dir, String... expectedPaths )
103 {
104 DirectoryScanner scanner = new DirectoryScanner();
105 scanner.setBasedir( dir );
106 scanner.addDefaultExcludes();
107 scanner.scan();
108
109 Set<String> actual = new TreeSet<String>();
110 for ( String path : scanner.getIncludedFiles() )
111 {
112 actual.add( path );
113 }
114 for ( String path : scanner.getIncludedDirectories() )
115 {
116 if ( path.length() > 0 )
117 {
118 actual.add( path + "/" );
119 }
120 }
121
122 Set<String> expected = new TreeSet<String>();
123 if ( expectedPaths != null )
124 {
125 for ( String path : expectedPaths )
126 {
127 expected.add( path );
128 }
129 }
130
131
132 Assert.assertEquals( toString( expected ), toString( actual ) );
133 }
134
135 private static String toString( Collection<String> strings )
136 {
137 StringBuilder sb = new StringBuilder();
138 for ( String string : strings )
139 {
140 sb.append( string ).append( '\n' );
141 }
142 return sb.toString();
143 }
144
145 public static void touch( File basedir, String path )
146 throws InterruptedException
147 {
148 touch( new File( basedir, path ) );
149 }
150
151 public static void touch( File file )
152 throws InterruptedException
153 {
154 if ( !file.isFile() )
155 {
156 throw new IllegalArgumentException( "Not a file " + file );
157 }
158 long lastModified = file.lastModified();
159 file.setLastModified( System.currentTimeMillis() );
160
161
162 if ( lastModified == file.lastModified() )
163 {
164 Thread.sleep( 1000L );
165 file.setLastModified( System.currentTimeMillis() );
166 }
167 }
168
169 public static void rm( File basedir, String path )
170 {
171 Assert.assertTrue( "delete " + path, new File( basedir, path ).delete() );
172 }
173
174 }