### Question:
DiskCache2 { public static boolean canWrite(File f) { Path path = f.toPath().toAbsolutePath(); try { if (Files.isDirectory(path)) { Files.delete(Files.createTempFile(path, "check", null)); } else if (Files.isRegularFile(path)) { Files.newOutputStream(path, StandardOpenOption.APPEND).close(); } else { Files.delete(Files.createTempFile(path.getParent(), "check", null)); } } catch (IOException | SecurityException e) { return false; } return true; } private  DiskCache2();  DiskCache2(String root, boolean reletiveToHome, int persistMinutes, int scourEveryMinutes); static void exit(); static DiskCache2 getDefault(); static DiskCache2 getNoop(); void setRootDirectory(String cacheDir); String getRootDirectory(); File getCacheFile(String fileLocation); File getFile(String fileLocation); static boolean canWrite(File f); File getExistingFileOrCache(String fileLocation); synchronized File createUniqueFile(String prefix, String suffix); void setPolicy(CachePathPolicy cachePathPolicy); void setCachePathPolicy(CachePathPolicy cachePathPolicy, String cachePathPolicyParam); void setPolicy(String policy); void setAlwaysUseCache(boolean alwaysUseCache); void setNeverUseCache(boolean neverUseCache); void showCache(Formatter f); void cleanCache(File dir, Formatter sbuff, boolean isRoot); @Override String toString();  }### Answer:
@Test public void canWriteDir() { File file = dirForStaticTests.toFile(); assertThat(file.exists()); assertThat(DiskCache2.canWrite(file)); }
@Test public void canWriteFile() throws IOException { File file = Files.createTempFile(dirForStaticTests, "temp", null).toFile(); assertThat(file.exists()); filesToDelete.add(file); assertThat(DiskCache2.canWrite(file)); }
@Test public void cantWriteFile() throws IOException { File file = Files.createTempFile(dirForStaticTests, "temp", null).toFile(); filesToDelete.add(file); Assert.assertTrue(file.exists()); assertThat(file.setWritable(false)).isTrue(); assertThat(DiskCache2.canWrite(file)); assertThat(file.setWritable(true)).isTrue(); assertThat(DiskCache2.canWrite(file)); }
@Test @Category(NeedsCdmUnitTest.class) public void testReletivePath() throws Exception { String org = System.getProperty("user.dir"); try { System.setProperty("user.dir", TestDir.cdmUnitTestDir); System.out.printf("user.dir = %s%n", System.getProperty("user.dir")); File pwd = new File(System.getProperty("user.dir")); String filename = "transforms/albers.ncml"; File rel2 = new File(pwd, filename); System.out.printf("abs = %s%n", rel2.getCanonicalFile()); assert rel2.exists(); assert rel2.canWrite(); } finally { System.setProperty("user.dir", org); } }