private static File getResource(String path) { String resourcePath = path; if (!resourcePath.startsWith("/")) { resourcePath = "/" + resourcePath; } URL url = TestUtils.class.getResource(resourcePath); if (url != null) { return FileUtils.toFile(url); } return null; } }
private FileInputStream getFileInputStream( URL url ) throws FileNotFoundException { return new FileInputStream( FileUtils.toFile( url ) ); }
"URL could not be converted to a File: " + url); files[i] = toFile(url);
private File getFile(String filename) { return FileUtils.toFile(getClass().getResource("/org/sonar/core/platform/" + filename)); } }
/** * 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 */ 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; }
/** * Get the artifact of plugins stored in src/test/projects */ 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(); } }
private void downloadRelease(Release release) throws URISyntaxException, IOException { String url = release.getDownloadUrl(); URI uri = new URI(url); if (url.startsWith("file:")) { // used for tests File file = toFile(uri.toURL()); copyFileToDirectory(file, downloadDir); } else { String filename = substringAfterLast(uri.getPath(), "/"); if (!filename.endsWith("." + PLUGIN_EXTENSION)) { filename = release.getKey() + "-" + release.getVersion() + "." + PLUGIN_EXTENSION; } File targetFile = new File(downloadDir, filename); File tempFile = new File(downloadDir, filename + "." + TMP_SUFFIX); downloader.download(uri, tempFile); copyFile(tempFile, targetFile); deleteQuietly(tempFile); } }
@Test public void testToFile1() throws Exception { final URL url = new URL("file", null, "a/b/c/file.txt"); final File file = FileUtils.toFile(url); assertTrue(file.toString().contains("file.txt")); }
@Test public void testToFile2() throws Exception { final URL url = new URL("file", null, "a/b/c/file%20n%61me%2520.tx%74"); final File file = FileUtils.toFile(url); assertTrue(file.toString().contains("file name%20.txt")); }
@Test public void testToFile4() throws Exception { final URL url = new URL("file", null, "a/b/c/file%%20%me.txt%"); final File file = FileUtils.toFile(url); assertTrue(file.toString().contains("file% %me.txt%")); }
@Test public void testToFileUtf8() throws Exception { final URL url = new URL("file", null, "/home/%C3%A4%C3%B6%C3%BC%C3%9F"); final File file = FileUtils.toFile(url); assertTrue(file.toString().contains("\u00E4\u00F6\u00FC\u00DF")); }
@Test public void testToFile5() throws Exception { final URL url = new URL("file", null, "both%20are%20100%20%25%20true"); final File file = FileUtils.toFile(url); assertEquals("both are 100 % true", file.toString()); }
private File loadFile(String filename) throws IOException { File src = FileUtils.toFile(getClass().getResource(getClass().getSimpleName() + "/" + filename)); File dest = new File(temp.newFolder(), filename); FileUtils.copyFile(src, dest); return dest; }
@Test public void create_from_file() { File checkstyleJar = FileUtils.toFile(getClass().getResource("/org/sonar/core/platform/sonar-checkstyle-plugin-2.8.jar")); PluginInfo checkstyleInfo = PluginInfo.create(checkstyleJar); assertThat(checkstyleInfo.getName()).isEqualTo("Checkstyle"); assertThat(checkstyleInfo.getMinimalSqVersion()).isEqualTo(Version.create("2.8")); }
@Test public void unzipping_file_extracts_subset_of_files() throws IOException { File zip = FileUtils.toFile(urlToZip()); File toDir = temp.newFolder(); ZipUtils.unzip(zip, toDir, (ZipUtils.ZipEntryFilter)ze -> ze.getName().equals("foo.txt")); assertThat(toDir.listFiles()).containsOnly(new File(toDir, "foo.txt")); }
@Test public void unzipping_creates_target_directory_if_it_does_not_exist() throws IOException { File zip = FileUtils.toFile(urlToZip()); File tempDir = temp.newFolder(); Files.delete(tempDir); File subDir = new File(tempDir, "subDir"); ZipUtils.unzip(zip, subDir); assertThat(subDir.list()).hasSize(3); }
@Test public void unzip_file() throws IOException { File zip = FileUtils.toFile(urlToZip()); File toDir = temp.newFolder(); ZipUtils.unzip(zip, toDir); assertThat(toDir.list()).hasSize(3); }
@Test public void zip_directory() throws IOException { File foo = FileUtils.toFile(getClass().getResource("/org/sonar/api/utils/ZipUtilsTest/shouldZipDirectory/foo.txt")); File dir = foo.getParentFile(); File zip = temp.newFile(); ZipUtils.zipDir(dir, zip); assertThat(zip).exists().isFile(); assertThat(zip.length()).isGreaterThan(1L); Iterator<? extends ZipEntry> zipEntries = Iterators.forEnumeration(new ZipFile(zip).entries()); assertThat(zipEntries).hasSize(4); File unzipDir = temp.newFolder(); ZipUtils.unzip(zip, unzipDir); assertThat(new File(unzipDir, "bar.txt")).exists().isFile(); assertThat(new File(unzipDir, "foo.txt")).exists().isFile(); assertThat(new File(unzipDir, "dir1/hello.properties")).exists().isFile(); }
boolean openFile() throws Exception { data.oneFileOpened = true; String realFilename = environmentSubstitute( meta.getFilename() ); if ( log.isBasic() ) { logBasic( BaseMessages.getString( PKG, "AccessOutput.log.WritingToFile", realFilename ) ); } FileObject fileObject = KettleVFS.getFileObject( realFilename, getTransMeta() ); File file = FileUtils.toFile( fileObject.getURL() ); // First open or create the access file if ( !file.exists() ) { if ( meta.isFileCreated() ) { data.createDatabase( file ); } else { logError( BaseMessages.getString( PKG, "AccessOutput.InitError.FileDoesNotExist", realFilename ) ); return false; } } else { data.openDatabase( file ); } // Add the filename to the result object... // if ( meta.isAddToResultFiles() ) { ResultFile resultFile = new ResultFile( ResultFile.FILE_TYPE_GENERAL, fileObject, getTransMeta().getName(), toString() ); resultFile.setComment( "This file was created with an access output step" ); addResultFile( resultFile ); } return true; }