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.sonar.plugins.scala.cpd.ScalaTokenizerTest.java

private File resourceToFile(String path) {
    return FileUtils.toFile(getClass().getResource(path));
}

From source file:org.sonar.process.BaseProcessTest.java

@Before
public void setup() throws IOException {
    ServerSocket socket = new ServerSocket(0);
    freePort = socket.getLocalPort();//from ww w. j  a  v a2  s. co m
    socket.close();

    dummyAppJar = FileUtils.toFile(getClass().getResource("/sonar-dummy-app.jar"));
}

From source file:org.sonar.server.mavendeployer.MavenRepositoryTest.java

private File getFile(String path) {
    URL url = getClass().getResource("/org/sonar/server/mavendeployer/MavenRepositoryTest/extensions/" + path);
    return FileUtils.toFile(url);
}

From source file:org.sonar.server.mavendeployer.MojoTest.java

@Test
public void shouldCopyJar() throws IOException {
    File jar = FileUtils
            .toFile(getClass().getResource("/org/sonar/server/mavendeployer/MojoTest/shouldCopyJar.jar"));
    Mojo mojo = Mojo.createMaven2Plugin("1.0", jar);
    File toDir = new File("target/test-tmp/MojoTest/");
    mojo.copyTo(toDir);/*from  ww w. ja  v a  2  s. c  om*/
    assertThat(new File(toDir, "shouldCopyJar.jar").exists(), is(true));
}

From source file:org.sonar.server.mavendeployer.MojoTest.java

@Test
public void shouldDeleteTemporaryDirectory() throws IOException {
    String version = "" + System.currentTimeMillis();
    assertThat(getTmpDir(version).exists(), is(false));

    File jar = FileUtils
            .toFile(getClass().getResource("/org/sonar/server/mavendeployer/MojoTest/shouldCopyJar.jar"));
    Mojo mojo = Mojo.createMaven2Plugin(version, jar);
    File toDir = new File("target/test-tmp/MojoTest/");
    mojo.copyTo(toDir);/*from   www. java  2  s.co  m*/
    assertThat(new File(toDir, "shouldCopyJar.jar").exists(), is(true));
    assertThat(getTmpDir(version).exists(), is(false));
}

From source file:org.sonar.server.plugins.DefaultServerPluginRepositoryTest.java

@Test
public void testStart() {
    PluginDeployer deployer = mock(PluginDeployer.class);
    File pluginFile = FileUtils.toFile(getClass().getResource(
            "/org/sonar/server/plugins/DefaultServerPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar"));
    PluginMetadata plugin = DefaultPluginMetadata.create(pluginFile).setKey("artifactsize")
            .setMainClass("org.sonar.plugins.artifactsize.ArtifactSizePlugin").addDeployedFile(pluginFile);
    when(deployer.getMetadata()).thenReturn(Arrays.asList(plugin));

    repository = new DefaultServerPluginRepository(deployer);
    repository.start();//w w w . j a  v  a  2 s  .c o  m

    assertThat(repository.getPlugins().size(), Is.is(1));
    assertThat(repository.getPlugin("artifactsize"), not(nullValue()));
    assertThat(repository.getClassloader("artifactsize"), not(nullValue()));
    assertThat(repository.getClass("artifactsize", "org.sonar.plugins.artifactsize.ArtifactSizeMetrics"),
            not(nullValue()));
    assertThat(repository.getClass("artifactsize", "org.Unknown"), nullValue());
    assertThat(repository.getClass("other", "org.sonar.plugins.artifactsize.ArtifactSizeMetrics"), nullValue());
}

From source file:org.sonar.server.plugins.PluginDeployerTest.java

@Test
public void deployPluginExtensions() throws IOException {
    setupData("shared");
    deployer.start();/*from  ww w  .j av  a 2  s  .c  o  m*/

    // check that the plugin is registered in database
    List<JpaPlugin> plugins = dao.getPlugins();
    assertThat(plugins.size(), is(1)); // no more checkstyle
    JpaPlugin plugin = plugins.get(0);
    assertThat(plugin.getFiles().size(), is(2));
    JpaPluginFile pluginFile = plugin.getFiles().get(1);
    assertThat(pluginFile.getFilename(), is("foo-extension.txt"));
    assertThat(pluginFile.getPath(), is("foo/foo-extension.txt"));

    // check that the extension file is deployed
    File deployedJar = new File(deployDir, "plugins/foo/foo-extension.txt");
    assertThat(deployedJar.exists(), is(true));
    assertThat(deployedJar.isFile(), is(true));

    // check that the extension is in the classloader
    classloaders.completeCreation();
    ClassLoader classloader = classloaders.getClassLoader("foo");
    File extensionFile = FileUtils.toFile(classloader.getResource("foo-extension.txt"));
    assertThat(extensionFile.exists(), is(true));
}

From source file:org.sonar.server.plugins.TestProjectUtils.java

/**
 * Get the artifact of plugins stored in src/test/projects
 *///from  ww w  .j av  a 2 s  .c o m
public static File jarOf(String dirName) {
    File target = FileUtils.toFile(TestProjectUtils.class.getResource(String.format("/%s/target/", dirName)));
    Collection<File> jars = FileUtils.listFiles(target, new String[] { "jar" }, false);
    if (jars == null || jars.size() != 1) {
        throw new IllegalArgumentException("Test project is badly defined: " + dirName);
    }
    return jars.iterator().next();
}

From source file:org.sonar.squidbridge.checks.ResourceParser.java

public static SourceFile scanFile(String filePath, SquidAstVisitor<Grammar>... visitors) {
    AstScanner<Grammar> scanner = MiniCAstScanner.create(visitors);
    File file = FileUtils.toFile(ResourceParser.class.getResource(filePath));
    if (file == null || !file.exists()) {
        throw new IllegalArgumentException("The file located under \"" + filePath + "\" was not found.");
    }/*from ww w. ja  v  a 2 s  . c  om*/
    scanner.scanFile(file);
    assertThat(scanner.getIndex().search(new QueryByType(SourceFile.class)).size()).isEqualTo(1);
    return (SourceFile) scanner.getIndex().search(new QueryByType(SourceFile.class)).iterator().next();
}

From source file:org.sonar.squidbridge.checks.ViolationCounterCheckTest.java

@Test
public void test() throws Exception {
    String projectsDir = FileUtils.toFile(ResourceParser.class.getResource("/checks/parse_error.mc"))
            .getParentFile().getAbsolutePath();

    File output = temporaryFolder.newFile();

    ViolationCounterCheck.ViolationCounter counter = new ViolationCounterCheck.ViolationCounter();
    ViolationCounterCheck<Grammar> violationCounterCheck = new ViolationCounterCheck<Grammar>(projectsDir,
            counter);/*from w  w w  .jav a  2  s . co m*/

    AbstractParseErrorCheck<Grammar> parseErrorCheck = new AbstractParseErrorCheck<Grammar>() {
    };

    scanFile("/checks/parse_error.mc", parseErrorCheck, violationCounterCheck);

    counter.saveToFile(output.getAbsolutePath());
    assertThat(output).isFile();

    ViolationCounterCheck.ViolationDifferenceAnalyzer analyzer = new ViolationCounterCheck.ViolationDifferenceAnalyzer(
            new ViolationCounterCheck.ViolationCounter(),
            ViolationCounterCheck.ViolationCounter.loadFromFile(output));
    analyzer.printReport();
    assertThat(analyzer.hasDifferences()).isTrue();

    analyzer = new ViolationCounterCheck.ViolationDifferenceAnalyzer(
            ViolationCounterCheck.ViolationCounter.loadFromFile(output),
            new ViolationCounterCheck.ViolationCounter());
    analyzer.printReport();
    assertThat(analyzer.hasDifferences()).isTrue();

    analyzer = new ViolationCounterCheck.ViolationDifferenceAnalyzer(
            ViolationCounterCheck.ViolationCounter.loadFromFile(output),
            ViolationCounterCheck.ViolationCounter.loadFromFile(output));
    analyzer.printReport();
    assertThat(analyzer.hasDifferences()).isFalse();
}