Example usage for java.nio.file Files isExecutable

List of usage examples for java.nio.file Files isExecutable

Introduction

In this page you can find the example usage for java.nio.file Files isExecutable.

Prototype

public static boolean isExecutable(Path path) 

Source Link

Document

Tests whether a file is executable.

Usage

From source file:uk.co.unclealex.executable.generator.ScripterImplTest.java

@Test
public void testScript() throws IOException, URISyntaxException, ExecutableScanException {
    Path classesDir = tempDir.resolve("classes");
    Path scripterClassesDir = classesDir.resolve("scripter");
    final Path workDir = tempDir.resolve("work");
    Path homeDir = tempDir.resolve("home");
    for (Path dir : new Path[] { scripterClassesDir, workDir, homeDir }) {
        Files.createDirectories(dir);
    }/*from w  w  w .j av a2s.  c  om*/
    generateClasses("First", "first", false, scripterClassesDir);
    generateClasses("Second", "second", true, scripterClassesDir);
    Injector injector = Guice.createInjector(new CodeGeneratorModule());
    Scripter scripter = injector.getInstance(Scripter.class);
    final JarService jarService = injector.getInstance(JarService.class);

    Path scriptPath = homeDir.resolve("script.sh");
    Function<Class<?>, Path> jarFunction = new Function<Class<?>, Path>() {
        @Override
        public Path apply(Class<?> clazz) {
            try {
                return jarService.findJarFileOrDirectoryDefiningClass(getClass().getClassLoader(),
                        clazz.getName());
            } catch (IOException e) {
                e.printStackTrace(System.err);
                Assert.fail("Could not find a jar file for class " + clazz);
                return null;
            }
        }
    };
    @SuppressWarnings("unchecked")
    List<Class<?>> classes = Lists.newArrayList(CommandLineInterface.class, Lists.class, Executable.class);
    Iterable<Path> jarFiles = Iterables.transform(classes, jarFunction);
    scripter.generate(scriptPath, classesDir, homeDir.toString(), "999", workDir, jarFiles);
    Assert.assertTrue("The generated script was not executable.", Files.isExecutable(scriptPath));
    checkRun(homeDir, scriptPath, "[One, Two, Three]\n", "", 0, "--runas", "first", "One", "Two", "Three");
    checkRun(homeDir, scriptPath, "[Three, Two, One]\n", "", 0, "--runas", "second", "One", "Two", "Three");
    checkRun(homeDir, scriptPath, "", "Exactly one of --links, --show or --runas must be specified.\n", 2);
}