Search in sources :

Example 61 with FileFilter

use of java.io.FileFilter in project opennms by OpenNMS.

the class TemporaryDatabasePostgreSQL method findIpLikeLibrary.

protected File findIpLikeLibrary() {
    File topDir = ConfigurationTestUtils.getTopProjectDirectory();
    File ipLikeDir = new File(topDir, "opennms-iplike");
    assertTrue("iplike directory exists at ../opennms-iplike: " + ipLikeDir.getAbsolutePath(), ipLikeDir.exists());
    File[] ipLikePlatformDirs = ipLikeDir.listFiles(new FileFilter() {

        @Override
        public boolean accept(File file) {
            if (file.getName().matches("opennms-iplike-.*") && file.isDirectory()) {
                return true;
            } else {
                return false;
            }
        }
    });
    assertTrue("expecting at least one opennms iplike platform directory in " + ipLikeDir.getAbsolutePath() + "; got: " + StringUtils.arrayToDelimitedString(ipLikePlatformDirs, ", "), ipLikePlatformDirs.length > 0);
    File ipLikeFile = null;
    for (File ipLikePlatformDir : ipLikePlatformDirs) {
        assertTrue("iplike platform directory does not exist but was listed in directory listing: " + ipLikePlatformDir.getAbsolutePath(), ipLikePlatformDir.exists());
        File ipLikeTargetDir = new File(ipLikePlatformDir, "target");
        if (!ipLikeTargetDir.exists() || !ipLikeTargetDir.isDirectory()) {
            // Skip this one
            continue;
        }
        File[] ipLikeFiles = ipLikeTargetDir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File file) {
                if (file.isFile() && file.getName().matches("opennms-iplike-.*\\.(so|dylib)")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        assertFalse("expecting zero or one iplike file in " + ipLikeTargetDir.getAbsolutePath() + "; got: " + StringUtils.arrayToDelimitedString(ipLikeFiles, ", "), ipLikeFiles.length > 1);
        if (ipLikeFiles.length == 1) {
            ipLikeFile = ipLikeFiles[0];
        }
    }
    assertNotNull("Could not find iplike shared object in a target directory in any of these directories: " + StringUtils.arrayToDelimitedString(ipLikePlatformDirs, ", "), ipLikeFile);
    return ipLikeFile;
}
Also used : FileFilter(java.io.FileFilter) File(java.io.File)

Example 62 with FileFilter

use of java.io.FileFilter in project jdk8u_jdk by JetBrains.

the class NulFile method test.

@SuppressWarnings("deprecation")
private static void test(File testFile, boolean derived) {
    boolean exceptionThrown = false;
    if (testFile == null) {
        throw new RuntimeException("test file should not be null.");
    }
    // getPath()
    if (testFile.getPath().indexOf(CHAR_NUL) < 0) {
        throw new RuntimeException("File path should contain Nul character");
    }
    // getAbsolutePath()
    if (testFile.getAbsolutePath().indexOf(CHAR_NUL) < 0) {
        throw new RuntimeException("File absolute path should contain Nul character");
    }
    // getAbsoluteFile()
    File derivedAbsFile = testFile.getAbsoluteFile();
    if (derived) {
        if (derivedAbsFile.getPath().indexOf(CHAR_NUL) < 0) {
            throw new RuntimeException("Derived file path should also contain Nul character");
        }
    } else {
        test(derivedAbsFile, true);
    }
    // getCanonicalPath()
    try {
        exceptionThrown = false;
        testFile.getCanonicalPath();
    } catch (IOException ex) {
        if (ExceptionMsg.equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("getCanonicalPath() should throw IOException with" + " message \"" + ExceptionMsg + "\"");
    }
    // getCanonicalFile()
    try {
        exceptionThrown = false;
        testFile.getCanonicalFile();
    } catch (IOException ex) {
        if (ExceptionMsg.equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("getCanonicalFile() should throw IOException with" + " message \"" + ExceptionMsg + "\"");
    }
    // toURL()
    try {
        exceptionThrown = false;
        testFile.toURL();
    } catch (MalformedURLException ex) {
        if (ExceptionMsg.equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("toURL() should throw IOException with" + " message \"" + ExceptionMsg + "\"");
    }
    // canRead()
    if (testFile.canRead())
        throw new RuntimeException("File should not be readable");
    // canWrite()
    if (testFile.canWrite())
        throw new RuntimeException("File should not be writable");
    // exists()
    if (testFile.exists())
        throw new RuntimeException("File should not be existed");
    // isDirectory()
    if (testFile.isDirectory())
        throw new RuntimeException("File should not be a directory");
    // isFile()
    if (testFile.isFile())
        throw new RuntimeException("File should not be a file");
    // isHidden()
    if (testFile.isHidden())
        throw new RuntimeException("File should not be hidden");
    // lastModified()
    if (testFile.lastModified() != 0L)
        throw new RuntimeException("File last modified time should be 0L");
    // length()
    if (testFile.length() != 0L)
        throw new RuntimeException("File length should be 0L");
    // createNewFile()
    try {
        exceptionThrown = false;
        testFile.createNewFile();
    } catch (IOException ex) {
        if (ExceptionMsg.equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("createNewFile() should throw IOException with" + " message \"" + ExceptionMsg + "\"");
    }
    // delete()
    if (testFile.delete())
        throw new RuntimeException("Delete operation should fail");
    // list()
    if (testFile.list() != null)
        throw new RuntimeException("File list() should return null");
    // list(FilenameFilter)
    FilenameFilter fnFilter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return false;
        }
    };
    if (testFile.list(fnFilter) != null) {
        throw new RuntimeException("File list(FilenameFilter) should" + " return null");
    }
    // listFiles()
    if (testFile.listFiles() != null)
        throw new RuntimeException("File listFiles() should return null");
    // listFiles(FilenameFilter)
    if (testFile.listFiles(fnFilter) != null) {
        throw new RuntimeException("File listFiles(FilenameFilter)" + " should return null");
    }
    // listFiles(FileFilter)
    FileFilter fFilter = new FileFilter() {

        @Override
        public boolean accept(File file) {
            return false;
        }
    };
    if (testFile.listFiles(fFilter) != null) {
        throw new RuntimeException("File listFiles(FileFilter)" + " should return null");
    }
    // mkdir()
    if (testFile.mkdir()) {
        throw new RuntimeException("File should not be able to" + " create directory");
    }
    // mkdirs()
    if (testFile.mkdirs()) {
        throw new RuntimeException("File should not be able to" + " create directories");
    }
    // renameTo(File)
    if (testFile.renameTo(new File("dest")))
        throw new RuntimeException("File rename should fail");
    if (new File("dest").renameTo(testFile))
        throw new RuntimeException("File rename should fail");
    try {
        exceptionThrown = false;
        testFile.renameTo(null);
    } catch (NullPointerException ex) {
        exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("File rename should thrown NPE");
    }
    // setLastModified(long)
    if (testFile.setLastModified(0L)) {
        throw new RuntimeException("File should fail to set" + " last modified time");
    }
    try {
        exceptionThrown = false;
        testFile.setLastModified(-1);
    } catch (IllegalArgumentException ex) {
        if ("Negative time".equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("File should fail to set" + " last modified time with message \"Negative time\"");
    }
    // setReadOnly()
    if (testFile.setReadOnly())
        throw new RuntimeException("File should fail to set read-only");
    // setWritable(boolean writable, boolean ownerOnly)
    if (testFile.setWritable(true, true))
        throw new RuntimeException("File should fail to set writable");
    if (testFile.setWritable(true, false))
        throw new RuntimeException("File should fail to set writable");
    if (testFile.setWritable(false, true))
        throw new RuntimeException("File should fail to set writable");
    if (testFile.setWritable(false, false))
        throw new RuntimeException("File should fail to set writable");
    // setWritable(boolean writable)
    if (testFile.setWritable(false))
        throw new RuntimeException("File should fail to set writable");
    if (testFile.setWritable(true))
        throw new RuntimeException("File should fail to set writable");
    // setReadable(boolean readable, boolean ownerOnly)
    if (testFile.setReadable(true, true))
        throw new RuntimeException("File should fail to set readable");
    if (testFile.setReadable(true, false))
        throw new RuntimeException("File should fail to set readable");
    if (testFile.setReadable(false, true))
        throw new RuntimeException("File should fail to set readable");
    if (testFile.setReadable(false, false))
        throw new RuntimeException("File should fail to set readable");
    // setReadable(boolean readable)
    if (testFile.setReadable(false))
        throw new RuntimeException("File should fail to set readable");
    if (testFile.setReadable(true))
        throw new RuntimeException("File should fail to set readable");
    // setExecutable(boolean executable, boolean ownerOnly)
    if (testFile.setExecutable(true, true))
        throw new RuntimeException("File should fail to set executable");
    if (testFile.setExecutable(true, false))
        throw new RuntimeException("File should fail to set executable");
    if (testFile.setExecutable(false, true))
        throw new RuntimeException("File should fail to set executable");
    if (testFile.setExecutable(false, false))
        throw new RuntimeException("File should fail to set executable");
    // setExecutable(boolean executable)
    if (testFile.setExecutable(false))
        throw new RuntimeException("File should fail to set executable");
    if (testFile.setExecutable(true))
        throw new RuntimeException("File should fail to set executable");
    // canExecute()
    if (testFile.canExecute())
        throw new RuntimeException("File should not be executable");
    // getTotalSpace()
    if (testFile.getTotalSpace() != 0L)
        throw new RuntimeException("The total space should be 0L");
    // getFreeSpace()
    if (testFile.getFreeSpace() != 0L)
        throw new RuntimeException("The free space should be 0L");
    // getUsableSpace()
    if (testFile.getUsableSpace() != 0L)
        throw new RuntimeException("The usable space should be 0L");
    // compareTo(File null)
    try {
        exceptionThrown = false;
        testFile.compareTo(null);
    } catch (NullPointerException ex) {
        exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("compareTo(null) should throw NPE");
    }
    // toString()
    if (testFile.toString().indexOf(CHAR_NUL) < 0) {
        throw new RuntimeException("File path should contain Nul character");
    }
    // toPath()
    try {
        exceptionThrown = false;
        testFile.toPath();
    } catch (InvalidPathException ex) {
        exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("toPath() should throw" + " InvalidPathException");
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileFilter(java.io.FileFilter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) InvalidPathException(java.nio.file.InvalidPathException)

Example 63 with FileFilter

use of java.io.FileFilter in project voldemort by voldemort.

the class HadoopStoreBuilderTest method testRowsLessThanNodes.

/**
     * Issue 258 : 'node--1' produced during store building if some reducer does
     * not get any data.
     *
     * @throws Exception
     */
@Test
public void testRowsLessThanNodes() throws Exception {
    Map<String, String> values = new HashMap<String, String>();
    File testDir = TestUtils.createTempDir();
    File tempDir = new File(testDir, "temp");
    File outputDir = new File(testDir, "output");
    // write test data to text file
    File inputFile = File.createTempFile("input", ".txt", testDir);
    inputFile.deleteOnExit();
    StringBuilder contents = new StringBuilder();
    for (Map.Entry<String, String> entry : values.entrySet()) contents.append(entry.getKey() + "\t" + entry.getValue() + "\n");
    FileUtils.writeStringToFile(inputFile, contents.toString());
    String storeName = "test";
    SerializerDefinition serDef = new SerializerDefinition("string");
    Cluster cluster = ServerTestUtils.getLocalCluster(10);
    // Test backwards compatibility
    StoreDefinition def = new StoreDefinitionBuilder().setName(storeName).setType(ReadOnlyStorageConfiguration.TYPE_NAME).setKeySerializer(serDef).setValueSerializer(serDef).setRoutingPolicy(RoutingTier.CLIENT).setRoutingStrategyType(RoutingStrategyType.CONSISTENT_STRATEGY).setReplicationFactor(1).setPreferredReads(1).setRequiredReads(1).setPreferredWrites(1).setRequiredWrites(1).build();
    HadoopStoreBuilder builder = new HadoopStoreBuilder("testRowsLessThanNodes", new Props(), new JobConf(), TextStoreMapper.class, TextInputFormat.class, cluster, def, new Path(tempDir.getAbsolutePath()), new Path(outputDir.getAbsolutePath()), new Path(inputFile.getAbsolutePath()), CheckSumType.MD5, saveKeys, false, 64 * 1024, false, 0L, false);
    builder.build();
    File[] nodeDirectories = outputDir.listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            // We are only interested in counting directories, not files.
            return pathname.isDirectory();
        }
    });
    // Should not produce node--1 directory + have one folder for every node
    Assert.assertEquals(cluster.getNumberOfNodes(), nodeDirectories.length);
    for (File f : outputDir.listFiles()) {
        Assert.assertFalse(f.toString().contains("node--1"));
    }
    // Check if individual nodes exist, along with their metadata file
    for (int nodeId = 0; nodeId < 10; nodeId++) {
        File nodeFile = new File(outputDir, "node-" + Integer.toString(nodeId));
        Assert.assertTrue(nodeFile.exists());
        Assert.assertTrue(new File(nodeFile, ".metadata").exists());
    }
}
Also used : StoreDefinitionBuilder(voldemort.store.StoreDefinitionBuilder) Path(org.apache.hadoop.fs.Path) HashMap(java.util.HashMap) Cluster(voldemort.cluster.Cluster) Props(voldemort.utils.Props) StoreDefinition(voldemort.store.StoreDefinition) FileFilter(java.io.FileFilter) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) JobConf(org.apache.hadoop.mapred.JobConf) SerializerDefinition(voldemort.serialization.SerializerDefinition) Test(org.junit.Test)

Example 64 with FileFilter

use of java.io.FileFilter in project Small by wequick.

the class AnalyticsManager method getNumCores.

private static int getNumCores() {
    // Private Class to display only CPU devices in the directory listing
    class CpuFilter implements FileFilter {

        @Override
        public boolean accept(File pathname) {
            // Check if filename is "cpu", followed by a single digit number
            return (Pattern.matches("cpu[0-9]", pathname.getName()));
        }
    }
    try {
        // Get directory containing CPU info
        File dir = new File("/sys/devices/system/cpu/");
        // Filter to only list the devices we care about
        File[] files = dir.listFiles(new CpuFilter());
        // Return the number of cores (virtual CPU devices)
        return files.length;
    } catch (Exception e) {
        // Default to return 1 core
        return 1;
    }
}
Also used : FileFilter(java.io.FileFilter) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 65 with FileFilter

use of java.io.FileFilter in project intellij-community by JetBrains.

the class FilteredResourceRootDescriptor method createFileFilter.

@NotNull
@Override
public FileFilter createFileFilter() {
    final JpsProject project = getTarget().getModule().getProject();
    final JpsJavaCompilerConfiguration configuration = JpsJavaExtensionService.getInstance().getOrCreateCompilerConfiguration(project);
    final JpsCompilerExcludes excludes = configuration.getCompilerExcludes();
    return file -> !excludes.isExcluded(file) && configuration.isResourceFile(file, getRootFile());
}
Also used : JpsProject(org.jetbrains.jps.model.JpsProject) JpsCompilerExcludes(org.jetbrains.jps.model.java.compiler.JpsCompilerExcludes) FileFilter(java.io.FileFilter) JpsJavaCompilerConfiguration(org.jetbrains.jps.model.java.compiler.JpsJavaCompilerConfiguration) ResourcesTarget(org.jetbrains.jps.incremental.ResourcesTarget) Set(java.util.Set) JpsJavaExtensionService(org.jetbrains.jps.model.java.JpsJavaExtensionService) NotNull(org.jetbrains.annotations.NotNull) File(java.io.File) JpsJavaCompilerConfiguration(org.jetbrains.jps.model.java.compiler.JpsJavaCompilerConfiguration) JpsCompilerExcludes(org.jetbrains.jps.model.java.compiler.JpsCompilerExcludes) JpsProject(org.jetbrains.jps.model.JpsProject) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FileFilter (java.io.FileFilter)281 File (java.io.File)267 ArrayList (java.util.ArrayList)59 IOException (java.io.IOException)53 JarFile (java.util.jar.JarFile)22 URL (java.net.URL)17 Test (org.junit.Test)16 HashMap (java.util.HashMap)15 FileNotFoundException (java.io.FileNotFoundException)11 FilenameFilter (java.io.FilenameFilter)11 HashSet (java.util.HashSet)11 FileInputStream (java.io.FileInputStream)10 Pattern (java.util.regex.Pattern)10 WildcardFileFilter (org.apache.commons.io.filefilter.WildcardFileFilter)10 MalformedURLException (java.net.MalformedURLException)9 URI (java.net.URI)9 Map (java.util.Map)7 Path (org.apache.hadoop.fs.Path)7 NotNull (org.jetbrains.annotations.NotNull)7 Treebank (edu.stanford.nlp.trees.Treebank)6