### Question:
TemporaryFileInstaller { public boolean install(File target, boolean reuse) throws IOException { File parent = target.getAbsoluteFile().getParentFile(); if (parent.mkdirs() == false && parent.isDirectory() == false) { throw new IOException(MessageFormat.format( "failed to create a file: {0}", target)); } try (RandomAccessFile file = new RandomAccessFile(target, "rw"); FileLock lock = file.getChannel().lock(0, 0, false)) { if (reuse && isReusable(target, file)) { return false; } doInstall(target, file); return true; } } private  TemporaryFileInstaller(byte[] bytes, boolean executable); static TemporaryFileInstaller newInstance(InputStream contents, boolean executable); boolean install(File target, boolean reuse);  }### Answer:
@Test public void simple() throws Exception { byte[] contents = { 1, 2, 3 }; File f = temporary.newFile(); TemporaryFileInstaller installer = create(false, contents); assertThat(installer.install(f, true), is(true)); check(f, contents); }
@Test public void reuse() throws Exception { byte[] contents = { 1, 2, 3 }; File f = temporary.newFile(); TemporaryFileInstaller installer = create(false, contents); assertThat(installer.install(f, true), is(true)); assertThat(installer.install(f, true), is(false)); check(f, contents); }
@Test public void reuse_update() throws Exception { byte[] c0 = { 1, 2, 3 }; byte[] c1 = { 1, 2, 4 }; File f = temporary.newFile(); TemporaryFileInstaller i0 = create(false, c0); TemporaryFileInstaller i1 = create(false, c1); assertThat(i0.install(f, true), is(true)); assertThat(i0.install(f, true), is(false)); check(f, c0); assertThat(i1.install(f, true), is(true)); assertThat(i1.install(f, true), is(false)); check(f, c1); }
@Test public void reuse_deny() throws Exception { byte[] contents = { 1, 2, 3 }; File f = temporary.newFile(); TemporaryFileInstaller installer = create(false, contents); assertThat(installer.install(f, false), is(true)); assertThat(installer.install(f, false), is(true)); check(f, contents); }