### Question:
StorageStrategy { public Path createFileAndApply(FileSystem fs, Path file) throws IOException { List<Path> locations = getNonExistentLocations(fs, file.getParent()); if (!fs.createNewFile(file)) { throw new IOException(String.format("File [%s] already exists on file system [%s].", file.toUri().getPath(), fs.getUri())); } applyToFile(fs, file); if (locations.isEmpty()) { return file; } for (Path location : locations) { applyStrategy(fs, location, getFolderPermission(), deleteOnExit); } return locations.get(locations.size() - 1); } @JsonCreator  StorageStrategy(@JsonProperty("umask") String umask,
                         @JsonProperty("deleteOnExit") boolean deleteOnExit); String getUmask(); boolean isDeleteOnExit(); @JsonIgnore FsPermission getFolderPermission(); @JsonIgnore FsPermission getFilePermission(); Path createPathAndApply(FileSystem fs, Path path); Path createFileAndApply(FileSystem fs, Path file); void applyToFile(FileSystem fs, Path file); static final StorageStrategy DEFAULT; static final StorageStrategy TEMPORARY; }### Answer:
@Test public void testPermissionAndDeleteOnExitFalseForFileWithParent() throws Exception { Path initialPath = prepareStorageDirectory(); Path file = addNLevelsAndFile(initialPath, 2, true); Path firstCreatedParentPath = addNLevelsAndFile(initialPath, 1, false); Path createdParentPath = PERSISTENT_STRATEGY.createFileAndApply(fs, file); assertEquals("Path should match", firstCreatedParentPath, createdParentPath); checkPathAndPermission(initialPath, file, true, 2, PERSISTENT_STRATEGY); checkDeleteOnExit(firstCreatedParentPath, false); }
@Test public void testPermissionAndDeleteOnExitTrueForFileWithParent() throws Exception { Path initialPath = prepareStorageDirectory(); Path file = addNLevelsAndFile(initialPath, 2, true); Path firstCreatedParentPath = addNLevelsAndFile(initialPath, 1, false); Path createdParentPath = TEMPORARY_STRATEGY.createFileAndApply(fs, file); assertEquals("Path should match", firstCreatedParentPath, createdParentPath); checkPathAndPermission(initialPath, file, true, 2, TEMPORARY_STRATEGY); checkDeleteOnExit(firstCreatedParentPath, true); }
@Test public void testPermissionAndDeleteOnExitFalseForFileOnly() throws Exception { Path initialPath = prepareStorageDirectory(); Path file = addNLevelsAndFile(initialPath, 0, true); Path createdFile = PERSISTENT_STRATEGY.createFileAndApply(fs, file); assertEquals("Path should match", file, createdFile); checkPathAndPermission(initialPath, file, true, 0, PERSISTENT_STRATEGY); checkDeleteOnExit(file, false); }
@Test public void testPermissionAndDeleteOnExitTrueForFileOnly() throws Exception { Path initialPath = prepareStorageDirectory(); Path file = addNLevelsAndFile(initialPath, 0, true); Path createdFile = TEMPORARY_STRATEGY.createFileAndApply(fs, file); assertEquals("Path should match", file, createdFile); checkPathAndPermission(initialPath, file, true, 0, TEMPORARY_STRATEGY); checkDeleteOnExit(file, true); }
@Test(expected = IOException.class) public void testFailureOnExistentFile() throws Exception { Path initialPath = prepareStorageDirectory(); Path file = addNLevelsAndFile(initialPath, 0, true); fs.createNewFile(file); assertTrue("File should exist", fs.exists(file)); try { PERSISTENT_STRATEGY.createFileAndApply(fs, file); } catch (IOException e) { assertEquals("Error message should match", String.format("File [%s] already exists on file system [%s].", file.toUri().getPath(), fs.getUri()), e.getMessage()); throw e; } }