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