tools/shell: fix non-isolated tests and file operations Tests are isolated since 7524f12d41e945a406dad98aa309d899bf2cc977. This commit fixes tools/shell tests that relied on running at the projectâs root. Fixes files operations, e.g. file copy now preserve permissions.
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/e504fe4a Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/e504fe4a Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/e504fe4a Branch: refs/heads/develop Commit: e504fe4a24de55c8faa92d2a59f09f9204f85948 Parents: a16b956 Author: Paul Merlin <[email protected]> Authored: Sat Nov 19 20:54:47 2016 +0100 Committer: Paul Merlin <[email protected]> Committed: Sat Nov 19 20:54:47 2016 +0100 ---------------------------------------------------------------------- .../org/apache/zest/tools/shell/FileUtils.java | 67 ++++++++++---------- .../create/project/AbstractProjectCreator.java | 26 +------- .../apache/zest/tools/shell/FileUtilsTest.java | 61 +++++++----------- .../org/apache/zest/tools/shell/TestHelper.java | 7 +- .../tools/shell/create/CreateProjectTest.java | 11 +--- .../project/DefaultProjectCreatorTest.java | 15 +---- .../create/project/NullProjectCreatorTest.java | 13 +--- .../create/project/RestProjectCreatorTest.java | 13 +--- 8 files changed, 71 insertions(+), 142 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/e504fe4a/tools/shell/src/main/java/org/apache/zest/tools/shell/FileUtils.java ---------------------------------------------------------------------- diff --git a/tools/shell/src/main/java/org/apache/zest/tools/shell/FileUtils.java b/tools/shell/src/main/java/org/apache/zest/tools/shell/FileUtils.java index 868d2eb..d7363db 100644 --- a/tools/shell/src/main/java/org/apache/zest/tools/shell/FileUtils.java +++ b/tools/shell/src/main/java/org/apache/zest/tools/shell/FileUtils.java @@ -20,14 +20,18 @@ package org.apache.zest.tools.shell; import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; +import java.io.UncheckedIOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -36,41 +40,42 @@ public class FileUtils { public static File createDir( String directoryName ) { - File dir = new File( directoryName ).getAbsoluteFile(); - if( !dir.mkdirs() ) + try { - System.err.println( "Unable to create directory " + dir ); -// System.exit( 1 ); during testing, I am tired of deleting directory over and over again. + File dir = new File( directoryName ).getAbsoluteFile(); + Files.createDirectories( dir.toPath() ); + return dir; + } + catch( IOException ex ) + { + throw new UncheckedIOException( ex ); } - return dir; } - public static boolean removeDir( File dir ) + public static void removeDir( File dir ) throws IOException { - File[] files = dir.listFiles(); - boolean success = true; - if( files != null ) + Files.walkFileTree( dir.toPath(), new SimpleFileVisitor<Path>() { - for( File f : files ) + @Override + public FileVisitResult visitFile( final Path file, final BasicFileAttributes attrs ) throws IOException { - if( f.isDirectory() ) - { - success = success && removeDir( f ); - } - else - { - success = success && f.delete(); - } + Files.delete( file ); + return FileVisitResult.CONTINUE; } - } - success = success && dir.delete(); - return success; + + @Override + public FileVisitResult postVisitDirectory( final Path dir, final IOException exc ) throws IOException + { + Files.delete( dir ); + return FileVisitResult.CONTINUE; + } + } ); } public static Map<String, String> readTemplateProperties( String templateName ) { File propertiesFile = new File( zestHome(), "etc/templates/" + templateName + "/template.properties" ); - try (InputStream in = new BufferedInputStream( new FileInputStream( propertiesFile ) )) + try( InputStream in = new BufferedInputStream( new FileInputStream( propertiesFile ) ) ) { Properties properties = readProperties( in ); Map<String, String> result = new HashMap<String, String>(); @@ -100,19 +105,11 @@ public class FileUtils throws IOException { System.out.println( "Creating " + dest.getAbsolutePath() ); - byte[] buffer = new byte[ 100000 ]; - try (BufferedInputStream in = new BufferedInputStream( new FileInputStream( srcFile ) )) + if( dest.exists() ) { - try (BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( dest ) )) - { - int bytes; - while( ( bytes = in.read( buffer ) ) != -1 ) - { - out.write( buffer, 0, bytes ); - } - out.flush(); - } + Files.delete( dest.toPath() ); } + Files.copy( srcFile.toPath(), dest.toPath() ); } public static File zestHome() http://git-wip-us.apache.org/repos/asf/zest-java/blob/e504fe4a/tools/shell/src/main/java/org/apache/zest/tools/shell/create/project/AbstractProjectCreator.java ---------------------------------------------------------------------- diff --git a/tools/shell/src/main/java/org/apache/zest/tools/shell/create/project/AbstractProjectCreator.java b/tools/shell/src/main/java/org/apache/zest/tools/shell/create/project/AbstractProjectCreator.java index b6c7b9f..966c709 100644 --- a/tools/shell/src/main/java/org/apache/zest/tools/shell/create/project/AbstractProjectCreator.java +++ b/tools/shell/src/main/java/org/apache/zest/tools/shell/create/project/AbstractProjectCreator.java @@ -23,8 +23,6 @@ package org.apache.zest.tools.shell.create.project; import java.io.File; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.attribute.PosixFilePermissions; import java.util.Map; import org.apache.zest.tools.shell.FileUtils; @@ -41,9 +39,6 @@ abstract class AbstractProjectCreator { File templateDir = new File( FileUtils.zestHome(), properties.get( "template.dir" ) ); copyFiles( templateDir, projectDir, properties.get( "root.package" ) ); - File gradlewFile = new File( projectDir, "gradlew" ); - Path gradlewPath = gradlewFile.toPath(); - setGradlewPermissions( gradlewFile, gradlewPath ); } private void copyFiles( File fromDir, File toDir, String rootpackage ) @@ -54,7 +49,7 @@ abstract class AbstractProjectCreator { return; } - toDir.mkdirs(); // create all directories needed. + Files.createDirectories( toDir.toPath() ); for( File f : files ) { String filename = f.getName(); @@ -84,23 +79,4 @@ abstract class AbstractProjectCreator } } } - - private void setGradlewPermissions( File gradlewFile, Path gradlewPath ) - throws IOException - { - try - { - if( gradlewFile.exists() ) - { - Files.setPosixFilePermissions( gradlewPath, PosixFilePermissions.fromString( "rwxr-xr-x" ) ); - } - } - catch( Exception e ) - { - if( !System.getProperty( "os.name" ).contains( "Windows" ) ) - { - throw new IOException( "Unable to set file permissions on " + gradlewPath.toString(), e ); - } - } - } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/e504fe4a/tools/shell/src/test/java/org/apache/zest/tools/shell/FileUtilsTest.java ---------------------------------------------------------------------- diff --git a/tools/shell/src/test/java/org/apache/zest/tools/shell/FileUtilsTest.java b/tools/shell/src/test/java/org/apache/zest/tools/shell/FileUtilsTest.java index 344c8c0..af9544a 100644 --- a/tools/shell/src/test/java/org/apache/zest/tools/shell/FileUtilsTest.java +++ b/tools/shell/src/test/java/org/apache/zest/tools/shell/FileUtilsTest.java @@ -21,6 +21,8 @@ package org.apache.zest.tools.shell; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.Map; import org.junit.Test; @@ -31,54 +33,45 @@ import static org.junit.Assert.assertThat; public class FileUtilsTest { @Test - public void createDirectoryTest() + public void createDirectoryTest() throws IOException { File f = new File( "habba-zout" ); - if( f.exists() ) - { - FileUtils.removeDir( f ); - } assertThat( f.exists(), equalTo( false ) ); FileUtils.createDir( "habba-zout" ); assertThat( f.exists(), equalTo( true ) ); - if( ! f.delete() ){ - System.err.println( "Unable to remove file. Why???" ); - } + Files.delete( f.toPath() ); assertThat( f.exists(), equalTo( false ) ); } @Test - public void removeDirTest() throws Exception { - File srcFile = new File("build.gradle"); + public void removeDirTest() throws Exception + { + File srcFile = new File( "build.gradle" ); + Files.write( srcFile.toPath(), "Some content".getBytes() ); File f = new File( "habba-zout" ); - if( f.exists() ) - { - FileUtils.removeDir( f ); - } assertThat( f.exists(), equalTo( false ) ); File f1 = FileUtils.createDir( "habba-zout" ); File f2 = FileUtils.createDir( "habba-zout/src" ); File f3 = FileUtils.createDir( "habba-zout/src/main" ); File f4 = FileUtils.createDir( "habba-zout/src/test" ); File f5 = FileUtils.createDir( "habba-zout/src/main/abc" ); - FileUtils.copyFile( srcFile, new File(f1, "build.gradle__") ); - FileUtils.copyFile( srcFile, new File(f2, "build.gradle__") ); - FileUtils.copyFile( srcFile, new File(f3, "build.gradle__") ); - FileUtils.copyFile( srcFile, new File(f4, "build.gradle__") ); - FileUtils.copyFile( srcFile, new File(f5, "build.gradle__") ); - boolean success = FileUtils.removeDir( f ); - assertThat(success, equalTo(true)); - assertThat(f1.exists(), equalTo(false)); - assertThat(f2.exists(), equalTo(false)); - assertThat(f3.exists(), equalTo(false)); - assertThat(f4.exists(), equalTo(false)); - assertThat(f5.exists(), equalTo(false)); + FileUtils.copyFile( srcFile, new File( f1, "build.gradle__" ) ); + FileUtils.copyFile( srcFile, new File( f2, "build.gradle__" ) ); + FileUtils.copyFile( srcFile, new File( f3, "build.gradle__" ) ); + FileUtils.copyFile( srcFile, new File( f4, "build.gradle__" ) ); + FileUtils.copyFile( srcFile, new File( f5, "build.gradle__" ) ); + FileUtils.removeDir( f ); + assertThat( f1.exists(), equalTo( false ) ); + assertThat( f2.exists(), equalTo( false ) ); + assertThat( f3.exists(), equalTo( false ) ); + assertThat( f4.exists(), equalTo( false ) ); + assertThat( f5.exists(), equalTo( false ) ); } @Test public void readPropertiesResourceTest() { - TestHelper.zetZestZome(); + TestHelper.setZestZome(); Map<String, String> map = FileUtils.readTemplateProperties( "restapp" ); assertThat( map, notNullValue() ); assertThat( map.get( "template.dir" ), equalTo( "etc/templates/restapp/files" ) ); @@ -88,19 +81,13 @@ public class FileUtilsTest public void copyFileTest() throws Exception { - File dest = new File( "build.gradle.copy" ); - if( dest.exists() ) // from an earlier aborted run. - { - if( ! dest.delete() ){ - System.err.println( "Unable to remove file. Why???" ); - } - } File srcFile = new File( "build.gradle" ); + Files.write( srcFile.toPath(), "Some content".getBytes() ); + File dest = new File( "build.gradle.copy" ); + assertThat( dest.exists(), equalTo( false ) ); FileUtils.copyFile( srcFile, dest ); assertThat( dest.exists(), equalTo( true ) ); - if( ! dest.delete() ){ - System.err.println( "Unable to remove file. Why???" ); - } + Files.delete( dest.toPath() ); assertThat( dest.exists(), equalTo( false ) ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/e504fe4a/tools/shell/src/test/java/org/apache/zest/tools/shell/TestHelper.java ---------------------------------------------------------------------- diff --git a/tools/shell/src/test/java/org/apache/zest/tools/shell/TestHelper.java b/tools/shell/src/test/java/org/apache/zest/tools/shell/TestHelper.java index a30109d..01d40bb 100644 --- a/tools/shell/src/test/java/org/apache/zest/tools/shell/TestHelper.java +++ b/tools/shell/src/test/java/org/apache/zest/tools/shell/TestHelper.java @@ -24,7 +24,7 @@ import java.io.File; public class TestHelper { - public static void zetZestZome() + public static void setZestZome() { String cwd = new File( ".").getAbsolutePath(); if( cwd.endsWith( "/java/." )) // IDEA default runner @@ -37,5 +37,10 @@ public class TestHelper String zestHome = new File( new File(".").getAbsoluteFile(), "src/dist" ).getAbsolutePath(); System.setProperty( "zest.home", zestHome ); } + if( cwd.endsWith( "test/work/." ) ) // Parallel Gradle build + { + String zestHome = new File( cwd + "./../../../../src/dist" ).getAbsolutePath(); + System.setProperty( "zest.home", zestHome ); + } } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/e504fe4a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/CreateProjectTest.java ---------------------------------------------------------------------- diff --git a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/CreateProjectTest.java b/tools/shell/src/test/java/org/apache/zest/tools/shell/create/CreateProjectTest.java index 882b19d..3f4159e 100644 --- a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/CreateProjectTest.java +++ b/tools/shell/src/test/java/org/apache/zest/tools/shell/create/CreateProjectTest.java @@ -21,7 +21,7 @@ package org.apache.zest.tools.shell.create; import java.io.File; -import org.apache.zest.tools.shell.FileUtils; +import java.io.IOException; import org.apache.zest.tools.shell.HelpNeededException; import org.apache.zest.tools.shell.TestHelper; import org.junit.Test; @@ -52,19 +52,14 @@ public class CreateProjectTest } @Test - public void givenCommandWhenTemplateExistExpectCreatedProject() + public void givenCommandWhenTemplateExistExpectCreatedProject() throws IOException { - TestHelper.zetZestZome(); + TestHelper.setZestZome(); File dest = new File( "ZestTest" ); new CreateProject().execute( new String[]{ "create-project", "null", "ZestTest", "org.apache.zest" }, null, null ); assertThat( dest.exists(), equalTo( true ) ); File file = new File( dest, "src/main/java/org/apache/zest/package.html" ); assertThat( file.exists(), equalTo( true ) ); - if( ! FileUtils.removeDir( dest ) ) - { - System.err.println( "Unable to remove file. Why???" ); - } - assertThat( dest.exists(), equalTo( false ) ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/e504fe4a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/DefaultProjectCreatorTest.java ---------------------------------------------------------------------- diff --git a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/DefaultProjectCreatorTest.java b/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/DefaultProjectCreatorTest.java index b269485..70a4bf1 100644 --- a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/DefaultProjectCreatorTest.java +++ b/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/DefaultProjectCreatorTest.java @@ -23,13 +23,12 @@ package org.apache.zest.tools.shell.create.project; import java.io.File; import java.util.HashMap; import java.util.Map; -import org.apache.zest.tools.shell.FileUtils; import org.apache.zest.tools.shell.TestHelper; import org.junit.Test; -import static org.apache.zest.tools.shell.FileUtils.removeDir; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class DefaultProjectCreatorTest { @@ -40,12 +39,8 @@ public class DefaultProjectCreatorTest throws Exception { - TestHelper.zetZestZome(); + TestHelper.setZestZome(); File projectDir = new File( "ZestTest" ); - if( projectDir.exists() ) - { - removeDir( projectDir ); - } Map<String, String> properties = new HashMap<>(); properties.put( "zest.home", System.getProperty( "zest.home" ) ); properties.put( "root.package", "org.apache.zest.test" ); @@ -96,13 +91,9 @@ public class DefaultProjectCreatorTest assertThat( new File( projectDir, "gradle/wrapper/gradle-wrapper.jar" ).exists(), equalTo( true ) ); assertThat( new File( projectDir, "gradle/wrapper/gradle-wrapper.properties" ).exists(), equalTo( true ) ); assertThat( new File( projectDir, "gradlew" ).exists(), equalTo( true ) ); + assertTrue( new File( projectDir, "gradlew" ).canExecute() ); assertThat( new File( projectDir, "gradlew.bat" ).exists(), equalTo( true ) ); assertThat( new File( projectDir, "build.gradle" ).exists(), equalTo( true ) ); assertThat( new File( projectDir, "settings.gradle" ).exists(), equalTo( true ) ); - if( !FileUtils.removeDir( projectDir ) ) - { - System.err.println( "Unable to remove file. Why???" ); - } - assertThat( projectDir.exists(), equalTo( false ) ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/e504fe4a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/NullProjectCreatorTest.java ---------------------------------------------------------------------- diff --git a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/NullProjectCreatorTest.java b/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/NullProjectCreatorTest.java index e0a470a..362fa3c 100644 --- a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/NullProjectCreatorTest.java +++ b/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/NullProjectCreatorTest.java @@ -23,11 +23,9 @@ package org.apache.zest.tools.shell.create.project; import java.io.File; import java.util.HashMap; import java.util.Map; -import org.apache.zest.tools.shell.FileUtils; import org.apache.zest.tools.shell.TestHelper; import org.junit.Test; -import static org.apache.zest.tools.shell.FileUtils.removeDir; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; @@ -40,12 +38,8 @@ public class NullProjectCreatorTest throws Exception { - TestHelper.zetZestZome(); + TestHelper.setZestZome(); File projectDir = new File( "ZestTest" ); - if( projectDir.exists() ) - { - removeDir( projectDir ); - } Map<String, String> properties = new HashMap<>(); properties.put( "zest.home", System.getProperty( "zest.home" ) ); properties.put( "root.package", "org.apache.zest.test" ); @@ -57,10 +51,5 @@ public class NullProjectCreatorTest assertThat( new File( projectDir, "gradlew" ).exists(), equalTo( true ) ); assertThat( new File( projectDir, "gradlew.bat" ).exists(), equalTo( true ) ); assertThat( new File( projectDir, "build.gradle" ).exists(), equalTo( true ) ); - if( ! FileUtils.removeDir( projectDir ) ) - { - System.err.println( "Unable to remove file. Why???" ); - } - assertThat( projectDir.exists(), equalTo( false ) ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/e504fe4a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/RestProjectCreatorTest.java ---------------------------------------------------------------------- diff --git a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/RestProjectCreatorTest.java b/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/RestProjectCreatorTest.java index e99695d..e4a37f5 100644 --- a/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/RestProjectCreatorTest.java +++ b/tools/shell/src/test/java/org/apache/zest/tools/shell/create/project/RestProjectCreatorTest.java @@ -23,11 +23,9 @@ package org.apache.zest.tools.shell.create.project; import java.io.File; import java.util.HashMap; import java.util.Map; -import org.apache.zest.tools.shell.FileUtils; import org.apache.zest.tools.shell.TestHelper; import org.junit.Test; -import static org.apache.zest.tools.shell.FileUtils.removeDir; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; @@ -40,12 +38,8 @@ public class RestProjectCreatorTest throws Exception { - TestHelper.zetZestZome(); + TestHelper.setZestZome(); File projectDir = new File( "ZestTest" ); - if( projectDir.exists() ) - { - removeDir( projectDir ); - } Map<String, String> properties = new HashMap<>(); properties.put( "zest.home", System.getProperty( "zest.home" ) ); properties.put( "root.package", "org.apache.zest.test" ); @@ -110,10 +104,5 @@ public class RestProjectCreatorTest assertThat( new File( projectDir, "gradlew.bat" ).exists(), equalTo( true ) ); assertThat( new File( projectDir, "build.gradle" ).exists(), equalTo( true ) ); assertThat( new File( projectDir, "settings.gradle" ).exists(), equalTo( true ) ); - if( ! FileUtils.removeDir( projectDir ) ) - { - System.err.println( "Unable to remove file. Why???" ); - } - assertThat( projectDir.exists(), equalTo( false ) ); } }
