@Test public void testChunkedCopy() throws Exception { File f = tempFolder.newFile(); byte[] bytes = new byte[]{(byte) 0x8, (byte) 0x9}; ByteArrayInputStream bis = new ByteArrayInputStream(bytes); NativeIO.chunkedCopy(bis, f); byte[] data = Files.readAllBytes(f.toPath()); Assert.assertTrue(Arrays.equals(bytes, data)); }
@Test public void shouldPreserveFileTimestampWhileGeneratingTheZipFile() throws Exception { File file = temporaryFolder.newFile("foo.txt"); file.setLastModified(1297989100000L); // Set this to any date in the past which is greater than the epoch File zip = zipUtil.zip(file, temporaryFolder.newFile("foo.zip"), Deflater.DEFAULT_COMPRESSION); ZipFile actualZip = new ZipFile(zip.getAbsolutePath()); ZipEntry entry = actualZip.getEntry(file.getName()); assertThat(entry.getTime(), is(file.lastModified())); }
@Test public void testDefaultsToLocal() throws Exception { URI justPath = new URI(tempFolder.newFile().toURI().getPath()); assertNull(justPath.getScheme()); FileSystem fs = FileSystem.get(justPath); assertEquals("file", fs.getUri().getScheme()); }
@Test public void always_try_utf8() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); // this is a valid 2 byte UTF-8. out.write(194); out.write(128); Path filePath = temp.newFile().toPath(); Files.write(filePath, out.toByteArray()); assertThat(detectCharset(filePath, UTF_16)).isEqualTo(UTF_8); }
@Test public void testExistingFile() throws Exception { final File file = temporaryFolder.newFile(); assertTrue("Suppression file exists", FilterUtil.isFileExists(file.getPath())); }
@Test public void testManyComments() throws Exception { final File file = temporaryFolder.newFile("InputDetailASTManyComments.java"); try (Writer bw = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) { bw.write("class C {\n"); for (int i = 0; i <= 30000; i++) { bw.write("// " + i + "\n"); } bw.write("}\n"); } final DefaultConfiguration checkConfig = createModuleConfig(TodoCommentCheck.class); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkConfig, file.getAbsolutePath(), expected); }
@Test public void testDeleteDirectoryWhichIsAFile() throws Exception { // deleting a directory that is actually a file should fails File file = tmp.newFile(); try { FileUtils.deleteDirectory(file); fail("this should fail with an exception"); } catch (IOException ignored) { // this is what we expect } }
@Before public void setUp() throws Exception { initMocks(this); bundleDir = temporaryFolder.newFolder("bundleDir"); pluginExternalDir = temporaryFolder.newFolder("externalDir"); newJarFile = temporaryFolder.newFile("jar-file.jar"); nonJarFile = temporaryFolder.newFile("some-picture.jpg"); when(systemEnvironment.get(PLUGIN_BUNDLE_PATH)).thenReturn(bundleDir.getAbsolutePath()); when(systemEnvironment.get(PLUGIN_EXTERNAL_PROVIDED_PATH)).thenReturn(pluginExternalDir.getAbsolutePath()); }
@Test public void TestSerDesr() throws IOException { LookupConfig lookupConfig = new LookupConfig(temporaryFolder.newFile().getAbsolutePath()); Assert.assertEquals( lookupConfig, mapper.readerFor(LookupConfig.class).readValue(mapper.writeValueAsString(lookupConfig)) ); }
@Test public void validConfigResultsInNotNullConfigurationTest() throws IOException { File file1 = folder.newFile("mockfile.txt"); HashMap<String, Object> conf = new HashMap<>(); conf.put("java.security.auth.login.config", file1.getAbsolutePath()); Assert.assertNotNull(ClientAuthUtils.getConfiguration(conf)); }
@Test public void testMap() throws IOException { File dataFile = folder.newFile("data"); long buffersMemoryBefore = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers(); try (RandomAccessFile raf = new RandomAccessFile(dataFile, "rw")) { raf.write(42); raf.setLength(1 << 20); // 1 MB } try (MappedByteBufferHandler mappedByteBufferHandler = FileUtils.map(dataFile)) { Assert.assertEquals(42, mappedByteBufferHandler.get().get(0)); } long buffersMemoryAfter = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers(); Assert.assertEquals(buffersMemoryBefore, buffersMemoryAfter); }
@Test public void simpleZipTest() throws IOException, SegmentLoadingException { File file = new File(tmpDir, "test1data"); File zipFile = temporaryFolder.newFile("ziptest.zip"); try (OutputStream outputStream = new FileOutputStream(file)) { outputStream.write(new byte[0]); outputStream.flush(); } CompressionUtils.zip(tmpDir, zipFile); file.delete(); Assert.assertFalse(file.exists()); Assert.assertTrue(zipFile.exists()); puller.getSegmentFiles(zipFile, tmpDir); Assert.assertTrue(file.exists()); }
@Test public void testSanity() throws Exception { File baseDir = folder.newFolder("base"); try (FileSmoosher smoosher = new FileSmoosher(baseDir, 21)) { for (int i = 0; i < 20; ++i) { File tmpFile = folder.newFile(StringUtils.format("smoosh-%s.bin", i)); Files.write(Ints.toByteArray(i), tmpFile); smoosher.add(StringUtils.format("%d", i), tmpFile); } } validateOutput(baseDir); }
/** * Sets up the file path and file block reader before a test runs. */ @Before public void before() throws Exception { String testFilePath = mFolder.newFile().getAbsolutePath(); byte[] buffer = BufferUtils.getIncreasingByteArray((int) TEST_BLOCK_SIZE); BufferUtils.writeBufferToFile(testFilePath, buffer); mReader = new LocalFileBlockReader(testFilePath); }
private AndroidManifest newConfigWith(String packageName, String contents) throws IOException { String fileContents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + " package=\"" + packageName + "\">\n" + " " + contents + "\n" + "</manifest>\n"; File f = temporaryFolder.newFile("whatever.xml"); Files.asCharSink(f, Charsets.UTF_8).write(fileContents); return new AndroidManifest(f.toPath(), null, null); }
/** * Tests the {@link FileUtils#move(String, String)} method. */ @Test public void moveFile() throws IOException { File fromFile = mTestFolder.newFile("from.txt"); File toFile = mTestFolder.newFile("to.txt"); // Move a file and verify FileUtils.move(fromFile.getAbsolutePath(), toFile.getAbsolutePath()); assertFalse(fromFile.exists()); assertTrue(toFile.exists()); }
@Test public void loadPropsFromCommandLineArgs_file_does_not_exist() throws Exception { File propsFile = temp.newFile(); FileUtils.deleteQuietly(propsFile); try { ConfigurationUtils.loadPropsFromCommandLineArgs(new String[]{propsFile.getAbsolutePath()}); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Could not read properties from file: " + propsFile.getAbsolutePath()); } }
@Test public void testExplicitlySetToLocal() throws Exception { final Configuration conf = new Configuration(); conf.setString(CoreOptions.DEFAULT_FILESYSTEM_SCHEME, LocalFileSystem.getLocalFsURI().toString()); FileSystem.initialize(conf); URI justPath = new URI(tempFolder.newFile().toURI().getPath()); assertNull(justPath.getScheme()); FileSystem fs = FileSystem.get(justPath); assertEquals("file", fs.getUri().getScheme()); }