Example usage for org.apache.commons.io FileUtils toFile

List of usage examples for org.apache.commons.io FileUtils toFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils toFile.

Prototype

public static File toFile(URL url) 

Source Link

Document

Convert from a URL to a File.

Usage

From source file:org.rhq.core.pc.plugin.PluginManagerTest.java

void assertPluginJarFileExists(String pluginPath) {
    URL pluginURL = getClass().getResource(pluginPath);
    File pluginJarFile = FileUtils.toFile(pluginURL);

    assertTrue(pluginJarFile.exists(), pluginJarFile.getAbsolutePath() + " does not exist and is needed for "
            + "tests in " + PluginManagerTest.class.getName());
}

From source file:org.sonar.api.utils.ZipUtilsTest.java

@Test
public void shouldZipDirectory() throws IOException {
    File foo = FileUtils
            .toFile(getClass().getResource("/org/sonar/api/utils/ZipUtilsTest/shouldZipDirectory/foo.txt"));
    File dir = foo.getParentFile();
    File zip = new File("target/tmp/shouldZipDirectory.zip");

    ZipUtils.zipDir(dir, zip);//from  w  ww. j  a  v  a  2  s.  c o m

    assertThat(zip).exists();
    assertThat(zip.length()).isGreaterThan(1L);
    Iterator<? extends ZipEntry> zipEntries = Iterators.forEnumeration(new ZipFile(zip).entries());
    assertThat(zipEntries).hasSize(4);
}

From source file:org.sonar.api.utils.ZipUtilsTest.java

@Test
public void shouldUnzipFile() throws IOException {
    File zip = FileUtils
            .toFile(getClass().getResource("/org/sonar/api/utils/ZipUtilsTest/shouldUnzipFile.zip"));
    File toDir = new File("target/tmp/shouldUnzipFile/");
    ZipUtils.unzip(zip, toDir);//from   ww  w  . ja  v  a 2 s  . co  m
    assertThat(toDir.list()).hasSize(3);
}

From source file:org.sonar.batch.bootstrap.BatchPluginExploderTest.java

File getFileFromCache(String filename) throws IOException {
    File src = FileUtils.toFile(BatchPluginExploderTest.class
            .getResource("/org/sonar/batch/bootstrap/BatchPluginUnzipperTest/" + filename));
    File destFile = new File(new File(userHome, "" + filename.hashCode()), filename);
    FileUtils.copyFile(src, destFile);//w  w w  . j a v  a  2s  .c o  m
    return destFile;
}

From source file:org.sonar.batch.bootstrap.BatchPluginJarExploderTest.java

File getFileFromCache(String filename) throws IOException {
    File src = FileUtils.toFile(getClass().getResource(this.getClass().getSimpleName() + "/" + filename));
    File destFile = new File(new File(userHome, "" + filename.hashCode()), filename);
    FileUtils.copyFile(src, destFile);//from www.  j a  v  a 2s. com
    return destFile;
}

From source file:org.sonar.batch.bootstrap.BatchPluginJarInstallerTest.java

File getFileFromCache(String filename) throws IOException {
    File src = FileUtils.toFile(BatchPluginJarInstallerTest.class
            .getResource("/org/sonar/batch/bootstrap/BatchPluginJarInstallerTest/" + filename));
    File destFile = new File(new File(userHome, "" + filename.hashCode()), filename);
    FileUtils.copyFile(src, destFile);/*  w  w  w.  ja v a2s .  co m*/
    return destFile;
}

From source file:org.sonar.batch.DefaultProjectFileSystem2Test.java

@Test
public void shouldIgnoreInexistingSourceDirs() {
    File exists = FileUtils.toFile(getClass()
            .getResource("/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs"));
    File notExists = new File("target/unknown");

    ProjectDefinition definition = ProjectDefinition.create().addSourceDirs(exists, notExists);

    DefaultProjectFileSystem2 fs = new DefaultProjectFileSystem2(new Project("foo"), new Languages(),
            definition);/*from www . ja v  a 2s  .com*/
    assertThat(fs.getSourceDirs().size(), Is.is(1));
    assertThat(fs.getSourceDirs(), hasItem(exists));

}

From source file:org.sonar.batch.scan.ProjectReactorBuilderTest.java

/**
 * Search for a test resource in the classpath. For example getResource("org/sonar/MyClass/foo.txt");
 *
 * @param path the starting slash is optional
 * @return the resource. Null if resource not found
 *//*from  w  w w  . ja v a  2s.  c o m*/
public static File getResource(String path) {
    String resourcePath = path;
    if (!resourcePath.startsWith("/")) {
        resourcePath = "/" + resourcePath;
    }
    URL url = ProjectReactorBuilderTest.class.getResource(resourcePath);
    if (url != null) {
        return FileUtils.toFile(url);
    }
    return null;
}

From source file:org.sonar.c.checks.CheckUtils.java

public static Set<CheckMessage> extractViolations(String cFilePath, CAstVisitor... visitors) {
    SourceProject project = new SourceProject("cProject");
    SquidIndex index = new SquidIndex();
    index.index(project);/* w  ww  .  ja  v a  2s. c  o  m*/
    CAstScanner scanner = new CAstScanner(project, new CConfiguration());

    registerDefaultVisitors(scanner);
    registerVisitors(scanner, visitors);

    File cFileToTest = FileUtils.toFile(CheckUtils.class.getResource(cFilePath));
    if (cFileToTest == null || !cFileToTest.exists()) {
        throw new AssertionError("The c file to test '" + cFilePath + "' doesn't exist.");
    }
    scanner.scanFile(cFileToTest);

    SourceCodeTreeDecorator decorator = new SourceCodeTreeDecorator(project);
    decorator.decorateWith(CMetric.values());
    Collection<SourceCode> sources = index.search(new QueryByType(SourceFile.class));
    if (sources.size() != 1) {
        throw new AssertionError(
                "Only one SourceFile was expected whereas " + sources.size() + " has been returned.");
    } else {
        SourceFile file = (SourceFile) sources.iterator().next();
        return file.getCheckMessages();
    }
}

From source file:org.sonar.colorizer.HtmlRendererTest.java

@Test
public void renderJavaFile() throws IOException {
    File java = FileUtils.toFile(getClass().getResource("/org/sonar/colorizer/HtmlRendererTest/Sample.java"));

    String html = new HtmlRenderer().render(new FileReader(java), Arrays.asList(javaKeywordTokenizer));

    assertThat(html, containsString("<html>"));
    assertThat(html, containsString("<style"));
    assertThat(html, containsString("<table class=\"code\""));
    assertThat(html, containsString("public"));
    assertThat(html, containsString("class"));
    assertThat(html, containsString("Sample"));
    assertThat(html, containsString("</html>"));
}