You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by if...@apache.org on 2014/02/04 18:14:19 UTC
[3/3] git commit: MPLUGINTESTING-36 introduced TestResources junit4
test Rule
MPLUGINTESTING-36 introduced TestResources junit4 test Rule
Signed-off-by: Igor Fedorenko <if...@apache.org>
Project: http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/commit/603b53d8
Tree: http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/tree/603b53d8
Diff: http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/diff/603b53d8
Branch: refs/heads/master
Commit: 603b53d807819aeb6e0e2157459963a442b8cadb
Parents: 6ac53b2
Author: Igor Fedorenko <if...@apache.org>
Authored: Sat Feb 1 14:38:05 2014 -0500
Committer: Igor Fedorenko <if...@apache.org>
Committed: Tue Feb 4 12:07:12 2014 -0500
----------------------------------------------------------------------
.../plugin/testing/resources/TestResources.java | 174 +++++++++++++++++++
1 file changed, 174 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/blob/603b53d8/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/resources/TestResources.java
----------------------------------------------------------------------
diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/resources/TestResources.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/resources/TestResources.java
new file mode 100644
index 0000000..378c03e
--- /dev/null
+++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/resources/TestResources.java
@@ -0,0 +1,174 @@
+package org.apache.maven.plugin.testing.resources;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.codehaus.plexus.util.DirectoryScanner;
+import org.codehaus.plexus.util.FileUtils;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+/**
+ * Junit4 test {@link Rule} to extract and assert test resources.
+ *
+ * @since 3.1.0
+ */
+public class TestResources
+ extends TestWatcher
+{
+
+ private final String projectsDir;
+
+ private final String workDir;
+
+ public TestResources()
+ {
+ this( "src/test/projects", "target/ut/" );
+ }
+
+ public TestResources( String projectsDir, String workDir )
+ {
+ this.projectsDir = projectsDir;
+ this.workDir = workDir;
+ }
+
+ private String name;
+
+ @Override
+ protected void starting( Description d )
+ {
+ name = d.getTestClass().getSimpleName() + "_" + d.getMethodName();
+ }
+
+ /**
+ * Creates new clean copy of test project directory structure. The copy is named after both the test being executed
+ * and test project name, which allows the same test project can be used by multiple tests and by different
+ * instances of the same parametrized tests.<br/>
+ * TODO Provide alternative working directory naming for Windows, which still limits path names to ~250 charecters
+ */
+ public File getBasedir( String project )
+ throws IOException
+ {
+ File src = new File( projectsDir, project ).getCanonicalFile();
+ Assert.assertTrue( "Test project directory does not exist: " + src.getPath(), src.isDirectory() );
+ File basedir = new File( workDir, name + "_" + project ).getCanonicalFile();
+ FileUtils.deleteDirectory( basedir );
+ Assert.assertTrue( "Test project working directory created", basedir.mkdirs() );
+ FileUtils.copyDirectoryStructure( src, basedir );
+ return basedir;
+ }
+
+ // static helpers
+
+ public static void cp( File basedir, String from, String to )
+ throws IOException
+ {
+ // TODO ensure destination lastModified timestamp changes
+ FileUtils.copyFile( new File( basedir, from ), new File( basedir, to ) );
+ }
+
+ public static void assertFileContents( File basedir, String expectedPath, String actualPath )
+ throws IOException
+ {
+ String expected = FileUtils.fileRead( new File( basedir, expectedPath ) );
+ String actual = FileUtils.fileRead( new File( basedir, actualPath ) );
+ Assert.assertEquals( expected, actual );
+ }
+
+ public static void assertDirectoryContents( File dir, String... expectedPaths )
+ {
+ DirectoryScanner scanner = new DirectoryScanner();
+ scanner.setBasedir( dir );
+ scanner.addDefaultExcludes();
+ scanner.scan();
+
+ Set<String> actual = new TreeSet<String>();
+ for ( String path : scanner.getIncludedFiles() )
+ {
+ actual.add( path );
+ }
+ for ( String path : scanner.getIncludedDirectories() )
+ {
+ if ( path.length() > 0 )
+ {
+ actual.add( path + "/" );
+ }
+ }
+
+ Set<String> expected = new TreeSet<String>();
+ if ( expectedPaths != null )
+ {
+ for ( String path : expectedPaths )
+ {
+ expected.add( path );
+ }
+ }
+
+ // compare textual representation to make diff easier to understand
+ Assert.assertEquals( toString( expected ), toString( actual ) );
+ }
+
+ private static String toString( Collection<String> strings )
+ {
+ StringBuilder sb = new StringBuilder();
+ for ( String string : strings )
+ {
+ sb.append( string ).append( '\n' );
+ }
+ return sb.toString();
+ }
+
+ public static void touch( File basedir, String path )
+ throws InterruptedException
+ {
+ touch( new File( basedir, path ) );
+ }
+
+ public static void touch( File file )
+ throws InterruptedException
+ {
+ if ( !file.isFile() )
+ {
+ throw new IllegalArgumentException( "Not a file " + file );
+ }
+ long lastModified = file.lastModified();
+ file.setLastModified( System.currentTimeMillis() );
+
+ // TODO do modern filesystems still have this silly lastModified resolution?
+ if ( lastModified == file.lastModified() )
+ {
+ Thread.sleep( 1000L );
+ file.setLastModified( System.currentTimeMillis() );
+ }
+ }
+
+ public static void rm( File basedir, String path )
+ {
+ Assert.assertTrue( "delete " + path, new File( basedir, path ).delete() );
+ }
+
+}