View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Junit4 test {@link Rule} to extract and assert test resources.
37   *
38   * @deprecated As of version 3.4.0, it is advised to work with JUnit5 tests which do not
39   * use rules but extensions {@link MojoExtension}
40   * instead.
41   *
42   * @since 3.1.0
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       * Creates new clean copy of test project directory structure. The copy is named after both the test being executed
73       * and test project name, which allows the same test project can be used by multiple tests and by different
74       * instances of the same parametrized tests.
75       * <p>
76       * TODO Provide alternative working directory naming for Windows, which still limits path names to ~250 charecters
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      // static helpers
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         // compare textual representation to make diff easier to understand
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         // TODO do modern filesystems still have this silly lastModified resolution?
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      * @since 3.2.0
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 }