View Javadoc

1   package org.apache.maven.plugin.testing.resources;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Junit4 test {@link Rule} to extract and assert test resources.
37   * 
38   * @since 3.1.0
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       * Creates new clean copy of test project directory structure. The copy is named after both the test being executed
69       * and test project name, which allows the same test project can be used by multiple tests and by different
70       * instances of the same parametrized tests.<br/>
71       * TODO Provide alternative working directory naming for Windows, which still limits path names to ~250 charecters
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      // static helpers
86  
87      public static void cp( File basedir, String from, String to )
88          throws IOException
89      {
90          // TODO ensure destination lastModified timestamp changes
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         // compare textual representation to make diff easier to understand
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         // TODO do modern filesystems still have this silly lastModified resolution?
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 }